#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 /*<<< orphan*/ O_RDONLY;
int O_WRONLY;
int /*<<< orphan*/ S_IRWXU;
int /*<<< orphan*/ close(int);
int open(char *, int, ...);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
int write(int, char *, int);
int main(int argc, char *argv[]) {
  int fd = 0;
  int nbytes = 0;
  if (argc == 1) {
    printf("Error in use\n");
    return -1;
  }
  fd = open(argv[1], O_CREAT | O_WRONLY, S_IRWXU);
  if (fd > 0) {
    nbytes = write(fd, "12345", 6);
    printf("Wrote %d bytes\n", nbytes);
    perror("write status");
    close(fd);
  }
  fd = open(argv[1], O_RDONLY);
  printf("file des: %d\n", fd);
  perror("open status");
  if (fd > 0) {
    char buf[10];
    nbytes = read(fd, buf, 10);
    perror("Read status");
    printf("Read %d bytes, data = %s\n", nbytes, buf);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
cmpl   $0x1,-0x8(%rbp)
jne    11a8 <main+0x48>
lea    0xe6b(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1299 <main+0x139>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e41(%rip),%rax
mov    (%rax),%esi
mov    0x2e00(%rip),%rax
or     (%rax),%esi
mov    0x2e17(%rip),%rax
mov    (%rax),%edx
mov    $0x0,%al
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jle    121b <main+0xbb>
mov    -0x14(%rbp),%edi
lea    0xe25(%rip),%rsi
mov    $0x6,%edx
call   1030 <write@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%esi
lea    0xe14(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe16(%rip),%rdi
call   1080 <perror@plt>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2dae(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%esi
lea    0xdf1(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xdf1(%rip),%rdi
call   1080 <perror@plt>
cmpl   $0x0,-0x14(%rbp)
jle    1292 <main+0x132>
mov    -0x14(%rbp),%edi
lea    -0x22(%rbp),%rsi
mov    $0xa,%edx
call   1060 <read@plt>
mov    %eax,-0x18(%rbp)
lea    0xdd3(%rip),%rdi
call   1080 <perror@plt>
mov    -0x18(%rbp),%esi
lea    -0x22(%rbp),%rdx
lea    0xdcc(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
cmp    $0x1,%edi
jne    1199 <main+0x29>
lea    0xedf(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    %rsi,%r14
mov    0x8(%rsi),%rdi
mov    0x2e51(%rip),%rax
mov    0x2e12(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e27(%rip),%rax
mov    (%rax),%edx
xor    %ebx,%ebx
xor    %eax,%eax
call   1080 <open@plt>
test   %eax,%eax
jle    1200 <main+0x90>
mov    %eax,%ebp
lea    0xe2f(%rip),%rsi
mov    %eax,%edi
mov    $0x6,%edx
call   1040 <write@plt>
lea    0xe22(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xe22(%rip),%rdi
call   1090 <perror@plt>
mov    %ebp,%edi
call   1060 <close@plt>
mov    0x8(%r14),%rdi
mov    0x2dcd(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
call   1080 <open@plt>
mov    %eax,%ebp
lea    0xe06(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xe04(%rip),%rdi
call   1090 <perror@plt>
test   %ebp,%ebp
jle    118e <main+0x1e>
lea    0x6(%rsp),%r14
mov    %ebp,%edi
mov    %r14,%rsi
mov    $0xa,%edx
call   1070 <read@plt>
mov    %eax,%ebp
lea    0xde6(%rip),%rdi
call   1090 <perror@plt>
lea    0xde6(%rip),%rdi
xor    %ebx,%ebx
mov    %ebp,%esi
mov    %r14,%rdx
xor    %eax,%eax
call   1050 <printf@plt>
jmp    118e <main+0x1e>

LLVM IR (-O0):
@.str = [14 x i8] c"Error in use\0A\00"
@O_CREAT = global i32 0
@O_WRONLY = global i32 0
@S_IRWXU = global i32 0
@.str.1 = [6 x i8] c"12345\00"
@.str.2 = [16 x i8] c"Wrote %d bytes\0A\00"
@.str.3 = [13 x i8] c"write status\00"
@O_RDONLY = global i32 0
@.str.4 = [14 x i8] c"file des: %d\0A\00"
@.str.5 = [12 x i8] c"open status\00"
@.str.6 = [12 x i8] c"Read status\00"
@.str.7 = [26 x i8] c"Read %d bytes, data = %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 [10 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %6
store i32 0, i32* %7
%9 = load i32, i32* %4
%10 = icmp eq i32 %9, 1
br i1 %10, label %11, label %13
11: ; preds = %2
%12 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %52
13: ; preds = %2
%14 = load i8**, i8*** %5
%15 = getelementptr inbounds i8*, i8** %14, i64 1
%16 = load i8*, i8** %15
%17 = load i32, i32* @O_CREAT
%18 = load i32, i32* @O_WRONLY
%19 = or i32 %17, %18
%20 = load i32, i32* @S_IRWXU
%21 = call i32 (i8*, i32, ...) @open(i8* %16, i32 %19, i32 %20)
store i32 %21, i32* %6
%22 = load i32, i32* %6
%23 = icmp sgt i32 %22, 0
br i1 %23, label %24, label %32
24: ; preds = %13
%25 = load i32, i32* %6
%26 = call i32 @write(i32 %25, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i32 6)
store i32 %26, i32* %7
%27 = load i32, i32* %7
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i32 %27)
%29 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
%30 = load i32, i32* %6
%31 = call i32 @close(i32 %30)
br label %32
32: ; preds = %24, %13
%33 = load i8**, i8*** %5
%34 = getelementptr inbounds i8*, i8** %33, i64 1
%35 = load i8*, i8** %34
%36 = load i32, i32* @O_RDONLY
%37 = call i32 (i8*, i32, ...) @open(i8* %35, i32 %36)
store i32 %37, i32* %6
%38 = load i32, i32* %6
%39 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0), i32 %38)
%40 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.5, i64 0, i64 0))
%41 = load i32, i32* %6
%42 = icmp sgt i32 %41, 0
br i1 %42, label %43, label %51
43: ; preds = %32
%44 = load i32, i32* %6
%45 = getelementptr inbounds [10 x i8], [10 x i8]* %8, i64 0, i64 0
%46 = call i32 @read(i32 %44, i8* %45, i32 10)
store i32 %46, i32* %7
%47 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.6, i64 0, i64 0))
%48 = load i32, i32* %7
%49 = getelementptr inbounds [10 x i8], [10 x i8]* %8, i64 0, i64 0
%50 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.7, i64 0, i64 0), i32 %48, i8* %49)
br label %51
51: ; preds = %43, %32
store i32 0, i32* %3
br label %52
52: ; preds = %51, %11
%53 = load i32, i32* %3
ret i32 %53
}

LLVM IR (-O2):
@O_CREAT = global i32 0
@O_WRONLY = global i32 0
@S_IRWXU = global i32 0
@.str.1 = [6 x i8] c"12345\00"
@.str.2 = [16 x i8] c"Wrote %d bytes\0A\00"
@.str.3 = [13 x i8] c"write status\00"
@O_RDONLY = global i32 0
@.str.4 = [14 x i8] c"file des: %d\0A\00"
@.str.5 = [12 x i8] c"open status\00"
@.str.6 = [12 x i8] c"Read status\00"
@.str.7 = [26 x i8] c"Read %d bytes, data = %s\0A\00"
@str = [13 x i8] c"Error in use\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [10 x i8]
%4 = icmp eq i32 %0, 1
br i1 %4, label %5, label %7
5: ; preds = %2
%6 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str, i64 0, i64 0))
br label %33
7: ; preds = %2
%8 = getelementptr inbounds i8*, i8** %1, i64 1
%9 = load i8*, i8** %8
%10 = load i32, i32* @O_CREAT
%11 = load i32, i32* @O_WRONLY
%12 = or i32 %11, %10
%13 = load i32, i32* @S_IRWXU
%14 = tail call i32 (i8*, i32, ...) @open(i8* %9, i32 %12, i32 %13)
%15 = icmp sgt i32 %14, 0
br i1 %15, label %16, label %21
16: ; preds = %7
%17 = tail call i32 @write(i32 %14, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i32 6)
%18 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i32 %17)
%19 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
%20 = tail call i32 @close(i32 %14)
br label %21
21: ; preds = %16, %7
%22 = load i8*, i8** %8
%23 = load i32, i32* @O_RDONLY
%24 = tail call i32 (i8*, i32, ...) @open(i8* %22, i32 %23)
%25 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0), i32 %24)
%26 = tail call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.5, i64 0, i64 0))
%27 = icmp sgt i32 %24, 0
br i1 %27, label %28, label %33
28: ; preds = %21
%29 = getelementptr inbounds [10 x i8], [10 x i8]* %3, i64 0, i64 0
%30 = call i32 @read(i32 %24, i8* %29, i32 10)
%31 = tail call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.6, i64 0, i64 0))
%32 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.7, i64 0, i64 0), i32 %30, i8* %29)
br label %33
33: ; preds = %21, %28, %5
%34 = phi i32 [ -1, %5 ], [ 0, %28 ], [ 0, %21 ]
ret i32 %34
}