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

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ read(int, char *, int const);
int /*<<< orphan*/ write(int, char *, int /*<<< orphan*/);
int main(void) {
  const int len = 64;
  ssize_t ret;
  int fd = open("read.c", O_RDONLY);
  printf("fd=%d\n", fd);
  char buf[len];
  while ((ret = read(fd, buf, len)) != 0) {
    write(1, buf, ret);
  }
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x40,-0x8(%rbp)
mov    0x2e7b(%rip),%rax
mov    (%rax),%esi
lea    0xe8a(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%esi
lea    0xe7f(%rip),%rdi
call   1040 <printf@plt>
mov    %rsp,%rax
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%edi
lea    -0x60(%rbp),%rsi
mov    $0x40,%edx
call   1060 <read@plt>
mov    %eax,-0xc(%rbp)
cmp    $0x0,%eax
je     11c7 <main+0x77>
mov    -0xc(%rbp),%edx
mov    $0x1,%edi
lea    -0x60(%rbp),%rsi
call   1030 <write@plt>
jmp    1194 <main+0x44>
mov    -0x10(%rbp),%edi
call   1050 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
sub    $0x48,%rsp
mov    0x2e8b(%rip),%rax
mov    (%rax),%esi
lea    0xe9a(%rip),%rdi
call   1070 <open@plt>
mov    %eax,%ebp
lea    0xe93(%rip),%rdi
mov    %eax,%esi
call   1040 <printf@plt>
mov    %rsp,%rsi
mov    %ebp,%edi
mov    $0x40,%edx
call   1060 <read@plt>
test   %eax,%eax
je     11c2 <main+0x72>
mov    %rsp,%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    $0x1,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x40,%edx
call   1060 <read@plt>
test   %eax,%eax
jne    11a0 <main+0x50>
mov    %ebp,%edi
call   1050 <close@plt>
xor    %eax,%eax
add    $0x48,%rsp
pop    %rbx
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [7 x i8] c"read.c\00"
@O_RDONLY = global i32 0
@.str.1 = [7 x i8] c"fd=%d\0A\00"
define i32 @main() {
%1 = load i32, i32* @O_RDONLY
%2 = tail call i32 @open(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32 %1)
%3 = tail call i32 @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i32 %2)
%4 = alloca [64 x i8]
%5 = getelementptr inbounds [64 x i8], [64 x i8]* %4, i64 0, i64 0
%6 = call i32 @read(i32 %2, i8* %5, i32 64)
%7 = icmp eq i32 %6, 0
br i1 %7, label %13, label %8
8: ; preds = %0, %8
%9 = phi i32 [ %11, %8 ], [ %6, %0 ]
%10 = call i32 @write(i32 1, i8* %5, i32 %9)
%11 = call i32 @read(i32 %2, i8* %5, i32 64)
%12 = icmp eq i32 %11, 0
br i1 %12, label %13, label %8
13: ; preds = %8, %0
%14 = tail call i32 @close(i32 %2)
ret i32 0
}