#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*/ abort();
int /*<<< orphan*/ assert(int);
int /*<<< orphan*/ close(int);
int dup(int);
int errno;
int dup_at_least_3(int old) {
  int intermediary[3];
  int i = 0, saved_errno;
  do {
    do {
      if (old = dup(old), old == -1)
        goto fail;
    } while (0);
    assert(i < 3);
    if (i >= 3)
      abort();
    intermediary[i++] = old;
  } while (old < 3);
  i--;
fail:
  saved_errno = errno;
  while (i--)
    close(intermediary[i]);
  errno = saved_errno;
  return old;
}


x86 assembly (clang, -O0):
<dup_at_least_3>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
movl   $0x0,-0x14(%rbp)
jmp    1157 <dup_at_least_3+0x17>
mov    -0x4(%rbp),%edi
call   1040 <dup@plt>
mov    %eax,-0x4(%rbp)
cmpl   $0xffffffff,-0x4(%rbp)
jne    1171 <dup_at_least_3+0x31>
jmp    11bf <dup_at_least_3+0x7f>
jmp    1176 <dup_at_least_3+0x36>
cmpl   $0x3,-0x14(%rbp)
setl   %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
cmpl   $0x3,-0x14(%rbp)
jl     1198 <dup_at_least_3+0x58>
mov    $0x0,%al
call   1030 <abort@plt>
mov    -0x4(%rbp),%ecx
mov    -0x14(%rbp),%eax
mov    %eax,%edx
add    $0x1,%edx
mov    %edx,-0x14(%rbp)
cltq
mov    %ecx,-0x10(%rbp,%rax,4)
cmpl   $0x3,-0x4(%rbp)
jl     1152 <dup_at_least_3+0x12>
mov    -0x14(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x14(%rbp)
mov    0x2e1a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,%ecx
add    $0xffffffff,%ecx
mov    %ecx,-0x14(%rbp)
cmp    $0x0,%eax
je     11f1 <dup_at_least_3+0xb1>
movslq -0x14(%rbp),%rax
mov    -0x10(%rbp,%rax,4),%edi
call   1050 <close@plt>
jmp    11cb <dup_at_least_3+0x8b>
mov    -0x18(%rbp),%ecx
mov    0x2de5(%rip),%rax
mov    %ecx,(%rax)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dup_at_least_3>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
call   1040 <dup@plt>
mov    %eax,%r15d
cmp    $0xffffffff,%eax
je     11ea <dup_at_least_3+0xaa>
mov    $0x1,%edi
call   1060 <assert@plt>
mov    %r15d,0xc(%rsp)
cmp    $0x3,%r15d
jge    11ea <dup_at_least_3+0xaa>
mov    %r15d,%edi
call   1040 <dup@plt>
mov    $0xffffffff,%r15d
mov    $0x1,%ebp
cmp    $0xffffffff,%eax
je     11f8 <dup_at_least_3+0xb8>
mov    %eax,%r14d
mov    $0x1,%edi
call   1060 <assert@plt>
mov    %r14d,0x10(%rsp)
cmp    $0x2,%r14d
jg     11f5 <dup_at_least_3+0xb5>
mov    %r14d,%edi
call   1040 <dup@plt>
mov    $0x2,%ebp
cmp    $0xffffffff,%eax
je     11f8 <dup_at_least_3+0xb8>
mov    %eax,%r14d
mov    $0x1,%edi
call   1060 <assert@plt>
mov    %r14d,0x14(%rsp)
cmp    $0x2,%r14d
jg     11f5 <dup_at_least_3+0xb5>
mov    %r14d,%edi
call   1040 <dup@plt>
mov    $0x3,%ebp
cmp    $0xffffffff,%eax
je     11f8 <dup_at_least_3+0xb8>
xor    %edi,%edi
call   1060 <assert@plt>
xor    %eax,%eax
call   1030 <abort@plt>
mov    0x2def(%rip),%rax
mov    (%rax),%ebx
jmp    121f <dup_at_least_3+0xdf>
mov    %r14d,%r15d
mov    0x2de1(%rip),%rax
mov    (%rax),%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    0x8(%rsp,%rbp,4),%edi
call   1050 <close@plt>
add    $0xffffffffffffffff,%rbp
jne    1210 <dup_at_least_3+0xd0>
mov    0x2dba(%rip),%rax
mov    %ebx,(%rax)
mov    %r15d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@errno = global i32 0
define i32 @dup_at_least_3(i32 %0) {
%2 = alloca i32
%3 = alloca [3 x i32]
%4 = alloca i32
%5 = alloca i32
store i32 %0, i32* %2
store i32 0, i32* %4
br label %6
6: ; preds = %29, %1
br label %7
7: ; preds = %6
%8 = load i32, i32* %2
%9 = call i32 @dup(i32 %8)
store i32 %9, i32* %2
%10 = load i32, i32* %2
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %13
12: ; preds = %7
br label %35
13: ; preds = %7
br label %14
14: ; preds = %13
%15 = load i32, i32* %4
%16 = icmp slt i32 %15, 3
%17 = zext i1 %16 to i32
%18 = call i32 @assert(i32 %17)
%19 = load i32, i32* %4
%20 = icmp sge i32 %19, 3
br i1 %20, label %21, label %23
21: ; preds = %14
%22 = call i32 (...) @abort()
unreachable
23: ; preds = %14
%24 = load i32, i32* %2
%25 = load i32, i32* %4
%26 = add nsw i32 %25, 1
store i32 %26, i32* %4
%27 = sext i32 %25 to i64
%28 = getelementptr inbounds [3 x i32], [3 x i32]* %3, i64 0, i64 %27
store i32 %24, i32* %28
br label %29
29: ; preds = %23
%30 = load i32, i32* %2
%31 = icmp slt i32 %30, 3
br i1 %31, label %6, label %32
32: ; preds = %29
%33 = load i32, i32* %4
%34 = add nsw i32 %33, -1
store i32 %34, i32* %4
br label %35
35: ; preds = %32, %12
%36 = load i32, i32* @errno
store i32 %36, i32* %5
br label %37
37: ; preds = %41, %35
%38 = load i32, i32* %4
%39 = add nsw i32 %38, -1
store i32 %39, i32* %4
%40 = icmp ne i32 %38, 0
br i1 %40, label %41, label %47
41: ; preds = %37
%42 = load i32, i32* %4
%43 = sext i32 %42 to i64
%44 = getelementptr inbounds [3 x i32], [3 x i32]* %3, i64 0, i64 %43
%45 = load i32, i32* %44
%46 = call i32 @close(i32 %45)
br label %37
47: ; preds = %37
%48 = load i32, i32* %5
store i32 %48, i32* @errno
%49 = load i32, i32* %2
ret i32 %49
}

LLVM IR (-O2):
@errno = global i32 0
define i32 @dup_at_least_3(i32 %0) {
%2 = alloca [3 x i32]
%3 = bitcast [3 x i32]* %2 to i8*
%4 = tail call i32 @dup(i32 %0)
%5 = icmp eq i32 %4, -1
br i1 %5, label %30, label %6
6: ; preds = %1
%7 = tail call i32 @assert(i32 1)
%8 = getelementptr inbounds [3 x i32], [3 x i32]* %2, i64 0, i64 0
store i32 %4, i32* %8
%9 = icmp slt i32 %4, 3
br i1 %9, label %13, label %30
10: ; preds = %27
%11 = tail call i32 @assert(i32 0)
%12 = tail call i32 (...) @abort()
unreachable
13: ; preds = %6
%14 = tail call i32 @dup(i32 %4)
%15 = icmp eq i32 %14, -1
br i1 %15, label %32, label %16
16: ; preds = %13
%17 = tail call i32 @assert(i32 1)
%18 = getelementptr inbounds [3 x i32], [3 x i32]* %2, i64 0, i64 1
store i32 %14, i32* %18
%19 = icmp slt i32 %14, 3
br i1 %19, label %20, label %32
20: ; preds = %16
%21 = tail call i32 @dup(i32 %14)
%22 = icmp eq i32 %21, -1
br i1 %22, label %32, label %23
23: ; preds = %20
%24 = tail call i32 @assert(i32 1)
%25 = getelementptr inbounds [3 x i32], [3 x i32]* %2, i64 0, i64 2
store i32 %21, i32* %25
%26 = icmp slt i32 %21, 3
br i1 %26, label %27, label %32
27: ; preds = %23
%28 = tail call i32 @dup(i32 %21)
%29 = icmp eq i32 %28, -1
br i1 %29, label %32, label %10
30: ; preds = %6, %1
%31 = load i32, i32* @errno
br label %43
32: ; preds = %27, %23, %20, %16, %13
%33 = phi i64 [ 1, %13 ], [ 1, %16 ], [ 2, %20 ], [ 2, %23 ], [ 3, %27 ]
%34 = phi i32 [ -1, %13 ], [ %14, %16 ], [ -1, %20 ], [ %21, %23 ], [ -1, %27 ]
%35 = load i32, i32* @errno
br label %36
36: ; preds = %32, %36
%37 = phi i64 [ %33, %32 ], [ %38, %36 ]
%38 = add nsw i64 %37, -1
%39 = getelementptr inbounds [3 x i32], [3 x i32]* %2, i64 0, i64 %38
%40 = load i32, i32* %39
%41 = tail call i32 @close(i32 %40)
%42 = icmp eq i64 %38, 0
br i1 %42, label %43, label %36
43: ; preds = %36, %30
%44 = phi i32 [ %31, %30 ], [ %35, %36 ]
%45 = phi i32 [ %4, %30 ], [ %34, %36 ]
store i32 %44, i32* @errno
ret i32 %45
}