#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 /*<<< orphan*/ lseek(int, int, int /*<<< orphan*/);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int open(char *, int, int);
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 {
    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[] = "hello sundy!\n";
      int returnum = write(fd, buf, strlen(buf));
      lseek(fd, 5, SEEK_SET);
      char buf2[1024];
      memset(buf2, 0, 1024);
      read(fd, buf2, 1024);
      printf("buf2=%s\n", buf2);
      if (returnum != -1) {
      } else {
        printf("error\n");
      }
      close(fd);
    }
  }
  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    11db <main+0x3b>
lea    0xe36(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
xor    %edi,%edi
call   10a0 <umask@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2def(%rip),%rax
mov    (%rax),%esi
mov    0x2dfe(%rip),%rax
or     (%rax),%esi
mov    $0x1b4,%edx
call   10b0 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    122b <main+0x8b>
lea    0xdfd(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    -0x14(%rbp),%esi
lea    0xdea(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    0xde8(%rip),%rax
mov    %rax,-0x22(%rbp)
mov    0xde6(%rip),%eax
mov    %eax,-0x1a(%rbp)
mov    0xde0(%rip),%ax
mov    %ax,-0x16(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x43c(%rbp)
lea    -0x22(%rbp),%rax
mov    %rax,-0x438(%rbp)
lea    -0x22(%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)
mov    -0x14(%rbp),%edi
mov    0x2d37(%rip),%rax
mov    (%rax),%edx
mov    $0x5,%esi
call   1060 <lseek@plt>
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>
cmpl   $0xffffffff,-0x28(%rbp)
je     12f0 <main+0x150>
jmp    12fe <main+0x15e>
lea    0xd21(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x14(%rbp),%edi
call   1080 <close@plt>
jmp    130b <main+0x16b>
xor    %eax,%eax
add    $0x440,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x410,%rsp
cmp    $0x1,%edi
jle    12af <main+0xff>
mov    %rsi,%rbx
xor    %edi,%edi
call   10b0 <umask@plt>
mov    0x8(%rbx),%rdi
mov    0x2e07(%rip),%rax
mov    0x2e18(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    $0x1b4,%edx
call   10c0 <open@plt>
test   %eax,%eax
js     12b8 <main+0x108>
mov    %eax,%ebx
lea    0xe01(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
movabs $0xa2179646e7573,%rax
mov    %rax,0x6(%rsp)
movabs $0x7573206f6c6c6568,%rax
20 73 75
mov    %rax,(%rsp)
mov    %rsp,%rbp
mov    %rbp,%rdi
call   1050 <strlen@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    %eax,%edx
call   1040 <write@plt>
mov    %eax,%r14d
mov    0x2d8a(%rip),%rax
mov    (%rax),%edx
mov    %ebx,%edi
mov    $0x5,%esi
call   1070 <lseek@plt>
lea    0x10(%rsp),%rbp
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1080 <memset@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0x400,%edx
call   10a0 <read@plt>
lea    0xd9c(%rip),%rdi
mov    %rbp,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
cmp    $0xffffffff,%r14d
jne    129a <main+0xea>
lea    0xd8f(%rip),%rdi
call   1030 <puts@plt>
mov    %ebx,%edi
call   1090 <close@plt>
xor    %eax,%eax
add    $0x410,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xd74(%rip),%rdi
jmp    12bf <main+0x10f>
lea    0xd64(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   10d0 <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"
@__const.main.buf = [14 x i8] c"hello sundy!\0A\00"
@SEEK_SET = global i32 0
@.str.3 = [9 x i8] c"buf2=%s\0A\00"
@.str.4 = [7 x i8] c"error\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca [14 x i8]
%8 = alloca i32
%9 = alloca [1024 x i8]
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 = bitcast [14 x i8]* %7 to i8*
%33 = load i32, i32* %6
%34 = getelementptr inbounds [14 x i8], [14 x i8]* %7, i64 0, i64 0
%35 = getelementptr inbounds [14 x i8], [14 x i8]* %7, i64 0, i64 0
%36 = call i32 @strlen(i8* %35)
%37 = call i32 @write(i32 %33, i8* %34, i32 %36)
store i32 %37, i32* %8
%38 = load i32, i32* %6
%39 = load i32, i32* @SEEK_SET
%40 = call i32 @lseek(i32 %38, i32 5, i32 %39)
%41 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%42 = call i32 @memset(i8* %41, i32 0, i32 1024)
%43 = load i32, i32* %6
%44 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%45 = call i32 @read(i32 %43, i8* %44, i32 1024)
%46 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%47 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i8* %46)
%48 = load i32, i32* %8
%49 = icmp ne i32 %48, -1
br i1 %49, label %50, label %51
50: ; preds = %29
br label %53
51: ; preds = %29
%52 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
br label %53
53: ; preds = %51, %50
%54 = load i32, i32* %6
%55 = call i32 @close(i32 %54)
br label %56
56: ; preds = %53
br label %57
57: ; preds = %56
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"
@__const.main.buf = [14 x i8] c"hello sundy!\0A\00"
@SEEK_SET = global i32 0
@.str.3 = [9 x i8] c"buf2=%s\0A\00"
@str = [6 x i8] c"error\00"
@str.5 = [7 x i8] c" error\00"
@str.6 = [22 x i8] c"please input filename\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [14 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.6, 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 436)
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %9
%19 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @str.5, i64 0, i64 0))
%20 = tail call i32 @exit(i32 1)
unreachable
21: ; preds = %9
%22 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i32 %16)
%23 = getelementptr inbounds [14 x i8], [14 x i8]* %3, i64 0, i64 0
%24 = call i32 @strlen(i8* %23)
%25 = call i32 @write(i32 %16, i8* %23, i32 %24)
%26 = load i32, i32* @SEEK_SET
%27 = call i32 @lseek(i32 %16, i32 5, i32 %26)
%28 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%29 = call i32 @memset(i8* %28, i32 0, i32 1024)
%30 = call i32 @read(i32 %16, i8* %28, i32 1024)
%31 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i8* %28)
%32 = icmp eq i32 %25, -1
br i1 %32, label %33, label %35
33: ; preds = %21
%34 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @str, i64 0, i64 0))
br label %35
35: ; preds = %21, %33
%36 = call i32 @close(i32 %16)
ret i32 0
}