#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 STDOUT_FILENO;
int /*<<< orphan*/ _exit(int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
int dup(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ puts(char *);
int write(int, char *, int);
void _write2stdout() {
  int newfd = dup(STDOUT_FILENO);
  int wt_len = 0;
  if (0 > newfd) {
    puts("Error in duplicating STDOUT_FILENO.\n");
    _exit(errno);
  }
  wt_len = write(newfd, "->Newfd\n", 8);
  if (wt_len <= 0)
    puts("Error in writing files.\n");
  write(STDOUT_FILENO, "->Newfd\n", 8);
  close(newfd);
  write(STDOUT_FILENO, "->Newfd\n", 8);
}


x86 assembly (clang, -O0):
<_write2stdout>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    0x2e91(%rip),%rax
mov    (%rax),%edi
call   1060 <dup@plt>
mov    %eax,-0x4(%rbp)
movl   $0x0,-0x8(%rbp)
xor    %eax,%eax
cmp    -0x4(%rbp),%eax
jle    1195 <_write2stdout+0x45>
lea    0xe7e(%rip),%rdi
call   1040 <puts@plt>
mov    0x2e4a(%rip),%rax
mov    (%rax),%edi
call   1030 <_exit@plt>
mov    -0x4(%rbp),%edi
lea    0xe86(%rip),%rsi
mov    $0x8,%edx
call   1050 <write@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jg     11c2 <_write2stdout+0x72>
lea    0xe71(%rip),%rdi
call   1040 <puts@plt>
mov    0x2e27(%rip),%rax
mov    (%rax),%edi
lea    0xe53(%rip),%rsi
mov    $0x8,%edx
call   1050 <write@plt>
mov    -0x4(%rbp),%edi
call   1070 <close@plt>
mov    0x2e05(%rip),%rax
mov    (%rax),%edi
lea    0xe31(%rip),%rsi
mov    $0x8,%edx
call   1050 <write@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_write2stdout>:
push   %r15
push   %r14
push   %rbx
mov    0x2e94(%rip),%r15
mov    (%r15),%edi
call   1060 <dup@plt>
test   %eax,%eax
js     11c0 <_write2stdout+0x70>
mov    %eax,%ebx
lea    0xeb4(%rip),%rsi
mov    %eax,%edi
mov    $0x8,%edx
call   1050 <write@plt>
test   %eax,%eax
jg     118d <_write2stdout+0x3d>
lea    0xea6(%rip),%rdi
call   1040 <puts@plt>
mov    (%r15),%edi
lea    0xe8e(%rip),%r14
mov    %r14,%rsi
mov    $0x8,%edx
call   1050 <write@plt>
mov    %ebx,%edi
call   1070 <close@plt>
mov    (%r15),%edi
mov    %r14,%rsi
mov    $0x8,%edx
pop    %rbx
pop    %r14
pop    %r15
jmp    1050 <write@plt>
lea    0xe39(%rip),%rdi
call   1040 <puts@plt>
mov    0x2e05(%rip),%rax
mov    (%rax),%edi
call   1030 <_exit@plt>

LLVM IR (-O0):
@STDOUT_FILENO = global i32 0
@.str = [37 x i8] c"Error in duplicating STDOUT_FILENO.\0A\00"
@errno = global i32 0
@.str.1 = [9 x i8] c"->Newfd\0A\00"
@.str.2 = [25 x i8] c"Error in writing files.\0A\00"
define void @_write2stdout() {
%1 = alloca i32
%2 = alloca i32
%3 = load i32, i32* @STDOUT_FILENO
%4 = call i32 @dup(i32 %3)
store i32 %4, i32* %1
store i32 0, i32* %2
%5 = load i32, i32* %1
%6 = icmp sgt i32 0, %5
br i1 %6, label %7, label %11
7: ; preds = %0
%8 = call i32 @puts(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0))
%9 = load i32, i32* @errno
%10 = call i32 @_exit(i32 %9)
unreachable
11: ; preds = %0
%12 = load i32, i32* %1
%13 = call i32 @write(i32 %12, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i32 8)
store i32 %13, i32* %2
%14 = load i32, i32* %2
%15 = icmp sle i32 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %11
%17 = call i32 @puts(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0))
br label %18
18: ; preds = %16, %11
%19 = load i32, i32* @STDOUT_FILENO
%20 = call i32 @write(i32 %19, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i32 8)
%21 = load i32, i32* %1
%22 = call i32 @close(i32 %21)
%23 = load i32, i32* @STDOUT_FILENO
%24 = call i32 @write(i32 %23, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i32 8)
ret void
}

LLVM IR (-O2):
@STDOUT_FILENO = global i32 0
@.str = [37 x i8] c"Error in duplicating STDOUT_FILENO.\0A\00"
@errno = global i32 0
@.str.1 = [9 x i8] c"->Newfd\0A\00"
@.str.2 = [25 x i8] c"Error in writing files.\0A\00"
define void @_write2stdout() {
%1 = load i32, i32* @STDOUT_FILENO
%2 = tail call i32 @dup(i32 %1)
%3 = icmp slt i32 %2, 0
br i1 %3, label %4, label %8
4: ; preds = %0
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0))
%6 = load i32, i32* @errno
%7 = tail call i32 @_exit(i32 %6)
unreachable
8: ; preds = %0
%9 = tail call i32 @write(i32 %2, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i32 8)
%10 = icmp slt i32 %9, 1
br i1 %10, label %11, label %13
11: ; preds = %8
%12 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0))
br label %13
13: ; preds = %11, %8
%14 = load i32, i32* @STDOUT_FILENO
%15 = tail call i32 @write(i32 %14, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i32 8)
%16 = tail call i32 @close(i32 %2)
%17 = load i32, i32* @STDOUT_FILENO
%18 = tail call i32 @write(i32 %17, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i32 8)
ret void
}