#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 */
typedef int off_t;

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ EXIT_SUCCESS;
int O_APPEND;
int O_CREAT;
int O_TRUNC;
int O_WRONLY;
int /*<<< orphan*/ SEEK_SET;
int S_IRUSR;
int S_IWUSR;
int close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int lseek(int, int /*<<< orphan*/, int /*<<< orphan*/);
int open(char *, int, int);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int write(int, char *, int);
int main(int argc, char *argv[]) {
  int fd;
  off_t off;
  fd = open("foo", O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
  if (fd == -1) {
    fprintf(stderr, "error on open\n");
    exit(EXIT_FAILURE);
  }
  if (write(fd, "hello_world", 11) == -1) {
    fprintf(stderr, "error on write\n");
    exit(EXIT_FAILURE);
  }
  if (close(fd) == -1) {
    fprintf(stderr, "error on close\n");
    exit(EXIT_FAILURE);
  }
  fd = open("foo", O_APPEND | O_WRONLY, S_IRUSR | S_IWUSR);
  if (fd == -1) {
    fprintf(stderr, "error on open\n");
    exit(EXIT_FAILURE);
  }
  if ((off = lseek(fd, 0, SEEK_SET)) == -1) {
    fprintf(stderr, "error on lseek %s\n", strerror(errno));
    exit(EXIT_FAILURE);
  }
  if (write(fd, "foo bar goo", 11) == -1) {
    fprintf(stderr, "error on write\n");
    exit(EXIT_FAILURE);
  }
  if (close(fd) == -1) {
    fprintf(stderr, "error on close\n");
    exit(EXIT_FAILURE);
  }
  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)
mov    0x2e03(%rip),%rax
mov    (%rax),%esi
mov    0x2e62(%rip),%rax
or     (%rax),%esi
mov    0x2e51(%rip),%rax
or     (%rax),%esi
mov    0x2e00(%rip),%rax
mov    (%rax),%edx
mov    0x2e27(%rip),%rax
or     (%rax),%edx
lea    0xe46(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11f1 <main+0x81>
mov    0x2df5(%rip),%rax
mov    (%rax),%edi
lea    0xe28(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    0x2dc6(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    -0x14(%rbp),%edi
lea    0xe18(%rip),%rsi
mov    $0xb,%edx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
jne    1233 <main+0xc3>
mov    0x2db3(%rip),%rax
mov    (%rax),%edi
lea    0xe01(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    0x2d84(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
cmp    $0xffffffff,%eax
jne    1269 <main+0xf9>
mov    0x2d7d(%rip),%rax
mov    (%rax),%edi
lea    0xddb(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    0x2d4e(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    0x2d18(%rip),%rax
mov    (%rax),%esi
mov    0x2d17(%rip),%rax
or     (%rax),%esi
mov    0x2d26(%rip),%rax
mov    (%rax),%edx
mov    0x2d4d(%rip),%rax
or     (%rax),%edx
lea    0xd6c(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    12cb <main+0x15b>
mov    0x2d1b(%rip),%rax
mov    (%rax),%edi
lea    0xd4e(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    0x2cec(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    -0x14(%rbp),%edi
mov    0x2ce3(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1040 <lseek@plt>
mov    %eax,-0x18(%rbp)
cmp    $0xffffffff,%eax
jne    1326 <main+0x1b6>
mov    0x2cd7(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    0x2cc3(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x1c(%rbp),%edi
mov    %rax,%rdx
lea    0xd2e(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    0x2c91(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    -0x14(%rbp),%edi
lea    0xd22(%rip),%rsi
mov    $0xb,%edx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
jne    1368 <main+0x1f8>
mov    0x2c7e(%rip),%rax
mov    (%rax),%edi
lea    0xccc(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    0x2c4f(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
cmp    $0xffffffff,%eax
jne    139e <main+0x22e>
mov    0x2c48(%rip),%rax
mov    (%rax),%edi
lea    0xca6(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    0x2c19(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    0x2bfb(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    0x2e11(%rip),%r14
mov    0x2e72(%rip),%rax
mov    (%rax),%esi
or     (%r14),%esi
mov    0x2e5e(%rip),%rax
or     (%rax),%esi
mov    0x2e0d(%rip),%r15
mov    0x2e36(%rip),%r12
mov    (%r12),%edx
or     (%r15),%edx
lea    0xe50(%rip),%rdi
call   1070 <open@plt>
cmp    $0xffffffff,%eax
jne    11cc <main+0x5c>
mov    0x2e07(%rip),%rax
mov    (%rax),%edi
lea    0xe3a(%rip),%rsi
jmp    1214 <main+0xa4>
mov    %eax,%ebx
lea    0xe3e(%rip),%rsi
mov    %eax,%edi
mov    $0xb,%edx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
jne    11f8 <main+0x88>
mov    0x2ddb(%rip),%rax
mov    (%rax),%edi
lea    0xe29(%rip),%rsi
jmp    1214 <main+0xa4>
mov    %ebx,%edi
call   1050 <close@plt>
cmp    $0xffffffff,%eax
jne    1229 <main+0xb9>
mov    0x2dbd(%rip),%rax
mov    (%rax),%edi
lea    0xe1b(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    0x2d8e(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    0x2d58(%rip),%rax
mov    (%r14),%esi
or     (%rax),%esi
mov    (%r12),%edx
or     (%r15),%edx
lea    0xdbd(%rip),%rdi
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     11ba <main+0x4a>
mov    %eax,%ebx
mov    0x2d5e(%rip),%rax
mov    (%rax),%edx
mov    %ebx,%edi
xor    %esi,%esi
call   1040 <lseek@plt>
cmp    $0xffffffff,%eax
jne    1296 <main+0x126>
mov    0x2d57(%rip),%rax
mov    (%rax),%ebx
mov    0x2d46(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xdb7(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   1060 <fprintf@plt>
jmp    121b <main+0xab>
lea    0xdb5(%rip),%rsi
mov    %ebx,%edi
mov    $0xb,%edx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
je     11e6 <main+0x76>
mov    %ebx,%edi
call   1050 <close@plt>
cmp    $0xffffffff,%eax
je     1204 <main+0x94>
mov    0x2cd7(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [4 x i8] c"foo\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@stderr = global i32 0
@.str.1 = [15 x i8] c"error on open\0A\00"
@EXIT_FAILURE = global i32 0
@.str.2 = [12 x i8] c"hello_world\00"
@.str.3 = [16 x i8] c"error on write\0A\00"
@.str.4 = [16 x i8] c"error on close\0A\00"
@O_APPEND = global i32 0
@SEEK_SET = global i32 0
@.str.5 = [19 x i8] c"error on lseek %s\0A\00"
@errno = global i32 0
@.str.6 = [12 x i8] c"foo bar goo\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = load i32, i32* @O_WRONLY
%9 = load i32, i32* @O_CREAT
%10 = or i32 %8, %9
%11 = load i32, i32* @O_TRUNC
%12 = or i32 %10, %11
%13 = load i32, i32* @S_IRUSR
%14 = load i32, i32* @S_IWUSR
%15 = or i32 %13, %14
%16 = call i32 @open(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %12, i32 %15)
store i32 %16, i32* %6
%17 = load i32, i32* %6
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %24
19: ; preds = %2
%20 = load i32, i32* @stderr
%21 = call i32 (i32, i8*, ...) @fprintf(i32 %20, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
%22 = load i32, i32* @EXIT_FAILURE
%23 = call i32 @exit(i32 %22)
unreachable
24: ; preds = %2
%25 = load i32, i32* %6
%26 = call i32 @write(i32 %25, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i32 11)
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %33
28: ; preds = %24
%29 = load i32, i32* @stderr
%30 = call i32 (i32, i8*, ...) @fprintf(i32 %29, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0))
%31 = load i32, i32* @EXIT_FAILURE
%32 = call i32 @exit(i32 %31)
unreachable
33: ; preds = %24
%34 = load i32, i32* %6
%35 = call i32 @close(i32 %34)
%36 = icmp eq i32 %35, -1
br i1 %36, label %37, label %42
37: ; preds = %33
%38 = load i32, i32* @stderr
%39 = call i32 (i32, i8*, ...) @fprintf(i32 %38, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0))
%40 = load i32, i32* @EXIT_FAILURE
%41 = call i32 @exit(i32 %40)
unreachable
42: ; preds = %33
%43 = load i32, i32* @O_APPEND
%44 = load i32, i32* @O_WRONLY
%45 = or i32 %43, %44
%46 = load i32, i32* @S_IRUSR
%47 = load i32, i32* @S_IWUSR
%48 = or i32 %46, %47
%49 = call i32 @open(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %45, i32 %48)
store i32 %49, i32* %6
%50 = load i32, i32* %6
%51 = icmp eq i32 %50, -1
br i1 %51, label %52, label %57
52: ; preds = %42
%53 = load i32, i32* @stderr
%54 = call i32 (i32, i8*, ...) @fprintf(i32 %53, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
%55 = load i32, i32* @EXIT_FAILURE
%56 = call i32 @exit(i32 %55)
unreachable
57: ; preds = %42
%58 = load i32, i32* %6
%59 = load i32, i32* @SEEK_SET
%60 = call i32 @lseek(i32 %58, i32 0, i32 %59)
store i32 %60, i32* %7
%61 = icmp eq i32 %60, -1
br i1 %61, label %62, label %69
62: ; preds = %57
%63 = load i32, i32* @stderr
%64 = load i32, i32* @errno
%65 = call i8* @strerror(i32 %64)
%66 = call i32 (i32, i8*, ...) @fprintf(i32 %63, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.5, i64 0, i64 0), i8* %65)
%67 = load i32, i32* @EXIT_FAILURE
%68 = call i32 @exit(i32 %67)
unreachable
69: ; preds = %57
%70 = load i32, i32* %6
%71 = call i32 @write(i32 %70, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.6, i64 0, i64 0), i32 11)
%72 = icmp eq i32 %71, -1
br i1 %72, label %73, label %78
73: ; preds = %69
%74 = load i32, i32* @stderr
%75 = call i32 (i32, i8*, ...) @fprintf(i32 %74, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0))
%76 = load i32, i32* @EXIT_FAILURE
%77 = call i32 @exit(i32 %76)
unreachable
78: ; preds = %69
%79 = load i32, i32* %6
%80 = call i32 @close(i32 %79)
%81 = icmp eq i32 %80, -1
br i1 %81, label %82, label %87
82: ; preds = %78
%83 = load i32, i32* @stderr
%84 = call i32 (i32, i8*, ...) @fprintf(i32 %83, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0))
%85 = load i32, i32* @EXIT_FAILURE
%86 = call i32 @exit(i32 %85)
unreachable
87: ; preds = %78
%88 = load i32, i32* @EXIT_SUCCESS
%89 = call i32 @exit(i32 %88)
unreachable
}

LLVM IR (-O2):
@.str = [4 x i8] c"foo\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@stderr = global i32 0
@.str.1 = [15 x i8] c"error on open\0A\00"
@EXIT_FAILURE = global i32 0
@.str.2 = [12 x i8] c"hello_world\00"
@.str.3 = [16 x i8] c"error on write\0A\00"
@.str.4 = [16 x i8] c"error on close\0A\00"
@O_APPEND = global i32 0
@SEEK_SET = global i32 0
@.str.5 = [19 x i8] c"error on lseek %s\0A\00"
@errno = global i32 0
@.str.6 = [12 x i8] c"foo bar goo\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = load i32, i32* @O_WRONLY
%4 = load i32, i32* @O_CREAT
%5 = or i32 %4, %3
%6 = load i32, i32* @O_TRUNC
%7 = or i32 %5, %6
%8 = load i32, i32* @S_IRUSR
%9 = load i32, i32* @S_IWUSR
%10 = or i32 %9, %8
%11 = tail call i32 @open(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %7, i32 %10)
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %18
13: ; preds = %2
%14 = load i32, i32* @stderr
%15 = tail call i32 (i32, i8*, ...) @fprintf(i32 %14, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
%16 = load i32, i32* @EXIT_FAILURE
%17 = tail call i32 @exit(i32 %16)
unreachable
18: ; preds = %2
%19 = tail call i32 @write(i32 %11, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i32 11)
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %26
21: ; preds = %18
%22 = load i32, i32* @stderr
%23 = tail call i32 (i32, i8*, ...) @fprintf(i32 %22, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0))
%24 = load i32, i32* @EXIT_FAILURE
%25 = tail call i32 @exit(i32 %24)
unreachable
26: ; preds = %18
%27 = tail call i32 @close(i32 %11)
%28 = icmp eq i32 %27, -1
br i1 %28, label %29, label %34
29: ; preds = %26
%30 = load i32, i32* @stderr
%31 = tail call i32 (i32, i8*, ...) @fprintf(i32 %30, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0))
%32 = load i32, i32* @EXIT_FAILURE
%33 = tail call i32 @exit(i32 %32)
unreachable
34: ; preds = %26
%35 = load i32, i32* @O_APPEND
%36 = load i32, i32* @O_WRONLY
%37 = or i32 %36, %35
%38 = load i32, i32* @S_IRUSR
%39 = load i32, i32* @S_IWUSR
%40 = or i32 %39, %38
%41 = tail call i32 @open(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %37, i32 %40)
%42 = icmp eq i32 %41, -1
br i1 %42, label %43, label %48
43: ; preds = %34
%44 = load i32, i32* @stderr
%45 = tail call i32 (i32, i8*, ...) @fprintf(i32 %44, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
%46 = load i32, i32* @EXIT_FAILURE
%47 = tail call i32 @exit(i32 %46)
unreachable
48: ; preds = %34
%49 = load i32, i32* @SEEK_SET
%50 = tail call i32 @lseek(i32 %41, i32 0, i32 %49)
%51 = icmp eq i32 %50, -1
br i1 %51, label %52, label %59
52: ; preds = %48
%53 = load i32, i32* @stderr
%54 = load i32, i32* @errno
%55 = tail call i8* @strerror(i32 %54)
%56 = tail call i32 (i32, i8*, ...) @fprintf(i32 %53, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.5, i64 0, i64 0), i8* %55)
%57 = load i32, i32* @EXIT_FAILURE
%58 = tail call i32 @exit(i32 %57)
unreachable
59: ; preds = %48
%60 = tail call i32 @write(i32 %41, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.6, i64 0, i64 0), i32 11)
%61 = icmp eq i32 %60, -1
br i1 %61, label %62, label %67
62: ; preds = %59
%63 = load i32, i32* @stderr
%64 = tail call i32 (i32, i8*, ...) @fprintf(i32 %63, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0))
%65 = load i32, i32* @EXIT_FAILURE
%66 = tail call i32 @exit(i32 %65)
unreachable
67: ; preds = %59
%68 = tail call i32 @close(i32 %41)
%69 = icmp eq i32 %68, -1
br i1 %69, label %70, label %75
70: ; preds = %67
%71 = load i32, i32* @stderr
%72 = tail call i32 (i32, i8*, ...) @fprintf(i32 %71, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0))
%73 = load i32, i32* @EXIT_FAILURE
%74 = tail call i32 @exit(i32 %73)
unreachable
75: ; preds = %67
%76 = load i32, i32* @EXIT_SUCCESS
%77 = tail call i32 @exit(i32 %76)
unreachable
}