#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 O_CREAT;
int O_RDWR;
int /*<<< orphan*/ close(int);
int open(char *, int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
int main() {
  int fd;
  int ret;
  fd = open("1.log", O_RDWR | O_CREAT, 0640);
  if (fd < 0) {
    perror("open");
    return -1;
  }
  char buffer[1024] = {0};
  ret = read(fd, buffer, 1024);
  if (ret < 0) {
    printf("read error:\n");
    close(fd);
    return -1;
  }
  printf("ret = %d , read:%s\n", ret, buffer);
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x410,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e67(%rip),%rax
mov    (%rax),%esi
mov    0x2e76(%rip),%rax
or     (%rax),%esi
lea    0xe75(%rip),%rdi
mov    $0x1a0,%edx
call   1070 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    11ba <main+0x5a>
lea    0xe5d(%rip),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1237 <main+0xd7>
lea    -0x410(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1040 <memset@plt>
mov    -0x8(%rbp),%edi
lea    -0x410(%rbp),%rsi
mov    $0x400,%edx
call   1060 <read@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    1210 <main+0xb0>
lea    0xe16(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1237 <main+0xd7>
mov    -0xc(%rbp),%esi
lea    -0x410(%rbp),%rdx
lea    0xdf7(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x410,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x400,%rsp
mov    0x2e5e(%rip),%rax
mov    0x2e6f(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe6c(%rip),%rdi
mov    $0x1a0,%edx
call   1080 <open@plt>
test   %eax,%eax
js     1210 <main+0xa0>
mov    %eax,%ebx
mov    %rsp,%rbp
xor    %r14d,%r14d
mov    $0x400,%edx
mov    %rbp,%rdi
xor    %esi,%esi
call   1050 <memset@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0x400,%edx
call   1070 <read@plt>
test   %eax,%eax
js     11e8 <main+0x78>
lea    0xe38(%rip),%rdi
mov    %rsp,%rdx
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %ebx,%edi
call   1060 <close@plt>
jmp    1201 <main+0x91>
lea    0xe30(%rip),%rdi
call   1030 <puts@plt>
mov    %ebx,%edi
call   1060 <close@plt>
mov    $0xffffffff,%r14d
mov    %r14d,%eax
add    $0x400,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xdef(%rip),%rdi
call   1090 <perror@plt>
jmp    11fb <main+0x8b>

LLVM IR (-O0):
@.str = [6 x i8] c"1.log\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@.str.1 = [5 x i8] c"open\00"
@.str.2 = [13 x i8] c"read error:\0A\00"
@.str.3 = [20 x i8] c"ret = %d , read:%s\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca [1024 x i8]
store i32 0, i32* %1
%5 = load i32, i32* @O_RDWR
%6 = load i32, i32* @O_CREAT
%7 = or i32 %5, %6
%8 = call i32 @open(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32 %7, i32 416)
store i32 %8, i32* %2
%9 = load i32, i32* %2
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %0
%12 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %1
br label %30
13: ; preds = %0
%14 = bitcast [1024 x i8]* %4 to i8*
%15 = load i32, i32* %2
%16 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%17 = call i32 @read(i32 %15, i8* %16, i32 1024)
store i32 %17, i32* %3
%18 = load i32, i32* %3
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %24
20: ; preds = %13
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
%22 = load i32, i32* %2
%23 = call i32 @close(i32 %22)
store i32 -1, i32* %1
br label %30
24: ; preds = %13
%25 = load i32, i32* %3
%26 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0), i32 %25, i8* %26)
%28 = load i32, i32* %2
%29 = call i32 @close(i32 %28)
store i32 0, i32* %1
br label %30
30: ; preds = %24, %20, %11
%31 = load i32, i32* %1
ret i32 %31
}

LLVM IR (-O2):
@.str = [6 x i8] c"1.log\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@.str.1 = [5 x i8] c"open\00"
@.str.3 = [20 x i8] c"ret = %d , read:%s\0A\00"
@str = [12 x i8] c"read error:\00"
define i32 @main() {
%1 = alloca [1024 x i8]
%2 = load i32, i32* @O_RDWR
%3 = load i32, i32* @O_CREAT
%4 = or i32 %3, %2
%5 = tail call i32 @open(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32 %4, i32 416)
%6 = icmp slt i32 %5, 0
br i1 %6, label %7, label %9
7: ; preds = %0
%8 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %21
9: ; preds = %0
%10 = getelementptr inbounds [1024 x i8], [1024 x i8]* %1, i64 0, i64 0
%11 = call i32 @read(i32 %5, i8* %10, i32 1024)
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %16
13: ; preds = %9
%14 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str, i64 0, i64 0))
%15 = tail call i32 @close(i32 %5)
br label %19
16: ; preds = %9
%17 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0), i32 %11, i8* %10)
%18 = call i32 @close(i32 %5)
br label %19
19: ; preds = %16, %13
%20 = phi i32 [ -1, %13 ], [ 0, %16 ]
br label %21
21: ; preds = %19, %7
%22 = phi i32 [ -1, %7 ], [ %20, %19 ]
ret i32 %22
}