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

/* Variables and functions */
int BUFSIZ;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
scalar_t__ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, char, ...);
int /*<<< orphan*/ printf(char *, ...);
int main() {
  char *inname = "file.txt";
  char *outname = "fileout.txt";
  FILE *infile;
  FILE *outfile;
  char line_buffer[BUFSIZ];
  char line_number;
  infile = fopen(inname, "r");
  outfile = fopen(outname, "w");
  if (!infile) {
    printf("Couldn't open file %s for reading.\n", inname);
    return 0;
  }
  printf("Opened file %s for reading.\n", inname);
  line_number = 0;
  while (fgets(line_buffer, sizeof(line_buffer), infile)) {
    ++line_number;
    printf("%4d: %s", line_number, line_buffer);
    fprintf(outfile, "%4d: %s", line_number, line_buffer);
  }
  printf("\nTotal number of lines = %d\n", line_number);
  fprintf(outfile, "\nTotal number of lines = %d\n", line_number);
  fclose(infile);
  fclose(outfile);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe9a(%rip),%rax
mov    %rax,-0x10(%rbp)
lea    0xe98(%rip),%rax
mov    %rax,-0x18(%rbp)
mov    0x2e64(%rip),%rax
mov    (%rax),%eax
mov    %rax,-0x50(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x30(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x48(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x38(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe65(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xe53(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11fd <main+0xad>
mov    -0x10(%rbp),%rsi
lea    0xe36(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x40(%rbp)
jmp    12b5 <main+0x165>
mov    -0x10(%rbp),%rsi
lea    0xe35(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movb   $0x0,-0x39(%rbp)
mov    -0x48(%rbp),%rdi
mov    -0x50(%rbp),%rax
mov    %eax,%esi
mov    -0x20(%rbp),%rdx
call   1050 <fgets@plt>
cmp    $0x0,%rax
je     126d <main+0x11d>
mov    -0x48(%rbp),%rdx
mov    -0x39(%rbp),%al
add    $0x1,%al
mov    %al,-0x39(%rbp)
movsbl -0x39(%rbp),%esi
lea    0xe13(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x48(%rbp),%rcx
mov    -0x28(%rbp),%rdi
lea    0xdfd(%rip),%rsi
movsbl -0x39(%rbp),%edx
mov    $0x0,%al
call   1060 <fprintf@plt>
jmp    1213 <main+0xc3>
movsbl -0x39(%rbp),%esi
lea    0xdea(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x28(%rbp),%rdi
lea    0xdd8(%rip),%rsi
movsbl -0x39(%rbp),%edx
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x28(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x40(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    0x2e78(%rip),%rax
mov    (%rax),%ebx
mov    %rsp,%r13
lea    0xf(%rbx),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r13
mov    %r13,%rsp
lea    0xe7e(%rip),%rdi
lea    0xe8c(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,%r15
lea    0xe71(%rip),%rdi
lea    0xe78(%rip),%rsi
call   1070 <fopen@plt>
test   %r15,%r15
je     127d <main+0x12d>
mov    %rax,-0x30(%rbp)
lea    0xe85(%rip),%rdi
lea    0xe41(%rip),%rsi
xor    %r14d,%r14d
xor    %eax,%eax
call   1040 <printf@plt>
mov    %r13,%rdi
mov    %ebx,%esi
mov    %r15,%rdx
call   1050 <fgets@plt>
test   %rax,%rax
je     123f <main+0xef>
xor    %r14d,%r14d
mov    %rbx,-0x38(%rbp)
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
add    $0x1,%r14b
mov    %r15,%rbx
movsbl %r14b,%r15d
lea    0xe58(%rip),%r12
mov    %r12,%rdi
mov    %r15d,%esi
mov    %r13,%rdx
xor    %eax,%eax
call   1040 <printf@plt>
mov    -0x30(%rbp),%rdi
mov    %r12,%rsi
mov    %r15d,%edx
mov    %rbx,%r15
mov    -0x38(%rbp),%rbx
mov    %r13,%rcx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    %r13,%rdi
mov    %ebx,%esi
mov    %r15,%rdx
call   1050 <fgets@plt>
test   %rax,%rax
jne    11f0 <main+0xa0>
movsbl %r14b,%r12d
lea    0xe18(%rip),%r14
mov    %r14,%rdi
mov    %r12d,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    -0x30(%rbp),%rbx
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %r12d,%edx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    %r15,%rdi
call   1030 <fclose@plt>
mov    %rbx,%rdi
call   1030 <fclose@plt>
jmp    1292 <main+0x142>
lea    0xd95(%rip),%rdi
lea    0xd75(%rip),%rsi
xor    %eax,%eax
call   1040 <printf@plt>
xor    %eax,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [9 x i8] c"file.txt\00"
@.str.1 = [12 x i8] c"fileout.txt\00"
@BUFSIZ = global i32 0
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [2 x i8] c"w\00"
@.str.4 = [36 x i8] c"Couldn't open file %s for reading.\0A\00"
@.str.5 = [29 x i8] c"Opened file %s for reading.\0A\00"
@.str.6 = [8 x i8] c"%4d: %s\00"
@.str.7 = [29 x i8] c"\0ATotal number of lines = %d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca i32*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i8
%9 = alloca i32
store i32 0, i32* %1
store i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8** %2
store i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i8** %3
%10 = load i32, i32* @BUFSIZ
%11 = zext i32 %10 to i64
%12 = call i8* @llvm.stacksave()
store i8* %12, i8** %6
%13 = alloca i8, i64 %11
store i64 %11, i64* %7
%14 = load i8*, i8** %2
%15 = call i32* @fopen(i8* %14, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %15, i32** %4
%16 = load i8*, i8** %3
%17 = call i32* @fopen(i8* %16, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i32* %17, i32** %5
%18 = load i32*, i32** %4
%19 = icmp ne i32* %18, null
br i1 %19, label %23, label %20
20: ; preds = %0
%21 = load i8*, i8** %2
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.4, i64 0, i64 0), i8* %21)
store i32 0, i32* %1
store i32 1, i32* %9
br label %51
23: ; preds = %0
%24 = load i8*, i8** %2
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.5, i64 0, i64 0), i8* %24)
store i8 0, i8* %8
br label %26
26: ; preds = %31, %23
%27 = trunc i64 %11 to i32
%28 = load i32*, i32** %4
%29 = call i64 @fgets(i8* %13, i32 %27, i32* %28)
%30 = icmp ne i64 %29, 0
br i1 %30, label %31, label %40
31: ; preds = %26
%32 = load i8, i8* %8
%33 = add i8 %32, 1
store i8 %33, i8* %8
%34 = load i8, i8* %8
%35 = sext i8 %34 to i32
%36 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.6, i64 0, i64 0), i32 %35, i8* %13)
%37 = load i32*, i32** %5
%38 = load i8, i8* %8
%39 = call i32 (i32*, i8*, i8, ...) @fprintf(i32* %37, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.6, i64 0, i64 0), i8 signext %38, i8* %13)
br label %26
40: ; preds = %26
%41 = load i8, i8* %8
%42 = sext i8 %41 to i32
%43 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.7, i64 0, i64 0), i32 %42)
%44 = load i32*, i32** %5
%45 = load i8, i8* %8
%46 = call i32 (i32*, i8*, i8, ...) @fprintf(i32* %44, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.7, i64 0, i64 0), i8 signext %45)
%47 = load i32*, i32** %4
%48 = call i32 @fclose(i32* %47)
%49 = load i32*, i32** %5
%50 = call i32 @fclose(i32* %49)
store i32 0, i32* %1
store i32 1, i32* %9
br label %51
51: ; preds = %40, %20
%52 = load i8*, i8** %6
%53 = load i32, i32* %1
ret i32 %53
}

LLVM IR (-O2):
@.str = [9 x i8] c"file.txt\00"
@.str.1 = [12 x i8] c"fileout.txt\00"
@BUFSIZ = global i32 0
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [2 x i8] c"w\00"
@.str.4 = [36 x i8] c"Couldn't open file %s for reading.\0A\00"
@.str.5 = [29 x i8] c"Opened file %s for reading.\0A\00"
@.str.6 = [8 x i8] c"%4d: %s\00"
@.str.7 = [29 x i8] c"\0ATotal number of lines = %d\0A\00"
define i32 @main() {
%1 = load i32, i32* @BUFSIZ
%2 = zext i32 %1 to i64
%3 = alloca i8, i64 %2
%4 = tail call i32* @fopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%5 = tail call i32* @fopen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%6 = icmp eq i32* %4, null
br i1 %6, label %7, label %9
7: ; preds = %0
%8 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
br label %28
9: ; preds = %0
%10 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
%11 = call i64 @fgets(i8* %3, i32 %1, i32* %4)
%12 = icmp eq i64 %11, 0
br i1 %12, label %21, label %13
13: ; preds = %9, %13
%14 = phi i8 [ %15, %13 ], [ 0, %9 ]
%15 = add i8 %14, 1
%16 = sext i8 %15 to i32
%17 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str.6, i64 0, i64 0), i32 %16, i8* %3)
%18 = call i32 (i32*, i8*, i8, ...) @fprintf(i32* %5, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.6, i64 0, i64 0), i8 signext %15, i8* %3)
%19 = call i64 @fgets(i8* %3, i32 %1, i32* %4)
%20 = icmp eq i64 %19, 0
br i1 %20, label %21, label %13
21: ; preds = %13, %9
%22 = phi i8 [ 0, %9 ], [ %15, %13 ]
%23 = sext i8 %22 to i32
%24 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @.str.7, i64 0, i64 0), i32 %23)
%25 = call i32 (i32*, i8*, i8, ...) @fprintf(i32* %5, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.7, i64 0, i64 0), i8 signext %22)
%26 = call i32 @fclose(i32* %4)
%27 = call i32 @fclose(i32* %5)
br label %28
28: ; preds = %21, %7
ret i32 0
}