#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 /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ feof(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ fscanf(int /*<<< orphan*/ *, char *, char *);
char *malloc(int);
int /*<<< orphan*/ printf(char *, ...);
void file_read(void) {
  FILE *f = fopen("tmp", "r");
  if (f == NULL) {
    printf("IO error\n");
    exit(1);
  }
  while (!feof(f)) {
    char *buffer = malloc(sizeof(char));
    fscanf(f, "%s", buffer);
    printf("%s\n", buffer);
    free(buffer);
  }
  fclose(f);
}


x86 assembly (clang, -O0):
<file_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
lea    0xe71(%rip),%rdi
lea    0xe6e(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    11c2 <file_read+0x42>
lea    0xe55(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
jmp    11c7 <file_read+0x47>
mov    -0x8(%rbp),%rdi
call   1070 <feof@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    11e5 <file_read+0x65>
jmp    1227 <file_read+0xa7>
mov    $0x1,%edi
call   1080 <malloc@plt>
mov    %rax,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rdx
lea    0xe0e(%rip),%rsi
call   1060 <fscanf@plt>
mov    -0x10(%rbp),%rsi
lea    0xe01(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
jmp    11c7 <file_read+0x47>
mov    -0x8(%rbp),%rdi
call   1040 <fclose@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<file_read>:
push   %r15
push   %r14
push   %rbx
lea    0xe74(%rip),%rdi
lea    0xe7b(%rip),%rsi
call   1090 <fopen@plt>
test   %rax,%rax
je     1204 <file_read+0x84>
mov    %rax,%r15
mov    %rax,%rdi
call   1070 <feof@plt>
test   %eax,%eax
jne    11f7 <file_read+0x77>
lea    0xe51(%rip),%r14
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    $0x1,%edi
call   1080 <malloc@plt>
mov    %rax,%rbx
mov    %r15,%rdi
mov    %r14,%rsi
mov    %rax,%rdx
call   1060 <fscanf@plt>
mov    %rbx,%rdi
call   1040 <puts@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %r15,%rdi
call   1070 <feof@plt>
test   %eax,%eax
je     11c0 <file_read+0x40>
mov    %r15,%rdi
pop    %rbx
pop    %r14
pop    %r15
jmp    1050 <fclose@plt>
lea    0xdfc(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@.str = [4 x i8] c"tmp\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [10 x i8] c"IO error\0A\00"
@.str.3 = [3 x i8] c"%s\00"
@.str.4 = [4 x i8] c"%s\0A\00"
define void @file_read() {
%1 = alloca i32*
%2 = alloca i8*
%3 = call i32* @fopen(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %3, i32** %1
%4 = load i32*, i32** %1
%5 = icmp eq i32* %4, null
br i1 %5, label %6, label %9
6: ; preds = %0
%7 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
%8 = call i32 @exit(i32 1)
unreachable
9: ; preds = %0
br label %10
10: ; preds = %15, %9
%11 = load i32*, i32** %1
%12 = call i32 @feof(i32* %11)
%13 = icmp ne i32 %12, 0
%14 = xor i1 %13, true
br i1 %14, label %15, label %24
15: ; preds = %10
%16 = call i8* @malloc(i32 1)
store i8* %16, i8** %2
%17 = load i32*, i32** %1
%18 = load i8*, i8** %2
%19 = call i32 @fscanf(i32* %17, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* %18)
%20 = load i8*, i8** %2
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i8* %20)
%22 = load i8*, i8** %2
%23 = call i32 @free(i8* %22)
br label %10
24: ; preds = %10
%25 = load i32*, i32** %1
%26 = call i32 @fclose(i32* %25)
ret void
}

LLVM IR (-O2):
@.str = [4 x i8] c"tmp\00"
@.str.1 = [2 x i8] c"r\00"
@.str.3 = [3 x i8] c"%s\00"
@str = [9 x i8] c"IO error\00"
define void @file_read() {
%1 = tail call i32* @fopen(i8* getelementptr inbounds ([4 x i8], [4 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 %6, label %3
3: ; preds = %0
%4 = tail call i32 @feof(i32* %1)
%5 = icmp eq i32 %4, 0
br i1 %5, label %9, label %16
6: ; preds = %0
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @str, i64 0, i64 0))
%8 = tail call i32 @exit(i32 1)
unreachable
9: ; preds = %3, %9
%10 = tail call dereferenceable_or_null(1) i8* @malloc(i32 1)
%11 = tail call i32 @fscanf(i32* %1, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* %10)
%12 = tail call i32 @puts(i8* dereferenceable(1) %10)
%13 = tail call i32 @free(i8* %10)
%14 = tail call i32 @feof(i32* %1)
%15 = icmp eq i32 %14, 0
br i1 %15, label %9, label %16
16: ; preds = %9, %3
%17 = tail call i32 @fclose(i32* %1)
ret void
}