#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 /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ EXIT_SUCCESS;
int O_CREAT;
int O_RDONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ memset(char *, int, int);
int open(char *, int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int, char *);
int read(int, char *, int);
int main(void) {
  int fd;
  int numbytes;
  char path[] = "read_file.txt";
  char buf[256];
  fd = open(path, O_CREAT | O_RDONLY, 0644);
  if (fd < 0) {
    perror("open()");
    exit(EXIT_FAILURE);
  }
  memset(buf, 0x00, 256);
  while ((numbytes = read(fd, buf, 255)) > 0) {
    printf("%d bytes read: %s", numbytes, buf);
    memset(buf, 0x00, 256);
  }
  close(fd);
  exit(EXIT_SUCCESS);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x120,%rsp
movl   $0x0,-0x4(%rbp)
mov    0xe77(%rip),%rax
mov    %rax,-0x1a(%rbp)
mov    0xe75(%rip),%eax
mov    %eax,-0x12(%rbp)
mov    0xe6f(%rip),%ax
mov    %ax,-0xe(%rbp)
lea    -0x1a(%rbp),%rdi
mov    0x2e4c(%rip),%rax
mov    (%rax),%esi
mov    0x2e2b(%rip),%rax
or     (%rax),%esi
mov    $0x1a4,%edx
call   1070 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    11e8 <main+0x78>
lea    0xe39(%rip),%rdi
call   1080 <perror@plt>
mov    0x2def(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
lea    -0x120(%rbp),%rdi
xor    %esi,%esi
mov    $0x100,%edx
call   1040 <memset@plt>
mov    -0x8(%rbp),%edi
lea    -0x120(%rbp),%rsi
mov    $0xff,%edx
call   1060 <read@plt>
mov    %eax,-0xc(%rbp)
cmp    $0x0,%eax
jle    1249 <main+0xd9>
mov    -0xc(%rbp),%esi
lea    -0x120(%rbp),%rdx
lea    0xde9(%rip),%rdi
call   1030 <printf@plt>
lea    -0x120(%rbp),%rdi
xor    %esi,%esi
mov    $0x100,%edx
call   1040 <memset@plt>
jmp    11fb <main+0x8b>
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
mov    0x2d70(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x110,%rsp
movabs $0x7478742e656c69,%rax
mov    %rax,0x6(%rsp)
movabs $0x6c69665f64616572,%rax
66 69 6c
mov    %rax,(%rsp)
mov    0x2e59(%rip),%rax
mov    0x2e3a(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %rsp,%rdi
mov    $0x1a4,%edx
call   1070 <open@plt>
test   %eax,%eax
jns    11d5 <main+0x65>
lea    0xe4c(%rip),%rdi
call   1080 <perror@plt>
mov    0x2e02(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    %eax,%ebp
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x100,%edx
call   1040 <memset@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0xff,%edx
call   1060 <read@plt>
test   %eax,%eax
jle    123f <main+0xcf>
lea    0xe10(%rip),%r14
lea    0x10(%rsp),%rbx
nopw   0x0(%rax,%rax,1)
mov    %r14,%rdi
mov    %eax,%esi
mov    %rbx,%rdx
call   1030 <printf@plt>
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x100,%edx
call   1040 <memset@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0xff,%edx
call   1060 <read@plt>
test   %eax,%eax
jg     1210 <main+0xa0>
mov    %ebp,%edi
call   1050 <close@plt>
mov    0x2d7b(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@__const.main.path = [14 x i8] c"read_file.txt\00"
@O_CREAT = global i32 0
@O_RDONLY = global i32 0
@.str = [7 x i8] c"open()\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [18 x i8] c"%d bytes read: %s\00"
@EXIT_SUCCESS = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca [14 x i8]
%5 = alloca [256 x i8]
store i32 0, i32* %1
%6 = bitcast [14 x i8]* %4 to i8*
%7 = getelementptr inbounds [14 x i8], [14 x i8]* %4, i64 0, i64 0
%8 = load i32, i32* @O_CREAT
%9 = load i32, i32* @O_RDONLY
%10 = or i32 %8, %9
%11 = call i32 @open(i8* %7, i32 %10, i32 420)
store i32 %11, i32* %2
%12 = load i32, i32* %2
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %18
14: ; preds = %0
%15 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%16 = load i32, i32* @EXIT_FAILURE
%17 = call i32 @exit(i32 %16)
unreachable
18: ; preds = %0
%19 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%20 = call i32 @memset(i8* %19, i32 0, i32 256)
br label %21
21: ; preds = %26, %18
%22 = load i32, i32* %2
%23 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%24 = call i32 @read(i32 %22, i8* %23, i32 255)
store i32 %24, i32* %3
%25 = icmp sgt i32 %24, 0
br i1 %25, label %26, label %32
26: ; preds = %21
%27 = load i32, i32* %3
%28 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%29 = call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i32 %27, i8* %28)
%30 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%31 = call i32 @memset(i8* %30, i32 0, i32 256)
br label %21
32: ; preds = %21
%33 = load i32, i32* %2
%34 = call i32 @close(i32 %33)
%35 = load i32, i32* @EXIT_SUCCESS
%36 = call i32 @exit(i32 %35)
unreachable
}

LLVM IR (-O2):
@__const.main.path = [14 x i8] c"read_file.txt\00"
@O_CREAT = global i32 0
@O_RDONLY = global i32 0
@.str = [7 x i8] c"open()\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [18 x i8] c"%d bytes read: %s\00"
@EXIT_SUCCESS = global i32 0
define i32 @main() {
%1 = alloca [14 x i8]
%2 = alloca [256 x i8]
%3 = getelementptr inbounds [14 x i8], [14 x i8]* %1, i64 0, i64 0
%4 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%5 = load i32, i32* @O_CREAT
%6 = load i32, i32* @O_RDONLY
%7 = or i32 %6, %5
%8 = call i32 @open(i8* %3, i32 %7, i32 420)
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %14
10: ; preds = %0
%11 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%12 = load i32, i32* @EXIT_FAILURE
%13 = tail call i32 @exit(i32 %12)
unreachable
14: ; preds = %0
%15 = call i32 @memset(i8* %4, i32 0, i32 256)
%16 = call i32 @read(i32 %8, i8* %4, i32 255)
%17 = icmp sgt i32 %16, 0
br i1 %17, label %18, label %24
18: ; preds = %14, %18
%19 = phi i32 [ %22, %18 ], [ %16, %14 ]
%20 = call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i32 %19, i8* %4)
%21 = call i32 @memset(i8* %4, i32 0, i32 256)
%22 = call i32 @read(i32 %8, i8* %4, i32 255)
%23 = icmp sgt i32 %22, 0
br i1 %23, label %18, label %24
24: ; preds = %18, %14
%25 = call i32 @close(i32 %8)
%26 = load i32, i32* @EXIT_SUCCESS
%27 = call i32 @exit(i32 %26)
unreachable
}