#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 /*<<< orphan*/ O_RDWR;
int O_WRONLY;
int /*<<< orphan*/ close(int);
int dup(int);
int /*<<< orphan*/ dup2(int, int);
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
int open(char *, int, ...);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ read(int, char *, int);
int /*<<< orphan*/ stdout;
int /*<<< orphan*/ write(int, char *, int);
int main() {
  int fd1 = open("myfile", O_WRONLY | O_CREAT, 0666);
  if (fd1 < 0) {
    perror("myfile");
    return 1;
  }
  int fd2 = dup(fd1);
  write(fd1, "a", 1);
  write(fd2, "b", 1);
  close(fd1);
  close(fd2);
  int fd3 = open("myfile", O_RDWR);
  if (fd3 < 0) {
    perror("myfile");
    return 1;
  }
  dup2(fd3, 1);
  char c;
  read(fd3, &c, 1);
  write(2, &c, 1);
  close(fd3);
  read(1, &c, 1);
  write(2, &c, 1);
  printf("hello my dear friends\n");
  fflush(stdout);
  close(1);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e1a(%rip),%rax
mov    (%rax),%esi
mov    0x2e49(%rip),%rax
or     (%rax),%esi
lea    0xe48(%rip),%rdi
mov    $0x1b6,%edx
mov    $0x0,%al
call   10a0 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    11e9 <main+0x59>
lea    0xe28(%rip),%rdi
call   10b0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    12e8 <main+0x158>
mov    -0x8(%rbp),%edi
call   1060 <dup@plt>
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%edi
lea    0xe09(%rip),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
mov    -0xc(%rbp),%edi
lea    0xdf7(%rip),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
mov    -0x8(%rbp),%edi
call   1070 <close@plt>
mov    -0xc(%rbp),%edi
call   1070 <close@plt>
mov    0x2dad(%rip),%rax
mov    (%rax),%esi
lea    0xdc4(%rip),%rdi
mov    $0x0,%al
call   10a0 <open@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jge    1268 <main+0xd8>
lea    0xda9(%rip),%rdi
call   10b0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    12e8 <main+0x158>
mov    -0x10(%rbp),%edi
mov    $0x1,%esi
call   1040 <dup2@plt>
mov    -0x10(%rbp),%edi
lea    -0x11(%rbp),%rsi
mov    $0x1,%edx
call   1080 <read@plt>
mov    $0x2,%edi
lea    -0x11(%rbp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
mov    -0x10(%rbp),%edi
call   1070 <close@plt>
mov    $0x1,%edx
lea    -0x11(%rbp),%rsi
mov    %edx,%edi
call   1080 <read@plt>
mov    $0x2,%edi
lea    -0x11(%rbp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
lea    0xd40(%rip),%rdi
call   1050 <printf@plt>
mov    0x2cf9(%rip),%rax
mov    (%rax),%edi
call   1090 <fflush@plt>
mov    $0x1,%edi
call   1070 <close@plt>
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    0x2e21(%rip),%rax
mov    0x2e52(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe4f(%rip),%rdi
mov    $0x1b6,%edx
xor    %eax,%eax
call   10a0 <open@plt>
test   %eax,%eax
js     12bb <main+0x12b>
mov    %eax,%ebp
mov    %eax,%edi
call   1060 <dup@plt>
mov    %eax,%ebx
lea    0xe30(%rip),%rsi
mov    $0x1,%r14d
mov    %ebp,%edi
mov    $0x1,%edx
call   1040 <write@plt>
lea    0xe19(%rip),%rsi
mov    %ebx,%edi
mov    $0x1,%edx
call   1040 <write@plt>
mov    %ebp,%edi
call   1070 <close@plt>
mov    %ebx,%edi
call   1070 <close@plt>
mov    0x2dcf(%rip),%rax
mov    (%rax),%esi
lea    0xde6(%rip),%rdi
xor    %eax,%eax
call   10a0 <open@plt>
test   %eax,%eax
js     12cf <main+0x13f>
mov    %eax,%ebp
mov    %eax,%edi
mov    $0x1,%esi
call   1050 <dup2@plt>
lea    0xf(%rsp),%rbx
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1080 <read@plt>
mov    $0x2,%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1040 <write@plt>
mov    %ebp,%edi
call   1070 <close@plt>
mov    $0x1,%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1080 <read@plt>
mov    $0x2,%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1040 <write@plt>
lea    0xd7c(%rip),%rdi
call   1030 <puts@plt>
mov    0x2d35(%rip),%rax
mov    (%rax),%edi
call   1090 <fflush@plt>
mov    $0x1,%edi
call   1070 <close@plt>
xor    %r14d,%r14d
mov    %r14d,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xd3e(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%r14d
jmp    12af <main+0x11f>
lea    0xd2a(%rip),%rdi
call   10b0 <perror@plt>
jmp    12af <main+0x11f>

LLVM IR (-O0):
@.str = [7 x i8] c"myfile\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@.str.1 = [2 x i8] c"a\00"
@.str.2 = [2 x i8] c"b\00"
@O_RDWR = global i32 0
@.str.3 = [23 x i8] c"hello my dear friends\0A\00"
@stdout = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8
store i32 0, i32* %1
%6 = load i32, i32* @O_WRONLY
%7 = load i32, i32* @O_CREAT
%8 = or i32 %6, %7
%9 = call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32 %8, i32 438)
store i32 %9, i32* %2
%10 = load i32, i32* %2
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %0
%13 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %1
br label %45
14: ; preds = %0
%15 = load i32, i32* %2
%16 = call i32 @dup(i32 %15)
store i32 %16, i32* %3
%17 = load i32, i32* %2
%18 = call i32 @write(i32 %17, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32 1)
%19 = load i32, i32* %3
%20 = call i32 @write(i32 %19, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i32 1)
%21 = load i32, i32* %2
%22 = call i32 @close(i32 %21)
%23 = load i32, i32* %3
%24 = call i32 @close(i32 %23)
%25 = load i32, i32* @O_RDWR
%26 = call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32 %25)
store i32 %26, i32* %4
%27 = load i32, i32* %4
%28 = icmp slt i32 %27, 0
br i1 %28, label %29, label %31
29: ; preds = %14
%30 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %1
br label %45
31: ; preds = %14
%32 = load i32, i32* %4
%33 = call i32 @dup2(i32 %32, i32 1)
%34 = load i32, i32* %4
%35 = call i32 @read(i32 %34, i8* %5, i32 1)
%36 = call i32 @write(i32 2, i8* %5, i32 1)
%37 = load i32, i32* %4
%38 = call i32 @close(i32 %37)
%39 = call i32 @read(i32 1, i8* %5, i32 1)
%40 = call i32 @write(i32 2, i8* %5, i32 1)
%41 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0))
%42 = load i32, i32* @stdout
%43 = call i32 @fflush(i32 %42)
%44 = call i32 @close(i32 1)
br label %45
45: ; preds = %31, %29, %12
%46 = load i32, i32* %1
ret i32 %46
}

LLVM IR (-O2):
@.str = [7 x i8] c"myfile\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@.str.1 = [2 x i8] c"a\00"
@.str.2 = [2 x i8] c"b\00"
@O_RDWR = global i32 0
@stdout = global i32 0
@str = [22 x i8] c"hello my dear friends\00"
define i32 @main() {
%1 = alloca i8
%2 = load i32, i32* @O_WRONLY
%3 = load i32, i32* @O_CREAT
%4 = or i32 %3, %2
%5 = tail call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32 %4, i32 438)
%6 = icmp slt i32 %5, 0
br i1 %6, label %7, label %9
7: ; preds = %0
%8 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %31
9: ; preds = %0
%10 = tail call i32 @dup(i32 %5)
%11 = tail call i32 @write(i32 %5, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32 1)
%12 = tail call i32 @write(i32 %10, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i32 1)
%13 = tail call i32 @close(i32 %5)
%14 = tail call i32 @close(i32 %10)
%15 = load i32, i32* @O_RDWR
%16 = tail call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32 %15)
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %9
%19 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %31
20: ; preds = %9
%21 = tail call i32 @dup2(i32 %16, i32 1)
%22 = call i32 @read(i32 %16, i8* %1, i32 1)
%23 = call i32 @write(i32 2, i8* %1, i32 1)
%24 = tail call i32 @close(i32 %16)
%25 = call i32 @read(i32 1, i8* %1, i32 1)
%26 = call i32 @write(i32 2, i8* %1, i32 1)
%27 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @str, i64 0, i64 0))
%28 = load i32, i32* @stdout
%29 = tail call i32 @fflush(i32 %28)
%30 = tail call i32 @close(i32 1)
br label %31
31: ; preds = %18, %20, %7
%32 = phi i32 [ 1, %7 ], [ 1, %18 ], [ 0, %20 ]
ret i32 %32
}