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

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
int main(int argc, char *argv[]) {
  int fd = -1, i;
  ssize_t size = -1;
  char buf[1024 + 1];
  char filename[] = "test.txt";
  fd = open(filename, O_RDONLY);
  if (-1 == fd) {
    printf("Open file %s failure,fd:%d\n", filename, fd);
    close(fd);
    return -1;
  } else {
    printf("open file %s success,fd:%d", filename, fd);
    do {
      size = read(fd, buf, 1024);
      if (-1 == size) {
        close(fd);
        printf("read file error occurs!\n");
        return -1;
      } else if (0 < size) {
        printf("read %d bytes:", size);
        printf("\"");
        for (i = 0; i < size; i++) {
          printf("%c", *(buf + i));
        }
        printf("\"");
      } else {
        printf("reach the end of file!\n");
      }
    } while (size);
    close(fd);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0xffffffff,-0x14(%rbp)
movl   $0xffffffff,-0x1c(%rbp)
mov    0xe92(%rip),%rax
mov    %rax,-0x429(%rbp)
mov    0xe8d(%rip),%al
mov    %al,-0x421(%rbp)
lea    -0x429(%rbp),%rdi
mov    0x2e59(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x14(%rbp)
mov    $0xffffffff,%eax
cmp    -0x14(%rbp),%eax
jne    11d3 <main+0x93>
lea    -0x429(%rbp),%rsi
mov    -0x14(%rbp),%edx
lea    0xe51(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12db <main+0x19b>
lea    -0x429(%rbp),%rsi
mov    -0x14(%rbp),%edx
lea    0xe41(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x14(%rbp),%edi
lea    -0x420(%rbp),%rsi
mov    $0x400,%edx
call   1050 <read@plt>
mov    %eax,-0x1c(%rbp)
mov    $0xffffffff,%eax
cmp    -0x1c(%rbp),%eax
jne    1232 <main+0xf2>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
lea    0xe21(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12db <main+0x19b>
xor    %eax,%eax
cmp    -0x1c(%rbp),%eax
jge    12aa <main+0x16a>
mov    -0x1c(%rbp),%esi
lea    0xe12(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xe13(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    1297 <main+0x157>
movslq -0x18(%rbp),%rax
movsbl -0x420(%rbp,%rax,1),%esi
ff
lea    0xde8(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1263 <main+0x123>
lea    0xdca(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    12b8 <main+0x178>
lea    0xdbc(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    12bd <main+0x17d>
jmp    12c2 <main+0x182>
cmpl   $0x0,-0x1c(%rbp)
jne    11eb <main+0xab>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x418,%rsp
movabs $0x7478742e74736574,%rax
74 78 74
mov    %rax,(%rsp)
movb   $0x0,0x8(%rsp)
mov    0x2e5d(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdi
call   1080 <open@plt>
cmp    $0xffffffff,%eax
je     124e <main+0xee>
mov    %eax,%r13d
lea    0xe7d(%rip),%rdi
mov    %rsp,%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1050 <printf@plt>
lea    0x10(%rsp),%r14
lea    0xe80(%rip),%r15
jmp    11e0 <main+0x80>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
lea    0xe78(%rip),%rdi
call   1040 <puts@plt>
test   %ebp,%ebp
je     1242 <main+0xe2>
mov    %r13d,%edi
mov    %r14,%rsi
mov    $0x400,%edx
call   1070 <read@plt>
cmp    $0xffffffff,%eax
je     1275 <main+0x115>
mov    %eax,%ebp
test   %eax,%eax
jle    11d0 <main+0x70>
mov    %r15,%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x22,%edi
call   1030 <putchar@plt>
mov    %ebp,%ebx
xor    %r12d,%r12d
nopw   0x0(%rax,%rax,1)
movsbl 0x10(%rsp,%r12,1),%edi
call   1030 <putchar@plt>
add    $0x1,%r12
cmp    %r12,%rbx
jne    1220 <main+0xc0>
mov    $0x22,%edi
call   1030 <putchar@plt>
test   %ebp,%ebp
jne    11e0 <main+0x80>
mov    %r13d,%edi
call   1060 <close@plt>
xor    %ebx,%ebx
jmp    128e <main+0x12e>
lea    0xdb4(%rip),%rdi
mov    %rsp,%rsi
mov    $0xffffffff,%ebx
mov    $0xffffffff,%edx
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0xffffffff,%edi
call   1060 <close@plt>
jmp    128e <main+0x12e>
mov    %r13d,%edi
call   1060 <close@plt>
lea    0xde2(%rip),%rdi
call   1040 <puts@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x418,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@__const.main.filename = [9 x i8] c"test.txt\00"
@O_RDONLY = global i32 0
@.str = [28 x i8] c"Open file %s failure,fd:%d\0A\00"
@.str.1 = [27 x i8] c"open file %s success,fd:%d\00"
@.str.2 = [25 x i8] c"read file error occurs!\0A\00"
@.str.3 = [15 x i8] c"read %d bytes:\00"
@.str.4 = [2 x i8] c"\22\00"
@.str.5 = [3 x i8] c"%c\00"
@.str.6 = [24 x i8] c"reach the end of file!\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca [1025 x i8]
%10 = alloca [9 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 -1, i32* %6
store i32 -1, i32* %8
%11 = bitcast [9 x i8]* %10 to i8*
%12 = getelementptr inbounds [9 x i8], [9 x i8]* %10, i64 0, i64 0
%13 = load i32, i32* @O_RDONLY
%14 = call i32 @open(i8* %12, i32 %13)
store i32 %14, i32* %6
%15 = load i32, i32* %6
%16 = icmp eq i32 -1, %15
br i1 %16, label %17, label %23
17: ; preds = %2
%18 = getelementptr inbounds [9 x i8], [9 x i8]* %10, i64 0, i64 0
%19 = load i32, i32* %6
%20 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i8* %18, i32 %19)
%21 = load i32, i32* %6
%22 = call i32 @close(i32 %21)
store i32 -1, i32* %3
br label %72
23: ; preds = %2
%24 = getelementptr inbounds [9 x i8], [9 x i8]* %10, i64 0, i64 0
%25 = load i32, i32* %6
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i8* %24, i32 %25)
br label %27
27: ; preds = %65, %23
%28 = load i32, i32* %6
%29 = getelementptr inbounds [1025 x i8], [1025 x i8]* %9, i64 0, i64 0
%30 = call i32 @read(i32 %28, i8* %29, i32 1024)
store i32 %30, i32* %8
%31 = load i32, i32* %8
%32 = icmp eq i32 -1, %31
br i1 %32, label %33, label %37
33: ; preds = %27
%34 = load i32, i32* %6
%35 = call i32 @close(i32 %34)
%36 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %3
br label %72
37: ; preds = %27
%38 = load i32, i32* %8
%39 = icmp slt i32 0, %38
br i1 %39, label %40, label %61
40: ; preds = %37
%41 = load i32, i32* %8
%42 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0), i32 %41)
%43 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
store i32 0, i32* %7
br label %44
44: ; preds = %56, %40
%45 = load i32, i32* %7
%46 = load i32, i32* %8
%47 = icmp slt i32 %45, %46
br i1 %47, label %48, label %59
48: ; preds = %44
%49 = getelementptr inbounds [1025 x i8], [1025 x i8]* %9, i64 0, i64 0
%50 = load i32, i32* %7
%51 = sext i32 %50 to i64
%52 = getelementptr inbounds i8, i8* %49, i64 %51
%53 = load i8, i8* %52
%54 = sext i8 %53 to i32
%55 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i32 %54)
br label %56
56: ; preds = %48
%57 = load i32, i32* %7
%58 = add nsw i32 %57, 1
store i32 %58, i32* %7
br label %44
59: ; preds = %44
%60 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
br label %63
61: ; preds = %37
%62 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.6, i64 0, i64 0))
br label %63
63: ; preds = %61, %59
br label %64
64: ; preds = %63
br label %65
65: ; preds = %64
%66 = load i32, i32* %8
%67 = icmp ne i32 %66, 0
br i1 %67, label %27, label %68
68: ; preds = %65
%69 = load i32, i32* %6
%70 = call i32 @close(i32 %69)
br label %71
71: ; preds = %68
store i32 0, i32* %3
br label %72
72: ; preds = %71, %33, %17
%73 = load i32, i32* %3
ret i32 %73
}

LLVM IR (-O2):
@__const.main.filename = [9 x i8] c"test.txt\00"
@O_RDONLY = global i32 0
@.str = [28 x i8] c"Open file %s failure,fd:%d\0A\00"
@.str.1 = [27 x i8] c"open file %s success,fd:%d\00"
@.str.3 = [15 x i8] c"read %d bytes:\00"
@str = [23 x i8] c"reach the end of file!\00"
@str.7 = [24 x i8] c"read file error occurs!\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [1025 x i8]
%4 = alloca [9 x i8]
%5 = getelementptr inbounds [1025 x i8], [1025 x i8]* %3, i64 0, i64 0
%6 = getelementptr inbounds [9 x i8], [9 x i8]* %4, i64 0, i64 0
%7 = load i32, i32* @O_RDONLY
%8 = call i32 @open(i8* %6, i32 %7)
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %13
10: ; preds = %2
%11 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i8* %6, i32 -1)
%12 = call i32 @close(i32 -1)
br label %43
13: ; preds = %2
%14 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i8* %6, i32 %8)
br label %15
15: ; preds = %39, %13
%16 = call i32 @read(i32 %8, i8* %5, i32 1024)
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %21
18: ; preds = %15
%19 = call i32 @close(i32 %8)
%20 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @str.7, i64 0, i64 0))
br label %43
21: ; preds = %15
%22 = icmp sgt i32 %16, 0
br i1 %22, label %23, label %37
23: ; preds = %21
%24 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0), i32 %16)
%25 = call i32 @putchar(i32 34)
%26 = zext i32 %16 to i64
br label %27
27: ; preds = %23, %27
%28 = phi i64 [ 0, %23 ], [ %33, %27 ]
%29 = getelementptr inbounds [1025 x i8], [1025 x i8]* %3, i64 0, i64 %28
%30 = load i8, i8* %29
%31 = sext i8 %30 to i32
%32 = call i32 @putchar(i32 %31)
%33 = add nuw nsw i64 %28, 1
%34 = icmp eq i64 %33, %26
br i1 %34, label %35, label %27
35: ; preds = %27
%36 = call i32 @putchar(i32 34)
br label %39
37: ; preds = %21
%38 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @str, i64 0, i64 0))
br label %39
39: ; preds = %37, %35
%40 = icmp eq i32 %16, 0
br i1 %40, label %41, label %15
41: ; preds = %39
%42 = call i32 @close(i32 %8)
br label %43
43: ; preds = %41, %18, %10
%44 = phi i32 [ -1, %10 ], [ -1, %18 ], [ 0, %41 ]
ret i32 %44
}