#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 /*<<< orphan*/ EXIT_FAILURE;
int O_CREAT;
int O_TRUNC;
int O_WRONLY;
int /*<<< orphan*/ STDOUT_FILENO;
int S_IRUSR;
int S_IWUSR;
int dup2(int, int /*<<< orphan*/);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int open(char *, int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ stderr;
int main(void) {
  int file = open("dup2.temp", O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
  if (file == -1) {
    perror("open");
    exit(EXIT_FAILURE);
  }
  if (dup2(file, STDOUT_FILENO) == -1) {
    perror("dup");
    exit(EXIT_FAILURE);
  }
  printf("Hello world\n");
  fprintf(stderr, "Look in dup2.temp\n");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e2a(%rip),%rax
mov    (%rax),%esi
mov    0x2e79(%rip),%rax
or     (%rax),%esi
mov    0x2e68(%rip),%rax
or     (%rax),%esi
mov    0x2e1f(%rip),%rax
mov    (%rax),%edx
mov    0x2e36(%rip),%rax
or     (%rax),%edx
lea    0xe5d(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    11cf <main+0x6f>
lea    0xe4e(%rip),%rdi
call   1070 <perror@plt>
mov    0x2df0(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    -0x8(%rbp),%edi
mov    0x2e07(%rip),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
cmp    $0xffffffff,%eax
jne    1203 <main+0xa3>
lea    0xe1f(%rip),%rdi
call   1070 <perror@plt>
mov    0x2dbc(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
lea    0xe09(%rip),%rdi
call   1040 <printf@plt>
mov    0x2daa(%rip),%rax
mov    (%rax),%edi
lea    0xe01(%rip),%rsi
call   1050 <fprintf@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rax
mov    0x2e38(%rip),%rax
mov    0x2e89(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e76(%rip),%rax
or     (%rax),%esi
mov    0x2e2d(%rip),%rax
mov    0x2e46(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
lea    0xe6b(%rip),%rdi
call   1060 <open@plt>
cmp    $0xffffffff,%eax
je     11d9 <main+0x79>
mov    0x2e3a(%rip),%rcx
mov    (%rcx),%esi
mov    %eax,%edi
call   1040 <dup2@plt>
cmp    $0xffffffff,%eax
je     11e2 <main+0x82>
lea    0xe6b(%rip),%rdi
call   1030 <puts@plt>
mov    0x2df9(%rip),%rax
mov    (%rax),%edi
lea    0xe43(%rip),%rsi
call   1050 <fprintf@plt>
xor    %eax,%eax
pop    %rcx
ret
lea    0xe2a(%rip),%rdi
jmp    11e9 <main+0x89>
lea    0xe26(%rip),%rdi
call   1070 <perror@plt>
mov    0x2dc3(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [10 x i8] c"dup2.temp\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@.str.1 = [5 x i8] c"open\00"
@EXIT_FAILURE = global i32 0
@STDOUT_FILENO = global i32 0
@.str.2 = [4 x i8] c"dup\00"
@.str.3 = [13 x i8] c"Hello world\0A\00"
@stderr = global i32 0
@.str.4 = [19 x i8] c"Look in dup2.temp\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
store i32 0, i32* %1
%3 = load i32, i32* @O_WRONLY
%4 = load i32, i32* @O_CREAT
%5 = or i32 %3, %4
%6 = load i32, i32* @O_TRUNC
%7 = or i32 %5, %6
%8 = load i32, i32* @S_IRUSR
%9 = load i32, i32* @S_IWUSR
%10 = or i32 %8, %9
%11 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %7, i32 %10)
store i32 %11, i32* %2
%12 = load i32, i32* %2
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %18
14: ; preds = %0
%15 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%16 = load i32, i32* @EXIT_FAILURE
%17 = call i32 @exit(i32 %16)
unreachable
18: ; preds = %0
%19 = load i32, i32* %2
%20 = load i32, i32* @STDOUT_FILENO
%21 = call i32 @dup2(i32 %19, i32 %20)
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %27
23: ; preds = %18
%24 = call i32 @perror(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0))
%25 = load i32, i32* @EXIT_FAILURE
%26 = call i32 @exit(i32 %25)
unreachable
27: ; preds = %18
%28 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
%29 = load i32, i32* @stderr
%30 = call i32 @fprintf(i32 %29, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0))
ret i32 0
}

LLVM IR (-O2):
@.str = [10 x i8] c"dup2.temp\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@.str.1 = [5 x i8] c"open\00"
@EXIT_FAILURE = global i32 0
@STDOUT_FILENO = global i32 0
@.str.2 = [4 x i8] c"dup\00"
@stderr = global i32 0
@.str.4 = [19 x i8] c"Look in dup2.temp\0A\00"
@str = [12 x i8] c"Hello world\00"
define i32 @main() {
%1 = load i32, i32* @O_WRONLY
%2 = load i32, i32* @O_CREAT
%3 = or i32 %2, %1
%4 = load i32, i32* @O_TRUNC
%5 = or i32 %3, %4
%6 = load i32, i32* @S_IRUSR
%7 = load i32, i32* @S_IWUSR
%8 = or i32 %7, %6
%9 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %5, i32 %8)
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %15
11: ; preds = %0
%12 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%13 = load i32, i32* @EXIT_FAILURE
%14 = tail call i32 @exit(i32 %13)
unreachable
15: ; preds = %0
%16 = load i32, i32* @STDOUT_FILENO
%17 = tail call i32 @dup2(i32 %9, i32 %16)
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %23
19: ; preds = %15
%20 = tail call i32 @perror(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0))
%21 = load i32, i32* @EXIT_FAILURE
%22 = tail call i32 @exit(i32 %21)
unreachable
23: ; preds = %15
%24 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str, i64 0, i64 0))
%25 = load i32, i32* @stderr
%26 = tail call i32 @fprintf(i32 %25, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0))
ret i32 0
}