#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 kernel_thread_fd(unsigned int);
int /*<<< orphan*/ test_lookup(int);
int /*<<< orphan*/ test_readdir(int);
int main(void) {
  unsigned int pid;
  int fd;
  pid = 2;
  while ((fd = kernel_thread_fd(pid)) == -1 && pid < 1024) {
    pid++;
  }
  if (pid >= 1024)
    return 1;
  test_readdir(fd);
  test_lookup(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x2,-0x8(%rbp)
mov    -0x8(%rbp),%edi
call   1050 <kernel_thread_fd@plt>
mov    %eax,%ecx
mov    %ecx,-0xc(%rbp)
xor    %eax,%eax
cmp    $0xffffffff,%ecx
mov    %al,-0xd(%rbp)
jne    116e <main+0x3e>
cmpl   $0x400,-0x8(%rbp)
setb   %al
mov    %al,-0xd(%rbp)
mov    -0xd(%rbp),%al
test   $0x1,%al
jne    117e <main+0x4e>
jmp    118c <main+0x5c>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    1146 <main+0x16>
cmpl   $0x400,-0x8(%rbp)
jb     11a5 <main+0x75>
movl   $0x1,-0x4(%rbp)
jmp    11bc <main+0x8c>
mov    -0xc(%rbp),%edi
call   1030 <test_readdir@plt>
mov    -0xc(%rbp),%edi
call   1040 <test_lookup@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
mov    $0x1,%ebp
nopl   0x0(%rax,%rax,1)
add    $0x1,%ebp
mov    %ebp,%edi
call   1050 <kernel_thread_fd@plt>
mov    %eax,%ebx
cmp    $0x3ff,%ebp
ja     1159 <main+0x29>
cmp    $0xffffffff,%ebx
je     1140 <main+0x10>
mov    $0x1,%eax
cmp    $0x3ff,%ebp
ja     1176 <main+0x46>
mov    %ebx,%edi
call   1030 <test_readdir@plt>
mov    %ebx,%edi
call   1040 <test_lookup@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
store i32 0, i32* %1
store i32 2, i32* %2
br label %4
4: ; preds = %13, %0
%5 = load i32, i32* %2
%6 = call i32 @kernel_thread_fd(i32 %5)
store i32 %6, i32* %3
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %11
8: ; preds = %4
%9 = load i32, i32* %2
%10 = icmp ult i32 %9, 1024
br label %11
11: ; preds = %8, %4
%12 = phi i1 [ false, %4 ], [ %10, %8 ]
br i1 %12, label %13, label %16
13: ; preds = %11
%14 = load i32, i32* %2
%15 = add i32 %14, 1
store i32 %15, i32* %2
br label %4
16: ; preds = %11
%17 = load i32, i32* %2
%18 = icmp uge i32 %17, 1024
br i1 %18, label %19, label %20
19: ; preds = %16
store i32 1, i32* %1
br label %25
20: ; preds = %16
%21 = load i32, i32* %3
%22 = call i32 @test_readdir(i32 %21)
%23 = load i32, i32* %3
%24 = call i32 @test_lookup(i32 %23)
store i32 0, i32* %1
br label %25
25: ; preds = %20, %19
%26 = load i32, i32* %1
ret i32 %26
}

LLVM IR (-O2):
define i32 @main() {
br label %1
1: ; preds = %1, %0
%2 = phi i32 [ 2, %0 ], [ %7, %1 ]
%3 = tail call i32 @kernel_thread_fd(i32 %2)
%4 = icmp eq i32 %3, -1
%5 = icmp ult i32 %2, 1024
%6 = and i1 %5, %4
%7 = add nuw nsw i32 %2, 1
br i1 %6, label %1, label %8
8: ; preds = %1
%9 = icmp ugt i32 %2, 1023
br i1 %9, label %13, label %10
10: ; preds = %8
%11 = tail call i32 @test_readdir(i32 %3)
%12 = tail call i32 @test_lookup(i32 %3)
br label %13
13: ; preds = %8, %10
%14 = phi i32 [ 0, %10 ], [ 1, %8 ]
ret i32 %14
}