#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_RDONLY;
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ O_WRONLY;
int /*<<< orphan*/ close(int);
int open(char *, int, ...);
int /*<<< orphan*/ read(int, char *, int);
int /*<<< orphan*/ write(int, char *, int);
int main() {
  int fd1 = open("fff2", O_RDONLY | O_CREAT, 0777);
  int fd2 = open("fff2", O_WRONLY);
  int fd3 = open("fff2", O_RDWR);
  write(fd3, "hello", 5);
  write(fd2, "worlds", 6);
  char buffer[20];
  read(fd1, buffer, 6);
  write(1, buffer, 6);
  close(fd2);
  write(fd3, "oops", 4);
  read(fd1, buffer, 6);
  write(1, buffer, 6);
  close(fd1);
  close(fd3);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e82(%rip),%rax
mov    (%rax),%esi
mov    0x2e99(%rip),%rax
or     (%rax),%esi
lea    0xe98(%rip),%rdi
mov    $0x1ff,%edx
mov    $0x0,%al
call   1060 <open@plt>
mov    %eax,-0x8(%rbp)
mov    0x2e42(%rip),%rax
mov    (%rax),%esi
lea    0xe79(%rip),%rdi
mov    $0x0,%al
call   1060 <open@plt>
mov    %eax,-0xc(%rbp)
mov    0x2e48(%rip),%rax
mov    (%rax),%esi
lea    0xe5f(%rip),%rdi
mov    $0x0,%al
call   1060 <open@plt>
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%edi
lea    0xe50(%rip),%rsi
mov    $0x5,%edx
call   1030 <write@plt>
mov    -0xc(%rbp),%edi
lea    0xe42(%rip),%rsi
mov    $0x6,%edx
call   1030 <write@plt>
mov    -0x8(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0x6,%edx
call   1050 <read@plt>
lea    -0x30(%rbp),%rsi
mov    $0x1,%edi
mov    $0x6,%edx
call   1030 <write@plt>
mov    -0xc(%rbp),%edi
call   1040 <close@plt>
mov    -0x10(%rbp),%edi
lea    0xe09(%rip),%rsi
mov    $0x4,%edx
call   1030 <write@plt>
mov    -0x8(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0x6,%edx
call   1050 <read@plt>
lea    -0x30(%rbp),%rsi
mov    $0x1,%edi
mov    $0x6,%edx
call   1030 <write@plt>
mov    -0x8(%rbp),%edi
call   1040 <close@plt>
mov    -0x10(%rbp),%edi
call   1040 <close@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    0x2e87(%rip),%rax
mov    0x2ea0(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe9d(%rip),%rbp
mov    %rbp,%rdi
mov    $0x1ff,%edx
xor    %eax,%eax
call   1060 <open@plt>
mov    %eax,%r14d
mov    0x2e44(%rip),%rax
mov    (%rax),%esi
mov    %rbp,%rdi
xor    %eax,%eax
call   1060 <open@plt>
mov    %eax,%ebx
mov    0x2e4f(%rip),%rax
mov    (%rax),%esi
mov    %rbp,%rdi
xor    %eax,%eax
call   1060 <open@plt>
mov    %eax,%ebp
lea    0xe5f(%rip),%rsi
mov    %eax,%edi
mov    $0x5,%edx
call   1030 <write@plt>
lea    0xe52(%rip),%rsi
mov    %ebx,%edi
mov    $0x6,%edx
call   1030 <write@plt>
mov    %rsp,%r15
mov    %r14d,%edi
mov    %r15,%rsi
mov    $0x6,%edx
call   1050 <read@plt>
mov    $0x1,%edi
mov    %r15,%rsi
mov    $0x6,%edx
call   1030 <write@plt>
mov    %ebx,%edi
call   1040 <close@plt>
lea    0xe1a(%rip),%rsi
mov    %ebp,%edi
mov    $0x4,%edx
call   1030 <write@plt>
mov    %r14d,%edi
mov    %r15,%rsi
mov    $0x6,%edx
call   1050 <read@plt>
mov    $0x1,%edi
mov    %r15,%rsi
mov    $0x6,%edx
call   1030 <write@plt>
mov    %r14d,%edi
call   1040 <close@plt>
mov    %ebp,%edi
call   1040 <close@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [5 x i8] c"fff2\00"
@O_RDONLY = global i32 0
@O_CREAT = global i32 0
@O_WRONLY = global i32 0
@O_RDWR = global i32 0
@.str.1 = [6 x i8] c"hello\00"
@.str.2 = [7 x i8] c"worlds\00"
@.str.3 = [5 x i8] c"oops\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca [20 x i8]
store i32 0, i32* %1
%6 = load i32, i32* @O_RDONLY
%7 = load i32, i32* @O_CREAT
%8 = or i32 %6, %7
%9 = call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 %8, i32 511)
store i32 %9, i32* %2
%10 = load i32, i32* @O_WRONLY
%11 = call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 %10)
store i32 %11, i32* %3
%12 = load i32, i32* @O_RDWR
%13 = call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 %12)
store i32 %13, i32* %4
%14 = load i32, i32* %4
%15 = call i32 @write(i32 %14, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i32 5)
%16 = load i32, i32* %3
%17 = call i32 @write(i32 %16, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i32 6)
%18 = load i32, i32* %2
%19 = getelementptr inbounds [20 x i8], [20 x i8]* %5, i64 0, i64 0
%20 = call i32 @read(i32 %18, i8* %19, i32 6)
%21 = getelementptr inbounds [20 x i8], [20 x i8]* %5, i64 0, i64 0
%22 = call i32 @write(i32 1, i8* %21, i32 6)
%23 = load i32, i32* %3
%24 = call i32 @close(i32 %23)
%25 = load i32, i32* %4
%26 = call i32 @write(i32 %25, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i32 4)
%27 = load i32, i32* %2
%28 = getelementptr inbounds [20 x i8], [20 x i8]* %5, i64 0, i64 0
%29 = call i32 @read(i32 %27, i8* %28, i32 6)
%30 = getelementptr inbounds [20 x i8], [20 x i8]* %5, i64 0, i64 0
%31 = call i32 @write(i32 1, i8* %30, i32 6)
%32 = load i32, i32* %2
%33 = call i32 @close(i32 %32)
%34 = load i32, i32* %4
%35 = call i32 @close(i32 %34)
ret i32 0
}

LLVM IR (-O2):
@.str = [5 x i8] c"fff2\00"
@O_RDONLY = global i32 0
@O_CREAT = global i32 0
@O_WRONLY = global i32 0
@O_RDWR = global i32 0
@.str.1 = [6 x i8] c"hello\00"
@.str.2 = [7 x i8] c"worlds\00"
@.str.3 = [5 x i8] c"oops\00"
define i32 @main() {
%1 = alloca [20 x i8]
%2 = load i32, i32* @O_RDONLY
%3 = load i32, i32* @O_CREAT
%4 = or i32 %3, %2
%5 = tail call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 %4, i32 511)
%6 = load i32, i32* @O_WRONLY
%7 = tail call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 %6)
%8 = load i32, i32* @O_RDWR
%9 = tail call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 %8)
%10 = tail call i32 @write(i32 %9, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i32 5)
%11 = tail call i32 @write(i32 %7, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i32 6)
%12 = getelementptr inbounds [20 x i8], [20 x i8]* %1, i64 0, i64 0
%13 = call i32 @read(i32 %5, i8* %12, i32 6)
%14 = call i32 @write(i32 1, i8* %12, i32 6)
%15 = tail call i32 @close(i32 %7)
%16 = tail call i32 @write(i32 %9, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i32 4)
%17 = call i32 @read(i32 %5, i8* %12, i32 6)
%18 = call i32 @write(i32 1, i8* %12, i32 6)
%19 = tail call i32 @close(i32 %5)
%20 = tail call i32 @close(i32 %9)
ret i32 0
}