#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 /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ SEEK_END;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int lseek(int, int, int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int);
int write(int, char *, int);
main() {
  int fd, nread, nwrite, ret;
  char buff[100];
  fd = open("123", O_RDWR);
  printf("open system call return value:%d\n", fd);
  if (fd < 0) {
    perror("Open FAILS\n");
    exit(0);
  }
  ret = lseek(fd, 3, SEEK_END);
  printf("lseek ret value:%d\n", ret);
  nwrite = write(fd, "Linux", 3);
  printf("write ret value:%d\n", nwrite);
  close(fd);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    0x2e66(%rip),%rax
mov    (%rax),%esi
lea    0xe75(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%esi
lea    0xe67(%rip),%rdi
call   1040 <printf@plt>
cmpl   $0x0,-0x4(%rbp)
jge    11bf <main+0x4f>
lea    0xe73(%rip),%rdi
call   1080 <perror@plt>
xor    %edi,%edi
call   1090 <exit@plt>
mov    -0x4(%rbp),%edi
mov    0x2e17(%rip),%rax
mov    (%rax),%edx
mov    $0x3,%esi
call   1050 <lseek@plt>
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%esi
lea    0xe50(%rip),%rdi
call   1040 <printf@plt>
mov    -0x4(%rbp),%edi
lea    0xe55(%rip),%rsi
mov    $0x3,%edx
call   1030 <write@plt>
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%esi
lea    0xe44(%rip),%rdi
call   1040 <printf@plt>
mov    -0x4(%rbp),%edi
call   1060 <close@plt>
xor    %eax,%eax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
mov    0x2e70(%rip),%rax
mov    (%rax),%esi
lea    0xe7f(%rip),%rdi
call   1070 <open@plt>
mov    %eax,%ebx
lea    0xe75(%rip),%rdi
mov    %eax,%esi
call   1040 <printf@plt>
test   %ebx,%ebx
js     11e9 <main+0x79>
mov    0x2e3f(%rip),%rax
mov    (%rax),%edx
mov    %ebx,%edi
mov    $0x3,%esi
call   1050 <lseek@plt>
lea    0xe7c(%rip),%rdi
mov    %eax,%esi
call   1040 <printf@plt>
lea    0xe82(%rip),%rsi
mov    %ebx,%edi
mov    $0x3,%edx
call   1030 <write@plt>
lea    0xe75(%rip),%rdi
mov    %eax,%esi
call   1040 <printf@plt>
mov    %ebx,%edi
call   1060 <close@plt>
xor    %eax,%eax
pop    %rbx
ret
lea    0xe36(%rip),%rdi
call   1080 <perror@plt>
xor    %edi,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [4 x i8] c"123\00"
@O_RDWR = global i32 0
@.str.1 = [34 x i8] c"open system call return value:%d\0A\00"
@.str.2 = [12 x i8] c"Open FAILS\0A\00"
@SEEK_END = global i32 0
@.str.3 = [20 x i8] c"lseek ret value:%d\0A\00"
@.str.4 = [6 x i8] c"Linux\00"
@.str.5 = [20 x i8] c"write ret value:%d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca [100 x i8]
%6 = load i32, i32* @O_RDWR
%7 = call i32 @open(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %6)
store i32 %7, i32* %1
%8 = load i32, i32* %1
%9 = call i32 @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0), i32 %8)
%10 = load i32, i32* %1
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %0
%13 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
%14 = call i32 @exit(i32 0)
unreachable
15: ; preds = %0
%16 = load i32, i32* %1
%17 = load i32, i32* @SEEK_END
%18 = call i32 @lseek(i32 %16, i32 3, i32 %17)
store i32 %18, i32* %4
%19 = load i32, i32* %4
%20 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0), i32 %19)
%21 = load i32, i32* %1
%22 = call i32 @write(i32 %21, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), i32 3)
store i32 %22, i32* %3
%23 = load i32, i32* %3
%24 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.5, i64 0, i64 0), i32 %23)
%25 = load i32, i32* %1
%26 = call i32 @close(i32 %25)
ret i32 0
}

LLVM IR (-O2):
@.str = [4 x i8] c"123\00"
@O_RDWR = global i32 0
@.str.1 = [34 x i8] c"open system call return value:%d\0A\00"
@.str.2 = [12 x i8] c"Open FAILS\0A\00"
@SEEK_END = global i32 0
@.str.3 = [20 x i8] c"lseek ret value:%d\0A\00"
@.str.4 = [6 x i8] c"Linux\00"
@.str.5 = [20 x i8] c"write ret value:%d\0A\00"
define i32 @main() {
%1 = load i32, i32* @O_RDWR
%2 = tail call i32 @open(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %1)
%3 = tail call i32 @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0), i32 %2)
%4 = icmp slt i32 %2, 0
br i1 %4, label %5, label %8
5: ; preds = %0
%6 = tail call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
%7 = tail call i32 @exit(i32 0)
unreachable
8: ; preds = %0
%9 = load i32, i32* @SEEK_END
%10 = tail call i32 @lseek(i32 %2, i32 3, i32 %9)
%11 = tail call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0), i32 %10)
%12 = tail call i32 @write(i32 %2, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), i32 3)
%13 = tail call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.5, i64 0, i64 0), i32 %12)
%14 = tail call i32 @close(i32 %2)
ret i32 0
}