#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 fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int fscanf(int /*<<< orphan*/ *, char *, int *);
int /*<<< orphan*/ printf(char *, ...);
int main(int argc, char *argv[]) {
  printf("The command line had %d part(s)\n", argc);
  int i;
  for (i = 0; i < argc; i++)
    printf("argv[%d] = %s\n", i, argv[i]);
  if (argc != 2) {
    printf("\nBad command; should be %s filename\n", argv[0]);
    return 1;
  }
  char *filename = argv[1];
  FILE *in_file;
  in_file = fopen(filename, "r");
  if (in_file == NULL) {
    printf("Couldn't open file %s\n", filename);
    return 1;
  }
  int nbr_values_read;
  int value_read;
  nbr_values_read = fscanf(in_file, "%d", &value_read);
  while (nbr_values_read > 0) {
    printf("Read %d\n", value_read);
    nbr_values_read = fscanf(in_file, "%d", &value_read);
  }
  int close_err = fclose(in_file);
  if (close_err) {
    printf("File close failed!\n");
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%esi
lea    0xea0(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    11a5 <main+0x65>
mov    -0x14(%rbp),%esi
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    (%rax,%rcx,8),%rdx
lea    0xe91(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    116e <main+0x2e>
cmpl   $0x2,-0x8(%rbp)
je     11d0 <main+0x90>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe73(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1292 <main+0x152>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
lea    0xe6e(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1219 <main+0xd9>
mov    -0x20(%rbp),%rsi
lea    0xe51(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1292 <main+0x152>
mov    -0x28(%rbp),%rdi
lea    0xe4a(%rip),%rsi
lea    -0x30(%rbp),%rdx
call   1050 <fscanf@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jle    1267 <main+0x127>
mov    -0x30(%rbp),%esi
lea    0xe2d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x28(%rbp),%rdi
lea    0xe18(%rip),%rsi
lea    -0x30(%rbp),%rdx
call   1050 <fscanf@plt>
mov    %eax,-0x2c(%rbp)
jmp    1230 <main+0xf0>
mov    -0x28(%rbp),%rdi
call   1030 <fclose@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
je     128b <main+0x14b>
lea    0xdf6(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r15
mov    %edi,%ebx
lea    0xe9c(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1050 <printf@plt>
test   %ebx,%ebx
jle    123c <main+0xec>
mov    %ebx,%r12d
lea    0xea2(%rip),%r14
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    (%r15,%rbx,8),%rdx
mov    %r14,%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1050 <printf@plt>
add    $0x1,%rbx
cmp    %rbx,%r12
jne    1190 <main+0x40>
cmp    $0x2,%r12d
jne    123c <main+0xec>
mov    0x8(%r15),%r14
lea    0xe97(%rip),%rsi
mov    %r14,%rdi
call   1070 <fopen@plt>
test   %rax,%rax
je     125e <main+0x10e>
mov    %rax,%rbx
lea    0xe95(%rip),%rsi
lea    0x4(%rsp),%rdx
mov    %rax,%rdi
call   1060 <fscanf@plt>
test   %eax,%eax
jle    1220 <main+0xd0>
lea    0xe80(%rip),%r14
lea    0xe76(%rip),%r15
lea    0x4(%rsp),%r12
nopl   (%rax)
mov    0x4(%rsp),%esi
mov    %r14,%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %r12,%rdx
call   1060 <fscanf@plt>
test   %eax,%eax
jg     1200 <main+0xb0>
mov    %rbx,%rdi
call   1040 <fclose@plt>
test   %eax,%eax
je     1238 <main+0xe8>
lea    0xe47(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
jmp    1252 <main+0x102>
mov    (%r15),%rsi
lea    0xdea(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x1,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
lea    0xdf2(%rip),%rdi
mov    %r14,%rsi
jmp    1246 <main+0xf6>

LLVM IR (-O0):
@.str = [33 x i8] c"The command line had %d part(s)\0A\00"
@.str.1 = [15 x i8] c"argv[%d] = %s\0A\00"
@.str.2 = [37 x i8] c"\0ABad command; should be %s filename\0A\00"
@.str.3 = [2 x i8] c"r\00"
@.str.4 = [23 x i8] c"Couldn't open file %s\0A\00"
@.str.5 = [3 x i8] c"%d\00"
@.str.6 = [9 x i8] c"Read %d\0A\00"
@.str.7 = [20 x i8] c"File close failed!\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32*
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = load i32, i32* %4
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0), i32 %12)
store i32 0, i32* %6
br label %14
14: ; preds = %26, %2
%15 = load i32, i32* %6
%16 = load i32, i32* %4
%17 = icmp slt i32 %15, %16
br i1 %17, label %18, label %29
18: ; preds = %14
%19 = load i32, i32* %6
%20 = load i8**, i8*** %5
%21 = load i32, i32* %6
%22 = sext i32 %21 to i64
%23 = getelementptr inbounds i8*, i8** %20, i64 %22
%24 = load i8*, i8** %23
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i32 %19, i8* %24)
br label %26
26: ; preds = %18
%27 = load i32, i32* %6
%28 = add nsw i32 %27, 1
store i32 %28, i32* %6
br label %14
29: ; preds = %14
%30 = load i32, i32* %4
%31 = icmp ne i32 %30, 2
br i1 %31, label %32, label %37
32: ; preds = %29
%33 = load i8**, i8*** %5
%34 = getelementptr inbounds i8*, i8** %33, i64 0
%35 = load i8*, i8** %34
%36 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.2, i64 0, i64 0), i8* %35)
store i32 1, i32* %3
br label %67
37: ; preds = %29
%38 = load i8**, i8*** %5
%39 = getelementptr inbounds i8*, i8** %38, i64 1
%40 = load i8*, i8** %39
store i8* %40, i8** %7
%41 = load i8*, i8** %7
%42 = call i32* @fopen(i8* %41, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i32* %42, i32** %8
%43 = load i32*, i32** %8
%44 = icmp eq i32* %43, null
br i1 %44, label %45, label %48
45: ; preds = %37
%46 = load i8*, i8** %7
%47 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0), i8* %46)
store i32 1, i32* %3
br label %67
48: ; preds = %37
%49 = load i32*, i32** %8
%50 = call i32 @fscanf(i32* %49, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i32* %10)
store i32 %50, i32* %9
br label %51
51: ; preds = %54, %48
%52 = load i32, i32* %9
%53 = icmp sgt i32 %52, 0
br i1 %53, label %54, label %59
54: ; preds = %51
%55 = load i32, i32* %10
%56 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.6, i64 0, i64 0), i32 %55)
%57 = load i32*, i32** %8
%58 = call i32 @fscanf(i32* %57, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i32* %10)
store i32 %58, i32* %9
br label %51
59: ; preds = %51
%60 = load i32*, i32** %8
%61 = call i32 @fclose(i32* %60)
store i32 %61, i32* %11
%62 = load i32, i32* %11
%63 = icmp ne i32 %62, 0
br i1 %63, label %64, label %66
64: ; preds = %59
%65 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.7, i64 0, i64 0))
br label %66
66: ; preds = %64, %59
store i32 0, i32* %3
br label %67
67: ; preds = %66, %45, %32
%68 = load i32, i32* %3
ret i32 %68
}

LLVM IR (-O2):
@.str = [33 x i8] c"The command line had %d part(s)\0A\00"
@.str.1 = [15 x i8] c"argv[%d] = %s\0A\00"
@.str.2 = [37 x i8] c"\0ABad command; should be %s filename\0A\00"
@.str.3 = [2 x i8] c"r\00"
@.str.4 = [23 x i8] c"Couldn't open file %s\0A\00"
@.str.5 = [3 x i8] c"%d\00"
@.str.6 = [9 x i8] c"Read %d\0A\00"
@str = [19 x i8] c"File close failed!\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i32
%4 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0), i32 %0)
%5 = icmp sgt i32 %0, 0
br i1 %5, label %6, label %18
6: ; preds = %2
%7 = zext i32 %0 to i64
br label %8
8: ; preds = %6, %8
%9 = phi i64 [ 0, %6 ], [ %14, %8 ]
%10 = getelementptr inbounds i8*, i8** %1, i64 %9
%11 = load i8*, i8** %10
%12 = trunc i64 %9 to i32
%13 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i32 %12, i8* %11)
%14 = add nuw nsw i64 %9, 1
%15 = icmp eq i64 %14, %7
br i1 %15, label %16, label %8
16: ; preds = %8
%17 = icmp eq i32 %0, 2
br i1 %17, label %21, label %18
18: ; preds = %2, %16
%19 = load i8*, i8** %1
%20 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @.str.2, i64 0, i64 0), i8* %19)
br label %43
21: ; preds = %16
%22 = getelementptr inbounds i8*, i8** %1, i64 1
%23 = load i8*, i8** %22
%24 = tail call i32* @fopen(i8* %23, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%25 = icmp eq i32* %24, null
br i1 %25, label %26, label %28
26: ; preds = %21
%27 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0), i8* %23)
br label %43
28: ; preds = %21
%29 = bitcast i32* %3 to i8*
%30 = call i32 @fscanf(i32* %24, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i32* %3)
%31 = icmp sgt i32 %30, 0
br i1 %31, label %32, label %37
32: ; preds = %28, %32
%33 = load i32, i32* %3
%34 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.6, i64 0, i64 0), i32 %33)
%35 = call i32 @fscanf(i32* %24, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i32* %3)
%36 = icmp sgt i32 %35, 0
br i1 %36, label %32, label %37
37: ; preds = %32, %28
%38 = call i32 @fclose(i32* %24)
%39 = icmp eq i32 %38, 0
br i1 %39, label %42, label %40
40: ; preds = %37
%41 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str, i64 0, i64 0))
br label %42
42: ; preds = %40, %37
br label %43
43: ; preds = %26, %42, %18
%44 = phi i32 [ 1, %18 ], [ 1, %26 ], [ 0, %42 ]
ret i32 %44
}