#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_CREAT;
int O_RDWR;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ lseek(int, int /*<<< orphan*/, int /*<<< orphan*/);
int open(char *, int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
int read(int, char *, int);
int /*<<< orphan*/ write(int, char *, int);
int main(int argc, char *argv[]) {
  int fd, aux;
  char buffer[16];
  if ((fd = open("/tmp/file.txt", O_CREAT | O_APPEND | O_RDWR, 0777)) < 0) {
    perror("open");
  }
  write(fd, "Hello", 5);
  lseek(fd, 0, SEEK_SET);
  write(fd, " World", 6);
  aux = read(fd, buffer, 11);
  buffer[aux] = '\0';
  printf("String: %s\n", buffer);
  lseek(fd, 0, SEEK_SET);
  aux = read(fd, buffer, 11);
  buffer[aux] = '\0';
  printf("String: %s\n", buffer);
  close(fd);
  exit(EXIT_SUCCESS);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e5b(%rip),%rax
mov    (%rax),%esi
mov    0x2e12(%rip),%rax
or     (%rax),%esi
mov    0x2e31(%rip),%rax
or     (%rax),%esi
lea    0xe48(%rip),%rdi
mov    $0x1ff,%edx
call   1080 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    11da <main+0x5a>
lea    0xe39(%rip),%rdi
call   1090 <perror@plt>
mov    -0x14(%rbp),%edi
lea    0xe2f(%rip),%rsi
mov    $0x5,%edx
call   1030 <write@plt>
mov    -0x14(%rbp),%edi
mov    0x2dd8(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1050 <lseek@plt>
mov    -0x14(%rbp),%edi
lea    0xe0e(%rip),%rsi
mov    $0x6,%edx
call   1030 <write@plt>
mov    -0x14(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0xb,%edx
call   1070 <read@plt>
mov    %eax,-0x18(%rbp)
movslq -0x18(%rbp),%rax
movb   $0x0,-0x30(%rbp,%rax,1)
lea    -0x30(%rbp),%rsi
lea    0xde3(%rip),%rdi
call   1040 <printf@plt>
mov    -0x14(%rbp),%edi
mov    0x2d84(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1050 <lseek@plt>
mov    -0x14(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0xb,%edx
call   1070 <read@plt>
mov    %eax,-0x18(%rbp)
movslq -0x18(%rbp),%rax
movb   $0x0,-0x30(%rbp,%rax,1)
lea    -0x30(%rbp),%rsi
lea    0xda3(%rip),%rdi
call   1040 <printf@plt>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
mov    0x2d37(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    0x2e66(%rip),%rax
mov    0x2e1f(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e3c(%rip),%rax
or     (%rax),%esi
lea    0xe53(%rip),%rdi
mov    $0x1ff,%edx
call   1080 <open@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    11c9 <main+0x49>
lea    0xe4a(%rip),%rdi
call   1090 <perror@plt>
lea    0xe43(%rip),%rsi
mov    %ebx,%edi
mov    $0x5,%edx
call   1030 <write@plt>
mov    0x2ded(%rip),%r12
mov    (%r12),%edx
mov    %ebx,%edi
xor    %esi,%esi
call   1050 <lseek@plt>
lea    0xe22(%rip),%rsi
mov    %ebx,%edi
mov    $0x6,%edx
call   1030 <write@plt>
mov    %rsp,%r14
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0xb,%edx
call   1070 <read@plt>
cltq
movb   $0x0,(%rsp,%rax,1)
lea    0xdfe(%rip),%r15
mov    %r15,%rdi
mov    %r14,%rsi
call   1040 <printf@plt>
mov    (%r12),%edx
mov    %ebx,%edi
xor    %esi,%esi
call   1050 <lseek@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0xb,%edx
call   1070 <read@plt>
cltq
movb   $0x0,(%rsp,%rax,1)
mov    %r15,%rdi
mov    %r14,%rsi
call   1040 <printf@plt>
mov    %ebx,%edi
call   1060 <close@plt>
mov    0x2d60(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@.str = [14 x i8] c"/tmp/file.txt\00"
@O_CREAT = global i32 0
@O_APPEND = global i32 0
@O_RDWR = global i32 0
@.str.1 = [5 x i8] c"open\00"
@.str.2 = [6 x i8] c"Hello\00"
@SEEK_SET = global i32 0
@.str.3 = [7 x i8] c" World\00"
@.str.4 = [12 x i8] c"String: %s\0A\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
%8 = alloca [16 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = load i32, i32* @O_CREAT
%10 = load i32, i32* @O_APPEND
%11 = or i32 %9, %10
%12 = load i32, i32* @O_RDWR
%13 = or i32 %11, %12
%14 = call i32 @open(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %13, i32 511)
store i32 %14, i32* %6
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %2
%17 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %18
18: ; preds = %16, %2
%19 = load i32, i32* %6
%20 = call i32 @write(i32 %19, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i32 5)
%21 = load i32, i32* %6
%22 = load i32, i32* @SEEK_SET
%23 = call i32 @lseek(i32 %21, i32 0, i32 %22)
%24 = load i32, i32* %6
%25 = call i32 @write(i32 %24, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0), i32 6)
%26 = load i32, i32* %6
%27 = getelementptr inbounds [16 x i8], [16 x i8]* %8, i64 0, i64 0
%28 = call i32 @read(i32 %26, i8* %27, i32 11)
store i32 %28, i32* %7
%29 = load i32, i32* %7
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds [16 x i8], [16 x i8]* %8, i64 0, i64 %30
store i8 0, i8* %31
%32 = getelementptr inbounds [16 x i8], [16 x i8]* %8, i64 0, i64 0
%33 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0), i8* %32)
%34 = load i32, i32* %6
%35 = load i32, i32* @SEEK_SET
%36 = call i32 @lseek(i32 %34, i32 0, i32 %35)
%37 = load i32, i32* %6
%38 = getelementptr inbounds [16 x i8], [16 x i8]* %8, i64 0, i64 0
%39 = call i32 @read(i32 %37, i8* %38, i32 11)
store i32 %39, i32* %7
%40 = load i32, i32* %7
%41 = sext i32 %40 to i64
%42 = getelementptr inbounds [16 x i8], [16 x i8]* %8, i64 0, i64 %41
store i8 0, i8* %42
%43 = getelementptr inbounds [16 x i8], [16 x i8]* %8, i64 0, i64 0
%44 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0), i8* %43)
%45 = load i32, i32* %6
%46 = call i32 @close(i32 %45)
%47 = load i32, i32* @EXIT_SUCCESS
%48 = call i32 @exit(i32 %47)
unreachable
}

LLVM IR (-O2):
@.str = [14 x i8] c"/tmp/file.txt\00"
@O_CREAT = global i32 0
@O_APPEND = global i32 0
@O_RDWR = global i32 0
@.str.1 = [5 x i8] c"open\00"
@.str.2 = [6 x i8] c"Hello\00"
@SEEK_SET = global i32 0
@.str.3 = [7 x i8] c" World\00"
@.str.4 = [12 x i8] c"String: %s\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [16 x i8]
%4 = getelementptr inbounds [16 x i8], [16 x i8]* %3, i64 0, i64 0
%5 = load i32, i32* @O_CREAT
%6 = load i32, i32* @O_APPEND
%7 = or i32 %6, %5
%8 = load i32, i32* @O_RDWR
%9 = or i32 %7, %8
%10 = tail call i32 @open(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %9, i32 511)
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %2
%13 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %14
14: ; preds = %12, %2
%15 = tail call i32 @write(i32 %10, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i32 5)
%16 = load i32, i32* @SEEK_SET
%17 = tail call i32 @lseek(i32 %10, i32 0, i32 %16)
%18 = tail call i32 @write(i32 %10, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0), i32 6)
%19 = call i32 @read(i32 %10, i8* %4, i32 11)
%20 = sext i32 %19 to i64
%21 = getelementptr inbounds [16 x i8], [16 x i8]* %3, i64 0, i64 %20
store i8 0, i8* %21
%22 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0), i8* %4)
%23 = load i32, i32* @SEEK_SET
%24 = call i32 @lseek(i32 %10, i32 0, i32 %23)
%25 = call i32 @read(i32 %10, i8* %4, i32 11)
%26 = sext i32 %25 to i64
%27 = getelementptr inbounds [16 x i8], [16 x i8]* %3, i64 0, i64 %26
store i8 0, i8* %27
%28 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0), i8* %4)
%29 = call i32 @close(i32 %10)
%30 = load i32, i32* @EXIT_SUCCESS
%31 = call i32 @exit(i32 %30)
unreachable
}