#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*/ SEEK_END;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int lseek(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int open(char *, int, int);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
int /*<<< orphan*/ umask(int);
int main(int argc, char *argv[]) {
  if (argc < 2) {
    printf("please input filename\n");
    exit(1);
  } else {
    int fd;
    umask(0000);
    fd = open(argv[1], O_RDWR | O_CREAT, 0664);
    if (fd < 0) {
      printf(" error\n");
      exit(1);
    } else {
      printf("success=%d\n", fd);
      char buf[1024];
      memset(buf, 0, 1024);
      int returnum = read(fd, buf, 1024);
      if (returnum != -1) {
        printf("buf=%s\n", buf);
        int file_len = lseek(fd, 0, SEEK_END);
        printf("file length=%d\n", file_len);
      } else {
        printf("error\n");
      }
      close(fd);
      printf("closed\n");
    }
  }
  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)
cmpl   $0x2,-0x8(%rbp)
jge    11bb <main+0x3b>
lea    0xe56(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
xor    %edi,%edi
call   1080 <umask@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e0f(%rip),%rax
mov    (%rax),%esi
mov    0x2e1e(%rip),%rax
or     (%rax),%esi
mov    $0x1b4,%edx
call   1090 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    120b <main+0x8b>
lea    0xe1d(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x14(%rbp),%esi
lea    0xe0a(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    -0x420(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1050 <memset@plt>
mov    -0x14(%rbp),%edi
lea    -0x420(%rbp),%rsi
mov    $0x400,%edx
call   1070 <read@plt>
mov    %eax,-0x424(%rbp)
cmpl   $0xffffffff,-0x424(%rbp)
je     129d <main+0x11d>
lea    -0x420(%rbp),%rsi
lea    0xdc7(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x14(%rbp),%edi
mov    0x2d63(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1040 <lseek@plt>
mov    %eax,-0x428(%rbp)
mov    -0x428(%rbp),%esi
lea    0xda2(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    12ab <main+0x12b>
lea    0xd74(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
lea    0xd89(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    12c6 <main+0x146>
xor    %eax,%eax
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x408,%rsp
cmp    $0x1,%edi
jle    126e <main+0xde>
mov    %rsi,%rbx
xor    %edi,%edi
call   1090 <umask@plt>
mov    0x8(%rbx),%rdi
mov    0x2e28(%rip),%rax
mov    0x2e39(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    $0x1b4,%edx
call   10a0 <open@plt>
test   %eax,%eax
js     1277 <main+0xe7>
mov    %eax,%ebx
lea    0xe22(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %rsp,%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1060 <memset@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x400,%edx
call   1080 <read@plt>
cmp    $0xffffffff,%eax
je     1242 <main+0xb2>
lea    0xdf8(%rip),%rdi
mov    %rsp,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x2db3(%rip),%rax
mov    (%rax),%edx
mov    %ebx,%edi
xor    %esi,%esi
call   1050 <lseek@plt>
lea    0xddd(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
jmp    124e <main+0xbe>
lea    0xde3(%rip),%rdi
call   1030 <puts@plt>
mov    %ebx,%edi
call   1070 <close@plt>
lea    0xdc8(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
add    $0x408,%rsp
pop    %rbx
pop    %r14
ret
lea    0xdbd(%rip),%rdi
jmp    127e <main+0xee>
lea    0xdad(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@.str = [23 x i8] c"please input filename\0A\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@.str.1 = [8 x i8] c" error\0A\00"
@.str.2 = [12 x i8] c"success=%d\0A\00"
@.str.3 = [8 x i8] c"buf=%s\0A\00"
@SEEK_END = global i32 0
@.str.4 = [16 x i8] c"file length=%d\0A\00"
@.str.5 = [7 x i8] c"error\0A\00"
@.str.6 = [8 x i8] c"closed\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca [1024 x i8]
%8 = alloca i32
%9 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* %4
%11 = icmp slt i32 %10, 2
br i1 %11, label %12, label %15
12: ; preds = %2
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
%14 = call i32 @exit(i32 1)
unreachable
15: ; preds = %2
%16 = call i32 @umask(i32 0)
%17 = load i8**, i8*** %5
%18 = getelementptr inbounds i8*, i8** %17, i64 1
%19 = load i8*, i8** %18
%20 = load i32, i32* @O_RDWR
%21 = load i32, i32* @O_CREAT
%22 = or i32 %20, %21
%23 = call i32 @open(i8* %19, i32 %22, i32 436)
store i32 %23, i32* %6
%24 = load i32, i32* %6
%25 = icmp slt i32 %24, 0
br i1 %25, label %26, label %29
26: ; preds = %15
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
%28 = call i32 @exit(i32 1)
unreachable
29: ; preds = %15
%30 = load i32, i32* %6
%31 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i32 %30)
%32 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%33 = call i32 @memset(i8* %32, i32 0, i32 1024)
%34 = load i32, i32* %6
%35 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%36 = call i32 @read(i32 %34, i8* %35, i32 1024)
store i32 %36, i32* %8
%37 = load i32, i32* %8
%38 = icmp ne i32 %37, -1
br i1 %38, label %39, label %47
39: ; preds = %29
%40 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%41 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i8* %40)
%42 = load i32, i32* %6
%43 = load i32, i32* @SEEK_END
%44 = call i32 @lseek(i32 %42, i32 0, i32 %43)
store i32 %44, i32* %9
%45 = load i32, i32* %9
%46 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0), i32 %45)
br label %49
47: ; preds = %29
%48 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0))
br label %49
49: ; preds = %47, %39
%50 = load i32, i32* %6
%51 = call i32 @close(i32 %50)
%52 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.6, i64 0, i64 0))
br label %53
53: ; preds = %49
br label %54
54: ; preds = %53
ret i32 0
}

LLVM IR (-O2):
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@.str.2 = [12 x i8] c"success=%d\0A\00"
@.str.3 = [8 x i8] c"buf=%s\0A\00"
@SEEK_END = global i32 0
@.str.4 = [16 x i8] c"file length=%d\0A\00"
@str = [6 x i8] c"error\00"
@str.7 = [7 x i8] c"closed\00"
@str.8 = [7 x i8] c" error\00"
@str.9 = [22 x i8] c"please input filename\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [1024 x i8]
%4 = icmp slt i32 %0, 2
br i1 %4, label %5, label %8
5: ; preds = %2
%6 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @str.9, i64 0, i64 0))
%7 = tail call i32 @exit(i32 1)
unreachable
8: ; preds = %2
%9 = tail call i32 @umask(i32 0)
%10 = getelementptr inbounds i8*, i8** %1, i64 1
%11 = load i8*, i8** %10
%12 = load i32, i32* @O_RDWR
%13 = load i32, i32* @O_CREAT
%14 = or i32 %13, %12
%15 = tail call i32 @open(i8* %11, i32 %14, i32 436)
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %8
%18 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @str.8, i64 0, i64 0))
%19 = tail call i32 @exit(i32 1)
unreachable
20: ; preds = %8
%21 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i32 %15)
%22 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%23 = call i32 @memset(i8* %22, i32 0, i32 1024)
%24 = call i32 @read(i32 %15, i8* %22, i32 1024)
%25 = icmp eq i32 %24, -1
br i1 %25, label %31, label %26
26: ; preds = %20
%27 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i8* %22)
%28 = load i32, i32* @SEEK_END
%29 = call i32 @lseek(i32 %15, i32 0, i32 %28)
%30 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0), i32 %29)
br label %33
31: ; preds = %20
%32 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @str, i64 0, i64 0))
br label %33
33: ; preds = %31, %26
%34 = call i32 @close(i32 %15)
%35 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @str.7, i64 0, i64 0))
ret i32 0
}