#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*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ F_DUPFD;
scalar_t__ STDOUT_FILENO;
int /*<<< orphan*/ SYS_dup2;
int /*<<< orphan*/ __darwintrace_close();
int /*<<< orphan*/ __darwintrace_setup();
int /*<<< orphan*/ *__darwintrace_sock();
int /*<<< orphan*/ __darwintrace_sock_set(int /*<<< orphan*/ *);
int /*<<< orphan*/ abort();
int fcntl(int, int /*<<< orphan*/, scalar_t__);
int /*<<< orphan*/ *fdopen(int, char *);
int fileno(int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
int syscall(int /*<<< orphan*/, int, int);
int _dt_dup2(int filedes, int filedes2) {
  __darwintrace_setup();
  FILE *stream = __darwintrace_sock();
  if (stream && filedes2 == fileno(stream)) {
    int new_darwintrace_fd;
    FILE *new_stream;
    if (-1 == (new_darwintrace_fd =
                   fcntl(fileno(stream), F_DUPFD, STDOUT_FILENO + 1))) {
      return -1;
    }
    __darwintrace_close();
    if (NULL == (new_stream = fdopen(new_darwintrace_fd, "a+"))) {
      perror("darwintrace: fdopen");
      abort();
    }
    __darwintrace_sock_set(new_stream);
  }
  return syscall(SYS_dup2, (filedes), (filedes2));
}


x86 assembly (clang, -O0):
<_dt_dup2>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    $0x0,%al
call   10b0 <__darwintrace_setup@plt>
mov    $0x0,%al
call   1070 <__darwintrace_sock@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     1270 <_dt_dup2+0xd0>
mov    -0xc(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x18(%rbp),%rdi
call   1080 <fileno@plt>
mov    %eax,%ecx
mov    -0x2c(%rbp),%eax
cmp    %ecx,%eax
jne    1270 <_dt_dup2+0xd0>
mov    -0x18(%rbp),%rdi
call   1080 <fileno@plt>
mov    %eax,%edi
mov    0x2de7(%rip),%rax
mov    (%rax),%esi
mov    0x2dee(%rip),%rax
mov    (%rax),%rdx
add    $0x1,%rdx
call   1050 <fcntl@plt>
mov    %eax,%ecx
mov    %ecx,-0x1c(%rbp)
mov    $0xffffffff,%eax
cmp    %ecx,%eax
jne    122c <_dt_dup2+0x8c>
movl   $0xffffffff,-0x4(%rbp)
jmp    1287 <_dt_dup2+0xe7>
mov    $0x0,%al
call   1030 <__darwintrace_close@plt>
mov    -0x1c(%rbp),%edi
lea    0xdc3(%rip),%rsi
call   1090 <fdopen@plt>
mov    %rax,%rcx
mov    %rcx,-0x28(%rbp)
xor    %eax,%eax
cmp    %rcx,%rax
jne    1267 <_dt_dup2+0xc7>
lea    0xda8(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x0,%al
call   1040 <abort@plt>
mov    -0x28(%rbp),%rdi
call   10c0 <__darwintrace_sock_set@plt>
mov    0x2d59(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%esi
mov    -0xc(%rbp),%edx
call   1060 <syscall@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_dt_dup2>:
push   %rbp
push   %r14
push   %rbx
mov    %esi,%ebx
mov    %edi,%r14d
xor    %eax,%eax
call   10b0 <__darwintrace_setup@plt>
xor    %eax,%eax
call   1070 <__darwintrace_sock@plt>
test   %rax,%rax
je     121a <_dt_dup2+0x7a>
mov    %rax,%rbp
mov    %rax,%rdi
call   1080 <fileno@plt>
cmp    %ebx,%eax
jne    121a <_dt_dup2+0x7a>
mov    %rbp,%rdi
call   1080 <fileno@plt>
mov    0x2e06(%rip),%rcx
mov    (%rcx),%esi
mov    0x2e0d(%rip),%rcx
mov    (%rcx),%rdx
add    $0x1,%rdx
mov    %eax,%edi
call   1050 <fcntl@plt>
cmp    $0xffffffff,%eax
je     1231 <_dt_dup2+0x91>
mov    %eax,%ebp
xor    %eax,%eax
call   1030 <__darwintrace_close@plt>
lea    0xdfa(%rip),%rsi
mov    %ebp,%edi
call   1090 <fdopen@plt>
test   %rax,%rax
je     123b <_dt_dup2+0x9b>
mov    %rax,%rdi
call   10c0 <__darwintrace_sock_set@plt>
mov    0x2daf(%rip),%rax
mov    (%rax),%edi
mov    %r14d,%esi
mov    %ebx,%edx
pop    %rbx
pop    %r14
pop    %rbp
jmp    1060 <syscall@plt>
mov    $0xffffffff,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xdc1(%rip),%rdi
call   10a0 <perror@plt>
xor    %eax,%eax
call   1040 <abort@plt>

LLVM IR (-O0):
@F_DUPFD = global i32 0
@STDOUT_FILENO = global i64 0
@.str = [3 x i8] c"a+\00"
@.str.1 = [20 x i8] c"darwintrace: fdopen\00"
@SYS_dup2 = global i32 0
define i32 @_dt_dup2(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32*
%7 = alloca i32
%8 = alloca i32*
store i32 %0, i32* %4
store i32 %1, i32* %5
%9 = call i32 (...) @__darwintrace_setup()
%10 = call i32* (...) @__darwintrace_sock()
store i32* %10, i32** %6
%11 = load i32*, i32** %6
%12 = icmp ne i32* %11, null
br i1 %12, label %13, label %38
13: ; preds = %2
%14 = load i32, i32* %5
%15 = load i32*, i32** %6
%16 = call i32 @fileno(i32* %15)
%17 = icmp eq i32 %14, %16
br i1 %17, label %18, label %38
18: ; preds = %13
%19 = load i32*, i32** %6
%20 = call i32 @fileno(i32* %19)
%21 = load i32, i32* @F_DUPFD
%22 = load i64, i64* @STDOUT_FILENO
%23 = add nsw i64 %22, 1
%24 = call i32 @fcntl(i32 %20, i32 %21, i64 %23)
store i32 %24, i32* %7
%25 = icmp eq i32 -1, %24
br i1 %25, label %26, label %27
26: ; preds = %18
store i32 -1, i32* %3
br label %43
27: ; preds = %18
%28 = call i32 (...) @__darwintrace_close()
%29 = load i32, i32* %7
%30 = call i32* @fdopen(i32 %29, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %30, i32** %8
%31 = icmp eq i32* null, %30
br i1 %31, label %32, label %35
32: ; preds = %27
%33 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
%34 = call i32 (...) @abort()
unreachable
35: ; preds = %27
%36 = load i32*, i32** %8
%37 = call i32 @__darwintrace_sock_set(i32* %36)
br label %38
38: ; preds = %35, %13, %2
%39 = load i32, i32* @SYS_dup2
%40 = load i32, i32* %4
%41 = load i32, i32* %5
%42 = call i32 @syscall(i32 %39, i32 %40, i32 %41)
store i32 %42, i32* %3
br label %43
43: ; preds = %38, %26
%44 = load i32, i32* %3
ret i32 %44
}

LLVM IR (-O2):
@F_DUPFD = global i32 0
@STDOUT_FILENO = global i64 0
@.str = [3 x i8] c"a+\00"
@.str.1 = [20 x i8] c"darwintrace: fdopen\00"
@SYS_dup2 = global i32 0
define i32 @_dt_dup2(i32 %0, i32 %1) {
%3 = tail call i32 (...) @__darwintrace_setup()
%4 = tail call i32* (...) @__darwintrace_sock()
%5 = icmp eq i32* %4, null
br i1 %5, label %25, label %6
6: ; preds = %2
%7 = tail call i32 @fileno(i32* %4)
%8 = icmp eq i32 %7, %1
br i1 %8, label %9, label %25
9: ; preds = %6
%10 = tail call i32 @fileno(i32* %4)
%11 = load i32, i32* @F_DUPFD
%12 = load i64, i64* @STDOUT_FILENO
%13 = add nsw i64 %12, 1
%14 = tail call i32 @fcntl(i32 %10, i32 %11, i64 %13)
%15 = icmp eq i32 %14, -1
br i1 %15, label %28, label %16
16: ; preds = %9
%17 = tail call i32 (...) @__darwintrace_close()
%18 = tail call i32* @fdopen(i32 %14, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%19 = icmp eq i32* %18, null
br i1 %19, label %20, label %23
20: ; preds = %16
%21 = tail call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
%22 = tail call i32 (...) @abort()
unreachable
23: ; preds = %16
%24 = tail call i32 @__darwintrace_sock_set(i32* %18)
br label %25
25: ; preds = %23, %6, %2
%26 = load i32, i32* @SYS_dup2
%27 = tail call i32 @syscall(i32 %26, i32 %0, i32 %1)
br label %28
28: ; preds = %9, %25
%29 = phi i32 [ %27, %25 ], [ -1, %9 ]
ret i32 %29
}