#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 O_WRONLY;
int /*<<< orphan*/ close(int);
int dup(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ jobs(int, int);
int open(char *, int);
int /*<<< orphan*/ printf(char *);
int read(int, char *, int);
int /*<<< orphan*/ write(int, char *, int);
int main() {
  char filepath[] = "file.test2";
  int in1, in2;
  in1 = open(filepath, O_WRONLY | O_CREAT);
  in2 = dup(in1);
  jobs(in1, in2);
  close(in1);
  close(in2);
  char buffer[20];
  int in3 = open(filepath, O_RDONLY);
  int nread = read(in3, buffer, 10);
  write(in3, buffer, nread);
  close(in3);
  printf("done");
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    0xe6a(%rip),%rax
mov    %rax,-0xf(%rbp)
mov    0xe67(%rip),%ax
mov    %ax,-0x7(%rbp)
mov    0xe5f(%rip),%al
mov    %al,-0x5(%rbp)
lea    -0xf(%rbp),%rdi
mov    0x2e0f(%rip),%rax
mov    (%rax),%esi
mov    0x2e36(%rip),%rax
or     (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%edi
call   1050 <dup@plt>
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%edi
mov    -0x18(%rbp),%esi
call   1090 <jobs@plt>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
mov    -0x18(%rbp),%edi
call   1060 <close@plt>
lea    -0xf(%rbp),%rdi
mov    0x2de3(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x34(%rbp)
mov    -0x34(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0xa,%edx
call   1070 <read@plt>
mov    %eax,-0x38(%rbp)
mov    -0x34(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    -0x38(%rbp),%edx
call   1030 <write@plt>
mov    -0x34(%rbp),%edi
call   1060 <close@plt>
lea    0xdd2(%rip),%rdi
call   1040 <printf@plt>
xor    %edi,%edi
call   10a0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x30,%rsp
movabs $0x7365742e656c6966,%rax
74 65 73
mov    %rax,(%rsp)
movl   $0x327473,0x7(%rsp)
mov    0x2e23(%rip),%rax
mov    0x2e4c(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %rsp,%r14
mov    %r14,%rdi
call   1080 <open@plt>
mov    %eax,%ebp
mov    %eax,%edi
call   1050 <dup@plt>
mov    %eax,%ebx
mov    %ebp,%edi
mov    %eax,%esi
call   1090 <jobs@plt>
mov    %ebp,%edi
call   1060 <close@plt>
mov    %ebx,%edi
call   1060 <close@plt>
mov    0x2dfc(%rip),%rax
mov    (%rax),%esi
mov    %r14,%rdi
call   1080 <open@plt>
mov    %eax,%ebx
lea    0x10(%rsp),%rbp
mov    %eax,%edi
mov    %rbp,%rsi
mov    $0xa,%edx
call   1070 <read@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    %ebx,%edi
call   1060 <close@plt>
lea    0xded(%rip),%rdi
call   1040 <printf@plt>
xor    %edi,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@__const.main.filepath = [11 x i8] c"file.test2\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_RDONLY = global i32 0
@.str = [5 x i8] c"done\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [11 x i8]
%3 = alloca i32
%4 = alloca i32
%5 = alloca [20 x i8]
%6 = alloca i32
%7 = alloca i32
store i32 0, i32* %1
%8 = bitcast [11 x i8]* %2 to i8*
%9 = getelementptr inbounds [11 x i8], [11 x i8]* %2, i64 0, i64 0
%10 = load i32, i32* @O_WRONLY
%11 = load i32, i32* @O_CREAT
%12 = or i32 %10, %11
%13 = call i32 @open(i8* %9, i32 %12)
store i32 %13, i32* %3
%14 = load i32, i32* %3
%15 = call i32 @dup(i32 %14)
store i32 %15, i32* %4
%16 = load i32, i32* %3
%17 = load i32, i32* %4
%18 = call i32 @jobs(i32 %16, i32 %17)
%19 = load i32, i32* %3
%20 = call i32 @close(i32 %19)
%21 = load i32, i32* %4
%22 = call i32 @close(i32 %21)
%23 = getelementptr inbounds [11 x i8], [11 x i8]* %2, i64 0, i64 0
%24 = load i32, i32* @O_RDONLY
%25 = call i32 @open(i8* %23, i32 %24)
store i32 %25, i32* %6
%26 = load i32, i32* %6
%27 = getelementptr inbounds [20 x i8], [20 x i8]* %5, i64 0, i64 0
%28 = call i32 @read(i32 %26, i8* %27, i32 10)
store i32 %28, i32* %7
%29 = load i32, i32* %6
%30 = getelementptr inbounds [20 x i8], [20 x i8]* %5, i64 0, i64 0
%31 = load i32, i32* %7
%32 = call i32 @write(i32 %29, i8* %30, i32 %31)
%33 = load i32, i32* %6
%34 = call i32 @close(i32 %33)
%35 = call i32 @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%36 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
@__const.main.filepath = [11 x i8] c"file.test2\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_RDONLY = global i32 0
@.str = [5 x i8] c"done\00"
define i32 @main() {
%1 = alloca [11 x i8]
%2 = alloca [20 x i8]
%3 = getelementptr inbounds [11 x i8], [11 x i8]* %1, i64 0, i64 0
%4 = load i32, i32* @O_WRONLY
%5 = load i32, i32* @O_CREAT
%6 = or i32 %5, %4
%7 = call i32 @open(i8* %3, i32 %6)
%8 = tail call i32 @dup(i32 %7)
%9 = tail call i32 @jobs(i32 %7, i32 %8)
%10 = tail call i32 @close(i32 %7)
%11 = tail call i32 @close(i32 %8)
%12 = getelementptr inbounds [20 x i8], [20 x i8]* %2, i64 0, i64 0
%13 = load i32, i32* @O_RDONLY
%14 = call i32 @open(i8* %3, i32 %13)
%15 = call i32 @read(i32 %14, i8* %12, i32 10)
%16 = call i32 @write(i32 %14, i8* %12, i32 %15)
%17 = tail call i32 @close(i32 %14)
%18 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%19 = tail call i32 @exit(i32 0)
unreachable
}