#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*/ STDOUT_FILENO;
int /*<<< orphan*/ close(int);
int dup(int /*<<< orphan*/);
int /*<<< orphan*/ err_sys(char *);
int /*<<< orphan*/ write(int /*<<< orphan*/, char *, int);
int main(int argc, char **argv) {
  int fd;
  if ((fd = dup(STDOUT_FILENO)) < 0) {
    err_sys("error while duping stdout");
  }
  close(fd);
  char buf[] = "Hello, world\n";
  write(STDOUT_FILENO, buf, 13);
  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    0x2e93(%rip),%rax
mov    (%rax),%edi
call   1050 <dup@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    117c <main+0x3c>
lea    0xe89(%rip),%rdi
call   1030 <err_sys@plt>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
mov    0xe8f(%rip),%rax
mov    %rax,-0x22(%rbp)
mov    0xe8d(%rip),%eax
mov    %eax,-0x1a(%rbp)
mov    0xe87(%rip),%ax
mov    %ax,-0x16(%rbp)
mov    0x2e46(%rip),%rax
mov    (%rax),%edi
lea    -0x22(%rbp),%rsi
mov    $0xd,%edx
call   1040 <write@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    0x2ea2(%rip),%r14
mov    (%r14),%edi
call   1050 <dup@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    1168 <main+0x28>
lea    0xe9d(%rip),%rdi
call   1030 <err_sys@plt>
mov    %ebx,%edi
call   1060 <close@plt>
movabs $0xa646c726f7720,%rax
mov    %rax,0xe(%rsp)
movabs $0x77202c6f6c6c6548,%rax
2c 20 77
mov    %rax,0x8(%rsp)
mov    (%r14),%edi
lea    0x8(%rsp),%rsi
mov    $0xd,%edx
call   1040 <write@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@STDOUT_FILENO = global i32 0
@.str = [26 x i8] c"error while duping stdout\00"
@__const.main.buf = [14 x i8] c"Hello, world\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca [14 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = load i32, i32* @STDOUT_FILENO
%9 = call i32 @dup(i32 %8)
store i32 %9, i32* %6
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %2
%12 = call i32 @err_sys(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
br label %13
13: ; preds = %11, %2
%14 = load i32, i32* %6
%15 = call i32 @close(i32 %14)
%16 = bitcast [14 x i8]* %7 to i8*
%17 = load i32, i32* @STDOUT_FILENO
%18 = getelementptr inbounds [14 x i8], [14 x i8]* %7, i64 0, i64 0
%19 = call i32 @write(i32 %17, i8* %18, i32 13)
ret i32 0
}

LLVM IR (-O2):
@STDOUT_FILENO = global i32 0
@.str = [26 x i8] c"error while duping stdout\00"
@__const.main.buf = [14 x i8] c"Hello, world\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [14 x i8]
%4 = load i32, i32* @STDOUT_FILENO
%5 = tail call i32 @dup(i32 %4)
%6 = icmp slt i32 %5, 0
br i1 %6, label %7, label %9
7: ; preds = %2
%8 = tail call i32 @err_sys(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
br label %9
9: ; preds = %7, %2
%10 = tail call i32 @close(i32 %5)
%11 = getelementptr inbounds [14 x i8], [14 x i8]* %3, i64 0, i64 0
%12 = load i32, i32* @STDOUT_FILENO
%13 = call i32 @write(i32 %12, i8* %11, i32 13)
ret i32 0
}