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

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char);
int read(int, char *, int);
int main() {
  int fd = open("hello.txt", O_RDONLY);
  if (fd > 0) {
    char symbol;
    int res;
    do {
      res = read(fd, &symbol, sizeof(symbol));
      printf("%c", symbol);
    } while (res > 0);
    close(fd);
  } else {
    perror("Error opening file");
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e82(%rip),%rax
mov    (%rax),%esi
lea    0xe91(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jle    11c1 <main+0x71>
jmp    1186 <main+0x36>
mov    -0x8(%rbp),%edi
lea    -0x9(%rbp),%rsi
mov    $0x1,%edx
call   1050 <read@plt>
mov    %eax,-0x10(%rbp)
lea    0xe69(%rip),%rdi
movsbl -0x9(%rbp),%esi
call   1030 <printf@plt>
cmpl   $0x0,-0x10(%rbp)
jg     1186 <main+0x36>
mov    -0x8(%rbp),%edi
call   1040 <close@plt>
jmp    11cd <main+0x7d>
lea    0xe45(%rip),%rdi
call   1070 <perror@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    0x2e8a(%rip),%rax
mov    (%rax),%esi
lea    0xe99(%rip),%rdi
call   1060 <open@plt>
test   %eax,%eax
jle    11b6 <main+0x66>
mov    %eax,%ebx
lea    0x7(%rsp),%r14
lea    0xe8c(%rip),%r15
xchg   %ax,%ax
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x1,%edx
call   1050 <read@plt>
mov    %eax,%ebp
movsbl 0x7(%rsp),%esi
mov    %r15,%rdi
call   1030 <printf@plt>
test   %ebp,%ebp
jg     1180 <main+0x30>
mov    %ebx,%edi
call   1040 <close@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe50(%rip),%rdi
call   1070 <perror@plt>
jmp    11a9 <main+0x59>

LLVM IR (-O0):
@.str = [10 x i8] c"hello.txt\00"
@O_RDONLY = global i32 0
@.str.1 = [3 x i8] c"%c\00"
@.str.2 = [19 x i8] c"Error opening file\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i8
%4 = alloca i32
store i32 0, i32* %1
%5 = load i32, i32* @O_RDONLY
%6 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %5)
store i32 %6, i32* %2
%7 = load i32, i32* %2
%8 = icmp sgt i32 %7, 0
br i1 %8, label %9, label %21
9: ; preds = %0
br label %10
10: ; preds = %15, %9
%11 = load i32, i32* %2
%12 = call i32 @read(i32 %11, i8* %3, i32 1)
store i32 %12, i32* %4
%13 = load i8, i8* %3
%14 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8 signext %13)
br label %15
15: ; preds = %10
%16 = load i32, i32* %4
%17 = icmp sgt i32 %16, 0
br i1 %17, label %10, label %18
18: ; preds = %15
%19 = load i32, i32* %2
%20 = call i32 @close(i32 %19)
br label %23
21: ; preds = %0
%22 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0))
br label %23
23: ; preds = %21, %18
ret i32 0
}

LLVM IR (-O2):
@.str = [10 x i8] c"hello.txt\00"
@O_RDONLY = global i32 0
@.str.1 = [3 x i8] c"%c\00"
@.str.2 = [19 x i8] c"Error opening file\00"
define i32 @main() {
%1 = alloca i8
%2 = load i32, i32* @O_RDONLY
%3 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %2)
%4 = icmp sgt i32 %3, 0
br i1 %4, label %5, label %13
5: ; preds = %0
br label %6
6: ; preds = %6, %5
%7 = call i32 @read(i32 %3, i8* %1, i32 1)
%8 = load i8, i8* %1
%9 = tail call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8 signext %8)
%10 = icmp sgt i32 %7, 0
br i1 %10, label %6, label %11
11: ; preds = %6
%12 = tail call i32 @close(i32 %3)
br label %15
13: ; preds = %0
%14 = tail call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0))
br label %15
15: ; preds = %13, %11
ret i32 0
}