#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*/ buf2;
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int close(int);
int dup2(int, int);
int pipe(int *);
int read(int, char *, int);
int write(int, char *, int);
int tr_run_dup2_master2x_test(int to) {
  int fds[2];
  char buf = 42, buf2;
  if (pipe(fds) == -1)
    return (32);
  if (fds[1] == to)
    return (33);
  if (dup2(fds[1], to) == -1)
    return (34);
  if (write(to, &buf, sizeof(buf)) == -1)
    return (35);
  if (close(to) == -1)
    return (36);
  if (read(fds[0], &buf2, sizeof(buf2)) == -1)
    return (37);
  if (buf2 != buf)
    return (38);
  if (close(fds[0]) == -1)
    return (39);
  if (close(fds[1]) == -1)
    return (40);
  return (0);
}


x86 assembly (clang, -O0):
<tr_run_dup2_master2x_test>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
movb   $0x2a,-0x11(%rbp)
lea    -0x10(%rbp),%rdi
call   1060 <pipe@plt>
cmp    $0xffffffff,%eax
jne    117d <tr_run_dup2_master2x_test+0x2d>
movl   $0x20,-0x4(%rbp)
jmp    127b <tr_run_dup2_master2x_test+0x12b>
mov    -0xc(%rbp),%eax
cmp    -0x8(%rbp),%eax
jne    1195 <tr_run_dup2_master2x_test+0x45>
movl   $0x21,-0x4(%rbp)
jmp    127b <tr_run_dup2_master2x_test+0x12b>
mov    -0xc(%rbp),%edi
mov    -0x8(%rbp),%esi
call   1040 <dup2@plt>
cmp    $0xffffffff,%eax
jne    11b5 <tr_run_dup2_master2x_test+0x65>
movl   $0x22,-0x4(%rbp)
jmp    127b <tr_run_dup2_master2x_test+0x12b>
mov    -0x8(%rbp),%edi
lea    -0x11(%rbp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
jne    11db <tr_run_dup2_master2x_test+0x8b>
movl   $0x23,-0x4(%rbp)
jmp    127b <tr_run_dup2_master2x_test+0x12b>
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
cmp    $0xffffffff,%eax
jne    11f8 <tr_run_dup2_master2x_test+0xa8>
movl   $0x24,-0x4(%rbp)
jmp    127b <tr_run_dup2_master2x_test+0x12b>
mov    -0x10(%rbp),%edi
lea    -0x12(%rbp),%rsi
mov    $0x1,%edx
call   1070 <read@plt>
cmp    $0xffffffff,%eax
jne    121e <tr_run_dup2_master2x_test+0xce>
movl   $0x25,-0x4(%rbp)
jmp    127b <tr_run_dup2_master2x_test+0x12b>
movsbl -0x12(%rbp),%eax
movsbl -0x11(%rbp),%ecx
cmp    %ecx,%eax
je     123a <tr_run_dup2_master2x_test+0xea>
movl   $0x26,-0x4(%rbp)
jmp    127b <tr_run_dup2_master2x_test+0x12b>
mov    -0x10(%rbp),%edi
call   1050 <close@plt>
cmp    $0xffffffff,%eax
jne    1257 <tr_run_dup2_master2x_test+0x107>
movl   $0x27,-0x4(%rbp)
jmp    127b <tr_run_dup2_master2x_test+0x12b>
mov    -0xc(%rbp),%edi
call   1050 <close@plt>
cmp    $0xffffffff,%eax
jne    1274 <tr_run_dup2_master2x_test+0x124>
movl   $0x28,-0x4(%rbp)
jmp    127b <tr_run_dup2_master2x_test+0x12b>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tr_run_dup2_master2x_test>:
push   %rbx
sub    $0x10,%rsp
mov    %edi,%ebx
movb   $0x2a,0x7(%rsp)
lea    0x8(%rsp),%rdi
call   1060 <pipe@plt>
cmp    $0xffffffff,%eax
je     11f8 <tr_run_dup2_master2x_test+0xa8>
mov    0xc(%rsp),%edi
mov    $0x21,%eax
cmp    %ebx,%edi
je     1220 <tr_run_dup2_master2x_test+0xd0>
mov    %ebx,%esi
call   1040 <dup2@plt>
cmp    $0xffffffff,%eax
je     11ff <tr_run_dup2_master2x_test+0xaf>
lea    0x7(%rsp),%rsi
mov    %ebx,%edi
mov    $0x1,%edx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
je     1206 <tr_run_dup2_master2x_test+0xb6>
mov    %ebx,%edi
call   1050 <close@plt>
cmp    $0xffffffff,%eax
je     120d <tr_run_dup2_master2x_test+0xbd>
mov    0x8(%rsp),%edi
lea    0x6(%rsp),%rsi
mov    $0x1,%edx
call   1070 <read@plt>
cmp    $0xffffffff,%eax
je     1214 <tr_run_dup2_master2x_test+0xc4>
mov    $0x26,%eax
cmpb   $0x2a,0x6(%rsp)
jne    1220 <tr_run_dup2_master2x_test+0xd0>
mov    0x8(%rsp),%edi
call   1050 <close@plt>
cmp    $0xffffffff,%eax
je     121b <tr_run_dup2_master2x_test+0xcb>
mov    0xc(%rsp),%edi
call   1050 <close@plt>
xor    %ecx,%ecx
cmp    $0xffffffff,%eax
mov    $0x28,%eax
cmovne %ecx,%eax
jmp    1220 <tr_run_dup2_master2x_test+0xd0>
mov    $0x20,%eax
jmp    1220 <tr_run_dup2_master2x_test+0xd0>
mov    $0x22,%eax
jmp    1220 <tr_run_dup2_master2x_test+0xd0>
mov    $0x23,%eax
jmp    1220 <tr_run_dup2_master2x_test+0xd0>
mov    $0x24,%eax
jmp    1220 <tr_run_dup2_master2x_test+0xd0>
mov    $0x25,%eax
jmp    1220 <tr_run_dup2_master2x_test+0xd0>
mov    $0x27,%eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
define i32 @tr_run_dup2_master2x_test(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca [2 x i32]
%5 = alloca i8
%6 = alloca i8
store i32 %0, i32* %3
store i8 42, i8* %5
%7 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 0
%8 = call i32 @pipe(i32* %7)
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %11
10: ; preds = %1
store i32 32, i32* %2
br label %60
11: ; preds = %1
%12 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 1
%13 = load i32, i32* %12
%14 = load i32, i32* %3
%15 = icmp eq i32 %13, %14
br i1 %15, label %16, label %17
16: ; preds = %11
store i32 33, i32* %2
br label %60
17: ; preds = %11
%18 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 1
%19 = load i32, i32* %18
%20 = load i32, i32* %3
%21 = call i32 @dup2(i32 %19, i32 %20)
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %24
23: ; preds = %17
store i32 34, i32* %2
br label %60
24: ; preds = %17
%25 = load i32, i32* %3
%26 = call i32 @write(i32 %25, i8* %5, i32 1)
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %29
28: ; preds = %24
store i32 35, i32* %2
br label %60
29: ; preds = %24
%30 = load i32, i32* %3
%31 = call i32 @close(i32 %30)
%32 = icmp eq i32 %31, -1
br i1 %32, label %33, label %34
33: ; preds = %29
store i32 36, i32* %2
br label %60
34: ; preds = %29
%35 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 0
%36 = load i32, i32* %35
%37 = call i32 @read(i32 %36, i8* %6, i32 1)
%38 = icmp eq i32 %37, -1
br i1 %38, label %39, label %40
39: ; preds = %34
store i32 37, i32* %2
br label %60
40: ; preds = %34
%41 = load i8, i8* %6
%42 = sext i8 %41 to i32
%43 = load i8, i8* %5
%44 = sext i8 %43 to i32
%45 = icmp ne i32 %42, %44
br i1 %45, label %46, label %47
46: ; preds = %40
store i32 38, i32* %2
br label %60
47: ; preds = %40
%48 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 0
%49 = load i32, i32* %48
%50 = call i32 @close(i32 %49)
%51 = icmp eq i32 %50, -1
br i1 %51, label %52, label %53
52: ; preds = %47
store i32 39, i32* %2
br label %60
53: ; preds = %47
%54 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 1
%55 = load i32, i32* %54
%56 = call i32 @close(i32 %55)
%57 = icmp eq i32 %56, -1
br i1 %57, label %58, label %59
58: ; preds = %53
store i32 40, i32* %2
br label %60
59: ; preds = %53
store i32 0, i32* %2
br label %60
60: ; preds = %59, %58, %52, %46, %39, %33, %28, %23, %16, %10
%61 = load i32, i32* %2
ret i32 %61
}

LLVM IR (-O2):
define i32 @tr_run_dup2_master2x_test(i32 %0) {
%2 = alloca [2 x i32]
%3 = alloca i8
%4 = alloca i8
%5 = bitcast [2 x i32]* %2 to i8*
store i8 42, i8* %3
%6 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%7 = call i32 @pipe(i32* %6)
%8 = icmp eq i32 %7, -1
br i1 %8, label %38, label %9
9: ; preds = %1
%10 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%11 = load i32, i32* %10
%12 = icmp eq i32 %11, %0
br i1 %12, label %38, label %13
13: ; preds = %9
%14 = call i32 @dup2(i32 %11, i32 %0)
%15 = icmp eq i32 %14, -1
br i1 %15, label %38, label %16
16: ; preds = %13
%17 = call i32 @write(i32 %0, i8* %3, i32 1)
%18 = icmp eq i32 %17, -1
br i1 %18, label %38, label %19
19: ; preds = %16
%20 = call i32 @close(i32 %0)
%21 = icmp eq i32 %20, -1
br i1 %21, label %38, label %22
22: ; preds = %19
%23 = load i32, i32* %6
%24 = call i32 @read(i32 %23, i8* %4, i32 1)
%25 = icmp eq i32 %24, -1
br i1 %25, label %38, label %26
26: ; preds = %22
%27 = load i8, i8* %4
%28 = icmp eq i8 %27, 42
br i1 %28, label %29, label %38
29: ; preds = %26
%30 = load i32, i32* %6
%31 = call i32 @close(i32 %30)
%32 = icmp eq i32 %31, -1
br i1 %32, label %38, label %33
33: ; preds = %29
%34 = load i32, i32* %10
%35 = call i32 @close(i32 %34)
%36 = icmp eq i32 %35, -1
%37 = select i1 %36, i32 40, i32 0
br label %38
38: ; preds = %33, %29, %26, %22, %19, %16, %13, %9, %1
%39 = phi i32 [ 32, %1 ], [ 33, %9 ], [ 34, %13 ], [ 35, %16 ], [ 36, %19 ], [ 37, %22 ], [ 38, %26 ], [ 39, %29 ], [ %37, %33 ]
ret i32 %39
}