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

/* Variables and functions */
int /*<<< orphan*/ close(int);
int dup(int);
int dup2(int, int);
int /*<<< orphan*/ printf(char *, int, int);
int /*<<< orphan*/ write(int, char *, int);
int main(int argc, char *argv[]) {
  int fd1, fd2;
  char msg1[] = "Hi, \n";
  char msg2[] = "I am a genuis.\n";
  fd1 = dup(1);
  fd2 = dup2(1, 9);
  printf("New file descriptor: fd1=%d, fd2=%d\n", fd1, fd2);
  write(fd1, msg1, sizeof(msg1));
  write(fd2, msg2, sizeof(msg2));
  close(fd1);
  close(fd2);
  write(1, msg1, sizeof(msg1));
  close(1);
  write(1, msg2, sizeof(msg2));
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0xe94(%rip),%eax
mov    %eax,-0x1e(%rbp)
mov    0xe8e(%rip),%ax
mov    %ax,-0x1a(%rbp)
mov    0xeaf(%rip),%rax
mov    %rax,-0x30(%rbp)
mov    0xeac(%rip),%rax
mov    %rax,-0x28(%rbp)
mov    $0x1,%edi
call   1060 <dup@plt>
mov    %eax,-0x14(%rbp)
mov    $0x1,%edi
mov    $0x9,%esi
call   1040 <dup2@plt>
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%esi
mov    -0x18(%rbp),%edx
lea    0xe4a(%rip),%rdi
call   1050 <printf@plt>
mov    -0x14(%rbp),%edi
lea    -0x1e(%rbp),%rsi
mov    $0x6,%edx
call   1030 <write@plt>
mov    -0x18(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0x10,%edx
call   1030 <write@plt>
mov    -0x14(%rbp),%edi
call   1070 <close@plt>
mov    -0x18(%rbp),%edi
call   1070 <close@plt>
lea    -0x1e(%rbp),%rsi
mov    $0x1,%edi
mov    $0x6,%edx
call   1030 <write@plt>
mov    $0x1,%edi
call   1070 <close@plt>
lea    -0x30(%rbp),%rsi
mov    $0x1,%edi
mov    $0x10,%edx
call   1030 <write@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x28,%rsp
movw   $0xa,0xc(%rsp)
movl   $0x202c6948,0x8(%rsp)
20
movaps 0xec0(%rip),%xmm0
movaps %xmm0,0x10(%rsp)
mov    $0x1,%edi
call   1060 <dup@plt>
mov    %eax,%ebx
mov    $0x1,%edi
mov    $0x9,%esi
call   1040 <dup2@plt>
mov    %eax,%ebp
lea    0xe6d(%rip),%rdi
mov    %ebx,%esi
mov    %eax,%edx
call   1050 <printf@plt>
lea    0x8(%rsp),%r14
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x6,%edx
call   1030 <write@plt>
lea    0x10(%rsp),%r15
mov    %ebp,%edi
mov    %r15,%rsi
mov    $0x10,%edx
call   1030 <write@plt>
mov    %ebx,%edi
call   1070 <close@plt>
mov    %ebp,%edi
call   1070 <close@plt>
mov    $0x1,%edi
mov    %r14,%rsi
mov    $0x6,%edx
call   1030 <write@plt>
mov    $0x1,%edi
call   1070 <close@plt>
mov    $0x1,%edi
mov    %r15,%rsi
mov    $0x10,%edx
call   1030 <write@plt>
xor    %eax,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@__const.main.msg1 = [6 x i8] c"Hi, \0A\00"
@__const.main.msg2 = [16 x i8] c"I am a genuis.\0A\00"
@.str = [37 x i8] c"New file descriptor: fd1=%d, fd2=%d\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca [6 x i8]
%9 = alloca [16 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = bitcast [6 x i8]* %8 to i8*
%11 = bitcast [16 x i8]* %9 to i8*
%12 = call i32 @dup(i32 1)
store i32 %12, i32* %6
%13 = call i32 @dup2(i32 1, i32 9)
store i32 %13, i32* %7
%14 = load i32, i32* %6
%15 = load i32, i32* %7
%16 = call i32 @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0), i32 %14, i32 %15)
%17 = load i32, i32* %6
%18 = getelementptr inbounds [6 x i8], [6 x i8]* %8, i64 0, i64 0
%19 = call i32 @write(i32 %17, i8* %18, i32 6)
%20 = load i32, i32* %7
%21 = getelementptr inbounds [16 x i8], [16 x i8]* %9, i64 0, i64 0
%22 = call i32 @write(i32 %20, i8* %21, i32 16)
%23 = load i32, i32* %6
%24 = call i32 @close(i32 %23)
%25 = load i32, i32* %7
%26 = call i32 @close(i32 %25)
%27 = getelementptr inbounds [6 x i8], [6 x i8]* %8, i64 0, i64 0
%28 = call i32 @write(i32 1, i8* %27, i32 6)
%29 = call i32 @close(i32 1)
%30 = getelementptr inbounds [16 x i8], [16 x i8]* %9, i64 0, i64 0
%31 = call i32 @write(i32 1, i8* %30, i32 16)
ret i32 0
}

LLVM IR (-O2):
@__const.main.msg1 = [6 x i8] c"Hi, \0A\00"
@__const.main.msg2 = [16 x i8] c"I am a genuis.\0A\00"
@.str = [37 x i8] c"New file descriptor: fd1=%d, fd2=%d\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [6 x i8]
%4 = alloca [16 x i8]
%5 = getelementptr inbounds [6 x i8], [6 x i8]* %3, i64 0, i64 0
%6 = getelementptr inbounds [16 x i8], [16 x i8]* %4, i64 0, i64 0
%7 = tail call i32 @dup(i32 1)
%8 = tail call i32 @dup2(i32 1, i32 9)
%9 = tail call i32 @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0), i32 %7, i32 %8)
%10 = call i32 @write(i32 %7, i8* %5, i32 6)
%11 = call i32 @write(i32 %8, i8* %6, i32 16)
%12 = tail call i32 @close(i32 %7)
%13 = tail call i32 @close(i32 %8)
%14 = call i32 @write(i32 1, i8* %5, i32 6)
%15 = tail call i32 @close(i32 1)
%16 = call i32 @write(i32 1, i8* %6, i32 16)
ret i32 0
}