#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*/ EXIT_SUCCESS;
int O_APPEND;
int O_RDWR;
int /*<<< orphan*/ SEEK_SET;
int close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ exitErr(char *);
int /*<<< orphan*/ lseek(int, int /*<<< orphan*/, int /*<<< orphan*/);
int open(char const *, int);
int write(int, char *, int);
int main(__attribute__((unused)) int _argc,
         __attribute__((unused)) char **argv) {
  const char *filename = "example.txt";
  int fd;
  fd = open(filename, O_RDWR | O_APPEND);
  if (fd == -1)
    exitErr("open");
  lseek(fd, 0, SEEK_SET);
  if (write(fd, "whatup", 6) == -1)
    exitErr("write");
  if (close(fd) == -1)
    exitErr("close");
  exit(EXIT_SUCCESS);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xe83(%rip),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    0x2e5c(%rip),%rax
mov    (%rax),%esi
mov    0x2e2b(%rip),%rax
or     (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    11b5 <main+0x55>
lea    0xe5c(%rip),%rdi
call   1030 <exitErr@plt>
mov    -0x1c(%rbp),%edi
mov    0x2e19(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1050 <lseek@plt>
mov    -0x1c(%rbp),%edi
lea    0xe3f(%rip),%rsi
mov    $0x6,%edx
call   1040 <write@plt>
cmp    $0xffffffff,%eax
jne    11f1 <main+0x91>
lea    0xe2c(%rip),%rdi
call   1030 <exitErr@plt>
mov    -0x1c(%rbp),%edi
call   1060 <close@plt>
cmp    $0xffffffff,%eax
jne    120e <main+0xae>
lea    0xe15(%rip),%rdi
call   1030 <exitErr@plt>
mov    0x2dbb(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbx
mov    0x2e80(%rip),%rax
mov    0x2e51(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe86(%rip),%rdi
call   1070 <open@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
jne    1192 <main+0x32>
lea    0xe7f(%rip),%rdi
call   1030 <exitErr@plt>
mov    0x2e3f(%rip),%rax
mov    (%rax),%edx
mov    %ebx,%edi
xor    %esi,%esi
call   1050 <lseek@plt>
lea    0xe66(%rip),%rsi
mov    %ebx,%edi
mov    $0x6,%edx
call   1040 <write@plt>
cmp    $0xffffffff,%eax
jne    11c8 <main+0x68>
lea    0xe55(%rip),%rdi
call   1030 <exitErr@plt>
mov    %ebx,%edi
call   1060 <close@plt>
cmp    $0xffffffff,%eax
jne    11e0 <main+0x80>
lea    0xe43(%rip),%rdi
call   1030 <exitErr@plt>
mov    0x2de9(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [12 x i8] c"example.txt\00"
@O_RDWR = global i32 0
@O_APPEND = global i32 0
@.str.1 = [5 x i8] c"open\00"
@SEEK_SET = global i32 0
@.str.2 = [7 x i8] c"whatup\00"
@.str.3 = [6 x i8] c"write\00"
@.str.4 = [6 x i8] c"close\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8** %6
%8 = load i8*, i8** %6
%9 = load i32, i32* @O_RDWR
%10 = load i32, i32* @O_APPEND
%11 = or i32 %9, %10
%12 = call i32 @open(i8* %8, i32 %11)
store i32 %12, i32* %7
%13 = load i32, i32* %7
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %17
15: ; preds = %2
%16 = call i32 @exitErr(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %17
17: ; preds = %15, %2
%18 = load i32, i32* %7
%19 = load i32, i32* @SEEK_SET
%20 = call i32 @lseek(i32 %18, i32 0, i32 %19)
%21 = load i32, i32* %7
%22 = call i32 @write(i32 %21, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i32 6)
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %26
24: ; preds = %17
%25 = call i32 @exitErr(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
br label %26
26: ; preds = %24, %17
%27 = load i32, i32* %7
%28 = call i32 @close(i32 %27)
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %32
30: ; preds = %26
%31 = call i32 @exitErr(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0))
br label %32
32: ; preds = %30, %26
%33 = load i32, i32* @EXIT_SUCCESS
%34 = call i32 @exit(i32 %33)
unreachable
}

LLVM IR (-O2):
@.str = [12 x i8] c"example.txt\00"
@O_RDWR = global i32 0
@O_APPEND = global i32 0
@.str.1 = [5 x i8] c"open\00"
@SEEK_SET = global i32 0
@.str.2 = [7 x i8] c"whatup\00"
@.str.3 = [6 x i8] c"write\00"
@.str.4 = [6 x i8] c"close\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = load i32, i32* @O_RDWR
%4 = load i32, i32* @O_APPEND
%5 = or i32 %4, %3
%6 = tail call i32 @open(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %5)
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %10
8: ; preds = %2
%9 = tail call i32 @exitErr(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %10
10: ; preds = %8, %2
%11 = load i32, i32* @SEEK_SET
%12 = tail call i32 @lseek(i32 %6, i32 0, i32 %11)
%13 = tail call i32 @write(i32 %6, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i32 6)
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %17
15: ; preds = %10
%16 = tail call i32 @exitErr(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
br label %17
17: ; preds = %15, %10
%18 = tail call i32 @close(i32 %6)
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %22
20: ; preds = %17
%21 = tail call i32 @exitErr(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0))
br label %22
22: ; preds = %20, %17
%23 = load i32, i32* @EXIT_SUCCESS
%24 = tail call i32 @exit(i32 %23)
unreachable
}