#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*/ fclose(int /*<<< orphan*/ *);
scalar_t__ feof(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char);
int /*<<< orphan*/ fscanf(int /*<<< orphan*/ *, char *, char *);
int /*<<< orphan*/ stdout;
void main() {
  FILE *fd = fopen("test.txt", "r");
  if (NULL != fd) {
    char str;
    while (1) {
      fscanf(fd, "%c", &str);
      if (feof(fd))
        break;
      fprintf(stdout, "%c", str);
    }
    fclose(fd);
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
lea    0xea1(%rip),%rdi
lea    0xea3(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x8(%rbp)
xor    %eax,%eax
cmp    -0x8(%rbp),%rax
je     11d3 <main+0x83>
jmp    1180 <main+0x30>
mov    -0x8(%rbp),%rdi
lea    0xe80(%rip),%rsi
lea    -0x9(%rbp),%rdx
call   1040 <fscanf@plt>
mov    -0x8(%rbp),%rdi
call   1060 <feof@plt>
cmp    $0x0,%rax
je     11ac <main+0x5c>
jmp    11ca <main+0x7a>
mov    0x2e2d(%rip),%rax
mov    (%rax),%edi
lea    0xe4f(%rip),%rsi
movsbl -0x9(%rbp),%edx
call   1050 <fprintf@plt>
jmp    1180 <main+0x30>
mov    -0x8(%rbp),%rdi
call   1030 <fclose@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
lea    0xea1(%rip),%rdi
lea    0xea3(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     11e4 <main+0x94>
mov    %rax,%rbx
lea    0xe91(%rip),%rsi
lea    0x7(%rsp),%rdx
mov    %rax,%rdi
call   1040 <fscanf@plt>
mov    %rbx,%rdi
call   1060 <feof@plt>
test   %rax,%rax
jne    11dc <main+0x8c>
mov    0x2e45(%rip),%r12
lea    0xe69(%rip),%r14
lea    0x7(%rsp),%r15
nopw   0x0(%rax,%rax,1)
mov    (%r12),%edi
movsbl 0x7(%rsp),%edx
mov    %r14,%rsi
call   1050 <fprintf@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %r15,%rdx
call   1040 <fscanf@plt>
mov    %rbx,%rdi
call   1060 <feof@plt>
test   %rax,%rax
je     11b0 <main+0x60>
mov    %rbx,%rdi
call   1030 <fclose@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [9 x i8] c"test.txt\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [3 x i8] c"%c\00"
@stdout = global i32 0
define void @main() {
%1 = alloca i32*
%2 = alloca i8
%3 = 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* %3, i32** %1
%4 = load i32*, i32** %1
%5 = icmp ne i32* null, %4
br i1 %5, label %6, label %21
6: ; preds = %0
br label %7
7: ; preds = %6, %14
%8 = load i32*, i32** %1
%9 = call i32 @fscanf(i32* %8, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* %2)
%10 = load i32*, i32** %1
%11 = call i64 @feof(i32* %10)
%12 = icmp ne i64 %11, 0
br i1 %12, label %13, label %14
13: ; preds = %7
br label %18
14: ; preds = %7
%15 = load i32, i32* @stdout
%16 = load i8, i8* %2
%17 = call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8 signext %16)
br label %7
18: ; preds = %13
%19 = load i32*, i32** %1
%20 = call i32 @fclose(i32* %19)
br label %21
21: ; preds = %18, %0
ret void
}

LLVM IR (-O2):
@.str = [9 x i8] c"test.txt\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [3 x i8] c"%c\00"
@stdout = global i32 0
define void @main() {
%1 = alloca i8
%2 = 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))
%3 = icmp eq i32* %2, null
br i1 %3, label %17, label %4
4: ; preds = %0
%5 = call i32 @fscanf(i32* %2, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* %1)
%6 = call i64 @feof(i32* %2)
%7 = icmp eq i64 %6, 0
br i1 %7, label %8, label %15
8: ; preds = %4, %8
%9 = load i32, i32* @stdout
%10 = load i8, i8* %1
%11 = call i32 @fprintf(i32 %9, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8 signext %10)
%12 = call i32 @fscanf(i32* %2, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* %1)
%13 = call i64 @feof(i32* %2)
%14 = icmp eq i64 %13, 0
br i1 %14, label %8, label %15
15: ; preds = %8, %4
%16 = call i32 @fclose(i32* %2)
br label %17
17: ; preds = %15, %0
ret void
}