#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 */
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ memcpy(char *, char *, int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ putchar(char);
int strlen(char *);
int main(int argc, char **argv) {
  FILE *f = fopen("lib.txt", "r");
  char s[32];
  char strtab[1024];
  int strp = 0, count = 0, i;
  int off[1024];
  if (f == NULL)
    return 2;
  while (fgets(s, 32, f) != NULL) {
    s[strlen(s) - 1] = '\0';
    memcpy(strtab + strp, s, strlen(s) + 1);
    printf("#define\t%s(...)\t((int(*)())libc_call(%d))(__VA_ARGS__)\n", s,
           count);
    off[count] = strp;
    strp += strlen(s) + 1;
    count++;
  }
  printf("uint32_t v_got_plt[%d];\n", count);
  printf("char v_dyn_str[%d] = {\n", strp);
  for (i = 0; i < strp; i++) {
    if (i % 16 == 15)
      putchar('\n');
    printf("%d,", strtab[i]);
  }
  printf("\n};\n");
  printf("char v_dyn_off[%d] = {\n", count);
  for (i = 0; i < count; i++) {
    printf("%d, ", off[i]);
  }
  printf("\n};\n");
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1460,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xe80(%rip),%rdi
lea    0xe81(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x18(%rbp)
movl   $0x0,-0x444(%rbp)
movl   $0x0,-0x448(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11bb <main+0x5b>
movl   $0x2,-0x4(%rbp)
jmp    139d <main+0x23d>
jmp    11c0 <main+0x60>
lea    -0x40(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    $0x20,%esi
call   1060 <fgets@plt>
cmp    $0x0,%rax
je     128b <main+0x12b>
lea    -0x40(%rbp),%rdi
call   1040 <strlen@plt>
sub    $0x1,%eax
cltq
movb   $0x0,-0x40(%rbp,%rax,1)
lea    -0x440(%rbp),%rax
movslq -0x444(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x1460(%rbp)
lea    -0x40(%rbp),%rax
mov    %rax,-0x1458(%rbp)
lea    -0x40(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x1460(%rbp),%rdi
mov    -0x1458(%rbp),%rsi
mov    %eax,%edx
add    $0x1,%edx
call   1070 <memcpy@plt>
lea    -0x40(%rbp),%rsi
mov    -0x448(%rbp),%edx
lea    0xdc6(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x444(%rbp),%ecx
movslq -0x448(%rbp),%rax
mov    %ecx,-0x1450(%rbp,%rax,4)
lea    -0x40(%rbp),%rdi
call   1040 <strlen@plt>
add    $0x1,%eax
add    -0x444(%rbp),%eax
mov    %eax,-0x444(%rbp)
mov    -0x448(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x448(%rbp)
jmp    11c0 <main+0x60>
mov    -0x448(%rbp),%esi
lea    0xdaa(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x444(%rbp),%esi
lea    0xdaf(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x0,-0x44c(%rbp)
mov    -0x44c(%rbp),%eax
cmp    -0x444(%rbp),%eax
jge    1321 <main+0x1c1>
mov    -0x44c(%rbp),%eax
mov    $0x10,%ecx
cltd
idiv   %ecx
cmp    $0xf,%edx
jne    12f0 <main+0x190>
mov    $0xa,%edi
call   1030 <putchar@plt>
movslq -0x44c(%rbp),%rax
movsbl -0x440(%rbp,%rax,1),%esi
ff
lea    0xd6d(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x44c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x44c(%rbp)
jmp    12bd <main+0x15d>
lea    0xd4f(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x448(%rbp),%esi
lea    0xd40(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x0,-0x44c(%rbp)
mov    -0x44c(%rbp),%eax
cmp    -0x448(%rbp),%eax
jge    138f <main+0x22f>
movslq -0x44c(%rbp),%rax
mov    -0x1450(%rbp,%rax,4),%esi
lea    0xd20(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x44c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x44c(%rbp)
jmp    134d <main+0x1ed>
lea    0xce1(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x4(%rbp),%eax
add    $0x1460,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1428,%rsp
lea    0xe78(%rip),%rdi
lea    0xe79(%rip),%rsi
call   1090 <fopen@plt>
test   %rax,%rax
je     1242 <main+0xd2>
mov    %rax,%r14
mov    %rsp,%r15
mov    %r15,%rdi
mov    $0x20,%esi
mov    %rax,%rdx
call   1070 <fgets@plt>
test   %rax,%rax
je     124d <main+0xdd>
lea    0xe47(%rip),%r12
xor    %r13d,%r13d
xor    %ebp,%ebp
nopl   0x0(%rax,%rax,1)
mov    %r15,%rdi
call   1050 <strlen@plt>
cltq
movb   $0x0,-0x1(%rax,%r15,1)
movslq %ebp,%rbx
lea    (%rsp,%rbx,1),%rbp
add    $0x20,%rbp
mov    %r15,%rdi
call   1050 <strlen@plt>
lea    0x1(%rax),%edx
mov    %rbp,%rdi
mov    %r15,%rsi
call   1080 <memcpy@plt>
mov    %r12,%rdi
mov    %r15,%rsi
mov    %r13d,%edx
xor    %eax,%eax
call   1060 <printf@plt>
mov    %ebx,0x420(%rsp,%r13,4)
mov    %r15,%rdi
call   1050 <strlen@plt>
lea    (%rbx,%rax,1),%ebp
add    $0x1,%ebp
add    $0x1,%r13
mov    %r15,%rdi
mov    $0x20,%esi
mov    %r14,%rdx
call   1070 <fgets@plt>
test   %rax,%rax
jne    11d0 <main+0x60>
jmp    1252 <main+0xe2>
mov    $0x2,%r14d
jmp    1316 <main+0x1a6>
xor    %ebp,%ebp
xor    %r13d,%r13d
lea    0xde9(%rip),%rdi
mov    %r13d,%esi
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xdf1(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1060 <printf@plt>
test   %ebp,%ebp
jle    12bc <main+0x14c>
mov    %ebp,%ebp
lea    0xdf3(%rip),%r14
xor    %ebx,%ebx
jmp    12a8 <main+0x138>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
movsbl 0x20(%rsp,%rbx,1),%esi
mov    %r14,%rdi
xor    %eax,%eax
call   1060 <printf@plt>
add    $0x1,%rbx
cmp    %rbx,%rbp
je     12bc <main+0x14c>
mov    %ebx,%eax
not    %eax
test   $0xf,%al
jne    1290 <main+0x120>
mov    $0xa,%edi
call   1030 <putchar@plt>
jmp    1290 <main+0x120>
lea    0xdd1(%rip),%rdi
call   1040 <puts@plt>
lea    0xda8(%rip),%rdi
xor    %r14d,%r14d
mov    %r13d,%esi
xor    %eax,%eax
call   1060 <printf@plt>
test   %r13d,%r13d
je     130a <main+0x19a>
mov    %r13d,%ebp
lea    0xda4(%rip),%r15
xor    %ebx,%ebx
nopl   (%rax)
mov    0x420(%rsp,%rbx,4),%esi
mov    %r15,%rdi
xor    %eax,%eax
call   1060 <printf@plt>
add    $0x1,%rbx
cmp    %rbx,%rbp
jne    12f0 <main+0x180>
lea    0xd83(%rip),%rdi
call   1040 <puts@plt>
mov    %r14d,%eax
add    $0x1428,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [8 x i8] c"lib.txt\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [56 x i8] c"#define\09%s(...)\09((int(*)())libc_call(%d))(__VA_ARGS__)\0A\00"
@.str.3 = [25 x i8] c"uint32_t v_got_plt[%d];\0A\00"
@.str.4 = [24 x i8] c"char v_dyn_str[%d] = {\0A\00"
@.str.5 = [4 x i8] c"%d,\00"
@.str.6 = [5 x i8] c"\0A};\0A\00"
@.str.7 = [24 x i8] c"char v_dyn_off[%d] = {\0A\00"
@.str.8 = [5 x i8] c"%d, \00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32*
%7 = alloca [32 x i8]
%8 = alloca [1024 x i8]
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca [1024 x i32]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%13 = call i32* @fopen(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %13, i32** %6
store i32 0, i32* %9
store i32 0, i32* %10
%14 = load i32*, i32** %6
%15 = icmp eq i32* %14, null
br i1 %15, label %16, label %17
16: ; preds = %2
store i32 2, i32* %3
br label %96
17: ; preds = %2
br label %18
18: ; preds = %23, %17
%19 = getelementptr inbounds [32 x i8], [32 x i8]* %7, i64 0, i64 0
%20 = load i32*, i32** %6
%21 = call i32* @fgets(i8* %19, i32 32, i32* %20)
%22 = icmp ne i32* %21, null
br i1 %22, label %23, label %52
23: ; preds = %18
%24 = getelementptr inbounds [32 x i8], [32 x i8]* %7, i64 0, i64 0
%25 = call i32 @strlen(i8* %24)
%26 = sub nsw i32 %25, 1
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds [32 x i8], [32 x i8]* %7, i64 0, i64 %27
store i8 0, i8* %28
%29 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%30 = load i32, i32* %9
%31 = sext i32 %30 to i64
%32 = getelementptr inbounds i8, i8* %29, i64 %31
%33 = getelementptr inbounds [32 x i8], [32 x i8]* %7, i64 0, i64 0
%34 = getelementptr inbounds [32 x i8], [32 x i8]* %7, i64 0, i64 0
%35 = call i32 @strlen(i8* %34)
%36 = add nsw i32 %35, 1
%37 = call i32 @memcpy(i8* %32, i8* %33, i32 %36)
%38 = getelementptr inbounds [32 x i8], [32 x i8]* %7, i64 0, i64 0
%39 = load i32, i32* %10
%40 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str.2, i64 0, i64 0), i8* %38, i32 %39)
%41 = load i32, i32* %9
%42 = load i32, i32* %10
%43 = sext i32 %42 to i64
%44 = getelementptr inbounds [1024 x i32], [1024 x i32]* %12, i64 0, i64 %43
store i32 %41, i32* %44
%45 = getelementptr inbounds [32 x i8], [32 x i8]* %7, i64 0, i64 0
%46 = call i32 @strlen(i8* %45)
%47 = add nsw i32 %46, 1
%48 = load i32, i32* %9
%49 = add nsw i32 %48, %47
store i32 %49, i32* %9
%50 = load i32, i32* %10
%51 = add nsw i32 %50, 1
store i32 %51, i32* %10
br label %18
52: ; preds = %18
%53 = load i32, i32* %10
%54 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0), i32 %53)
%55 = load i32, i32* %9
%56 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.4, i64 0, i64 0), i32 %55)
store i32 0, i32* %11
br label %57
57: ; preds = %74, %52
%58 = load i32, i32* %11
%59 = load i32, i32* %9
%60 = icmp slt i32 %58, %59
br i1 %60, label %61, label %77
61: ; preds = %57
%62 = load i32, i32* %11
%63 = srem i32 %62, 16
%64 = icmp eq i32 %63, 15
br i1 %64, label %65, label %67
65: ; preds = %61
%66 = call i32 @putchar(i8 signext 10)
br label %67
67: ; preds = %65, %61
%68 = load i32, i32* %11
%69 = sext i32 %68 to i64
%70 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 %69
%71 = load i8, i8* %70
%72 = sext i8 %71 to i32
%73 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0), i32 %72)
br label %74
74: ; preds = %67
%75 = load i32, i32* %11
%76 = add nsw i32 %75, 1
store i32 %76, i32* %11
br label %57
77: ; preds = %57
%78 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.6, i64 0, i64 0))
%79 = load i32, i32* %10
%80 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.7, i64 0, i64 0), i32 %79)
store i32 0, i32* %11
br label %81
81: ; preds = %91, %77
%82 = load i32, i32* %11
%83 = load i32, i32* %10
%84 = icmp slt i32 %82, %83
br i1 %84, label %85, label %94
85: ; preds = %81
%86 = load i32, i32* %11
%87 = sext i32 %86 to i64
%88 = getelementptr inbounds [1024 x i32], [1024 x i32]* %12, i64 0, i64 %87
%89 = load i32, i32* %88
%90 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.8, i64 0, i64 0), i32 %89)
br label %91
91: ; preds = %85
%92 = load i32, i32* %11
%93 = add nsw i32 %92, 1
store i32 %93, i32* %11
br label %81
94: ; preds = %81
%95 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.6, i64 0, i64 0))
br label %96
96: ; preds = %94, %16
%97 = load i32, i32* %3
ret i32 %97
}

LLVM IR (-O2):
@.str = [8 x i8] c"lib.txt\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [56 x i8] c"#define\09%s(...)\09((int(*)())libc_call(%d))(__VA_ARGS__)\0A\00"
@.str.3 = [25 x i8] c"uint32_t v_got_plt[%d];\0A\00"
@.str.4 = [24 x i8] c"char v_dyn_str[%d] = {\0A\00"
@.str.5 = [4 x i8] c"%d,\00"
@.str.7 = [24 x i8] c"char v_dyn_off[%d] = {\0A\00"
@.str.8 = [5 x i8] c"%d, \00"
@str.9 = [4 x i8] c"\0A};\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [32 x i8]
%4 = alloca [1024 x i8]
%5 = alloca [1024 x i32]
%6 = tail call i32* @fopen(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%7 = getelementptr inbounds [32 x i8], [32 x i8]* %3, i64 0, i64 0
%8 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%9 = bitcast [1024 x i32]* %5 to i8*
%10 = icmp eq i32* %6, null
br i1 %10, label %73, label %11
11: ; preds = %2
%12 = call i32* @fgets(i8* %7, i32 32, i32* %6)
%13 = icmp eq i32* %12, null
br i1 %13, label %37, label %14
14: ; preds = %11, %14
%15 = phi i64 [ %32, %14 ], [ 0, %11 ]
%16 = phi i32 [ %31, %14 ], [ 0, %11 ]
%17 = call i32 @strlen(i8* %7)
%18 = add nsw i32 %17, -1
%19 = sext i32 %18 to i64
%20 = getelementptr inbounds [32 x i8], [32 x i8]* %3, i64 0, i64 %19
store i8 0, i8* %20
%21 = sext i32 %16 to i64
%22 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 %21
%23 = call i32 @strlen(i8* %7)
%24 = add nsw i32 %23, 1
%25 = call i32 @memcpy(i8* %22, i8* %7, i32 %24)
%26 = trunc i64 %15 to i32
%27 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([56 x i8], [56 x i8]* @.str.2, i64 0, i64 0), i8* %7, i32 %26)
%28 = getelementptr inbounds [1024 x i32], [1024 x i32]* %5, i64 0, i64 %15
store i32 %16, i32* %28
%29 = call i32 @strlen(i8* %7)
%30 = add i32 %16, 1
%31 = add i32 %30, %29
%32 = add nuw i64 %15, 1
%33 = call i32* @fgets(i8* %7, i32 32, i32* %6)
%34 = icmp eq i32* %33, null
br i1 %34, label %35, label %14
35: ; preds = %14
%36 = trunc i64 %32 to i32
br label %37
37: ; preds = %35, %11
%38 = phi i32 [ 0, %11 ], [ %31, %35 ]
%39 = phi i32 [ 0, %11 ], [ %36, %35 ]
%40 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0), i32 %39)
%41 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.4, i64 0, i64 0), i32 %38)
%42 = icmp sgt i32 %38, 0
br i1 %42, label %43, label %58
43: ; preds = %37
%44 = zext i32 %38 to i64
br label %45
45: ; preds = %43, %51
%46 = phi i64 [ 0, %43 ], [ %56, %51 ]
%47 = and i64 %46, 15
%48 = icmp eq i64 %47, 15
br i1 %48, label %49, label %51
49: ; preds = %45
%50 = call i32 @putchar(i8 signext 10)
br label %51
51: ; preds = %49, %45
%52 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 %46
%53 = load i8, i8* %52
%54 = sext i8 %53 to i32
%55 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0), i32 %54)
%56 = add nuw nsw i64 %46, 1
%57 = icmp eq i64 %56, %44
br i1 %57, label %58, label %45
58: ; preds = %51, %37
%59 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @str.9, i64 0, i64 0))
%60 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.7, i64 0, i64 0), i32 %39)
%61 = icmp eq i32 %39, 0
br i1 %61, label %71, label %62
62: ; preds = %58
%63 = zext i32 %39 to i64
br label %64
64: ; preds = %62, %64
%65 = phi i64 [ 0, %62 ], [ %69, %64 ]
%66 = getelementptr inbounds [1024 x i32], [1024 x i32]* %5, i64 0, i64 %65
%67 = load i32, i32* %66
%68 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.8, i64 0, i64 0), i32 %67)
%69 = add nuw nsw i64 %65, 1
%70 = icmp eq i64 %69, %63
br i1 %70, label %71, label %64
71: ; preds = %64, %58
%72 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @str.9, i64 0, i64 0))
br label %73
73: ; preds = %2, %71
%74 = phi i32 [ 0, %71 ], [ 2, %2 ]
ret i32 %74
}