#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 STDOUT_FILENO;
int close(int);
int dup2(int, int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
void make_std_writer(int *p) {
  static const int read_end = 0;
  static const int write_end = 1;
  if (close(p[read_end]) == -1) {
    perror("error calling close() in make_std_writer()");
    exit(EXIT_FAILURE);
  }
  if (p[write_end] != STDOUT_FILENO) {
    if (dup2(p[write_end], STDOUT_FILENO) == -1) {
      perror("error calling dup2() in make_std_writer()");
      exit(EXIT_FAILURE);
    }
    if (close(p[write_end]) == -1) {
      perror("error calling close() in make_std_writer()");
      exit(EXIT_FAILURE);
    }
  }
}


x86 assembly (clang, -O0):
<make_std_writer>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%edi
call   1040 <close@plt>
cmp    $0xffffffff,%eax
jne    117a <make_std_writer+0x3a>
lea    0xea1(%rip),%rdi
call   1050 <perror@plt>
mov    0x2e65(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>
mov    -0x8(%rbp),%rax
mov    0x4(%rax),%eax
mov    0x2e68(%rip),%rcx
cmp    (%rcx),%eax
je     11fc <make_std_writer+0xbc>
mov    -0x8(%rbp),%rax
mov    0x4(%rax),%edi
mov    0x2e52(%rip),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
cmp    $0xffffffff,%eax
jne    11c8 <make_std_writer+0x88>
lea    0xe7e(%rip),%rdi
call   1050 <perror@plt>
mov    0x2e17(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>
mov    -0x8(%rbp),%rax
mov    0x4(%rax),%edi
call   1040 <close@plt>
cmp    $0xffffffff,%eax
jne    11f7 <make_std_writer+0xb7>
lea    0xe24(%rip),%rdi
call   1050 <perror@plt>
mov    0x2de8(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>
jmp    11fc <make_std_writer+0xbc>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<make_std_writer>:
push   %rbx
mov    %rdi,%rbx
mov    (%rdi),%edi
call   1040 <close@plt>
cmp    $0xffffffff,%eax
je     1179 <make_std_writer+0x39>
mov    0x4(%rbx),%edi
mov    0x2e96(%rip),%rax
mov    (%rax),%esi
cmp    %esi,%edi
je     1177 <make_std_writer+0x37>
call   1030 <dup2@plt>
cmp    $0xffffffff,%eax
je     1182 <make_std_writer+0x42>
mov    0x4(%rbx),%edi
call   1040 <close@plt>
cmp    $0xffffffff,%eax
je     1179 <make_std_writer+0x39>
pop    %rbx
ret
lea    0xe80(%rip),%rdi
jmp    1189 <make_std_writer+0x49>
lea    0xea2(%rip),%rdi
call   1050 <perror@plt>
mov    0x2e43(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@make_std_writer.read_end = internal constant i32 0
@make_std_writer.write_end = internal constant i32 1
@.str = [43 x i8] c"error calling close() in make_std_writer()\00"
@EXIT_FAILURE = global i32 0
@STDOUT_FILENO = global i32 0
@.str.1 = [42 x i8] c"error calling dup2() in make_std_writer()\00"
define void @make_std_writer(i32* %0) {
%2 = alloca i32*
store i32* %0, i32** %2
%3 = load i32*, i32** %2
%4 = getelementptr inbounds i32, i32* %3, i64 0
%5 = load i32, i32* %4
%6 = call i32 @close(i32 %5)
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %12
8: ; preds = %1
%9 = call i32 @perror(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str, i64 0, i64 0))
%10 = load i32, i32* @EXIT_FAILURE
%11 = call i32 @exit(i32 %10)
unreachable
12: ; preds = %1
%13 = load i32*, i32** %2
%14 = getelementptr inbounds i32, i32* %13, i64 1
%15 = load i32, i32* %14
%16 = load i32, i32* @STDOUT_FILENO
%17 = icmp ne i32 %15, %16
br i1 %17, label %18, label %40
18: ; preds = %12
%19 = load i32*, i32** %2
%20 = getelementptr inbounds i32, i32* %19, i64 1
%21 = load i32, i32* %20
%22 = load i32, i32* @STDOUT_FILENO
%23 = call i32 @dup2(i32 %21, i32 %22)
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %29
25: ; preds = %18
%26 = call i32 @perror(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.1, i64 0, i64 0))
%27 = load i32, i32* @EXIT_FAILURE
%28 = call i32 @exit(i32 %27)
unreachable
29: ; preds = %18
%30 = load i32*, i32** %2
%31 = getelementptr inbounds i32, i32* %30, i64 1
%32 = load i32, i32* %31
%33 = call i32 @close(i32 %32)
%34 = icmp eq i32 %33, -1
br i1 %34, label %35, label %39
35: ; preds = %29
%36 = call i32 @perror(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str, i64 0, i64 0))
%37 = load i32, i32* @EXIT_FAILURE
%38 = call i32 @exit(i32 %37)
unreachable
39: ; preds = %29
br label %40
40: ; preds = %39, %12
ret void
}

LLVM IR (-O2):
@.str = [43 x i8] c"error calling close() in make_std_writer()\00"
@EXIT_FAILURE = global i32 0
@STDOUT_FILENO = global i32 0
@.str.1 = [42 x i8] c"error calling dup2() in make_std_writer()\00"
define void @make_std_writer(i32* nocapture %0) {
%2 = load i32, i32* %0
%3 = tail call i32 @close(i32 %2)
%4 = icmp eq i32 %3, -1
br i1 %4, label %5, label %9
5: ; preds = %1
%6 = tail call i32 @perror(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str, i64 0, i64 0))
%7 = load i32, i32* @EXIT_FAILURE
%8 = tail call i32 @exit(i32 %7)
unreachable
9: ; preds = %1
%10 = getelementptr inbounds i32, i32* %0, i64 1
%11 = load i32, i32* %10
%12 = load i32, i32* @STDOUT_FILENO
%13 = icmp eq i32 %11, %12
br i1 %13, label %29, label %14
14: ; preds = %9
%15 = tail call i32 @dup2(i32 %11, i32 %12)
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %21
17: ; preds = %14
%18 = tail call i32 @perror(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.1, i64 0, i64 0))
%19 = load i32, i32* @EXIT_FAILURE
%20 = tail call i32 @exit(i32 %19)
unreachable
21: ; preds = %14
%22 = load i32, i32* %10
%23 = tail call i32 @close(i32 %22)
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %29
25: ; preds = %21
%26 = tail call i32 @perror(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str, i64 0, i64 0))
%27 = load i32, i32* @EXIT_FAILURE
%28 = tail call i32 @exit(i32 %27)
unreachable
29: ; preds = %21, %9
ret void
}