#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_RDWR;
int /*<<< orphan*/ close(int);
int dup(int);
int open(char *, int, int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ strcpy(char *, char *);
int main() {
  int fd;
  char buf[512];
  fd = open("test", O_RDWR | O_CREAT, 0666);
  if (!fd) {
    printf("cannot open test file\n");
  }
  close(1);
  if (dup(fd) != 1) {
    printf("cannot open test file %d new node\n", fd);
    return 1;
  }
  printf("before writing\n");
  strcpy(buf, "Testing, this should go to file test");
  printf("%s\n", buf);
  printf("after writing\n");
  close(fd);
  fd = open("/dev/console", O_RDONLY, 0);
  close(1);
  dup(fd);
  printf("back to normal\n");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x210,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e77(%rip),%rax
mov    (%rax),%esi
mov    0x2e86(%rip),%rax
or     (%rax),%esi
lea    0xeef(%rip),%rdi
mov    $0x1b6,%edx
call   1070 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jne    11a0 <main+0x50>
lea    0xe67(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x1,%edi
call   1060 <close@plt>
mov    -0x8(%rbp),%edi
call   1050 <dup@plt>
cmp    $0x1,%eax
je     11d8 <main+0x88>
mov    -0x8(%rbp),%esi
lea    0xe52(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1265 <main+0x115>
lea    0xe5b(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0x210(%rbp),%rdi
lea    0xe56(%rip),%rsi
call   1030 <strcpy@plt>
lea    -0x210(%rbp),%rsi
lea    0xe68(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe5e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x8(%rbp),%edi
call   1060 <close@plt>
mov    0x2dad(%rip),%rax
mov    (%rax),%esi
lea    0xe4e(%rip),%rdi
xor    %edx,%edx
call   1070 <open@plt>
mov    %eax,-0x8(%rbp)
mov    $0x1,%edi
call   1060 <close@plt>
mov    -0x8(%rbp),%edi
call   1050 <dup@plt>
lea    0xe38(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x210,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
sub    $0x208,%rsp
mov    0x2e70(%rip),%rax
mov    0x2e81(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xec1(%rip),%rdi
mov    $0x1b6,%edx
call   1080 <open@plt>
mov    %eax,%ebx
test   %eax,%eax
jne    119e <main+0x3e>
lea    0xebc(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%ebp
mov    $0x1,%edi
call   1070 <close@plt>
mov    %ebx,%edi
call   1060 <dup@plt>
cmp    $0x1,%eax
jne    122c <main+0xcc>
lea    0xeab(%rip),%rdi
call   1040 <puts@plt>
lea    0xe57(%rip),%rsi
mov    %rsp,%rbp
mov    %rbp,%rdi
call   1030 <strcpy@plt>
mov    %rbp,%rdi
call   1040 <puts@plt>
lea    0xe94(%rip),%rdi
call   1040 <puts@plt>
mov    %ebx,%edi
call   1070 <close@plt>
mov    0x2ddf(%rip),%rax
mov    (%rax),%esi
lea    0xe46(%rip),%rdi
xor    %ebp,%ebp
xor    %edx,%edx
call   1080 <open@plt>
mov    %eax,%ebx
mov    $0x1,%edi
call   1070 <close@plt>
mov    %ebx,%edi
call   1060 <dup@plt>
lea    0xe63(%rip),%rdi
call   1040 <puts@plt>
jmp    123c <main+0xdc>
lea    0xdcd(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %ebp,%eax
add    $0x208,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [5 x i8] c"test\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@.str.1 = [23 x i8] c"cannot open test file\0A\00"
@.str.2 = [35 x i8] c"cannot open test file %d new node\0A\00"
@.str.3 = [16 x i8] c"before writing\0A\00"
@.str.4 = [37 x i8] c"Testing, this should go to file test\00"
@.str.5 = [4 x i8] c"%s\0A\00"
@.str.6 = [15 x i8] c"after writing\0A\00"
@.str.7 = [13 x i8] c"/dev/console\00"
@O_RDONLY = global i32 0
@.str.8 = [16 x i8] c"back to normal\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca [512 x i8]
store i32 0, i32* %1
%4 = load i32, i32* @O_RDWR
%5 = load i32, i32* @O_CREAT
%6 = or i32 %4, %5
%7 = call i32 @open(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 %6, i32 438)
store i32 %7, i32* %2
%8 = load i32, i32* %2
%9 = icmp ne i32 %8, 0
br i1 %9, label %12, label %10
10: ; preds = %0
%11 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0))
br label %12
12: ; preds = %10, %0
%13 = call i32 @close(i32 1)
%14 = load i32, i32* %2
%15 = call i32 @dup(i32 %14)
%16 = icmp ne i32 %15, 1
br i1 %16, label %17, label %20
17: ; preds = %12
%18 = load i32, i32* %2
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.2, i64 0, i64 0), i32 %18)
store i32 1, i32* %1
br label %35
20: ; preds = %12
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0))
%22 = getelementptr inbounds [512 x i8], [512 x i8]* %3, i64 0, i64 0
%23 = call i32 @strcpy(i8* %22, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.4, i64 0, i64 0))
%24 = getelementptr inbounds [512 x i8], [512 x i8]* %3, i64 0, i64 0
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0), i8* %24)
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.6, i64 0, i64 0))
%27 = load i32, i32* %2
%28 = call i32 @close(i32 %27)
%29 = load i32, i32* @O_RDONLY
%30 = call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.7, i64 0, i64 0), i32 %29, i32 0)
store i32 %30, i32* %2
%31 = call i32 @close(i32 1)
%32 = load i32, i32* %2
%33 = call i32 @dup(i32 %32)
%34 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.8, i64 0, i64 0))
store i32 0, i32* %1
br label %35
35: ; preds = %20, %17
%36 = load i32, i32* %1
ret i32 %36
}

LLVM IR (-O2):
@.str = [5 x i8] c"test\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@.str.2 = [35 x i8] c"cannot open test file %d new node\0A\00"
@.str.4 = [37 x i8] c"Testing, this should go to file test\00"
@.str.7 = [13 x i8] c"/dev/console\00"
@O_RDONLY = global i32 0
@str = [22 x i8] c"cannot open test file\00"
@str.9 = [15 x i8] c"before writing\00"
@str.10 = [14 x i8] c"after writing\00"
@str.11 = [15 x i8] c"back to normal\00"
define i32 @main() {
%1 = alloca [512 x i8]
%2 = getelementptr inbounds [512 x i8], [512 x i8]* %1, i64 0, i64 0
%3 = load i32, i32* @O_RDWR
%4 = load i32, i32* @O_CREAT
%5 = or i32 %4, %3
%6 = tail call i32 @open(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 %5, i32 438)
%7 = icmp eq i32 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %0
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @str, i64 0, i64 0))
br label %10
10: ; preds = %8, %0
%11 = tail call i32 @close(i32 1)
%12 = tail call i32 @dup(i32 %6)
%13 = icmp eq i32 %12, 1
br i1 %13, label %16, label %14
14: ; preds = %10
%15 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @.str.2, i64 0, i64 0), i32 %6)
br label %27
16: ; preds = %10
%17 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str.9, i64 0, i64 0))
%18 = call i32 @strcpy(i8* %2, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.4, i64 0, i64 0))
%19 = call i32 @puts(i8* %2)
%20 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str.10, i64 0, i64 0))
%21 = call i32 @close(i32 %6)
%22 = load i32, i32* @O_RDONLY
%23 = call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.7, i64 0, i64 0), i32 %22, i32 0)
%24 = call i32 @close(i32 1)
%25 = call i32 @dup(i32 %23)
%26 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str.11, i64 0, i64 0))
br label %27
27: ; preds = %16, %14
%28 = phi i32 [ 1, %14 ], [ 0, %16 ]
ret i32 %28
}