#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 */
struct stat {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fstat(int, struct stat *);
int /*<<< orphan*/ printf(char *, int);
int time(int /*<<< orphan*/ *);
int /*<<< orphan*/ usleep(int);
int main(int argc, char **argv) {
  int i;
  struct stat buf;
  usleep(time(NULL) % 100000);
  for (i = 3; i < 256; i++) {
    if (fstat(i, &buf))
      continue;
    printf("FAILED: File descriptor %d is open\n", i);
  }
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
xor    %eax,%eax
mov    %eax,%edi
call   1040 <time@plt>
mov    $0x186a0,%ecx
cltd
idiv   %ecx
mov    %edx,%edi
call   1070 <usleep@plt>
movl   $0x3,-0x14(%rbp)
cmpl   $0x100,-0x14(%rbp)
jge    11ca <main+0x7a>
mov    -0x14(%rbp),%edi
lea    -0x18(%rbp),%rsi
call   1060 <fstat@plt>
cmp    $0x0,%rax
je     11ad <main+0x5d>
jmp    11bc <main+0x6c>
mov    -0x14(%rbp),%esi
lea    0xe49(%rip),%rdi
call   1030 <printf@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1185 <main+0x35>
xor    %edi,%edi
call   1050 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
xor    %edi,%edi
call   1040 <time@plt>
movslq %eax,%rdi
imul   $0x14f8b589,%rdi,%rax
mov    %rax,%rcx
shr    $0x3f,%rcx
sar    $0x2d,%rax
add    %ecx,%eax
imul   $0x186a0,%eax,%eax
sub    %eax,%edi
call   1070 <usleep@plt>
mov    $0x3,%ebx
lea    0x8(%rsp),%r14
lea    0xe6b(%rip),%r15
jmp    11ab <main+0x5b>
nopw   0x0(%rax,%rax,1)
add    $0x1,%ebx
cmp    $0x100,%ebx
je     11c6 <main+0x76>
mov    %ebx,%edi
mov    %r14,%rsi
call   1060 <fstat@plt>
test   %rax,%rax
jne    11a0 <main+0x50>
mov    %r15,%rdi
mov    %ebx,%esi
call   1030 <printf@plt>
jmp    11a0 <main+0x50>
xor    %edi,%edi
call   1050 <exit@plt>

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [36 x i8] c"FAILED: File descriptor %d is open\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca %struct.stat
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = call i32 @time(i32* null)
%9 = srem i32 %8, 100000
%10 = call i32 @usleep(i32 %9)
store i32 3, i32* %6
br label %11
11: ; preds = %22, %2
%12 = load i32, i32* %6
%13 = icmp slt i32 %12, 256
br i1 %13, label %14, label %25
14: ; preds = %11
%15 = load i32, i32* %6
%16 = call i64 @fstat(i32 %15, %struct.stat* %7)
%17 = icmp ne i64 %16, 0
br i1 %17, label %18, label %19
18: ; preds = %14
br label %22
19: ; preds = %14
%20 = load i32, i32* %6
%21 = call i32 @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), i32 %20)
br label %22
22: ; preds = %19, %18
%23 = load i32, i32* %6
%24 = add nsw i32 %23, 1
store i32 %24, i32* %6
br label %11
25: ; preds = %11
%26 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [36 x i8] c"FAILED: File descriptor %d is open\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca %struct.stat
%4 = bitcast %struct.stat* %3 to i8*
%5 = tail call i32 @time(i32* null)
%6 = srem i32 %5, 100000
%7 = tail call i32 @usleep(i32 %6)
br label %8
8: ; preds = %2, %14
%9 = phi i32 [ 3, %2 ], [ %15, %14 ]
%10 = call i64 @fstat(i32 %9, %struct.stat* %3)
%11 = icmp eq i64 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %8
%13 = tail call i32 @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), i32 %9)
br label %14
14: ; preds = %8, %12
%15 = add nuw nsw i32 %9, 1
%16 = icmp eq i32 %15, 256
br i1 %16, label %17, label %8
17: ; preds = %14
%18 = tail call i32 @exit(i32 0)
unreachable
}