#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 */
typedef int /*<<< orphan*/ s;

/* Variables and functions */
int O_CREAT;
int O_RDWR;
int O_TRUNC;
int STDOUT_FILENO;
int dup(int);
int /*<<< orphan*/ dup2(int, int);
int open(char *, int, int);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ write(int, char *, int);
int main(void) {
  int fd = dup(STDOUT_FILENO);
  char buf[] = "for testing...\n";
  write(fd, buf, sizeof buf);
  fd = open("file4dup", O_RDWR | O_CREAT | O_TRUNC, 0644);
  dup2(fd, STDOUT_FILENO);
  char s[] = "abcd\n";
  write(STDOUT_FILENO, s, sizeof(s) - 1);
  printf("hello world\n");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e7a(%rip),%rax
mov    (%rax),%edi
call   1060 <dup@plt>
mov    %eax,-0x8(%rbp)
mov    0xe89(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    0xe86(%rip),%rax
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x10,%edx
call   1030 <write@plt>
mov    0x2e32(%rip),%rax
mov    (%rax),%esi
mov    0x2e51(%rip),%rax
or     (%rax),%esi
mov    0x2e40(%rip),%rax
or     (%rax),%esi
lea    0xe57(%rip),%rdi
mov    $0x1a4,%edx
call   1070 <open@plt>
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e10(%rip),%rax
mov    (%rax),%esi
call   1040 <dup2@plt>
mov    0xe3c(%rip),%eax
mov    %eax,-0x26(%rbp)
mov    0xe36(%rip),%ax
mov    %ax,-0x22(%rbp)
mov    0x2dee(%rip),%rax
mov    (%rax),%edi
lea    -0x26(%rbp),%rsi
mov    $0x5,%edx
call   1030 <write@plt>
lea    0xe16(%rip),%rdi
call   1050 <printf@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x20,%rsp
mov    0x2e84(%rip),%rbx
mov    (%rbx),%edi
call   1060 <dup@plt>
movaps 0xe96(%rip),%xmm0
movaps %xmm0,0x10(%rsp)
lea    0x10(%rsp),%rsi
mov    %eax,%edi
mov    $0x10,%edx
call   1040 <write@plt>
mov    0x2e49(%rip),%rax
mov    0x2e6a(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e57(%rip),%rax
or     (%rax),%esi
lea    0xe6e(%rip),%rdi
mov    $0x1a4,%edx
call   1070 <open@plt>
mov    (%rbx),%esi
mov    %eax,%edi
call   1050 <dup2@plt>
movw   $0xa,0xc(%rsp)
movl   $0x64636261,0x8(%rsp)
64
mov    (%rbx),%edi
lea    0x8(%rsp),%rsi
mov    $0x5,%edx
call   1040 <write@plt>
lea    0xe43(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@STDOUT_FILENO = global i32 0
@__const.main.buf = [16 x i8] c"for testing...\0A\00"
@.str = [9 x i8] c"file4dup\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@__const.main.s = [6 x i8] c"abcd\0A\00"
@.str.1 = [13 x i8] c"hello world\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca [16 x i8]
%4 = alloca [6 x i8]
store i32 0, i32* %1
%5 = load i32, i32* @STDOUT_FILENO
%6 = call i32 @dup(i32 %5)
store i32 %6, i32* %2
%7 = bitcast [16 x i8]* %3 to i8*
%8 = load i32, i32* %2
%9 = getelementptr inbounds [16 x i8], [16 x i8]* %3, i64 0, i64 0
%10 = call i32 @write(i32 %8, i8* %9, i32 16)
%11 = load i32, i32* @O_RDWR
%12 = load i32, i32* @O_CREAT
%13 = or i32 %11, %12
%14 = load i32, i32* @O_TRUNC
%15 = or i32 %13, %14
%16 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %15, i32 420)
store i32 %16, i32* %2
%17 = load i32, i32* %2
%18 = load i32, i32* @STDOUT_FILENO
%19 = call i32 @dup2(i32 %17, i32 %18)
%20 = bitcast [6 x i8]* %4 to i8*
%21 = load i32, i32* @STDOUT_FILENO
%22 = getelementptr inbounds [6 x i8], [6 x i8]* %4, i64 0, i64 0
%23 = call i32 @write(i32 %21, i8* %22, i32 5)
%24 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
ret i32 0
}

LLVM IR (-O2):
@STDOUT_FILENO = global i32 0
@__const.main.buf = [16 x i8] c"for testing...\0A\00"
@.str = [9 x i8] c"file4dup\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@__const.main.s = [6 x i8] c"abcd\0A\00"
@str = [12 x i8] c"hello world\00"
define i32 @main() {
%1 = alloca [16 x i8]
%2 = alloca [6 x i8]
%3 = load i32, i32* @STDOUT_FILENO
%4 = tail call i32 @dup(i32 %3)
%5 = getelementptr inbounds [16 x i8], [16 x i8]* %1, i64 0, i64 0
%6 = call i32 @write(i32 %4, i8* %5, i32 16)
%7 = load i32, i32* @O_RDWR
%8 = load i32, i32* @O_CREAT
%9 = or i32 %8, %7
%10 = load i32, i32* @O_TRUNC
%11 = or i32 %9, %10
%12 = tail call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %11, i32 420)
%13 = load i32, i32* @STDOUT_FILENO
%14 = tail call i32 @dup2(i32 %12, i32 %13)
%15 = getelementptr inbounds [6 x i8], [6 x i8]* %2, i64 0, i64 0
%16 = load i32, i32* @STDOUT_FILENO
%17 = call i32 @write(i32 %16, i8* %15, i32 5)
%18 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str, i64 0, i64 0))
ret i32 0
}