#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 EOF;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char);
int putc(char, int /*<<< orphan*/ *);
int main(void) {
  FILE *fp;
  int i;
  int ret;
  fp = fopen("test.txt", "w");
  if (fp == NULL) {
    perror("test.txt");
    return -1;
  }
  for (i = 0; i < 5; i++) {
    putc('A' + i, fp);
  }
  fclose(fp);
  ret = putc('A' + 5, fp);
  if (ret != EOF) {
    printf("ret = \'%c\'\n", (char)ret);
  } else {
    perror("ret == EOF");
    return -2;
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe9a(%rip),%rdi
lea    0xe9c(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1199 <main+0x49>
lea    0xe78(%rip),%rdi
call   1070 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    122c <main+0xdc>
movl   $0x0,-0x14(%rbp)
cmpl   $0x5,-0x14(%rbp)
jge    11ca <main+0x7a>
mov    -0x14(%rbp),%eax
add    $0x41,%eax
mov    -0x10(%rbp),%rsi
movsbl %al,%edi
call   1050 <putc@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    11a0 <main+0x50>
mov    -0x10(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x10(%rbp),%rsi
mov    $0x46,%edi
call   1050 <putc@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%eax
mov    0x2dfa(%rip),%rcx
cmp    (%rcx),%eax
je     120d <main+0xbd>
mov    -0x18(%rbp),%eax
lea    0xe0b(%rip),%rdi
movsbl %al,%esi
call   1040 <printf@plt>
jmp    1225 <main+0xd5>
lea    0xe03(%rip),%rdi
call   1070 <perror@plt>
movl   $0xfffffffe,-0x4(%rbp)
jmp    122c <main+0xdc>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
lea    0xea8(%rip),%rdi
lea    0xeaa(%rip),%rsi
call   1060 <fopen@plt>
test   %rax,%rax
je     11e0 <main+0x90>
mov    %rax,%rbx
mov    $0x41,%edi
mov    %rax,%rsi
call   1050 <putc@plt>
mov    $0x42,%edi
mov    %rbx,%rsi
call   1050 <putc@plt>
mov    $0x43,%edi
mov    %rbx,%rsi
call   1050 <putc@plt>
mov    $0x44,%edi
mov    %rbx,%rsi
call   1050 <putc@plt>
mov    $0x45,%edi
mov    %rbx,%rsi
call   1050 <putc@plt>
mov    %rbx,%rdi
call   1030 <fclose@plt>
mov    $0x46,%edi
mov    %rbx,%rsi
call   1050 <putc@plt>
mov    0x2e1f(%rip),%rcx
cmp    (%rcx),%eax
je     11f3 <main+0xa3>
movsbl %al,%esi
lea    0xe34(%rip),%rdi
call   1040 <printf@plt>
xor    %eax,%eax
pop    %rbx
ret
lea    0xe19(%rip),%rdi
call   1070 <perror@plt>
mov    $0xffffffff,%eax
pop    %rbx
ret
lea    0xe1d(%rip),%rdi
call   1070 <perror@plt>
mov    $0xfffffffe,%eax
pop    %rbx
ret

LLVM IR (-O0):
@.str = [9 x i8] c"test.txt\00"
@.str.1 = [2 x i8] c"w\00"
@EOF = global i32 0
@.str.2 = [12 x i8] c"ret = '%c'\0A\00"
@.str.3 = [11 x i8] c"ret == EOF\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca i32
%4 = alloca i32
store i32 0, i32* %1
%5 = 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.1, i64 0, i64 0))
store i32* %5, i32** %2
%6 = load i32*, i32** %2
%7 = icmp eq i32* %6, null
br i1 %7, label %8, label %10
8: ; preds = %0
%9 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %1
br label %38
10: ; preds = %0
store i32 0, i32* %3
br label %11
11: ; preds = %20, %10
%12 = load i32, i32* %3
%13 = icmp slt i32 %12, 5
br i1 %13, label %14, label %23
14: ; preds = %11
%15 = load i32, i32* %3
%16 = add nsw i32 65, %15
%17 = trunc i32 %16 to i8
%18 = load i32*, i32** %2
%19 = call i32 @putc(i8 signext %17, i32* %18)
br label %20
20: ; preds = %14
%21 = load i32, i32* %3
%22 = add nsw i32 %21, 1
store i32 %22, i32* %3
br label %11
23: ; preds = %11
%24 = load i32*, i32** %2
%25 = call i32 @fclose(i32* %24)
%26 = load i32*, i32** %2
%27 = call i32 @putc(i8 signext 70, i32* %26)
store i32 %27, i32* %4
%28 = load i32, i32* %4
%29 = load i32, i32* @EOF
%30 = icmp ne i32 %28, %29
br i1 %30, label %31, label %35
31: ; preds = %23
%32 = load i32, i32* %4
%33 = trunc i32 %32 to i8
%34 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i8 signext %33)
br label %37
35: ; preds = %23
%36 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0))
store i32 -2, i32* %1
br label %38
37: ; preds = %31
store i32 0, i32* %1
br label %38
38: ; preds = %37, %35, %8
%39 = load i32, i32* %1
ret i32 %39
}

LLVM IR (-O2):
@.str = [9 x i8] c"test.txt\00"
@.str.1 = [2 x i8] c"w\00"
@EOF = global i32 0
@.str.2 = [12 x i8] c"ret = '%c'\0A\00"
@.str.3 = [11 x i8] c"ret == EOF\00"
define i32 @main() {
%1 = 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.1, i64 0, i64 0))
%2 = icmp eq i32* %1, null
br i1 %2, label %13, label %3
3: ; preds = %0
%4 = tail call i32 @putc(i8 signext 65, i32* %1)
%5 = tail call i32 @putc(i8 signext 66, i32* %1)
%6 = tail call i32 @putc(i8 signext 67, i32* %1)
%7 = tail call i32 @putc(i8 signext 68, i32* %1)
%8 = tail call i32 @putc(i8 signext 69, i32* %1)
%9 = tail call i32 @fclose(i32* %1)
%10 = tail call i32 @putc(i8 signext 70, i32* %1)
%11 = load i32, i32* @EOF
%12 = icmp eq i32 %10, %11
br i1 %12, label %18, label %15
13: ; preds = %0
%14 = tail call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
br label %20
15: ; preds = %3
%16 = trunc i32 %10 to i8
%17 = tail call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i8 signext %16)
br label %20
18: ; preds = %3
%19 = tail call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0))
br label %20
20: ; preds = %15, %18, %13
%21 = phi i32 [ -1, %13 ], [ 0, %15 ], [ -2, %18 ]
ret i32 %21
}