#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*/ buf;

/* Variables and functions */
int INT_MAX;
int INT_MIN;
int UINT_MAX;
int /*<<< orphan*/ snprintf(char *, int, char *, unsigned int, ...);
int /*<<< orphan*/ test_lookup_fail(char *);
int /*<<< orphan*/ test_lookup_pass(char *);
void test_lookup(unsigned int fd) {
  char buf[64];
  unsigned int c;
  unsigned int u;
  int i;
  snprintf(buf, sizeof(buf), "/proc/self/fd/%u", fd);
  test_lookup_pass(buf);
  for (c = 1; c <= 255; c++) {
    if (c == '/')
      continue;
    snprintf(buf, sizeof(buf), "/proc/self/fd/%c%u", c, fd);
    test_lookup_fail(buf);
  }
  for (c = 1; c <= 255; c++) {
    if (c == '/')
      continue;
    snprintf(buf, sizeof(buf), "/proc/self/fd/%u%c", fd, c);
    test_lookup_fail(buf);
  }
  for (i = INT_MIN; i < INT_MIN + 1024; i++) {
    snprintf(buf, sizeof(buf), "/proc/self/fd/%d", i);
    test_lookup_fail(buf);
  }
  for (i = -1024; i < 0; i++) {
    snprintf(buf, sizeof(buf), "/proc/self/fd/%d", i);
    test_lookup_fail(buf);
  }
  for (u = INT_MAX - 1024; u <= (unsigned int)INT_MAX + 1024; u++) {
    snprintf(buf, sizeof(buf), "/proc/self/fd/%u", u);
    test_lookup_fail(buf);
  }
  for (u = UINT_MAX - 1024; u != 0; u++) {
    snprintf(buf, sizeof(buf), "/proc/self/fd/%u", u);
    test_lookup_fail(buf);
  }
}


x86 assembly (clang, -O0):
<test_lookup>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %edi,-0x4(%rbp)
lea    -0x50(%rbp),%rdi
mov    -0x4(%rbp),%ecx
mov    $0x40,%esi
lea    0xeb2(%rip),%rdx
mov    $0x0,%al
call   1030 <snprintf@plt>
lea    -0x50(%rbp),%rdi
call   1040 <test_lookup_pass@plt>
movl   $0x1,-0x54(%rbp)
cmpl   $0xff,-0x54(%rbp)
ja     11b6 <test_lookup+0x86>
cmpl   $0x2f,-0x54(%rbp)
jne    1181 <test_lookup+0x51>
jmp    11a8 <test_lookup+0x78>
lea    -0x50(%rbp),%rdi
mov    -0x54(%rbp),%ecx
mov    -0x4(%rbp),%r8d
mov    $0x40,%esi
lea    0xe79(%rip),%rdx
mov    $0x0,%al
call   1030 <snprintf@plt>
lea    -0x50(%rbp),%rdi
call   1050 <test_lookup_fail@plt>
mov    -0x54(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x54(%rbp)
jmp    1165 <test_lookup+0x35>
movl   $0x1,-0x54(%rbp)
cmpl   $0xff,-0x54(%rbp)
ja     120e <test_lookup+0xde>
cmpl   $0x2f,-0x54(%rbp)
jne    11d9 <test_lookup+0xa9>
jmp    1200 <test_lookup+0xd0>
lea    -0x50(%rbp),%rdi
mov    -0x4(%rbp),%ecx
mov    -0x54(%rbp),%r8d
mov    $0x40,%esi
lea    0xe34(%rip),%rdx
mov    $0x0,%al
call   1030 <snprintf@plt>
lea    -0x50(%rbp),%rdi
call   1050 <test_lookup_fail@plt>
mov    -0x54(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x54(%rbp)
jmp    11bd <test_lookup+0x8d>
mov    0x2dc3(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x5c(%rbp)
mov    -0x5c(%rbp),%eax
mov    0x2db4(%rip),%rcx
mov    (%rcx),%ecx
add    $0x400,%ecx
cmp    %ecx,%eax
jge    1265 <test_lookup+0x135>
lea    -0x50(%rbp),%rdi
mov    -0x5c(%rbp),%ecx
mov    $0x40,%esi
lea    0xdf0(%rip),%rdx
mov    $0x0,%al
call   1030 <snprintf@plt>
lea    -0x50(%rbp),%rdi
call   1050 <test_lookup_fail@plt>
mov    -0x5c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x5c(%rbp)
jmp    121a <test_lookup+0xea>
movl   $0xfffffc00,-0x5c(%rbp)
cmpl   $0x0,-0x5c(%rbp)
jge    12a7 <test_lookup+0x177>
lea    -0x50(%rbp),%rdi
mov    -0x5c(%rbp),%ecx
mov    $0x40,%esi
lea    0xdae(%rip),%rdx
mov    $0x0,%al
call   1030 <snprintf@plt>
lea    -0x50(%rbp),%rdi
call   1050 <test_lookup_fail@plt>
mov    -0x5c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x5c(%rbp)
jmp    126c <test_lookup+0x13c>
mov    0x2d3a(%rip),%rax
mov    (%rax),%eax
sub    $0x400,%eax
mov    %eax,-0x58(%rbp)
mov    -0x58(%rbp),%eax
mov    0x2d26(%rip),%rcx
mov    (%rcx),%ecx
add    $0x400,%ecx
cmp    %ecx,%eax
ja     1303 <test_lookup+0x1d3>
lea    -0x50(%rbp),%rdi
mov    -0x58(%rbp),%ecx
mov    $0x40,%esi
lea    0xd1b(%rip),%rdx
mov    $0x0,%al
call   1030 <snprintf@plt>
lea    -0x50(%rbp),%rdi
call   1050 <test_lookup_fail@plt>
mov    -0x58(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x58(%rbp)
jmp    12b8 <test_lookup+0x188>
mov    0x2cd6(%rip),%rax
mov    (%rax),%eax
sub    $0x400,%eax
mov    %eax,-0x58(%rbp)
cmpl   $0x0,-0x58(%rbp)
je     134f <test_lookup+0x21f>
lea    -0x50(%rbp),%rdi
mov    -0x58(%rbp),%ecx
mov    $0x40,%esi
lea    0xccf(%rip),%rdx
mov    $0x0,%al
call   1030 <snprintf@plt>
lea    -0x50(%rbp),%rdi
call   1050 <test_lookup_fail@plt>
mov    -0x58(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x58(%rbp)
jmp    1314 <test_lookup+0x1e4>
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_lookup>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x40,%rsp
mov    %edi,%ebx
lea    0xebb(%rip),%rdx
mov    %rsp,%r14
mov    %r14,%rdi
mov    $0x40,%esi
mov    %ebx,%ecx
xor    %eax,%eax
call   1030 <snprintf@plt>
mov    %r14,%rdi
call   1040 <test_lookup_pass@plt>
mov    $0x1,%ebp
lea    0xea4(%rip),%r15
jmp    117b <test_lookup+0x4b>
nop
add    $0x1,%ebp
cmp    $0x100,%ebp
je     11a1 <test_lookup+0x71>
cmp    $0x2f,%ebp
je     1170 <test_lookup+0x40>
mov    %r14,%rdi
mov    $0x40,%esi
mov    %r15,%rdx
mov    %ebp,%ecx
mov    %ebx,%r8d
xor    %eax,%eax
call   1030 <snprintf@plt>
mov    %r14,%rdi
call   1050 <test_lookup_fail@plt>
jmp    1170 <test_lookup+0x40>
mov    $0x1,%ebp
lea    0xe77(%rip),%r14
mov    %rsp,%r15
jmp    11cb <test_lookup+0x9b>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
add    $0x1,%ebp
cmp    $0x100,%ebp
je     11f1 <test_lookup+0xc1>
cmp    $0x2f,%ebp
je     11c0 <test_lookup+0x90>
mov    %r15,%rdi
mov    $0x40,%esi
mov    %r14,%rdx
mov    %ebx,%ecx
mov    %ebp,%r8d
xor    %eax,%eax
call   1030 <snprintf@plt>
mov    %r15,%rdi
call   1050 <test_lookup_fail@plt>
jmp    11c0 <test_lookup+0x90>
mov    0x2de0(%rip),%r15
mov    (%r15),%ebp
lea    0xe35(%rip),%r14
mov    %rsp,%rbx
mov    $0x3ff,%r12d
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    $0x40,%esi
mov    %r14,%rdx
mov    %ebp,%ecx
xor    %eax,%eax
call   1030 <snprintf@plt>
mov    %rbx,%rdi
call   1050 <test_lookup_fail@plt>
lea    0x1(%rbp),%eax
mov    (%r15),%ecx
add    %r12d,%ecx
cmp    %ecx,%ebp
mov    %eax,%ebp
jl     1210 <test_lookup+0xe0>
mov    $0xfffffc00,%ebx
lea    0xdf0(%rip),%r14
mov    %rsp,%rbp
nopw   0x0(%rax,%rax,1)
mov    %rbp,%rdi
mov    $0x40,%esi
mov    %r14,%rdx
mov    %ebx,%ecx
xor    %eax,%eax
call   1030 <snprintf@plt>
mov    %rbp,%rdi
call   1050 <test_lookup_fail@plt>
inc    %ebx
jne    1250 <test_lookup+0x120>
mov    0x2d71(%rip),%r15
mov    (%r15),%eax
lea    -0x400(%rax),%ebx
add    $0x400,%eax
cmp    %eax,%ebx
ja     12c9 <test_lookup+0x199>
lea    0xd70(%rip),%r14
mov    %rsp,%rbp
mov    $0x400,%r12d
nopl   0x0(%rax)
mov    %rbp,%rdi
mov    $0x40,%esi
mov    %r14,%rdx
mov    %ebx,%ecx
xor    %eax,%eax
call   1030 <snprintf@plt>
mov    %rbp,%rdi
call   1050 <test_lookup_fail@plt>
add    $0x1,%ebx
mov    (%r15),%eax
add    %r12d,%eax
cmp    %eax,%ebx
jbe    12a0 <test_lookup+0x170>
mov    0x2d10(%rip),%rax
mov    $0xfffffc00,%ebx
add    (%rax),%ebx
je     1310 <test_lookup+0x1e0>
lea    0xd20(%rip),%r14
mov    %rsp,%rbp
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %rbp,%rdi
mov    $0x40,%esi
mov    %r14,%rdx
mov    %ebx,%ecx
xor    %eax,%eax
call   1030 <snprintf@plt>
mov    %rbp,%rdi
call   1050 <test_lookup_fail@plt>
inc    %ebx
jne    12f0 <test_lookup+0x1c0>
add    $0x40,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [17 x i8] c"/proc/self/fd/%u\00"
@.str.1 = [19 x i8] c"/proc/self/fd/%c%u\00"
@.str.2 = [19 x i8] c"/proc/self/fd/%u%c\00"
@INT_MIN = global i32 0
@.str.3 = [17 x i8] c"/proc/self/fd/%d\00"
@INT_MAX = global i32 0
@UINT_MAX = global i32 0
define void @test_lookup(i32 %0) {
%2 = alloca i32
%3 = alloca [64 x i8]
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i32 %0, i32* %2
%7 = getelementptr inbounds [64 x i8], [64 x i8]* %3, i64 0, i64 0
%8 = load i32, i32* %2
%9 = call i32 (i8*, i32, i8*, i32, ...) @snprintf(i8* %7, i32 64, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %8)
%10 = getelementptr inbounds [64 x i8], [64 x i8]* %3, i64 0, i64 0
%11 = call i32 @test_lookup_pass(i8* %10)
store i32 1, i32* %4
br label %12
12: ; preds = %26, %1
%13 = load i32, i32* %4
%14 = icmp ule i32 %13, 255
br i1 %14, label %15, label %29
15: ; preds = %12
%16 = load i32, i32* %4
%17 = icmp eq i32 %16, 47
br i1 %17, label %18, label %19
18: ; preds = %15
br label %26
19: ; preds = %15
%20 = getelementptr inbounds [64 x i8], [64 x i8]* %3, i64 0, i64 0
%21 = load i32, i32* %4
%22 = load i32, i32* %2
%23 = call i32 (i8*, i32, i8*, i32, ...) @snprintf(i8* %20, i32 64, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i32 %21, i32 %22)
%24 = getelementptr inbounds [64 x i8], [64 x i8]* %3, i64 0, i64 0
%25 = call i32 @test_lookup_fail(i8* %24)
br label %26
26: ; preds = %19, %18
%27 = load i32, i32* %4
%28 = add i32 %27, 1
store i32 %28, i32* %4
br label %12
29: ; preds = %12
store i32 1, i32* %4
br label %30
30: ; preds = %44, %29
%31 = load i32, i32* %4
%32 = icmp ule i32 %31, 255
br i1 %32, label %33, label %47
33: ; preds = %30
%34 = load i32, i32* %4
%35 = icmp eq i32 %34, 47
br i1 %35, label %36, label %37
36: ; preds = %33
br label %44
37: ; preds = %33
%38 = getelementptr inbounds [64 x i8], [64 x i8]* %3, i64 0, i64 0
%39 = load i32, i32* %2
%40 = load i32, i32* %4
%41 = call i32 (i8*, i32, i8*, i32, ...) @snprintf(i8* %38, i32 64, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i32 %39, i32 %40)
%42 = getelementptr inbounds [64 x i8], [64 x i8]* %3, i64 0, i64 0
%43 = call i32 @test_lookup_fail(i8* %42)
br label %44
44: ; preds = %37, %36
%45 = load i32, i32* %4
%46 = add i32 %45, 1
store i32 %46, i32* %4
br label %30
47: ; preds = %30
%48 = load i32, i32* @INT_MIN
store i32 %48, i32* %6
br label %49
49: ; preds = %60, %47
%50 = load i32, i32* %6
%51 = load i32, i32* @INT_MIN
%52 = add nsw i32 %51, 1024
%53 = icmp slt i32 %50, %52
br i1 %53, label %54, label %63
54: ; preds = %49
%55 = getelementptr inbounds [64 x i8], [64 x i8]* %3, i64 0, i64 0
%56 = load i32, i32* %6
%57 = call i32 (i8*, i32, i8*, i32, ...) @snprintf(i8* %55, i32 64, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0), i32 %56)
%58 = getelementptr inbounds [64 x i8], [64 x i8]* %3, i64 0, i64 0
%59 = call i32 @test_lookup_fail(i8* %58)
br label %60
60: ; preds = %54
%61 = load i32, i32* %6
%62 = add nsw i32 %61, 1
store i32 %62, i32* %6
br label %49
63: ; preds = %49
store i32 -1024, i32* %6
br label %64
64: ; preds = %73, %63
%65 = load i32, i32* %6
%66 = icmp slt i32 %65, 0
br i1 %66, label %67, label %76
67: ; preds = %64
%68 = getelementptr inbounds [64 x i8], [64 x i8]* %3, i64 0, i64 0
%69 = load i32, i32* %6
%70 = call i32 (i8*, i32, i8*, i32, ...) @snprintf(i8* %68, i32 64, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0), i32 %69)
%71 = getelementptr inbounds [64 x i8], [64 x i8]* %3, i64 0, i64 0
%72 = call i32 @test_lookup_fail(i8* %71)
br label %73
73: ; preds = %67
%74 = load i32, i32* %6
%75 = add nsw i32 %74, 1
store i32 %75, i32* %6
br label %64
76: ; preds = %64
%77 = load i32, i32* @INT_MAX
%78 = sub nsw i32 %77, 1024
store i32 %78, i32* %5
br label %79
79: ; preds = %90, %76
%80 = load i32, i32* %5
%81 = load i32, i32* @INT_MAX
%82 = add i32 %81, 1024
%83 = icmp ule i32 %80, %82
br i1 %83, label %84, label %93
84: ; preds = %79
%85 = getelementptr inbounds [64 x i8], [64 x i8]* %3, i64 0, i64 0
%86 = load i32, i32* %5
%87 = call i32 (i8*, i32, i8*, i32, ...) @snprintf(i8* %85, i32 64, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %86)
%88 = getelementptr inbounds [64 x i8], [64 x i8]* %3, i64 0, i64 0
%89 = call i32 @test_lookup_fail(i8* %88)
br label %90
90: ; preds = %84
%91 = load i32, i32* %5
%92 = add i32 %91, 1
store i32 %92, i32* %5
br label %79
93: ; preds = %79
%94 = load i32, i32* @UINT_MAX
%95 = sub nsw i32 %94, 1024
store i32 %95, i32* %5
br label %96
96: ; preds = %105, %93
%97 = load i32, i32* %5
%98 = icmp ne i32 %97, 0
br i1 %98, label %99, label %108
99: ; preds = %96
%100 = getelementptr inbounds [64 x i8], [64 x i8]* %3, i64 0, i64 0
%101 = load i32, i32* %5
%102 = call i32 (i8*, i32, i8*, i32, ...) @snprintf(i8* %100, i32 64, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %101)
%103 = getelementptr inbounds [64 x i8], [64 x i8]* %3, i64 0, i64 0
%104 = call i32 @test_lookup_fail(i8* %103)
br label %105
105: ; preds = %99
%106 = load i32, i32* %5
%107 = add i32 %106, 1
store i32 %107, i32* %5
br label %96
108: ; preds = %96
ret void
}

LLVM IR (-O2):
@.str = [17 x i8] c"/proc/self/fd/%u\00"
@.str.1 = [19 x i8] c"/proc/self/fd/%c%u\00"
@.str.2 = [19 x i8] c"/proc/self/fd/%u%c\00"
@INT_MIN = global i32 0
@.str.3 = [17 x i8] c"/proc/self/fd/%d\00"
@INT_MAX = global i32 0
@UINT_MAX = global i32 0
define void @test_lookup(i32 %0) {
%2 = alloca [64 x i8]
%3 = getelementptr inbounds [64 x i8], [64 x i8]* %2, i64 0, i64 0
%4 = call i32 (i8*, i32, i8*, i32, ...) @snprintf(i8* %3, i32 64, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %0)
%5 = call i32 @test_lookup_pass(i8* %3)
br label %6
6: ; preds = %1, %12
%7 = phi i32 [ 1, %1 ], [ %13, %12 ]
%8 = icmp eq i32 %7, 47
br i1 %8, label %12, label %9
9: ; preds = %6
%10 = call i32 (i8*, i32, i8*, i32, ...) @snprintf(i8* %3, i32 64, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i32 %7, i32 %0)
%11 = call i32 @test_lookup_fail(i8* %3)
br label %12
12: ; preds = %6, %9
%13 = add nuw nsw i32 %7, 1
%14 = icmp eq i32 %13, 256
br i1 %14, label %15, label %6
15: ; preds = %12, %21
%16 = phi i32 [ %22, %21 ], [ 1, %12 ]
%17 = icmp eq i32 %16, 47
br i1 %17, label %21, label %18
18: ; preds = %15
%19 = call i32 (i8*, i32, i8*, i32, ...) @snprintf(i8* %3, i32 64, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i32 %0, i32 %16)
%20 = call i32 @test_lookup_fail(i8* %3)
br label %21
21: ; preds = %15, %18
%22 = add nuw nsw i32 %16, 1
%23 = icmp eq i32 %22, 256
br i1 %23, label %24, label %15
24: ; preds = %21
%25 = load i32, i32* @INT_MIN
br label %26
26: ; preds = %24, %26
%27 = phi i32 [ %30, %26 ], [ %25, %24 ]
%28 = call i32 (i8*, i32, i8*, i32, ...) @snprintf(i8* %3, i32 64, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0), i32 %27)
%29 = call i32 @test_lookup_fail(i8* %3)
%30 = add nsw i32 %27, 1
%31 = load i32, i32* @INT_MIN
%32 = add nsw i32 %31, 1023
%33 = icmp slt i32 %27, %32
br i1 %33, label %26, label %34
34: ; preds = %26, %34
%35 = phi i32 [ %38, %34 ], [ -1024, %26 ]
%36 = call i32 (i8*, i32, i8*, i32, ...) @snprintf(i8* %3, i32 64, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0), i32 %35)
%37 = call i32 @test_lookup_fail(i8* %3)
%38 = add nsw i32 %35, 1
%39 = icmp eq i32 %38, 0
br i1 %39, label %40, label %34
40: ; preds = %34
%41 = load i32, i32* @INT_MAX
%42 = add nsw i32 %41, -1024
%43 = add i32 %41, 1024
%44 = icmp ugt i32 %42, %43
br i1 %44, label %53, label %45
45: ; preds = %40, %45
%46 = phi i32 [ %49, %45 ], [ %42, %40 ]
%47 = call i32 (i8*, i32, i8*, i32, ...) @snprintf(i8* %3, i32 64, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %46)
%48 = call i32 @test_lookup_fail(i8* %3)
%49 = add i32 %46, 1
%50 = load i32, i32* @INT_MAX
%51 = add i32 %50, 1024
%52 = icmp ugt i32 %49, %51
br i1 %52, label %53, label %45
53: ; preds = %45, %40
%54 = load i32, i32* @UINT_MAX
%55 = add nsw i32 %54, -1024
%56 = icmp eq i32 %55, 0
br i1 %56, label %63, label %57
57: ; preds = %53, %57
%58 = phi i32 [ %61, %57 ], [ %55, %53 ]
%59 = call i32 (i8*, i32, i8*, i32, ...) @snprintf(i8* %3, i32 64, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %58)
%60 = call i32 @test_lookup_fail(i8* %3)
%61 = add i32 %58, 1
%62 = icmp eq i32 %61, 0
br i1 %62, label %63, label %57
63: ; preds = %57, %53
ret void
}