#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 /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ S_IRWXU;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ lseek(int, int /*<<< orphan*/, int /*<<< orphan*/);
int open(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ read(int, char *, int);
int /*<<< orphan*/ write(int, char *, int);
int main() {
  int fd;
  char buf[100];
  fd = open("/tmp/test.txt", O_CREAT | O_RDWR, S_IRWXU);
  write(fd, "Hello\n", 6);
  lseek(fd, 0, SEEK_SET);
  read(fd, buf, 6);
  buf[6] = 0;
  printf("File containes %s\n", buf);
  close(fd);
  exit(1);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e72(%rip),%rax
mov    (%rax),%esi
mov    0x2e49(%rip),%rax
or     (%rax),%esi
mov    0x2e48(%rip),%rax
mov    (%rax),%edx
lea    0xe5f(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%edi
lea    0xe5b(%rip),%rsi
mov    $0x6,%edx
call   1030 <write@plt>
mov    -0x8(%rbp),%edi
mov    0x2e01(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1050 <lseek@plt>
mov    -0x8(%rbp),%edi
lea    -0x70(%rbp),%rsi
mov    $0x6,%edx
call   1070 <read@plt>
movb   $0x0,-0x6a(%rbp)
lea    -0x70(%rbp),%rsi
lea    0xe25(%rip),%rdi
call   1040 <printf@plt>
mov    -0x8(%rbp),%edi
call   1060 <close@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x68,%rsp
mov    0x2e7a(%rip),%rax
mov    0x2e53(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e50(%rip),%rax
mov    (%rax),%edx
lea    0xe67(%rip),%rdi
call   1080 <open@plt>
mov    %eax,%ebx
lea    0xe67(%rip),%rsi
mov    %eax,%edi
mov    $0x6,%edx
call   1030 <write@plt>
mov    0x2e0e(%rip),%rax
mov    (%rax),%edx
mov    %ebx,%edi
xor    %esi,%esi
call   1050 <lseek@plt>
mov    %rsp,%r14
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x6,%edx
call   1070 <read@plt>
movb   $0x0,0x6(%rsp)
lea    0xe32(%rip),%rdi
mov    %r14,%rsi
call   1040 <printf@plt>
mov    %ebx,%edi
call   1060 <close@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [14 x i8] c"/tmp/test.txt\00"
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@S_IRWXU = global i32 0
@.str.1 = [7 x i8] c"Hello\0A\00"
@SEEK_SET = global i32 0
@.str.2 = [19 x i8] c"File containes %s\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca [100 x i8]
store i32 0, i32* %1
%4 = load i32, i32* @O_CREAT
%5 = load i32, i32* @O_RDWR
%6 = or i32 %4, %5
%7 = load i32, i32* @S_IRWXU
%8 = call i32 @open(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %6, i32 %7)
store i32 %8, i32* %2
%9 = load i32, i32* %2
%10 = call i32 @write(i32 %9, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i32 6)
%11 = load i32, i32* %2
%12 = load i32, i32* @SEEK_SET
%13 = call i32 @lseek(i32 %11, i32 0, i32 %12)
%14 = load i32, i32* %2
%15 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 0
%16 = call i32 @read(i32 %14, i8* %15, i32 6)
%17 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 6
store i8 0, i8* %17
%18 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 0
%19 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i8* %18)
%20 = load i32, i32* %2
%21 = call i32 @close(i32 %20)
%22 = call i32 @exit(i32 1)
unreachable
}

LLVM IR (-O2):
@.str = [14 x i8] c"/tmp/test.txt\00"
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@S_IRWXU = global i32 0
@.str.1 = [7 x i8] c"Hello\0A\00"
@SEEK_SET = global i32 0
@.str.2 = [19 x i8] c"File containes %s\0A\00"
define i32 @main() {
%1 = alloca [100 x i8]
%2 = getelementptr inbounds [100 x i8], [100 x i8]* %1, i64 0, i64 0
%3 = load i32, i32* @O_CREAT
%4 = load i32, i32* @O_RDWR
%5 = or i32 %4, %3
%6 = load i32, i32* @S_IRWXU
%7 = tail call i32 @open(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %5, i32 %6)
%8 = tail call i32 @write(i32 %7, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i32 6)
%9 = load i32, i32* @SEEK_SET
%10 = tail call i32 @lseek(i32 %7, i32 0, i32 %9)
%11 = call i32 @read(i32 %7, i8* %2, i32 6)
%12 = getelementptr inbounds [100 x i8], [100 x i8]* %1, i64 0, i64 6
store i8 0, i8* %12
%13 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i8* %2)
%14 = call i32 @close(i32 %7)
%15 = call i32 @exit(i32 1)
unreachable
}