#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 dup(int);
int dup2(int, int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int main(int argc, char **argv) {
  if (dup(STDOUT_FILENO) == -1) {
    fprintf(stderr, "dup: %s\n", strerror(errno));
    return 1;
  }
  if (dup2(STDOUT_FILENO, 4) == -1) {
    fprintf(stderr, "dup2: %s\n", strerror(errno));
    return 1;
  }
  if (dup2(4, 4) != 4) {
    fprintf(stderr, "dup2: It should have returned the same fd\n");
    return 1;
  }
  if (dup2(5, 6) != -1) {
    fprintf(stderr, "dup2: It should have failed\n");
    return 1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e93(%rip),%rax
mov    (%rax),%edi
call   1040 <dup@plt>
cmp    $0xffffffff,%eax
jne    11a7 <main+0x67>
mov    0x2e64(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    0x2e50(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
mov    -0x14(%rbp),%edi
mov    %rax,%rdx
lea    0xe6c(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1277 <main+0x137>
mov    0x2e42(%rip),%rax
mov    (%rax),%edi
mov    $0x4,%esi
call   1030 <dup2@plt>
cmp    $0xffffffff,%eax
jne    11fd <main+0xbd>
mov    0x2e0e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    0x2dfa(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
mov    -0x18(%rbp),%edi
mov    %rax,%rdx
lea    0xe1f(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1277 <main+0x137>
mov    $0x4,%esi
mov    %esi,%edi
call   1030 <dup2@plt>
cmp    $0x4,%eax
je     1235 <main+0xf5>
mov    0x2dbf(%rip),%rax
mov    (%rax),%edi
lea    0xdf1(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1277 <main+0x137>
mov    $0x5,%edi
mov    $0x6,%esi
call   1030 <dup2@plt>
cmp    $0xffffffff,%eax
je     1270 <main+0x130>
mov    0x2d84(%rip),%rax
mov    (%rax),%edi
lea    0xde1(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1277 <main+0x137>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
mov    0x2ea8(%rip),%rbx
mov    (%rbx),%edi
call   1040 <dup@plt>
cmp    $0xffffffff,%eax
je     119f <main+0x5f>
mov    (%rbx),%edi
mov    $0x4,%esi
call   1030 <dup2@plt>
cmp    $0xffffffff,%eax
je     11bf <main+0x7f>
mov    $0x4,%edi
mov    $0x4,%esi
call   1030 <dup2@plt>
cmp    $0x4,%eax
jne    11eb <main+0xab>
mov    $0x5,%edi
mov    $0x6,%esi
call   1030 <dup2@plt>
cmp    $0xffffffff,%eax
je     1209 <main+0xc9>
mov    0x2e44(%rip),%rax
mov    (%rax),%edi
lea    0xea1(%rip),%rsi
jmp    11fb <main+0xbb>
mov    0x2e32(%rip),%rax
mov    (%rax),%ebx
mov    0x2e21(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
lea    0xe43(%rip),%rsi
jmp    11dd <main+0x9d>
mov    0x2e12(%rip),%rax
mov    (%rax),%ebx
mov    0x2e01(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
lea    0xe2c(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   1050 <fprintf@plt>
jmp    1202 <main+0xc2>
mov    0x2de6(%rip),%rax
mov    (%rax),%edi
lea    0xe18(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    $0x1,%eax
pop    %rbx
ret
xor    %eax,%eax
pop    %rbx
ret

LLVM IR (-O0):
@STDOUT_FILENO = global i32 0
@stderr = global i32 0
@.str = [9 x i8] c"dup: %s\0A\00"
@errno = global i32 0
@.str.1 = [10 x i8] c"dup2: %s\0A\00"
@.str.2 = [43 x i8] c"dup2: It should have returned the same fd\0A\00"
@.str.3 = [29 x i8] c"dup2: It should have failed\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%6 = load i32, i32* @STDOUT_FILENO
%7 = call i32 @dup(i32 %6)
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %14
9: ; preds = %2
%10 = load i32, i32* @stderr
%11 = load i32, i32* @errno
%12 = call i8* @strerror(i32 %11)
%13 = call i32 (i32, i8*, ...) @fprintf(i32 %10, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* %12)
store i32 1, i32* %3
br label %36
14: ; preds = %2
%15 = load i32, i32* @STDOUT_FILENO
%16 = call i32 @dup2(i32 %15, i32 4)
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %23
18: ; preds = %14
%19 = load i32, i32* @stderr
%20 = load i32, i32* @errno
%21 = call i8* @strerror(i32 %20)
%22 = call i32 (i32, i8*, ...) @fprintf(i32 %19, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8* %21)
store i32 1, i32* %3
br label %36
23: ; preds = %14
%24 = call i32 @dup2(i32 4, i32 4)
%25 = icmp ne i32 %24, 4
br i1 %25, label %26, label %29
26: ; preds = %23
%27 = load i32, i32* @stderr
%28 = call i32 (i32, i8*, ...) @fprintf(i32 %27, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %3
br label %36
29: ; preds = %23
%30 = call i32 @dup2(i32 5, i32 6)
%31 = icmp ne i32 %30, -1
br i1 %31, label %32, label %35
32: ; preds = %29
%33 = load i32, i32* @stderr
%34 = call i32 (i32, i8*, ...) @fprintf(i32 %33, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0))
store i32 1, i32* %3
br label %36
35: ; preds = %29
store i32 0, i32* %3
br label %36
36: ; preds = %35, %32, %26, %18, %9
%37 = load i32, i32* %3
ret i32 %37
}

LLVM IR (-O2):
@STDOUT_FILENO = global i32 0
@stderr = global i32 0
@.str = [9 x i8] c"dup: %s\0A\00"
@errno = global i32 0
@.str.1 = [10 x i8] c"dup2: %s\0A\00"
@.str.2 = [43 x i8] c"dup2: It should have returned the same fd\0A\00"
@.str.3 = [29 x i8] c"dup2: It should have failed\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = load i32, i32* @STDOUT_FILENO
%4 = tail call i32 @dup(i32 %3)
%5 = icmp eq i32 %4, -1
br i1 %5, label %6, label %11
6: ; preds = %2
%7 = load i32, i32* @stderr
%8 = load i32, i32* @errno
%9 = tail call i8* @strerror(i32 %8)
%10 = tail call i32 (i32, i8*, ...) @fprintf(i32 %7, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* %9)
br label %32
11: ; preds = %2
%12 = load i32, i32* @STDOUT_FILENO
%13 = tail call i32 @dup2(i32 %12, i32 4)
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %20
15: ; preds = %11
%16 = load i32, i32* @stderr
%17 = load i32, i32* @errno
%18 = tail call i8* @strerror(i32 %17)
%19 = tail call i32 (i32, i8*, ...) @fprintf(i32 %16, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8* %18)
br label %32
20: ; preds = %11
%21 = tail call i32 @dup2(i32 4, i32 4)
%22 = icmp eq i32 %21, 4
br i1 %22, label %26, label %23
23: ; preds = %20
%24 = load i32, i32* @stderr
%25 = tail call i32 (i32, i8*, ...) @fprintf(i32 %24, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.2, i64 0, i64 0))
br label %32
26: ; preds = %20
%27 = tail call i32 @dup2(i32 5, i32 6)
%28 = icmp eq i32 %27, -1
br i1 %28, label %32, label %29
29: ; preds = %26
%30 = load i32, i32* @stderr
%31 = tail call i32 (i32, i8*, ...) @fprintf(i32 %30, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0))
br label %32
32: ; preds = %26, %29, %23, %15, %6
%33 = phi i32 [ 1, %6 ], [ 1, %15 ], [ 1, %23 ], [ 1, %29 ], [ 0, %26 ]
ret i32 %33
}