#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 {
    if (old = dup(old), old == -1)
      goto fail;
    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)
mov    -0x4(%rbp),%edi
call   1040 <dup@plt>
mov    %eax,-0x4(%rbp)
cmpl   $0xffffffff,-0x4(%rbp)
jne    116c <dup_at_least_3+0x2c>
jmp    11b5 <dup_at_least_3+0x75>
cmpl   $0x3,-0x14(%rbp)
setl   %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
cmpl   $0x3,-0x14(%rbp)
jl     118e <dup_at_least_3+0x4e>
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    0x2e24(%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     11e7 <dup_at_least_3+0xa7>
movslq -0x14(%rbp),%rax
mov    -0x10(%rbp,%rax,4),%edi
call   1050 <close@plt>
jmp    11c1 <dup_at_least_3+0x81>
mov    -0x18(%rbp),%ecx
mov    0x2def(%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 = %27, %1
%7 = load i32, i32* %2
%8 = call i32 @dup(i32 %7)
store i32 %8, i32* %2
%9 = load i32, i32* %2
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %12
11: ; preds = %6
br label %33
12: ; preds = %6
%13 = load i32, i32* %4
%14 = icmp slt i32 %13, 3
%15 = zext i1 %14 to i32
%16 = call i32 @assert(i32 %15)
%17 = load i32, i32* %4
%18 = icmp sge i32 %17, 3
br i1 %18, label %19, label %21
19: ; preds = %12
%20 = call i32 (...) @abort()
unreachable
21: ; preds = %12
%22 = load i32, i32* %2
%23 = load i32, i32* %4
%24 = add nsw i32 %23, 1
store i32 %24, i32* %4
%25 = sext i32 %23 to i64
%26 = getelementptr inbounds [3 x i32], [3 x i32]* %3, i64 0, i64 %25
store i32 %22, i32* %26
br label %27
27: ; preds = %21
%28 = load i32, i32* %2
%29 = icmp slt i32 %28, 3
br i1 %29, label %6, label %30
30: ; preds = %27
%31 = load i32, i32* %4
%32 = add nsw i32 %31, -1
store i32 %32, i32* %4
br label %33
33: ; preds = %30, %11
%34 = load i32, i32* @errno
store i32 %34, i32* %5
br label %35
35: ; preds = %39, %33
%36 = load i32, i32* %4
%37 = add nsw i32 %36, -1
store i32 %37, i32* %4
%38 = icmp ne i32 %36, 0
br i1 %38, label %39, label %45
39: ; preds = %35
%40 = load i32, i32* %4
%41 = sext i32 %40 to i64
%42 = getelementptr inbounds [3 x i32], [3 x i32]* %3, i64 0, i64 %41
%43 = load i32, i32* %42
%44 = call i32 @close(i32 %43)
br label %35
45: ; preds = %35
%46 = load i32, i32* %5
store i32 %46, i32* @errno
%47 = load i32, i32* %2
ret i32 %47
}

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
}