#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 */
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
long atol(char *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fwrite(char *, int, int /*<<< orphan*/,
                          int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ sprintf(char *, char *, long);
int /*<<< orphan*/ strlen(char *);
int main(int argc, char *argv[]) {
  FILE *fp;
  long int length, i;
  char writeBuffer[(sizeof(long int) * 2) + 16], *str;
  if (argc < 2) {
    printf("Generates benchmarking program for ELF loader\n\n"
           "Usage: %s symbols [file]\n",
           argv[0]);
    return 0;
  }
  length = atol(argv[1]);
  if (length < 1) {
    printf("Invalid number\n");
    return 0;
  }
  if (argc == 3) {
    fp = fopen(argv[2], "w");
  } else {
    fp = fopen("benchmark.c", "w");
  }
  if (!fp) {
    printf("Unable to open file for writing\n");
    return -1;
  }
  for (i = 0; i < length; i++) {
    sprintf(writeBuffer, "void foo%lx() {}\n", i);
    fwrite(writeBuffer, 1, strlen(writeBuffer), fp);
  }
  str = "typedef void (foofunc)();\n"
        "static foofunc *ptrs[] = {\n";
  fwrite(str, 1, strlen(str), fp);
  for (i = 0; i < length; i++) {
    sprintf(writeBuffer, "    foo%lx,\n", i);
    fwrite(writeBuffer, 1, strlen(writeBuffer), fp);
  }
  str = "};\n"
        "int i = 0; //Hack to force gcc to generate a .got section\n"
        "int _start() { i++; return 0; }\n";
  fwrite(str, 1, strlen(str), fp);
  fclose(fp);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    11b4 <main+0x44>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe5f(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    137d <main+0x20d>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1060 <atol@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x1,-0x20(%rbp)
jge    11ea <main+0x7a>
lea    0xe72(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    137d <main+0x20d>
cmpl   $0x3,-0x8(%rbp)
jne    1211 <main+0xa1>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
lea    0xe56(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x18(%rbp)
jmp    1228 <main+0xb8>
lea    0xe43(%rip),%rdi
lea    0xe3a(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    124d <main+0xdd>
lea    0xe2d(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    137d <main+0x20d>
movq   $0x0,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x20(%rbp),%rax
jge    12ad <main+0x13d>
lea    -0x50(%rbp),%rdi
mov    -0x28(%rbp),%rdx
lea    0xe16(%rip),%rsi
call   1080 <sprintf@plt>
lea    -0x50(%rbp),%rax
mov    %rax,-0x60(%rbp)
lea    -0x50(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x60(%rbp),%rdi
mov    %eax,%edx
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
call   1090 <fwrite@plt>
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
jmp    1255 <main+0xe5>
lea    0xde6(%rip),%rax
mov    %rax,-0x58(%rbp)
mov    -0x58(%rbp),%rax
mov    %rax,-0x68(%rbp)
mov    -0x58(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x68(%rbp),%rdi
mov    %eax,%edx
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
call   1090 <fwrite@plt>
movq   $0x0,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x20(%rbp),%rax
jge    133d <main+0x1cd>
lea    -0x50(%rbp),%rdi
mov    -0x28(%rbp),%rdx
lea    0xdce(%rip),%rsi
call   1080 <sprintf@plt>
lea    -0x50(%rbp),%rax
mov    %rax,-0x70(%rbp)
lea    -0x50(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x70(%rbp),%rdi
mov    %eax,%edx
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
call   1090 <fwrite@plt>
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
jmp    12e5 <main+0x175>
lea    0xd99(%rip),%rax
mov    %rax,-0x58(%rbp)
mov    -0x58(%rbp),%rax
mov    %rax,-0x78(%rbp)
mov    -0x58(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x78(%rbp),%rdi
mov    %eax,%edx
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
call   1090 <fwrite@plt>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rsi,%rbx
cmp    $0x1,%edi
jg     11ae <main+0x2e>
mov    (%rbx),%rsi
lea    0xe60(%rip),%rdi
xor    %ebx,%ebx
xor    %eax,%eax
call   1060 <printf@plt>
jmp    12df <main+0x15f>
mov    %edi,%ebp
mov    0x8(%rbx),%rdi
call   1070 <atol@plt>
test   %rax,%rax
jle    11cc <main+0x4c>
mov    %rax,%r14
cmp    $0x3,%ebp
jne    11dd <main+0x5d>
mov    0x10(%rbx),%rdi
jmp    11e4 <main+0x64>
lea    0xf57(%rip),%rdi
call   1030 <puts@plt>
jmp    12ca <main+0x14a>
lea    0xe67(%rip),%rdi
lea    0xe5e(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     12ce <main+0x14e>
mov    %rax,%r15
xor    %ebx,%ebx
lea    0xe52(%rip),%r12
mov    %rsp,%r13
nopl   0x0(%rax,%rax,1)
mov    %r13,%rdi
mov    %r12,%rsi
mov    %rbx,%rdx
call   1090 <sprintf@plt>
mov    %r13,%rdi
call   1050 <strlen@plt>
mov    %r13,%rdi
mov    $0x1,%esi
mov    %eax,%edx
mov    %r15,%rcx
call   10a0 <fwrite@plt>
add    $0x1,%rbx
cmp    %rbx,%r14
jne    1210 <main+0x90>
lea    0xe21(%rip),%rbx
mov    %rbx,%rdi
call   1050 <strlen@plt>
mov    %rbx,%rdi
mov    $0x1,%esi
mov    %eax,%edx
mov    %r15,%rcx
call   10a0 <fwrite@plt>
xor    %ebx,%ebx
lea    0xe34(%rip),%r12
mov    %rsp,%r13
xchg   %ax,%ax
mov    %r13,%rdi
mov    %r12,%rsi
mov    %rbx,%rdx
call   1090 <sprintf@plt>
mov    %r13,%rdi
call   1050 <strlen@plt>
mov    %r13,%rdi
mov    $0x1,%esi
mov    %eax,%edx
mov    %r15,%rcx
call   10a0 <fwrite@plt>
add    $0x1,%rbx
cmp    %rbx,%r14
jne    1270 <main+0xf0>
lea    0xe04(%rip),%rbx
mov    %rbx,%rdi
call   1050 <strlen@plt>
mov    %rbx,%rdi
mov    $0x1,%esi
mov    %eax,%edx
mov    %r15,%rcx
call   10a0 <fwrite@plt>
mov    %r15,%rdi
call   1040 <fclose@plt>
xor    %ebx,%ebx
jmp    12df <main+0x15f>
lea    0xe35(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [73 x i8] c"Generates benchmarking program for ELF loader\0A\0AUsage: %s symbols [file]\0A\00"
@.str.1 = [16 x i8] c"Invalid number\0A\00"
@.str.2 = [2 x i8] c"w\00"
@.str.3 = [12 x i8] c"benchmark.c\00"
@.str.4 = [33 x i8] c"Unable to open file for writing\0A\00"
@.str.5 = [18 x i8] c"void foo%lx() {}\0A\00"
@.str.6 = [54 x i8] c"typedef void (foofunc)();\0Astatic foofunc *ptrs[] = {\0A\00"
@.str.7 = [13 x i8] c" foo%lx,\0A\00"
@.str.8 = [94 x i8] c"};\0Aint i = 0; //Hack to force gcc to generate a .got section\0Aint _start() { i++; return 0; }\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32*
%7 = alloca i64
%8 = alloca i64
%9 = alloca [32 x i8]
%10 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = load i32, i32* %4
%12 = icmp slt i32 %11, 2
br i1 %12, label %13, label %18
13: ; preds = %2
%14 = load i8**, i8*** %5
%15 = getelementptr inbounds i8*, i8** %14, i64 0
%16 = load i8*, i8** %15
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([73 x i8], [73 x i8]* @.str, i64 0, i64 0), i8* %16)
store i32 0, i32* %3
br label %89
18: ; preds = %2
%19 = load i8**, i8*** %5
%20 = getelementptr inbounds i8*, i8** %19, i64 1
%21 = load i8*, i8** %20
%22 = call i64 @atol(i8* %21)
store i64 %22, i64* %7
%23 = load i64, i64* %7
%24 = icmp slt i64 %23, 1
br i1 %24, label %25, label %27
25: ; preds = %18
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
store i32 0, i32* %3
br label %89
27: ; preds = %18
%28 = load i32, i32* %4
%29 = icmp eq i32 %28, 3
br i1 %29, label %30, label %35
30: ; preds = %27
%31 = load i8**, i8*** %5
%32 = getelementptr inbounds i8*, i8** %31, i64 2
%33 = load i8*, i8** %32
%34 = call i32* @fopen(i8* %33, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %34, i32** %6
br label %37
35: ; preds = %27
%36 = call i32* @fopen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %36, i32** %6
br label %37
37: ; preds = %35, %30
%38 = load i32*, i32** %6
%39 = icmp ne i32* %38, null
br i1 %39, label %42, label %40
40: ; preds = %37
%41 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.4, i64 0, i64 0))
store i32 -1, i32* %3
br label %89
42: ; preds = %37
store i64 0, i64* %8
br label %43
43: ; preds = %56, %42
%44 = load i64, i64* %8
%45 = load i64, i64* %7
%46 = icmp slt i64 %44, %45
br i1 %46, label %47, label %59
47: ; preds = %43
%48 = getelementptr inbounds [32 x i8], [32 x i8]* %9, i64 0, i64 0
%49 = load i64, i64* %8
%50 = call i32 @sprintf(i8* %48, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.5, i64 0, i64 0), i64 %49)
%51 = getelementptr inbounds [32 x i8], [32 x i8]* %9, i64 0, i64 0
%52 = getelementptr inbounds [32 x i8], [32 x i8]* %9, i64 0, i64 0
%53 = call i32 @strlen(i8* %52)
%54 = load i32*, i32** %6
%55 = call i32 @fwrite(i8* %51, i32 1, i32 %53, i32* %54)
br label %56
56: ; preds = %47
%57 = load i64, i64* %8
%58 = add nsw i64 %57, 1
store i64 %58, i64* %8
br label %43
59: ; preds = %43
store i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str.6, i64 0, i64 0), i8** %10
%60 = load i8*, i8** %10
%61 = load i8*, i8** %10
%62 = call i32 @strlen(i8* %61)
%63 = load i32*, i32** %6
%64 = call i32 @fwrite(i8* %60, i32 1, i32 %62, i32* %63)
store i64 0, i64* %8
br label %65
65: ; preds = %78, %59
%66 = load i64, i64* %8
%67 = load i64, i64* %7
%68 = icmp slt i64 %66, %67
br i1 %68, label %69, label %81
69: ; preds = %65
%70 = getelementptr inbounds [32 x i8], [32 x i8]* %9, i64 0, i64 0
%71 = load i64, i64* %8
%72 = call i32 @sprintf(i8* %70, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.7, i64 0, i64 0), i64 %71)
%73 = getelementptr inbounds [32 x i8], [32 x i8]* %9, i64 0, i64 0
%74 = getelementptr inbounds [32 x i8], [32 x i8]* %9, i64 0, i64 0
%75 = call i32 @strlen(i8* %74)
%76 = load i32*, i32** %6
%77 = call i32 @fwrite(i8* %73, i32 1, i32 %75, i32* %76)
br label %78
78: ; preds = %69
%79 = load i64, i64* %8
%80 = add nsw i64 %79, 1
store i64 %80, i64* %8
br label %65
81: ; preds = %65
store i8* getelementptr inbounds ([94 x i8], [94 x i8]* @.str.8, i64 0, i64 0), i8** %10
%82 = load i8*, i8** %10
%83 = load i8*, i8** %10
%84 = call i32 @strlen(i8* %83)
%85 = load i32*, i32** %6
%86 = call i32 @fwrite(i8* %82, i32 1, i32 %84, i32* %85)
%87 = load i32*, i32** %6
%88 = call i32 @fclose(i32* %87)
store i32 0, i32* %3
br label %89
89: ; preds = %81, %40, %25, %13
%90 = load i32, i32* %3
ret i32 %90
}

LLVM IR (-O2):
@.str = [73 x i8] c"Generates benchmarking program for ELF loader\0A\0AUsage: %s symbols [file]\0A\00"
@.str.2 = [2 x i8] c"w\00"
@.str.3 = [12 x i8] c"benchmark.c\00"
@.str.5 = [18 x i8] c"void foo%lx() {}\0A\00"
@.str.6 = [54 x i8] c"typedef void (foofunc)();\0Astatic foofunc *ptrs[] = {\0A\00"
@.str.7 = [13 x i8] c" foo%lx,\0A\00"
@.str.8 = [94 x i8] c"};\0Aint i = 0; //Hack to force gcc to generate a .got section\0Aint _start() { i++; return 0; }\0A\00"
@str = [32 x i8] c"Unable to open file for writing\00"
@str.9 = [15 x i8] c"Invalid number\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [32 x i8]
%4 = getelementptr inbounds [32 x i8], [32 x i8]* %3, i64 0, i64 0
%5 = icmp slt i32 %0, 2
br i1 %5, label %6, label %9
6: ; preds = %2
%7 = load i8*, i8** %1
%8 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([73 x i8], [73 x i8]* @.str, i64 0, i64 0), i8* %7)
br label %48
9: ; preds = %2
%10 = getelementptr inbounds i8*, i8** %1, i64 1
%11 = load i8*, i8** %10
%12 = tail call i64 @atol(i8* %11)
%13 = icmp slt i64 %12, 1
br i1 %13, label %14, label %16
14: ; preds = %9
%15 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str.9, i64 0, i64 0))
br label %48
16: ; preds = %9
%17 = icmp eq i32 %0, 3
br i1 %17, label %18, label %21
18: ; preds = %16
%19 = getelementptr inbounds i8*, i8** %1, i64 2
%20 = load i8*, i8** %19
br label %21
21: ; preds = %16, %18
%22 = phi i8* [ %20, %18 ], [ getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), %16 ]
%23 = tail call i32* @fopen(i8* %22, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%24 = icmp eq i32* %23, null
br i1 %24, label %25, label %27
25: ; preds = %21
%26 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @str, i64 0, i64 0))
br label %48
27: ; preds = %21, %27
%28 = phi i64 [ %32, %27 ], [ 0, %21 ]
%29 = call i32 @sprintf(i8* %4, i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.5, i64 0, i64 0), i64 %28)
%30 = call i32 @strlen(i8* %4)
%31 = call i32 @fwrite(i8* %4, i32 1, i32 %30, i32* %23)
%32 = add nuw nsw i64 %28, 1
%33 = icmp eq i64 %32, %12
br i1 %33, label %34, label %27
34: ; preds = %27
%35 = call i32 @strlen(i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str.6, i64 0, i64 0))
%36 = call i32 @fwrite(i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str.6, i64 0, i64 0), i32 1, i32 %35, i32* %23)
br label %37
37: ; preds = %34, %37
%38 = phi i64 [ %42, %37 ], [ 0, %34 ]
%39 = call i32 @sprintf(i8* %4, i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.7, i64 0, i64 0), i64 %38)
%40 = call i32 @strlen(i8* %4)
%41 = call i32 @fwrite(i8* %4, i32 1, i32 %40, i32* %23)
%42 = add nuw nsw i64 %38, 1
%43 = icmp eq i64 %42, %12
br i1 %43, label %44, label %37
44: ; preds = %37
%45 = call i32 @strlen(i8* getelementptr inbounds ([94 x i8], [94 x i8]* @.str.8, i64 0, i64 0))
%46 = call i32 @fwrite(i8* getelementptr inbounds ([94 x i8], [94 x i8]* @.str.8, i64 0, i64 0), i32 1, i32 %45, i32* %23)
%47 = call i32 @fclose(i32* %23)
br label %48
48: ; preds = %44, %25, %14, %6
%49 = phi i32 [ 0, %6 ], [ 0, %14 ], [ 0, %44 ], [ -1, %25 ]
ret i32 %49
}