#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 */

/* Variables and functions */
int EOF;
int /*<<< orphan*/ O_APPEND;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *);
int scanf(char *, int *);
int /*<<< orphan*/ sprintf(char *, char *, char *, int);
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ strlen(char *);
int /*<<< orphan*/ write(int, char *, int /*<<< orphan*/);
int main(int argc, char const *argv[]) {
  int file = open("file.txt", O_APPEND);
  char name[50] = "";
  int grade = 0;
  bool success = true;
  while (success) {
    int error;
    printf("name? ");
    fgets(name, 50, stdin);
    if (name == NULL) {
      success = false;
      break;
    }
    printf("grade? ");
    error = scanf("%d", &grade);
    if (error == EOF) {
      success = false;
      break;
    }
    char value[70];
    sprintf(value, "%s, %d", name, grade);
    write(file, value, strlen(value));
  }
  close(file);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0xc0,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e18(%rip),%rax
mov    (%rax),%esi
lea    0xe47(%rip),%rdi
call   10a0 <open@plt>
mov    %eax,-0x14(%rbp)
lea    -0x50(%rbp),%rdi
xor    %esi,%esi
mov    $0x32,%edx
call   1060 <memset@plt>
movl   $0x0,-0x54(%rbp)
movl   $0x1,-0x58(%rbp)
cmpl   $0x0,-0x58(%rbp)
je     129f <main+0x10f>
lea    0xe19(%rip),%rdi
call   1050 <printf@plt>
lea    -0x50(%rbp),%rdi
mov    0x2de8(%rip),%rax
mov    (%rax),%edx
mov    $0x32,%esi
call   1080 <fgets@plt>
lea    0xdfd(%rip),%rdi
call   1050 <printf@plt>
lea    0xdfd(%rip),%rdi
lea    -0x54(%rbp),%rsi
call   1090 <scanf@plt>
mov    %eax,-0x5c(%rbp)
mov    -0x5c(%rbp),%eax
mov    0x2dab(%rip),%rcx
cmp    (%rcx),%eax
jne    1249 <main+0xb9>
movl   $0x0,-0x58(%rbp)
jmp    129f <main+0x10f>
lea    -0xb0(%rbp),%rdi
lea    -0x50(%rbp),%rdx
mov    -0x54(%rbp),%ecx
lea    0xdba(%rip),%rsi
call   10b0 <sprintf@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0xbc(%rbp)
lea    -0xb0(%rbp),%rax
mov    %rax,-0xb8(%rbp)
lea    -0xb0(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0xbc(%rbp),%edi
mov    -0xb8(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
jmp    11df <main+0x4f>
mov    -0x14(%rbp),%edi
call   1070 <close@plt>
xor    %eax,%eax
add    $0xc0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x98,%rsp
mov    0x2e30(%rip),%rax
mov    (%rax),%esi
lea    0xe5f(%rip),%rdi
call   1090 <open@plt>
mov    %eax,%r15d
xorps  %xmm0,%xmm0
movaps %xmm0,0x30(%rsp)
movaps %xmm0,0x20(%rsp)
movaps %xmm0,0x10(%rsp)
movw   $0x0,0x40(%rsp)
movl   $0x0,0xc(%rsp)
lea    0xe38(%rip),%rdi
call   1050 <printf@plt>
mov    0x2e0b(%rip),%rax
mov    (%rax),%edx
lea    0x10(%rsp),%rdi
mov    $0x32,%esi
call   1070 <fgets@plt>
lea    0xe1b(%rip),%rdi
call   1050 <printf@plt>
lea    0xe1b(%rip),%rdi
lea    0xc(%rsp),%rsi
call   1080 <scanf@plt>
mov    0x2dce(%rip),%r13
cmp    0x0(%r13),%eax
je     12aa <main+0x12a>
lea    0x50(%rsp),%rbx
lea    0x10(%rsp),%rbp
lea    0xdef(%rip),%r14
lea    0xc(%rsp),%r12
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    0xc(%rsp),%ecx
mov    %rbx,%rdi
lea    0xdca(%rip),%rsi
mov    %rbp,%rdx
call   10a0 <sprintf@plt>
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %r15d,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
lea    0xd97(%rip),%rdi
call   1050 <printf@plt>
mov    0x2d6a(%rip),%rax
mov    (%rax),%edx
mov    %rbp,%rdi
mov    $0x32,%esi
call   1070 <fgets@plt>
lea    0xd7c(%rip),%rdi
call   1050 <printf@plt>
mov    %r14,%rdi
mov    %r12,%rsi
call   1080 <scanf@plt>
cmp    0x0(%r13),%eax
jne    1240 <main+0xc0>
mov    %r15d,%edi
call   1060 <close@plt>
xor    %eax,%eax
add    $0x98,%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"
@O_APPEND = global i32 0
@.str.1 = [7 x i8] c"name? \00"
@stdin = global i32 0
@.str.2 = [8 x i8] c"grade? \00"
@.str.3 = [3 x i8] c"%d\00"
@EOF = global i32 0
@.str.4 = [7 x i8] c"%s, %d\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca [50 x i8]
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca [70 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = load i32, i32* @O_APPEND
%13 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %12)
store i32 %13, i32* %6
%14 = bitcast [50 x i8]* %7 to i8*
store i32 0, i32* %8
store i32 1, i32* %9
br label %15
15: ; preds = %29, %2
%16 = load i32, i32* %9
%17 = icmp ne i32 %16, 0
br i1 %17, label %18, label %39
18: ; preds = %15
%19 = call i32 @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%20 = getelementptr inbounds [50 x i8], [50 x i8]* %7, i64 0, i64 0
%21 = load i32, i32* @stdin
%22 = call i32 @fgets(i8* %20, i32 50, i32 %21)
%23 = call i32 @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
%24 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32* %8)
store i32 %24, i32* %10
%25 = load i32, i32* %10
%26 = load i32, i32* @EOF
%27 = icmp eq i32 %25, %26
br i1 %27, label %28, label %29
28: ; preds = %18
store i32 0, i32* %9
br label %39
29: ; preds = %18
%30 = getelementptr inbounds [70 x i8], [70 x i8]* %11, i64 0, i64 0
%31 = getelementptr inbounds [50 x i8], [50 x i8]* %7, i64 0, i64 0
%32 = load i32, i32* %8
%33 = call i32 @sprintf(i8* %30, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0), i8* %31, i32 %32)
%34 = load i32, i32* %6
%35 = getelementptr inbounds [70 x i8], [70 x i8]* %11, i64 0, i64 0
%36 = getelementptr inbounds [70 x i8], [70 x i8]* %11, i64 0, i64 0
%37 = call i32 @strlen(i8* %36)
%38 = call i32 @write(i32 %34, i8* %35, i32 %37)
br label %15
39: ; preds = %28, %15
%40 = load i32, i32* %6
%41 = call i32 @close(i32 %40)
ret i32 0
}

LLVM IR (-O2):
@.str = [9 x i8] c"file.txt\00"
@O_APPEND = global i32 0
@.str.1 = [7 x i8] c"name? \00"
@stdin = global i32 0
@.str.2 = [8 x i8] c"grade? \00"
@.str.3 = [3 x i8] c"%d\00"
@EOF = global i32 0
@.str.4 = [7 x i8] c"%s, %d\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [50 x i8]
%4 = alloca i32
%5 = alloca [70 x i8]
%6 = load i32, i32* @O_APPEND
%7 = tail call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %6)
%8 = getelementptr inbounds [50 x i8], [50 x i8]* %3, i64 0, i64 0
%9 = bitcast i32* %4 to i8*
store i32 0, i32* %4
%10 = call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%11 = load i32, i32* @stdin
%12 = call i32 @fgets(i8* %8, i32 50, i32 %11)
%13 = call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
%14 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32* %4)
%15 = load i32, i32* @EOF
%16 = icmp eq i32 %14, %15
br i1 %16, label %31, label %17
17: ; preds = %2
%18 = getelementptr inbounds [70 x i8], [70 x i8]* %5, i64 0, i64 0
br label %19
19: ; preds = %17, %19
%20 = load i32, i32* %4
%21 = call i32 @sprintf(i8* %18, i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0), i8* %8, i32 %20)
%22 = call i32 @strlen(i8* %18)
%23 = call i32 @write(i32 %7, i8* %18, i32 %22)
%24 = call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%25 = load i32, i32* @stdin
%26 = call i32 @fgets(i8* %8, i32 50, i32 %25)
%27 = call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
%28 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32* %4)
%29 = load i32, i32* @EOF
%30 = icmp eq i32 %28, %29
br i1 %30, label %31, label %19
31: ; preds = %19, %2
%32 = call i32 @close(i32 %7)
ret i32 0
}