#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 atoi(char *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, int);
int /*<<< orphan*/ fputs(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ stdout;
int main() {
  FILE *fp;
  char fname[] = "data.csv";
  int in = 0;
  char s[5];
  printf("Practice 05\n");
  do {
    fputs("in > ", stdout);
    fgets(s, 5, stdin);
    in = atoi(s);
    printf("in=%d\n", in);
    fp = fopen(fname, "a");
    if (fp == NULL) {
      printf("ERROR\n");
      return -1;
    }
    fprintf(fp, "%d\n", in);
    fclose(fp);
  } while (in > 0);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    0xe7a(%rip),%rax
mov    %rax,-0x19(%rbp)
mov    0xe78(%rip),%al
mov    %al,-0x11(%rbp)
movl   $0x0,-0x20(%rbp)
lea    0xe68(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2e29(%rip),%rax
mov    (%rax),%esi
lea    0xe5e(%rip),%rdi
call   1050 <fputs@plt>
lea    -0x25(%rbp),%rdi
mov    0x2e20(%rip),%rax
mov    (%rax),%edx
mov    $0x5,%esi
call   1060 <fgets@plt>
lea    -0x25(%rbp),%rdi
call   1090 <atoi@plt>
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%esi
lea    0xe32(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0x19(%rbp),%rdi
lea    0xe27(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    122a <main+0xba>
lea    0xe0e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1257 <main+0xe7>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%edx
lea    0xde7(%rip),%rsi
call   1070 <fprintf@plt>
mov    -0x10(%rbp),%rdi
call   1030 <fclose@plt>
cmpl   $0x0,-0x20(%rbp)
jg     11a8 <main+0x38>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
movabs $0x7673632e61746164,%rax
63 73 76
mov    %rax,0x8(%rsp)
movb   $0x0,0x10(%rsp)
lea    0xe6f(%rip),%rdi
call   1030 <puts@plt>
lea    0x13(%rsp),%rbx
lea    0xe5c(%rip),%r12
lea    0x8(%rsp),%r13
lea    0xe4c(%rip),%rbp
cs nopw 0x0(%rax,%rax,1)
mov    0x2e01(%rip),%rax
mov    (%rax),%esi
lea    0xe29(%rip),%rdi
call   1060 <fputs@plt>
mov    0x2dfc(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
mov    $0x5,%esi
call   1070 <fgets@plt>
mov    %rbx,%rdi
call   10a0 <atoi@plt>
mov    %eax,%r14d
lea    0xe02(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %r13,%rdi
mov    %r12,%rsi
call   1090 <fopen@plt>
test   %rax,%rax
je     1248 <main+0xc8>
mov    %rax,%r15
mov    %rax,%rdi
mov    %rbp,%rsi
mov    %r14d,%edx
call   1080 <fprintf@plt>
mov    %r15,%rdi
call   1040 <fclose@plt>
test   %r14d,%r14d
jg     11d0 <main+0x50>
xor    %eax,%eax
jmp    1259 <main+0xd9>
lea    0xdd5(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@__const.main.fname = [9 x i8] c"data.csv\00"
@.str = [13 x i8] c"Practice 05\0A\00"
@.str.1 = [6 x i8] c"in > \00"
@stdout = global i32 0
@stdin = global i32 0
@.str.2 = [7 x i8] c"in=%d\0A\00"
@.str.3 = [2 x i8] c"a\00"
@.str.4 = [7 x i8] c"ERROR\0A\00"
@.str.5 = [4 x i8] c"%d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca [9 x i8]
%4 = alloca i32
%5 = alloca [5 x i8]
store i32 0, i32* %1
%6 = bitcast [9 x i8]* %3 to i8*
store i32 0, i32* %4
%7 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
br label %8
8: ; preds = %30, %0
%9 = load i32, i32* @stdout
%10 = call i32 @fputs(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i32 %9)
%11 = getelementptr inbounds [5 x i8], [5 x i8]* %5, i64 0, i64 0
%12 = load i32, i32* @stdin
%13 = call i32 @fgets(i8* %11, i32 5, i32 %12)
%14 = getelementptr inbounds [5 x i8], [5 x i8]* %5, i64 0, i64 0
%15 = call i32 @atoi(i8* %14)
store i32 %15, i32* %4
%16 = load i32, i32* %4
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i32 %16)
%18 = getelementptr inbounds [9 x i8], [9 x i8]* %3, i64 0, i64 0
%19 = call i32* @fopen(i8* %18, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i32* %19, i32** %2
%20 = load i32*, i32** %2
%21 = icmp eq i32* %20, null
br i1 %21, label %22, label %24
22: ; preds = %8
%23 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
store i32 -1, i32* %1
br label %34
24: ; preds = %8
%25 = load i32*, i32** %2
%26 = load i32, i32* %4
%27 = call i32 @fprintf(i32* %25, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0), i32 %26)
%28 = load i32*, i32** %2
%29 = call i32 @fclose(i32* %28)
br label %30
30: ; preds = %24
%31 = load i32, i32* %4
%32 = icmp sgt i32 %31, 0
br i1 %32, label %8, label %33
33: ; preds = %30
store i32 0, i32* %1
br label %34
34: ; preds = %33, %22
%35 = load i32, i32* %1
ret i32 %35
}

LLVM IR (-O2):
@__const.main.fname = [9 x i8] c"data.csv\00"
@.str.1 = [6 x i8] c"in > \00"
@stdout = global i32 0
@stdin = global i32 0
@.str.2 = [7 x i8] c"in=%d\0A\00"
@.str.3 = [2 x i8] c"a\00"
@.str.5 = [4 x i8] c"%d\0A\00"
@str = [12 x i8] c"Practice 05\00"
@str.6 = [6 x i8] c"ERROR\00"
define i32 @main() {
%1 = alloca [9 x i8]
%2 = alloca [5 x i8]
%3 = getelementptr inbounds [9 x i8], [9 x i8]* %1, i64 0, i64 0
%4 = getelementptr inbounds [5 x i8], [5 x i8]* %2, i64 0, i64 0
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str, i64 0, i64 0))
br label %6
6: ; preds = %17, %0
%7 = load i32, i32* @stdout
%8 = call i32 @fputs(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i32 %7)
%9 = load i32, i32* @stdin
%10 = call i32 @fgets(i8* %4, i32 5, i32 %9)
%11 = call i32 @atoi(i8* %4)
%12 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i32 %11)
%13 = call i32* @fopen(i8* %3, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%14 = icmp eq i32* %13, null
br i1 %14, label %15, label %17
15: ; preds = %6
%16 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @str.6, i64 0, i64 0))
br label %21
17: ; preds = %6
%18 = call i32 @fprintf(i32* %13, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0), i32 %11)
%19 = call i32 @fclose(i32* %13)
%20 = icmp sgt i32 %11, 0
br i1 %20, label %6, label %21
21: ; preds = %17, %15
%22 = phi i32 [ -1, %15 ], [ 0, %17 ]
ret i32 %22
}