#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 O_TRUNC;
int /*<<< orphan*/ SEEK_END;
int /*<<< orphan*/ S_IRWXU;
int /*<<< orphan*/ close(int);
int lseek(int, int, int /*<<< orphan*/);
int /*<<< orphan*/ my_err(char *, int);
int /*<<< orphan*/ my_read(int);
int open(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *);
scalar_t__ strlen(char *);
scalar_t__ write(int, char *, scalar_t__);
int main(int argc, char *argv[]) {
  int fd;
  char write_buf[32] = "Hello World!";
  if ((fd = open("example_rwl.c", O_RDWR | O_CREAT | O_TRUNC, S_IRWXU)) == -1) {
    my_err("open", 42);
  } else {
    printf("create file success\n");
  }
  if (write(fd, write_buf, strlen(write_buf)) != strlen(write_buf)) {
    my_err("write", 49);
  }
  my_read(fd);
  printf("/*------------------------------*/\n");
  if (lseek(fd, 10, SEEK_END) == -1) {
    my_err("lseek", 56);
  }
  if (write(fd, write_buf, strlen(write_buf)) != strlen(write_buf)) {
    my_err("write", 59);
  }
  my_read(fd);
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0xe63(%rip),%rax
mov    %rax,-0x40(%rbp)
mov    0xe60(%rip),%rax
mov    %rax,-0x38(%rbp)
mov    0xe5d(%rip),%rax
mov    %rax,-0x30(%rbp)
mov    0xe5a(%rip),%rax
mov    %rax,-0x28(%rbp)
mov    0x2e07(%rip),%rax
mov    (%rax),%esi
mov    0x2e26(%rip),%rax
or     (%rax),%esi
mov    0x2e15(%rip),%rax
or     (%rax),%esi
mov    0x2df4(%rip),%rax
mov    (%rax),%edx
lea    0xe33(%rip),%rdi
call   1090 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    1214 <main+0x94>
lea    0xe29(%rip),%rdi
mov    $0x2a,%esi
call   10a0 <my_err@plt>
jmp    1220 <main+0xa0>
lea    0xe18(%rip),%rdi
call   1060 <printf@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x54(%rbp)
lea    -0x40(%rbp),%rax
mov    %rax,-0x50(%rbp)
lea    -0x40(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x54(%rbp),%edi
mov    -0x50(%rbp),%rsi
mov    %rax,%rdx
call   1030 <write@plt>
mov    %rax,-0x48(%rbp)
lea    -0x40(%rbp),%rdi
call   1050 <strlen@plt>
mov    %rax,%rcx
mov    -0x48(%rbp),%rax
cmp    %rcx,%rax
je     1274 <main+0xf4>
lea    0xdde(%rip),%rdi
mov    $0x31,%esi
call   10a0 <my_err@plt>
mov    -0x14(%rbp),%edi
call   1040 <my_read@plt>
lea    0xdcb(%rip),%rdi
call   1060 <printf@plt>
mov    -0x14(%rbp),%edi
mov    0x2d36(%rip),%rax
mov    (%rax),%edx
mov    $0xa,%esi
call   1070 <lseek@plt>
cmp    $0xffffffff,%eax
jne    12b8 <main+0x138>
lea    0xdc4(%rip),%rdi
mov    $0x38,%esi
call   10a0 <my_err@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x6c(%rbp)
lea    -0x40(%rbp),%rax
mov    %rax,-0x68(%rbp)
lea    -0x40(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x6c(%rbp),%edi
mov    -0x68(%rbp),%rsi
mov    %rax,%rdx
call   1030 <write@plt>
mov    %rax,-0x60(%rbp)
lea    -0x40(%rbp),%rdi
call   1050 <strlen@plt>
mov    %rax,%rcx
mov    -0x60(%rbp),%rax
cmp    %rcx,%rax
je     130c <main+0x18c>
lea    0xd46(%rip),%rdi
mov    $0x3b,%esi
call   10a0 <my_err@plt>
mov    -0x14(%rbp),%edi
call   1040 <my_read@plt>
mov    -0x14(%rbp),%edi
call   1080 <close@plt>
xor    %eax,%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x28,%rsp
movaps 0xe7f(%rip),%xmm0
movaps %xmm0,0x10(%rsp)
movaps 0xe63(%rip),%xmm0
movaps %xmm0,(%rsp)
mov    0x2e28(%rip),%rax
mov    0x2e49(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e36(%rip),%rax
or     (%rax),%esi
mov    0x2e15(%rip),%rax
mov    (%rax),%edx
lea    0xe54(%rip),%rdi
call   1090 <open@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
je     11e6 <main+0x66>
lea    0xe60(%rip),%rdi
call   1030 <puts@plt>
jmp    11f7 <main+0x77>
lea    0xe41(%rip),%rdi
mov    $0x2a,%esi
call   10a0 <my_err@plt>
mov    %rsp,%r14
mov    %r14,%rdi
call   1060 <strlen@plt>
mov    %ebp,%edi
mov    %r14,%rsi
mov    %rax,%rdx
call   1040 <write@plt>
mov    %rax,%rbx
mov    %r14,%rdi
call   1060 <strlen@plt>
mov    %rax,%r14
cmp    %rax,%rbx
je     1233 <main+0xb3>
lea    0xe0a(%rip),%rdi
mov    $0x31,%esi
call   10a0 <my_err@plt>
mov    %ebp,%edi
call   1050 <my_read@plt>
lea    0xe12(%rip),%rdi
call   1030 <puts@plt>
mov    0x2d7b(%rip),%rax
mov    (%rax),%edx
mov    %ebp,%edi
mov    $0xa,%esi
call   1070 <lseek@plt>
cmp    $0xffffffff,%eax
jne    1271 <main+0xf1>
lea    0xdd2(%rip),%rdi
mov    $0x38,%esi
call   10a0 <my_err@plt>
mov    %rsp,%r15
mov    %ebp,%edi
mov    %r15,%rsi
mov    %r14,%rdx
call   1040 <write@plt>
mov    %rax,%rbx
mov    %r15,%rdi
call   1060 <strlen@plt>
cmp    %rax,%rbx
je     12a2 <main+0x122>
lea    0xd9b(%rip),%rdi
mov    $0x3b,%esi
call   10a0 <my_err@plt>
mov    %ebp,%edi
call   1050 <my_read@plt>
mov    %ebp,%edi
call   1080 <close@plt>
xor    %eax,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@__const.main.write_buf = [32 x i8] c"Hello World!\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00"
@.str = [14 x i8] c"example_rwl.c\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@S_IRWXU = global i32 0
@.str.1 = [5 x i8] c"open\00"
@.str.2 = [21 x i8] c"create file success\0A\00"
@.str.3 = [6 x i8] c"write\00"
@.str.4 = [36 x i8] c"/*------------------------------*/\0A\00"
@SEEK_END = global i32 0
@.str.5 = [6 x i8] c"lseek\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca [32 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = bitcast [32 x i8]* %7 to i8*
%9 = load i32, i32* @O_RDWR
%10 = load i32, i32* @O_CREAT
%11 = or i32 %9, %10
%12 = load i32, i32* @O_TRUNC
%13 = or i32 %11, %12
%14 = load i32, i32* @S_IRWXU
%15 = call i32 @open(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %13, i32 %14)
store i32 %15, i32* %6
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %19
17: ; preds = %2
%18 = call i32 @my_err(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 42)
br label %21
19: ; preds = %2
%20 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
br label %21
21: ; preds = %19, %17
%22 = load i32, i32* %6
%23 = getelementptr inbounds [32 x i8], [32 x i8]* %7, i64 0, i64 0
%24 = getelementptr inbounds [32 x i8], [32 x i8]* %7, i64 0, i64 0
%25 = call i64 @strlen(i8* %24)
%26 = call i64 @write(i32 %22, i8* %23, i64 %25)
%27 = getelementptr inbounds [32 x i8], [32 x i8]* %7, i64 0, i64 0
%28 = call i64 @strlen(i8* %27)
%29 = icmp ne i64 %26, %28
br i1 %29, label %30, label %32
30: ; preds = %21
%31 = call i32 @my_err(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i32 49)
br label %32
32: ; preds = %30, %21
%33 = load i32, i32* %6
%34 = call i32 @my_read(i32 %33)
%35 = call i32 @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.4, i64 0, i64 0))
%36 = load i32, i32* %6
%37 = load i32, i32* @SEEK_END
%38 = call i32 @lseek(i32 %36, i32 10, i32 %37)
%39 = icmp eq i32 %38, -1
br i1 %39, label %40, label %42
40: ; preds = %32
%41 = call i32 @my_err(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0), i32 56)
br label %42
42: ; preds = %40, %32
%43 = load i32, i32* %6
%44 = getelementptr inbounds [32 x i8], [32 x i8]* %7, i64 0, i64 0
%45 = getelementptr inbounds [32 x i8], [32 x i8]* %7, i64 0, i64 0
%46 = call i64 @strlen(i8* %45)
%47 = call i64 @write(i32 %43, i8* %44, i64 %46)
%48 = getelementptr inbounds [32 x i8], [32 x i8]* %7, i64 0, i64 0
%49 = call i64 @strlen(i8* %48)
%50 = icmp ne i64 %47, %49
br i1 %50, label %51, label %53
51: ; preds = %42
%52 = call i32 @my_err(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i32 59)
br label %53
53: ; preds = %51, %42
%54 = load i32, i32* %6
%55 = call i32 @my_read(i32 %54)
%56 = load i32, i32* %6
%57 = call i32 @close(i32 %56)
ret i32 0
}

LLVM IR (-O2):
@__const.main.write_buf = [32 x i8] c"Hello World!\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00"
@.str = [14 x i8] c"example_rwl.c\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@S_IRWXU = global i32 0
@.str.1 = [5 x i8] c"open\00"
@.str.3 = [6 x i8] c"write\00"
@SEEK_END = global i32 0
@.str.5 = [6 x i8] c"lseek\00"
@str = [20 x i8] c"create file success\00"
@str.6 = [35 x i8] c"/*------------------------------*/\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [32 x i8]
%4 = getelementptr inbounds [32 x i8], [32 x i8]* %3, i64 0, i64 0
%5 = load i32, i32* @O_RDWR
%6 = load i32, i32* @O_CREAT
%7 = or i32 %6, %5
%8 = load i32, i32* @O_TRUNC
%9 = or i32 %7, %8
%10 = load i32, i32* @S_IRWXU
%11 = tail call i32 @open(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %9, i32 %10)
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %15
13: ; preds = %2
%14 = tail call i32 @my_err(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 42)
br label %17
15: ; preds = %2
%16 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str, i64 0, i64 0))
br label %17
17: ; preds = %15, %13
%18 = call i64 @strlen(i8* %4)
%19 = call i64 @write(i32 %11, i8* %4, i64 %18)
%20 = call i64 @strlen(i8* %4)
%21 = icmp eq i64 %19, %20
br i1 %21, label %24, label %22
22: ; preds = %17
%23 = tail call i32 @my_err(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i32 49)
br label %24
24: ; preds = %22, %17
%25 = tail call i32 @my_read(i32 %11)
%26 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @str.6, i64 0, i64 0))
%27 = load i32, i32* @SEEK_END
%28 = tail call i32 @lseek(i32 %11, i32 10, i32 %27)
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %32
30: ; preds = %24
%31 = tail call i32 @my_err(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0), i32 56)
br label %32
32: ; preds = %30, %24
%33 = call i64 @write(i32 %11, i8* %4, i64 %20)
%34 = call i64 @strlen(i8* %4)
%35 = icmp eq i64 %33, %34
br i1 %35, label %38, label %36
36: ; preds = %32
%37 = tail call i32 @my_err(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i32 59)
br label %38
38: ; preds = %36, %32
%39 = tail call i32 @my_read(i32 %11)
%40 = tail call i32 @close(i32 %11)
ret i32 0
}