#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*/ exit(int);
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, char *);
scalar_t__ malloc(int);
scalar_t__ realloc(char **, int);
int /*<<< orphan*/ *stderr;
char *strdup(char *);
int main(int argc, char *argv[]) {
  int i, j;
  char buf[5000];
  char **lp = (char **)malloc(100 * sizeof(char *));
  int nbuf = 100;
  FILE *infp;
  FILE *outfp;
  char *infile = "../../src/bib";
  char *outfile = "foo";
  if (argc > 1)
    infile = argv[1];
  if (argc > 2)
    outfile = argv[2];
  infp = fopen(infile, "r");
  if (infp == NULL) {
    fprintf(stderr, "can't open %s\n", infile);
    exit(1);
  }
  outfp = fopen(outfile, "w");
  for (i = 0; fgets(buf, sizeof buf, infp) != NULL; i++) {
    if (i >= nbuf) {
      nbuf *= 2;
      lp = (char **)realloc(lp, nbuf * (sizeof(char *)));
    }
    lp[i] = strdup(buf);
  }
  for (j = i - 1; j >= 0; j--)
    fprintf(outfp, "%s", lp[j]);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x13d0,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0x320,%edi
call   1050 <malloc@plt>
mov    %rax,-0x13a8(%rbp)
movl   $0x64,-0x13ac(%rbp)
lea    0xe55(%rip),%rax
mov    %rax,-0x13c8(%rbp)
lea    0xe55(%rip),%rax
mov    %rax,-0x13d0(%rbp)
cmpl   $0x1,-0x8(%rbp)
jle    11d9 <main+0x69>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x13c8(%rbp)
cmpl   $0x2,-0x8(%rbp)
jle    11f2 <main+0x82>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rax
mov    %rax,-0x13d0(%rbp)
mov    -0x13c8(%rbp),%rdi
lea    0xe12(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x13b8(%rbp)
cmpq   $0x0,-0x13b8(%rbp)
jne    1241 <main+0xd1>
mov    0x2dbf(%rip),%rax
mov    (%rax),%rdi
mov    -0x13c8(%rbp),%rdx
lea    0xde2(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x13d0(%rbp),%rdi
lea    0xdd4(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x13c0(%rbp)
movl   $0x0,-0x14(%rbp)
lea    -0x13a0(%rbp),%rdi
mov    -0x13b8(%rbp),%rdx
mov    $0x1388,%esi
call   1030 <fgets@plt>
cmp    $0x0,%rax
je     12ee <main+0x17e>
mov    -0x14(%rbp),%eax
cmp    -0x13ac(%rbp),%eax
jl     12c2 <main+0x152>
mov    -0x13ac(%rbp),%eax
shl    $0x1,%eax
mov    %eax,-0x13ac(%rbp)
mov    -0x13a8(%rbp),%rdi
movslq -0x13ac(%rbp),%rax
shl    $0x3,%rax
mov    %eax,%esi
call   1060 <realloc@plt>
mov    %rax,-0x13a8(%rbp)
lea    -0x13a0(%rbp),%rdi
call   1090 <strdup@plt>
mov    %rax,%rdx
mov    -0x13a8(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    %rdx,(%rax,%rcx,8)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1262 <main+0xf2>
mov    -0x14(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jl     1331 <main+0x1c1>
mov    -0x13c0(%rbp),%rdi
mov    -0x13a8(%rbp),%rax
movslq -0x18(%rbp),%rcx
mov    (%rax,%rcx,8),%rdx
lea    0xd07(%rip),%rsi
call   1040 <fprintf@plt>
mov    -0x18(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x18(%rbp)
jmp    12f7 <main+0x187>
xor    %eax,%eax
add    $0x13d0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1398,%rsp
mov    %rsi,%rbx
mov    %edi,%ebp
mov    $0x320,%edi
call   1060 <malloc@plt>
mov    %rax,%r13
lea    0xe64(%rip),%r12
cmp    $0x2,%ebp
jl     11bb <main+0x3b>
mov    0x8(%rbx),%r14
je     11c2 <main+0x42>
mov    0x10(%rbx),%r12
jmp    11c2 <main+0x42>
lea    0xe3e(%rip),%r14
lea    0xe49(%rip),%rsi
mov    %r14,%rdi
call   1080 <fopen@plt>
test   %rax,%rax
je     12ae <main+0x12e>
mov    %rax,%r15
lea    0xe3f(%rip),%rsi
mov    %r12,%rdi
call   1080 <fopen@plt>
mov    %rax,%rbx
lea    0x10(%rsp),%rdi
mov    $0x1388,%esi
mov    %r15,%rdx
call   1040 <fgets@plt>
test   %rax,%rax
je     129a <main+0x11a>
mov    %rbx,0x8(%rsp)
mov    $0x64,%ebx
xor    %ebp,%ebp
lea    0x10(%rsp),%r12
jmp    1246 <main+0xc6>
nopl   (%rax)
mov    %r12,%rdi
call   10a0 <strdup@plt>
mov    %rax,0x0(%r13,%rbp,8)
add    $0x1,%rbp
mov    %r12,%rdi
mov    $0x1388,%esi
mov    %r15,%rdx
call   1040 <fgets@plt>
test   %rax,%rax
je     1267 <main+0xe7>
movslq %ebx,%rax
cmp    %rax,%rbp
jl     1220 <main+0xa0>
lea    (%rbx,%rbx,1),%r14d
shl    $0x4,%ebx
mov    %r13,%rdi
mov    %ebx,%esi
call   1070 <realloc@plt>
mov    %rax,%r13
mov    %r14d,%ebx
jmp    1220 <main+0xa0>
test   %ebp,%ebp
mov    0x8(%rsp),%r14
jle    129a <main+0x11a>
mov    %ebp,%ebx
add    $0x1,%rbx
cs nopw 0x0(%rax,%rax,1)
lea    -0x2(%rbx),%eax
mov    0x0(%r13,%rax,8),%rdi
mov    %r14,%rsi
call   1030 <fputs@plt>
add    $0xffffffffffffffff,%rbx
cmp    $0x1,%rbx
ja     1280 <main+0x100>
xor    %eax,%eax
add    $0x1398,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2d2b(%rip),%rax
mov    (%rax),%rdi
lea    0xd55(%rip),%rsi
mov    %r14,%rdx
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [14 x i8] c"../../src/bib\00"
@.str.1 = [4 x i8] c"foo\00"
@.str.2 = [2 x i8] c"r\00"
@stderr = global i32* null
@.str.3 = [15 x i8] c"can't open %s\0A\00"
@.str.4 = [2 x i8] c"w\00"
@.str.5 = [3 x i8] c"%s\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca [5000 x i8]
%9 = alloca i8**
%10 = alloca i32
%11 = alloca i32*
%12 = alloca i32*
%13 = alloca i8*
%14 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%15 = call i64 @malloc(i32 800)
%16 = inttoptr i64 %15 to i8**
store i8** %16, i8*** %9
store i32 100, i32* %10
store i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8** %13
store i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8** %14
%17 = load i32, i32* %4
%18 = icmp sgt i32 %17, 1
br i1 %18, label %19, label %23
19: ; preds = %2
%20 = load i8**, i8*** %5
%21 = getelementptr inbounds i8*, i8** %20, i64 1
%22 = load i8*, i8** %21
store i8* %22, i8** %13
br label %23
23: ; preds = %19, %2
%24 = load i32, i32* %4
%25 = icmp sgt i32 %24, 2
br i1 %25, label %26, label %30
26: ; preds = %23
%27 = load i8**, i8*** %5
%28 = getelementptr inbounds i8*, i8** %27, i64 2
%29 = load i8*, i8** %28
store i8* %29, i8** %14
br label %30
30: ; preds = %26, %23
%31 = load i8*, i8** %13
%32 = call i32* @fopen(i8* %31, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %32, i32** %11
%33 = load i32*, i32** %11
%34 = icmp eq i32* %33, null
br i1 %34, label %35, label %40
35: ; preds = %30
%36 = load i32*, i32** @stderr
%37 = load i8*, i8** %13
%38 = call i32 @fprintf(i32* %36, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0), i8* %37)
%39 = call i32 @exit(i32 1)
unreachable
40: ; preds = %30
%41 = load i8*, i8** %14
%42 = call i32* @fopen(i8* %41, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
store i32* %42, i32** %12
store i32 0, i32* %6
br label %43
43: ; preds = %69, %40
%44 = getelementptr inbounds [5000 x i8], [5000 x i8]* %8, i64 0, i64 0
%45 = load i32*, i32** %11
%46 = call i32* @fgets(i8* %44, i32 5000, i32* %45)
%47 = icmp ne i32* %46, null
br i1 %47, label %48, label %72
48: ; preds = %43
%49 = load i32, i32* %6
%50 = load i32, i32* %10
%51 = icmp sge i32 %49, %50
br i1 %51, label %52, label %62
52: ; preds = %48
%53 = load i32, i32* %10
%54 = mul nsw i32 %53, 2
store i32 %54, i32* %10
%55 = load i8**, i8*** %9
%56 = load i32, i32* %10
%57 = sext i32 %56 to i64
%58 = mul i64 %57, 8
%59 = trunc i64 %58 to i32
%60 = call i64 @realloc(i8** %55, i32 %59)
%61 = inttoptr i64 %60 to i8**
store i8** %61, i8*** %9
br label %62
62: ; preds = %52, %48
%63 = getelementptr inbounds [5000 x i8], [5000 x i8]* %8, i64 0, i64 0
%64 = call i8* @strdup(i8* %63)
%65 = load i8**, i8*** %9
%66 = load i32, i32* %6
%67 = sext i32 %66 to i64
%68 = getelementptr inbounds i8*, i8** %65, i64 %67
store i8* %64, i8** %68
br label %69
69: ; preds = %62
%70 = load i32, i32* %6
%71 = add nsw i32 %70, 1
store i32 %71, i32* %6
br label %43
72: ; preds = %43
%73 = load i32, i32* %6
%74 = sub nsw i32 %73, 1
store i32 %74, i32* %7
br label %75
75: ; preds = %86, %72
%76 = load i32, i32* %7
%77 = icmp sge i32 %76, 0
br i1 %77, label %78, label %89
78: ; preds = %75
%79 = load i32*, i32** %12
%80 = load i8**, i8*** %9
%81 = load i32, i32* %7
%82 = sext i32 %81 to i64
%83 = getelementptr inbounds i8*, i8** %80, i64 %82
%84 = load i8*, i8** %83
%85 = call i32 @fprintf(i32* %79, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i8* %84)
br label %86
86: ; preds = %78
%87 = load i32, i32* %7
%88 = add nsw i32 %87, -1
store i32 %88, i32* %7
br label %75
89: ; preds = %75
ret i32 0
}

LLVM IR (-O2):
@.str = [14 x i8] c"../../src/bib\00"
@.str.1 = [4 x i8] c"foo\00"
@.str.2 = [2 x i8] c"r\00"
@stderr = global i32* null
@.str.3 = [15 x i8] c"can't open %s\0A\00"
@.str.4 = [2 x i8] c"w\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [5000 x i8]
%4 = getelementptr inbounds [5000 x i8], [5000 x i8]* %3, i64 0, i64 0
%5 = tail call i64 @malloc(i32 800)
%6 = inttoptr i64 %5 to i8**
%7 = icmp sgt i32 %0, 1
br i1 %7, label %8, label %15
8: ; preds = %2
%9 = getelementptr inbounds i8*, i8** %1, i64 1
%10 = load i8*, i8** %9
%11 = icmp eq i32 %0, 2
br i1 %11, label %15, label %12
12: ; preds = %8
%13 = getelementptr inbounds i8*, i8** %1, i64 2
%14 = load i8*, i8** %13
br label %15
15: ; preds = %2, %12, %8
%16 = phi i8* [ %10, %12 ], [ %10, %8 ], [ getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), %2 ]
%17 = phi i8* [ %14, %12 ], [ getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), %8 ], [ getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), %2 ]
%18 = tail call i32* @fopen(i8* %16, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%19 = icmp eq i32* %18, null
br i1 %19, label %20, label %24
20: ; preds = %15
%21 = load i32*, i32** @stderr
%22 = tail call i32 @fprintf(i32* %21, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0), i8* %16)
%23 = tail call i32 @exit(i32 1)
unreachable
24: ; preds = %15
%25 = tail call i32* @fopen(i8* %17, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%26 = call i32* @fgets(i8* %4, i32 5000, i32* %18)
%27 = icmp eq i32* %26, null
br i1 %27, label %60, label %33
28: ; preds = %44
%29 = trunc i64 %49 to i32
%30 = icmp sgt i32 %29, 0
br i1 %30, label %31, label %60
31: ; preds = %28
%32 = and i64 %49, 4294967295
br label %52
33: ; preds = %24, %44
%34 = phi i64 [ %49, %44 ], [ 0, %24 ]
%35 = phi i32 [ %46, %44 ], [ 100, %24 ]
%36 = phi i8** [ %45, %44 ], [ %6, %24 ]
%37 = sext i32 %35 to i64
%38 = icmp slt i64 %34, %37
br i1 %38, label %44, label %39
39: ; preds = %33
%40 = shl nsw i32 %35, 1
%41 = shl i32 %35, 4
%42 = call i64 @realloc(i8** %36, i32 %41)
%43 = inttoptr i64 %42 to i8**
br label %44
44: ; preds = %39, %33
%45 = phi i8** [ %43, %39 ], [ %36, %33 ]
%46 = phi i32 [ %40, %39 ], [ %35, %33 ]
%47 = call i8* @strdup(i8* %4)
%48 = getelementptr inbounds i8*, i8** %45, i64 %34
store i8* %47, i8** %48
%49 = add nuw i64 %34, 1
%50 = call i32* @fgets(i8* %4, i32 5000, i32* %18)
%51 = icmp eq i32* %50, null
br i1 %51, label %28, label %33
52: ; preds = %31, %52
%53 = phi i64 [ %32, %31 ], [ %54, %52 ]
%54 = add nsw i64 %53, -1
%55 = and i64 %54, 4294967295
%56 = getelementptr inbounds i8*, i8** %45, i64 %55
%57 = load i8*, i8** %56
%58 = call i32 @fputs(i8* %57, i32* %25)
%59 = icmp ugt i64 %53, 1
br i1 %59, label %52, label %60
60: ; preds = %52, %24, %28
ret i32 0
}