Regarding some init code, don't know if this method is known to gcc or clang. In my past, when still coding a lot on my old Commodore Amiga, I used MaxonC++ and StormC++. In both a programmer could define init/exit functions in the form _INIT_X_MyFunction and _EXIT_X_MyFunction, where the X ia a number 1 to 9 designating the priority level for linking.
The linker did automatically link those functions into the init/cleanup sections of the final program, hence directly prior branching into main() or when returning/exiting from main(). The linking order is determined by the given level. At init the order is 1 to 9er _INIT functions, at exit respectively the opposite 9er to 1 _EXIT functions.