#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */

/* Variables and functions */
int /*<<< orphan*/ mem_alloc(int);
int /*<<< orphan*/ mem_free(struct exitlist *);
scalar_t__ memset(int /*<<< orphan*/, int /*<<< orphan*/, int);
void stub1();
void ang_atexit(void (*arg)(void)) {
  typedef struct exitlist exitlist;
  struct exitlist {
    void (*func)(void);
    exitlist *next;
  };
  static exitlist *list;
  exitlist *next;
  if (arg != 0) {
    next = ((exitlist *)(memset(((mem_alloc((1) * sizeof(exitlist)))), 0,
                                (1) * sizeof(exitlist))));
    next->func = arg;
    next->next = list;
    list = next;
    return;
  }
  while (list) {
    next = list->next;
    list->func();
    (mem_free(list), list = 0);
    list = next;
  }
}


x86 assembly (clang, -O0):
<ang_atexit>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
je     118d <ang_atexit+0x5d>
mov    $0x10,%edi
call   1050 <mem_alloc@plt>
mov    %eax,%edi
xor    %esi,%esi
mov    $0x10,%edx
call   1040 <memset@plt>
mov    %rax,-0x10(%rbp)
mov    -0x8(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    0x2ecb(%rip),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,0x8(%rax)
mov    -0x10(%rbp),%rax
mov    %rax,0x2eb8(%rip)
jmp    11df <ang_atexit+0xaf>
jmp    1192 <ang_atexit+0x62>
cmpq   $0x0,0x2ea6(%rip)
je     11df <ang_atexit+0xaf>
mov    0x2e99(%rip),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x10(%rbp)
mov    0x2e8a(%rip),%rax
call   *(%rax)
mov    0x2e81(%rip),%rdi
call   1030 <mem_free@plt>
movq   $0x0,0x2e71(%rip)
mov    -0x10(%rbp),%rax
mov    %rax,0x2e66(%rip)
jmp    1192 <ang_atexit+0x62>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ang_atexit>:
push   %rbx
test   %rdi,%rdi
je     1168 <ang_atexit+0x38>
mov    %rdi,%rbx
mov    $0x10,%edi
call   1050 <mem_alloc@plt>
mov    %eax,%edi
xor    %esi,%esi
mov    $0x10,%edx
call   1040 <memset@plt>
mov    %rbx,(%rax)
mov    0x2ee5(%rip),%rcx
mov    %rcx,0x8(%rax)
mov    %rax,0x2eda(%rip)
pop    %rbx
ret
mov    0x2ed1(%rip),%rax
test   %rax,%rax
je     11a1 <ang_atexit+0x71>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    0x8(%rax),%rbx
call   *(%rax)
mov    0x2eb3(%rip),%rdi
call   1030 <mem_free@plt>
mov    %rbx,0x2ea7(%rip)
mov    %rbx,%rax
test   %rbx,%rbx
jne    1180 <ang_atexit+0x50>
pop    %rbx
ret

LLVM IR (-O0):
%struct.exitlist = type { void ()*, %struct.exitlist* }
%struct.exitlist.0 = type opaque
@ang_atexit.list = internal global %struct.exitlist* null
define void @ang_atexit(void ()* %0) {
%2 = alloca void ()*
%3 = alloca %struct.exitlist*
store void ()* %0, void ()** %2
%4 = load void ()*, void ()** %2
%5 = icmp ne void ()* %4, null
br i1 %5, label %6, label %17
6: ; preds = %1
%7 = call i32 @mem_alloc(i32 16)
%8 = call i64 @memset(i32 %7, i32 0, i32 16)
%9 = inttoptr i64 %8 to %struct.exitlist*
store %struct.exitlist* %9, %struct.exitlist** %3
%10 = load void ()*, void ()** %2
%11 = load %struct.exitlist*, %struct.exitlist** %3
%12 = getelementptr inbounds %struct.exitlist, %struct.exitlist* %11, i32 0, i32 0
store void ()* %10, void ()** %12
%13 = load %struct.exitlist*, %struct.exitlist** @ang_atexit.list
%14 = load %struct.exitlist*, %struct.exitlist** %3
%15 = getelementptr inbounds %struct.exitlist, %struct.exitlist* %14, i32 0, i32 1
store %struct.exitlist* %13, %struct.exitlist** %15
%16 = load %struct.exitlist*, %struct.exitlist** %3
store %struct.exitlist* %16, %struct.exitlist** @ang_atexit.list
br label %32
17: ; preds = %1
br label %18
18: ; preds = %21, %17
%19 = load %struct.exitlist*, %struct.exitlist** @ang_atexit.list
%20 = icmp ne %struct.exitlist* %19, null
br i1 %20, label %21, label %32
21: ; preds = %18
%22 = load %struct.exitlist*, %struct.exitlist** @ang_atexit.list
%23 = getelementptr inbounds %struct.exitlist, %struct.exitlist* %22, i32 0, i32 1
%24 = load %struct.exitlist*, %struct.exitlist** %23
store %struct.exitlist* %24, %struct.exitlist** %3
%25 = load %struct.exitlist*, %struct.exitlist** @ang_atexit.list
%26 = getelementptr inbounds %struct.exitlist, %struct.exitlist* %25, i32 0, i32 0
%27 = load void ()*, void ()** %26
%28 = load %struct.exitlist*, %struct.exitlist** @ang_atexit.list
%29 = bitcast %struct.exitlist* %28 to %struct.exitlist.0*
%30 = call i32 @mem_free(%struct.exitlist.0* %29)
store %struct.exitlist* null, %struct.exitlist** @ang_atexit.list
%31 = load %struct.exitlist*, %struct.exitlist** %3
store %struct.exitlist* %31, %struct.exitlist** @ang_atexit.list
br label %18
32: ; preds = %6, %18
ret void
}

LLVM IR (-O2):
%struct.exitlist = type { void ()*, %struct.exitlist* }
%struct.exitlist.0 = type opaque
@ang_atexit.list = internal unnamed_addr global %struct.exitlist* null
define void @ang_atexit(void ()* %0) {
%2 = icmp eq void ()* %0, null
br i1 %2, label %10, label %3
3: ; preds = %1
%4 = tail call i32 @mem_alloc(i32 16)
%5 = tail call i64 @memset(i32 %4, i32 0, i32 16)
%6 = inttoptr i64 %5 to %struct.exitlist*
%7 = getelementptr inbounds %struct.exitlist, %struct.exitlist* %6, i64 0, i32 0
store void ()* %0, void ()** %7
%8 = load %struct.exitlist*, %struct.exitlist** @ang_atexit.list
%9 = getelementptr inbounds %struct.exitlist, %struct.exitlist* %6, i64 0, i32 1
store %struct.exitlist* %8, %struct.exitlist** %9
store %struct.exitlist* %6, %struct.exitlist** @ang_atexit.list
br label %22
10: ; preds = %1
%11 = load %struct.exitlist*, %struct.exitlist** @ang_atexit.list
%12 = icmp eq %struct.exitlist* %11, null
br i1 %12, label %22, label %13
13: ; preds = %10, %13
%14 = phi %struct.exitlist* [ %16, %13 ], [ %11, %10 ]
%15 = getelementptr inbounds %struct.exitlist, %struct.exitlist* %14, i64 0, i32 1
%16 = load %struct.exitlist*, %struct.exitlist** %15
%17 = getelementptr inbounds %struct.exitlist, %struct.exitlist* %14, i64 0, i32 0
%18 = load void ()*, void ()** %17
tail call void %18()
%19 = load %struct.exitlist.0*, %struct.exitlist.0** bitcast (%struct.exitlist** @ang_atexit.list to %struct.exitlist.0**)
%20 = tail call i32 @mem_free(%struct.exitlist.0* %19)
store %struct.exitlist* %16, %struct.exitlist** @ang_atexit.list
%21 = icmp eq %struct.exitlist* %16, null
br i1 %21, label %22, label %13
22: ; preds = %13, %10, %3
ret void
}