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


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e58(%rip),%rax
mov    (%rax),%esi
mov    0x2e67(%rip),%rax
or     (%rax),%esi
mov    $0x1a0,%edx
call   1070 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    11c2 <main+0x62>
lea    0xe4f(%rip),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1242 <main+0xe2>
lea    -0x420(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1040 <memset@plt>
mov    -0x18(%rbp),%edi
lea    -0x420(%rbp),%rsi
mov    $0x400,%edx
call   1060 <read@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1218 <main+0xb8>
lea    0xe08(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x18(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1242 <main+0xe2>
mov    -0x14(%rbp),%esi
mov    -0x8(%rbp),%edx
lea    -0x420(%rbp),%rcx
lea    0xde6(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x18(%rbp),%edi
call   1050 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x420,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x408,%rsp
mov    %edi,%r14d
mov    0x8(%rsi),%rdi
mov    0x2e55(%rip),%rax
mov    0x2e66(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    $0x1a0,%edx
call   1080 <open@plt>
test   %eax,%eax
js     1217 <main+0xa7>
mov    %eax,%ebp
mov    %rsp,%rbx
xor    %r15d,%r15d
mov    $0x400,%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1050 <memset@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x400,%edx
call   1070 <read@plt>
test   %eax,%eax
js     11ed <main+0x7d>
lea    0xe30(%rip),%rdi
mov    %rsp,%rcx
mov    %eax,%esi
mov    %r14d,%edx
xor    %eax,%eax
call   1040 <printf@plt>
mov    %ebp,%edi
call   1060 <close@plt>
jmp    1206 <main+0x96>
lea    0xe2e(%rip),%rdi
call   1030 <puts@plt>
mov    %ebp,%edi
call   1060 <close@plt>
mov    $0xffffffff,%r15d
mov    %r15d,%eax
add    $0x408,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xde2(%rip),%rdi
call   1090 <perror@plt>
jmp    1200 <main+0x90>

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

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