#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_SET;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int lseek(int, int, int /*<<< orphan*/);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int open(char *, int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ read(int, char *, int);
int /*<<< orphan*/ strlen(char *);
int /*<<< orphan*/ umask(int);
int write(int, char *, int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  if (argc < 2) {
    printf("please input filename\n");
    exit(1);
  } else {
    umask(0000);
    int fd;
    fd = open(argv[1], O_RDWR | O_CREAT, 0666);
    if (fd > 0) {
      char buf[] = "hello sundy\n";
      int rf = write(fd, buf, strlen(buf));
      if (rf != -1) {
        printf("file created success%d\n", rf);
        int pos = lseek(fd, 3, SEEK_SET);
        char buf2[1024];
        memset(buf2, 0, 1024);
        read(fd, buf2, 1024);
        printf("buf2=%s\n", buf2);
      } else {
        perror("err:");
      }
      close(fd);
    } else {
      perror("file creat failed\n");
      exit(1);
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x440,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    11eb <main+0x3b>
lea    0xe26(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
xor    %edi,%edi
call   10a0 <umask@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2ddf(%rip),%rax
mov    (%rax),%esi
mov    0x2dee(%rip),%rax
or     (%rax),%esi
mov    $0x1b6,%edx
call   10b0 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jle    1302 <main+0x152>
mov    0xded(%rip),%rax
mov    %rax,-0x21(%rbp)
mov    0xdeb(%rip),%eax
mov    %eax,-0x19(%rbp)
mov    0xde6(%rip),%al
mov    %al,-0x15(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x43c(%rbp)
lea    -0x21(%rbp),%rax
mov    %rax,-0x438(%rbp)
lea    -0x21(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x43c(%rbp),%edi
mov    -0x438(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0xffffffff,-0x28(%rbp)
je     12e9 <main+0x139>
mov    -0x28(%rbp),%esi
lea    0xd9c(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x14(%rbp),%edi
mov    0x2d37(%rip),%rax
mov    (%rax),%edx
mov    $0x3,%esi
call   1060 <lseek@plt>
mov    %eax,-0x2c(%rbp)
lea    -0x430(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1070 <memset@plt>
mov    -0x14(%rbp),%edi
lea    -0x430(%rbp),%rsi
mov    $0x400,%edx
call   1090 <read@plt>
lea    -0x430(%rbp),%rsi
lea    0xd5f(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    12f5 <main+0x145>
lea    0xd55(%rip),%rdi
call   10c0 <perror@plt>
mov    -0x14(%rbp),%edi
call   1080 <close@plt>
jmp    1318 <main+0x168>
lea    0xd41(%rip),%rdi
call   10c0 <perror@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
jmp    131d <main+0x16d>
xor    %eax,%eax
add    $0x440,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x418,%rsp
cmp    $0x1,%edi
jle    12bb <main+0xfb>
mov    %rsi,%rbx
xor    %edi,%edi
call   10b0 <umask@plt>
mov    0x8(%rbx),%rdi
mov    0x2df8(%rip),%rax
mov    0x2e09(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    $0x1b6,%edx
call   10c0 <open@plt>
test   %eax,%eax
jle    12d1 <main+0x111>
mov    %eax,%ebx
movabs $0xa79646e757320,%rax
mov    %rax,0x5(%rsp)
movabs $0x7573206f6c6c6568,%rax
20 73 75
mov    %rax,(%rsp)
mov    %rsp,%r14
mov    %r14,%rdi
call   1050 <strlen@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    %eax,%edx
call   1040 <write@plt>
cmp    $0xffffffff,%eax
je     12ad <main+0xed>
lea    0xdc6(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    0x2d79(%rip),%rax
mov    (%rax),%edx
mov    %ebx,%edi
mov    $0x3,%esi
call   1070 <lseek@plt>
lea    0x10(%rsp),%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1080 <memset@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x400,%edx
call   10a0 <read@plt>
lea    0xd96(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %ebx,%edi
call   1090 <close@plt>
xor    %eax,%eax
add    $0x418,%rsp
pop    %rbx
pop    %r14
ret
lea    0xd7a(%rip),%rdi
call   10d0 <perror@plt>
jmp    1299 <main+0xd9>
lea    0xd84(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>
lea    0xd5b(%rip),%rdi
call   10d0 <perror@plt>
mov    $0x1,%edi
call   10e0 <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
@__const.main.buf = [13 x i8] c"hello sundy\0A\00"
@.str.1 = [24 x i8] c"file created success%d\0A\00"
@SEEK_SET = global i32 0
@.str.2 = [9 x i8] c"buf2=%s\0A\00"
@.str.3 = [5 x i8] c"err:\00"
@.str.4 = [19 x i8] c"file creat failed\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca [13 x i8]
%8 = alloca i32
%9 = alloca i32
%10 = alloca [1024 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = load i32, i32* %4
%12 = icmp slt i32 %11, 2
br i1 %12, label %13, label %16
13: ; preds = %2
%14 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
%15 = call i32 @exit(i32 1)
unreachable
16: ; preds = %2
%17 = call i32 @umask(i32 0)
%18 = load i8**, i8*** %5
%19 = getelementptr inbounds i8*, i8** %18, i64 1
%20 = load i8*, i8** %19
%21 = load i32, i32* @O_RDWR
%22 = load i32, i32* @O_CREAT
%23 = or i32 %21, %22
%24 = call i32 @open(i8* %20, i32 %23, i32 438)
store i32 %24, i32* %6
%25 = load i32, i32* %6
%26 = icmp sgt i32 %25, 0
br i1 %26, label %27, label %54
27: ; preds = %16
%28 = bitcast [13 x i8]* %7 to i8*
%29 = load i32, i32* %6
%30 = getelementptr inbounds [13 x i8], [13 x i8]* %7, i64 0, i64 0
%31 = getelementptr inbounds [13 x i8], [13 x i8]* %7, i64 0, i64 0
%32 = call i32 @strlen(i8* %31)
%33 = call i32 @write(i32 %29, i8* %30, i32 %32)
store i32 %33, i32* %8
%34 = load i32, i32* %8
%35 = icmp ne i32 %34, -1
br i1 %35, label %36, label %49
36: ; preds = %27
%37 = load i32, i32* %8
%38 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i32 %37)
%39 = load i32, i32* %6
%40 = load i32, i32* @SEEK_SET
%41 = call i32 @lseek(i32 %39, i32 3, i32 %40)
store i32 %41, i32* %9
%42 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 0
%43 = call i32 @memset(i8* %42, i32 0, i32 1024)
%44 = load i32, i32* %6
%45 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 0
%46 = call i32 @read(i32 %44, i8* %45, i32 1024)
%47 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 0
%48 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i8* %47)
br label %51
49: ; preds = %27
%50 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
br label %51
51: ; preds = %49, %36
%52 = load i32, i32* %6
%53 = call i32 @close(i32 %52)
br label %57
54: ; preds = %16
%55 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0))
%56 = call i32 @exit(i32 1)
unreachable
57: ; preds = %51
br label %58
58: ; preds = %57
ret i32 0
}

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