#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 dup(int);
int errno;
int /*<<< orphan*/ fflush(int /*<<< orphan*/ *);
int handle_gez(int);
int /*<<< orphan*/ printf(char *, ...);
int dup_test(int fd) {
  int newfd;
  int save_errno;
  int passed;
  printf("dup():fd=%d\n", fd);
  fflush(NULL);
  passed = handle_gez(newfd = dup(fd));
  save_errno = errno;
  switch (passed) {
  case -1:
  case 0:
    printf("\t\tnewfd = %d\n", newfd);
    printf("\tSucceeded Phase 1\n");
    fflush(NULL);
    break;
  case -2:
    printf("\tFailed Phase 1: returned undefined value! "
           "newfd = %d\n",
           newfd);
    fflush(NULL);
    break;
  default:
    printf("Internal syscalltester error: passed = %d\n", passed);
    fflush(NULL);
    break;
  }
  errno = save_errno;
  return newfd;
}


x86 assembly (clang, -O0):
<dup_test>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%esi
lea    0xeab(%rip),%rdi
xor    %eax,%eax
mov    %eax,-0x18(%rbp)
call   1030 <printf@plt>
mov    -0x18(%rbp),%eax
mov    %eax,%edi
call   1050 <fflush@plt>
mov    -0x4(%rbp),%edi
call   1040 <dup@plt>
mov    %eax,%edi
mov    %edi,-0x8(%rbp)
call   1060 <handle_gez@plt>
mov    %eax,-0x10(%rbp)
mov    0x2e5b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xc(%rbp)
mov    -0x10(%rbp),%eax
mov    %eax,-0x14(%rbp)
sub    $0xfffffffe,%eax
je     11df <dup_test+0x9f>
jmp    119e <dup_test+0x5e>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
sub    $0x1,%eax
ja     11fe <dup_test+0xbe>
jmp    11b2 <dup_test+0x72>
mov    -0x8(%rbp),%esi
lea    0xe51(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xe51(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1050 <fflush@plt>
jmp    1218 <dup_test+0xd8>
mov    -0x8(%rbp),%esi
lea    0xe46(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1050 <fflush@plt>
jmp    1218 <dup_test+0xd8>
mov    -0x10(%rbp),%esi
lea    0xe5e(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1050 <fflush@plt>
mov    -0xc(%rbp),%ecx
mov    0x2dbe(%rip),%rax
mov    %ecx,(%rax)
mov    -0x8(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dup_test>:
push   %rbp
push   %r14
push   %rbx
mov    %edi,%ebx
lea    0xea3(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1040 <printf@plt>
xor    %edi,%edi
call   1060 <fflush@plt>
mov    %ebx,%edi
call   1050 <dup@plt>
mov    %eax,%ebx
mov    %eax,%edi
call   1070 <handle_gez@plt>
mov    0x2e5c(%rip),%r14
mov    (%r14),%ebp
lea    0x1(%rax),%ecx
cmp    $0x2,%ecx
jae    11ad <dup_test+0x5d>
lea    0xe77(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xed7(%rip),%rdi
call   1030 <puts@plt>
jmp    11cd <dup_test+0x7d>
cmp    $0xfffffffe,%eax
jne    11bd <dup_test+0x6d>
lea    0xe62(%rip),%rdi
mov    %ebx,%esi
jmp    11c6 <dup_test+0x76>
lea    0xe8e(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
xor    %edi,%edi
call   1060 <fflush@plt>
mov    %ebp,(%r14)
mov    %ebx,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [13 x i8] c"dup():fd=%d\0A\00"
@errno = global i32 0
@.str.1 = [14 x i8] c"\09\09newfd = %d\0A\00"
@.str.2 = [20 x i8] c"\09Succeeded Phase 1\0A\00"
@.str.3 = [55 x i8] c"\09Failed Phase 1: returned undefined value! newfd = %d\0A\00"
@.str.4 = [43 x i8] c"Internal syscalltester error: passed = %d\0A\00"
define i32 @dup_test(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
store i32 %0, i32* %2
%6 = load i32, i32* %2
%7 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %6)
%8 = call i32 @fflush(i32* null)
%9 = load i32, i32* %2
%10 = call i32 @dup(i32 %9)
store i32 %10, i32* %3
%11 = call i32 @handle_gez(i32 %10)
store i32 %11, i32* %5
%12 = load i32, i32* @errno
store i32 %12, i32* %4
%13 = load i32, i32* %5
switch i32 %13, label %23 [
i32 -1, label %14
i32 0, label %14
i32 -2, label %19
]
14: ; preds = %1, %1
%15 = load i32, i32* %3
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i32 %15)
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0))
%18 = call i32 @fflush(i32* null)
br label %27
19: ; preds = %1
%20 = load i32, i32* %3
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str.3, i64 0, i64 0), i32 %20)
%22 = call i32 @fflush(i32* null)
br label %27
23: ; preds = %1
%24 = load i32, i32* %5
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.4, i64 0, i64 0), i32 %24)
%26 = call i32 @fflush(i32* null)
br label %27
27: ; preds = %23, %19, %14
%28 = load i32, i32* %4
store i32 %28, i32* @errno
%29 = load i32, i32* %3
ret i32 %29
}

LLVM IR (-O2):
@.str = [13 x i8] c"dup():fd=%d\0A\00"
@errno = global i32 0
@.str.1 = [14 x i8] c"\09\09newfd = %d\0A\00"
@.str.3 = [55 x i8] c"\09Failed Phase 1: returned undefined value! newfd = %d\0A\00"
@.str.4 = [43 x i8] c"Internal syscalltester error: passed = %d\0A\00"
@str = [19 x i8] c"\09Succeeded Phase 1\00"
define i32 @dup_test(i32 %0) {
%2 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %0)
%3 = tail call i32 @fflush(i32* null)
%4 = tail call i32 @dup(i32 %0)
%5 = tail call i32 @handle_gez(i32 %4)
%6 = load i32, i32* @errno
switch i32 %5, label %12 [
i32 -1, label %7
i32 0, label %7
i32 -2, label %10
]
7: ; preds = %1, %1
%8 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i32 %4)
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str, i64 0, i64 0))
br label %14
10: ; preds = %1
%11 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([55 x i8], [55 x i8]* @.str.3, i64 0, i64 0), i32 %4)
br label %14
12: ; preds = %1
%13 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([43 x i8], [43 x i8]* @.str.4, i64 0, i64 0), i32 %5)
br label %14
14: ; preds = %12, %10, %7
%15 = tail call i32 @fflush(i32* null)
store i32 %6, i32* @errno
ret i32 %4
}