#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 flock {
  scalar_t__ l_type;
  int l_pid;
  scalar_t__ l_len;
  scalar_t__ l_start;
  scalar_t__ l_whence;
};
typedef int ssize_t;

/* Variables and functions */
int /*<<< orphan*/ F_GETFD;
int /*<<< orphan*/ F_GETFL;
int /*<<< orphan*/ F_GETLK;
scalar_t__ F_UNLCK;
scalar_t__ F_WRLCK;
int O_APPEND;
int O_DSYNC;
int O_NONBLOCK;
int O_RDONLY;
int O_RDWR;
int O_RSYNC;
int O_SYNC;
int O_WRONLY;
int fcntl(int, int /*<<< orphan*/, ...);
int getdtablesize();
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, ...);
int readlink(char *, char *, int);
int /*<<< orphan*/ sprintf(char *, char *, int);
int checkOpenFiles(int mentionAbove) {
  int i, num = getdtablesize();
  int numOpen = 0;
  for (i = 0; i < num; i++) {
    int fd_flags = fcntl(i, F_GETFD);
    if (fd_flags != -1) {
      int fl_flags = fcntl(i, F_GETFL);
      if (fl_flags != -1) {
        char path[256];
        sprintf(path, "/proc/self/fd/%d", i);
        char buf[256];
        memset(buf, 0, 256);
        ssize_t s = readlink(path, buf, 256);
        if (s != -1) {
          numOpen++;
          if (i >= mentionAbove) {
            printf("checkOpenFiles - UNEXPECTED FILE %s: ", buf);
            if (fl_flags & O_APPEND)
              printf("append ");
            if (fl_flags & O_NONBLOCK)
              printf("nonblock ");
            if (fl_flags & O_RDONLY)
              printf("read-only ");
            if (fl_flags & O_RDWR)
              printf("read-write ");
            if (fl_flags & O_WRONLY)
              printf("write-only ");
            if (fl_flags & O_DSYNC)
              printf("dsync ");
            if (fl_flags & O_RSYNC)
              printf("rsync ");
            if (fl_flags & O_SYNC)
              printf("sync ");
            struct flock fl;
            fl.l_type = F_WRLCK;
            fl.l_whence = 0;
            fl.l_start = 0;
            fl.l_len = 0;
            fcntl(i, F_GETLK, &fl);
            if (fl.l_type != F_UNLCK) {
              if (fl.l_type == F_WRLCK) {
                printf("write-locked");
              } else {
                printf("read-locked");
              }
              printf("(%d) ", fl.l_pid);
            }
            printf("\n");
          }
        }
      }
    }
  }
  return (numOpen);
}


x86 assembly (clang, -O0):
<checkOpenFiles>:
push   %rbp
mov    %rsp,%rbp
sub    $0x250,%rsp
mov    %edi,-0x4(%rbp)
mov    $0x0,%al
call   1080 <getdtablesize@plt>
mov    %eax,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
movl   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    1447 <checkOpenFiles+0x2e7>
mov    -0x8(%rbp),%edi
mov    0x2e04(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1040 <fcntl@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
je     1434 <checkOpenFiles+0x2d4>
mov    -0x8(%rbp),%edi
mov    0x2ddc(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1040 <fcntl@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
je     142f <checkOpenFiles+0x2cf>
lea    -0x120(%rbp),%rdi
mov    -0x8(%rbp),%edx
lea    0xe1d(%rip),%rsi
call   1070 <sprintf@plt>
lea    -0x220(%rbp),%rdi
xor    %esi,%esi
mov    $0x100,%edx
call   1060 <memset@plt>
lea    -0x120(%rbp),%rdi
lea    -0x220(%rbp),%rsi
mov    $0x100,%edx
call   1030 <readlink@plt>
mov    %eax,-0x224(%rbp)
cmpl   $0xffffffff,-0x224(%rbp)
je     142a <checkOpenFiles+0x2ca>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
mov    -0x8(%rbp),%eax
cmp    -0x4(%rbp),%eax
jl     1425 <checkOpenFiles+0x2c5>
lea    -0x220(%rbp),%rsi
lea    0xdc8(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x18(%rbp),%eax
mov    0x2d1e(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     1273 <checkOpenFiles+0x113>
lea    0xdcb(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x18(%rbp),%eax
mov    0x2d53(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     1296 <checkOpenFiles+0x136>
lea    0xdb0(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x18(%rbp),%eax
mov    0x2d28(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     12b9 <checkOpenFiles+0x159>
lea    0xd97(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x18(%rbp),%eax
mov    0x2d1d(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     12dc <checkOpenFiles+0x17c>
lea    0xd7f(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x18(%rbp),%eax
mov    0x2c9a(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     12ff <checkOpenFiles+0x19f>
lea    0xd68(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x18(%rbp),%eax
mov    0x2c9f(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     1322 <checkOpenFiles+0x1c2>
lea    0xd51(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x18(%rbp),%eax
mov    0x2ccc(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     1345 <checkOpenFiles+0x1e5>
lea    0xd35(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x18(%rbp),%eax
mov    0x2c89(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     1368 <checkOpenFiles+0x208>
lea    0xd0c(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    0x2c21(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x250(%rbp)
movq   $0x0,-0x230(%rbp)
movq   $0x0,-0x238(%rbp)
movq   $0x0,-0x240(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2c1c(%rip),%rax
mov    (%rax),%esi
lea    -0x250(%rbp),%rdx
mov    $0x0,%al
call   1040 <fcntl@plt>
mov    -0x250(%rbp),%rax
mov    0x2bf6(%rip),%rcx
cmp    (%rcx),%rax
je     1417 <checkOpenFiles+0x2b7>
mov    -0x250(%rbp),%rax
mov    0x2bb7(%rip),%rcx
cmp    (%rcx),%rax
jne    13f5 <checkOpenFiles+0x295>
lea    0xc91(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    1403 <checkOpenFiles+0x2a3>
lea    0xc8b(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x248(%rbp),%esi
lea    0xc83(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xc7b(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    142a <checkOpenFiles+0x2ca>
jmp    142f <checkOpenFiles+0x2cf>
jmp    1434 <checkOpenFiles+0x2d4>
jmp    1439 <checkOpenFiles+0x2d9>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    1186 <checkOpenFiles+0x26>
mov    -0x10(%rbp),%eax
add    $0x250,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<checkOpenFiles>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x238,%rsp
mov    %edi,0x4(%rsp)
movl   $0x0,(%rsp)
xor    %eax,%eax
call   1090 <getdtablesize@plt>
test   %eax,%eax
jle    13c2 <checkOpenFiles+0x252>
mov    %eax,%ebp
mov    0x2dfc(%rip),%r13
xor    %r14d,%r14d
movl   $0x0,(%rsp)
jmp    11ed <checkOpenFiles+0x7d>
xor    %eax,%eax
call   1060 <printf@plt>
mov    0x10(%rsp),%esi
lea    0xee1(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0xa,%edi
call   1030 <putchar@plt>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
add    $0x1,%r14d
cmp    %r14d,%ebp
je     13c2 <checkOpenFiles+0x252>
mov    0x0(%r13),%esi
mov    %r14d,%edi
xor    %eax,%eax
call   1050 <fcntl@plt>
cmp    $0xffffffff,%eax
je     11e0 <checkOpenFiles+0x70>
mov    0x2d91(%rip),%rax
mov    (%rax),%esi
mov    %r14d,%edi
xor    %eax,%eax
call   1050 <fcntl@plt>
cmp    $0xffffffff,%eax
je     11e0 <checkOpenFiles+0x70>
mov    %eax,%r15d
lea    0x130(%rsp),%rbx
mov    %rbx,%rdi
lea    0xde3(%rip),%rsi
mov    %r14d,%edx
call   1080 <sprintf@plt>
lea    0x30(%rsp),%r12
mov    %r12,%rdi
xor    %esi,%esi
mov    $0x100,%edx
call   1070 <memset@plt>
mov    %rbx,%rdi
mov    %r12,%rsi
mov    $0x100,%edx
call   1040 <readlink@plt>
cmp    $0xffffffff,%eax
je     11e0 <checkOpenFiles+0x70>
addl   $0x1,(%rsp)
cmp    0x4(%rsp),%r14d
jl     11e0 <checkOpenFiles+0x70>
lea    0xdad(%rip),%rdi
lea    0x30(%rsp),%rsi
xor    %eax,%eax
call   1060 <printf@plt>
mov    0x2cf1(%rip),%rax
test   %r15d,(%rax)
je     129a <checkOpenFiles+0x12a>
lea    0xdb4(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    0x2d2f(%rip),%rax
test   %r15d,(%rax)
je     12b4 <checkOpenFiles+0x144>
lea    0xda2(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    0x2d0d(%rip),%rax
test   %r15d,(%rax)
je     12ce <checkOpenFiles+0x15e>
lea    0xd92(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    0x2d0b(%rip),%rax
test   %r15d,(%rax)
je     12e8 <checkOpenFiles+0x178>
lea    0xd83(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    0x2c91(%rip),%rax
test   %r15d,(%rax)
je     1302 <checkOpenFiles+0x192>
lea    0xd75(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    0x2c9f(%rip),%rax
test   %r15d,(%rax)
je     131c <checkOpenFiles+0x1ac>
lea    0xd67(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    0x2cd5(%rip),%rax
test   %r15d,(%rax)
je     1336 <checkOpenFiles+0x1c6>
lea    0xd54(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    0x2c9b(%rip),%rax
test   %r15d,(%rax)
je     1350 <checkOpenFiles+0x1e0>
lea    0xd34(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    0x2c39(%rip),%rax
mov    (%rax),%rax
mov    %rax,0x8(%rsp)
lea    0x18(%rsp),%rax
xorps  %xmm0,%xmm0
movups %xmm0,(%rax)
movq   $0x0,0x10(%rax)
mov    0x2c47(%rip),%rax
mov    (%rax),%esi
mov    %r14d,%edi
lea    0x8(%rsp),%rdx
xor    %eax,%eax
call   1050 <fcntl@plt>
mov    0x8(%rsp),%rax
mov    0x2c22(%rip),%rcx
cmp    (%rcx),%rax
je     11c9 <checkOpenFiles+0x59>
lea    0xce4(%rip),%rdi
mov    0x2be3(%rip),%rcx
cmp    (%rcx),%rax
je     11b0 <checkOpenFiles+0x40>
lea    0xcda(%rip),%rdi
jmp    11b0 <checkOpenFiles+0x40>
mov    (%rsp),%eax
add    $0x238,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.flock = type { i64, i32, i64, i64, i64 }
@F_GETFD = global i32 0
@F_GETFL = global i32 0
@.str = [17 x i8] c"/proc/self/fd/%d\00"
@.str.1 = [38 x i8] c"checkOpenFiles - UNEXPECTED FILE %s: \00"
@O_APPEND = global i32 0
@.str.2 = [8 x i8] c"append \00"
@O_NONBLOCK = global i32 0
@.str.3 = [10 x i8] c"nonblock \00"
@O_RDONLY = global i32 0
@.str.4 = [11 x i8] c"read-only \00"
@O_RDWR = global i32 0
@.str.5 = [12 x i8] c"read-write \00"
@O_WRONLY = global i32 0
@.str.6 = [12 x i8] c"write-only \00"
@O_DSYNC = global i32 0
@.str.7 = [7 x i8] c"dsync \00"
@O_RSYNC = global i32 0
@.str.8 = [7 x i8] c"rsync \00"
@O_SYNC = global i32 0
@.str.9 = [6 x i8] c"sync \00"
@F_WRLCK = global i64 0
@F_GETLK = global i32 0
@F_UNLCK = global i64 0
@.str.10 = [13 x i8] c"write-locked\00"
@.str.11 = [12 x i8] c"read-locked\00"
@.str.12 = [6 x i8] c"(%d) \00"
@.str.13 = [2 x i8] c"\0A\00"
define i32 @checkOpenFiles(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca [256 x i8]
%9 = alloca [256 x i8]
%10 = alloca i32
%11 = alloca %struct.flock
store i32 %0, i32* %2
%12 = call i32 (...) @getdtablesize()
store i32 %12, i32* %4
store i32 0, i32* %5
store i32 0, i32* %3
br label %13
13: ; preds = %136, %1
%14 = load i32, i32* %3
%15 = load i32, i32* %4
%16 = icmp slt i32 %14, %15
br i1 %16, label %17, label %139
17: ; preds = %13
%18 = load i32, i32* %3
%19 = load i32, i32* @F_GETFD
%20 = call i32 (i32, i32, ...) @fcntl(i32 %18, i32 %19)
store i32 %20, i32* %6
%21 = load i32, i32* %6
%22 = icmp ne i32 %21, -1
br i1 %22, label %23, label %135
23: ; preds = %17
%24 = load i32, i32* %3
%25 = load i32, i32* @F_GETFL
%26 = call i32 (i32, i32, ...) @fcntl(i32 %24, i32 %25)
store i32 %26, i32* %7
%27 = load i32, i32* %7
%28 = icmp ne i32 %27, -1
br i1 %28, label %29, label %134
29: ; preds = %23
%30 = getelementptr inbounds [256 x i8], [256 x i8]* %8, i64 0, i64 0
%31 = load i32, i32* %3
%32 = call i32 @sprintf(i8* %30, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %31)
%33 = getelementptr inbounds [256 x i8], [256 x i8]* %9, i64 0, i64 0
%34 = call i32 @memset(i8* %33, i32 0, i32 256)
%35 = getelementptr inbounds [256 x i8], [256 x i8]* %8, i64 0, i64 0
%36 = getelementptr inbounds [256 x i8], [256 x i8]* %9, i64 0, i64 0
%37 = call i32 @readlink(i8* %35, i8* %36, i32 256)
store i32 %37, i32* %10
%38 = load i32, i32* %10
%39 = icmp ne i32 %38, -1
br i1 %39, label %40, label %133
40: ; preds = %29
%41 = load i32, i32* %5
%42 = add nsw i32 %41, 1
store i32 %42, i32* %5
%43 = load i32, i32* %3
%44 = load i32, i32* %2
%45 = icmp sge i32 %43, %44
br i1 %45, label %46, label %132
46: ; preds = %40
%47 = getelementptr inbounds [256 x i8], [256 x i8]* %9, i64 0, i64 0
%48 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.1, i64 0, i64 0), i8* %47)
%49 = load i32, i32* %7
%50 = load i32, i32* @O_APPEND
%51 = and i32 %49, %50
%52 = icmp ne i32 %51, 0
br i1 %52, label %53, label %55
53: ; preds = %46
%54 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
br label %55
55: ; preds = %53, %46
%56 = load i32, i32* %7
%57 = load i32, i32* @O_NONBLOCK
%58 = and i32 %56, %57
%59 = icmp ne i32 %58, 0
br i1 %59, label %60, label %62
60: ; preds = %55
%61 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0))
br label %62
62: ; preds = %60, %55
%63 = load i32, i32* %7
%64 = load i32, i32* @O_RDONLY
%65 = and i32 %63, %64
%66 = icmp ne i32 %65, 0
br i1 %66, label %67, label %69
67: ; preds = %62
%68 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0))
br label %69
69: ; preds = %67, %62
%70 = load i32, i32* %7
%71 = load i32, i32* @O_RDWR
%72 = and i32 %70, %71
%73 = icmp ne i32 %72, 0
br i1 %73, label %74, label %76
74: ; preds = %69
%75 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.5, i64 0, i64 0))
br label %76
76: ; preds = %74, %69
%77 = load i32, i32* %7
%78 = load i32, i32* @O_WRONLY
%79 = and i32 %77, %78
%80 = icmp ne i32 %79, 0
br i1 %80, label %81, label %83
81: ; preds = %76
%82 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.6, i64 0, i64 0))
br label %83
83: ; preds = %81, %76
%84 = load i32, i32* %7
%85 = load i32, i32* @O_DSYNC
%86 = and i32 %84, %85
%87 = icmp ne i32 %86, 0
br i1 %87, label %88, label %90
88: ; preds = %83
%89 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.7, i64 0, i64 0))
br label %90
90: ; preds = %88, %83
%91 = load i32, i32* %7
%92 = load i32, i32* @O_RSYNC
%93 = and i32 %91, %92
%94 = icmp ne i32 %93, 0
br i1 %94, label %95, label %97
95: ; preds = %90
%96 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.8, i64 0, i64 0))
br label %97
97: ; preds = %95, %90
%98 = load i32, i32* %7
%99 = load i32, i32* @O_SYNC
%100 = and i32 %98, %99
%101 = icmp ne i32 %100, 0
br i1 %101, label %102, label %104
102: ; preds = %97
%103 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.9, i64 0, i64 0))
br label %104
104: ; preds = %102, %97
%105 = load i64, i64* @F_WRLCK
%106 = getelementptr inbounds %struct.flock, %struct.flock* %11, i32 0, i32 0
store i64 %105, i64* %106
%107 = getelementptr inbounds %struct.flock, %struct.flock* %11, i32 0, i32 4
store i64 0, i64* %107
%108 = getelementptr inbounds %struct.flock, %struct.flock* %11, i32 0, i32 3
store i64 0, i64* %108
%109 = getelementptr inbounds %struct.flock, %struct.flock* %11, i32 0, i32 2
store i64 0, i64* %109
%110 = load i32, i32* %3
%111 = load i32, i32* @F_GETLK
%112 = call i32 (i32, i32, ...) @fcntl(i32 %110, i32 %111, %struct.flock* %11)
%113 = getelementptr inbounds %struct.flock, %struct.flock* %11, i32 0, i32 0
%114 = load i64, i64* %113
%115 = load i64, i64* @F_UNLCK
%116 = icmp ne i64 %114, %115
br i1 %116, label %117, label %130
117: ; preds = %104
%118 = getelementptr inbounds %struct.flock, %struct.flock* %11, i32 0, i32 0
%119 = load i64, i64* %118
%120 = load i64, i64* @F_WRLCK
%121 = icmp eq i64 %119, %120
br i1 %121, label %122, label %124
122: ; preds = %117
%123 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.10, i64 0, i64 0))
br label %126
124: ; preds = %117
%125 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.11, i64 0, i64 0))
br label %126
126: ; preds = %124, %122
%127 = getelementptr inbounds %struct.flock, %struct.flock* %11, i32 0, i32 1
%128 = load i32, i32* %127
%129 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.12, i64 0, i64 0), i32 %128)
br label %130
130: ; preds = %126, %104
%131 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.13, i64 0, i64 0))
br label %132
132: ; preds = %130, %40
br label %133
133: ; preds = %132, %29
br label %134
134: ; preds = %133, %23
br label %135
135: ; preds = %134, %17
br label %136
136: ; preds = %135
%137 = load i32, i32* %3
%138 = add nsw i32 %137, 1
store i32 %138, i32* %3
br label %13
139: ; preds = %13
%140 = load i32, i32* %5
ret i32 %140
}

LLVM IR (-O2):
%struct.flock = type { i64, i32, i64, i64, i64 }
@F_GETFD = global i32 0
@F_GETFL = global i32 0
@.str = [17 x i8] c"/proc/self/fd/%d\00"
@.str.1 = [38 x i8] c"checkOpenFiles - UNEXPECTED FILE %s: \00"
@O_APPEND = global i32 0
@.str.2 = [8 x i8] c"append \00"
@O_NONBLOCK = global i32 0
@.str.3 = [10 x i8] c"nonblock \00"
@O_RDONLY = global i32 0
@.str.4 = [11 x i8] c"read-only \00"
@O_RDWR = global i32 0
@.str.5 = [12 x i8] c"read-write \00"
@O_WRONLY = global i32 0
@.str.6 = [12 x i8] c"write-only \00"
@O_DSYNC = global i32 0
@.str.7 = [7 x i8] c"dsync \00"
@O_RSYNC = global i32 0
@.str.8 = [7 x i8] c"rsync \00"
@O_SYNC = global i32 0
@.str.9 = [6 x i8] c"sync \00"
@F_WRLCK = global i64 0
@F_GETLK = global i32 0
@F_UNLCK = global i64 0
@.str.10 = [13 x i8] c"write-locked\00"
@.str.11 = [12 x i8] c"read-locked\00"
@.str.12 = [6 x i8] c"(%d) \00"
define i32 @checkOpenFiles(i32 %0) {
%2 = alloca [256 x i8]
%3 = alloca [256 x i8]
%4 = alloca %struct.flock
%5 = tail call i32 (...) @getdtablesize()
%6 = icmp sgt i32 %5, 0
br i1 %6, label %7, label %104
7: ; preds = %1
%8 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%9 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%10 = bitcast %struct.flock* %4 to i8*
%11 = getelementptr inbounds %struct.flock, %struct.flock* %4, i64 0, i32 0
%12 = getelementptr inbounds %struct.flock, %struct.flock* %4, i64 0, i32 2
%13 = getelementptr inbounds %struct.flock, %struct.flock* %4, i64 0, i32 1
%14 = bitcast i64* %12 to i8*
br label %15
15: ; preds = %7, %100
%16 = phi i32 [ 0, %7 ], [ %102, %100 ]
%17 = phi i32 [ 0, %7 ], [ %101, %100 ]
%18 = load i32, i32* @F_GETFD
%19 = call i32 (i32, i32, ...) @fcntl(i32 %16, i32 %18)
%20 = icmp eq i32 %19, -1
br i1 %20, label %100, label %21
21: ; preds = %15
%22 = load i32, i32* @F_GETFL
%23 = call i32 (i32, i32, ...) @fcntl(i32 %16, i32 %22)
%24 = icmp eq i32 %23, -1
br i1 %24, label %100, label %25
25: ; preds = %21
%26 = call i32 @sprintf(i8* %8, i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %16)
%27 = call i32 @memset(i8* %9, i32 0, i32 256)
%28 = call i32 @readlink(i8* %8, i8* %9, i32 256)
%29 = icmp eq i32 %28, -1
br i1 %29, label %98, label %30
30: ; preds = %25
%31 = add nsw i32 %17, 1
%32 = icmp slt i32 %16, %0
br i1 %32, label %98, label %33
33: ; preds = %30
%34 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([38 x i8], [38 x i8]* @.str.1, i64 0, i64 0), i8* %9)
%35 = load i32, i32* @O_APPEND
%36 = and i32 %35, %23
%37 = icmp eq i32 %36, 0
br i1 %37, label %40, label %38
38: ; preds = %33
%39 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
br label %40
40: ; preds = %38, %33
%41 = load i32, i32* @O_NONBLOCK
%42 = and i32 %41, %23
%43 = icmp eq i32 %42, 0
br i1 %43, label %46, label %44
44: ; preds = %40
%45 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0))
br label %46
46: ; preds = %44, %40
%47 = load i32, i32* @O_RDONLY
%48 = and i32 %47, %23
%49 = icmp eq i32 %48, 0
br i1 %49, label %52, label %50
50: ; preds = %46
%51 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0))
br label %52
52: ; preds = %50, %46
%53 = load i32, i32* @O_RDWR
%54 = and i32 %53, %23
%55 = icmp eq i32 %54, 0
br i1 %55, label %58, label %56
56: ; preds = %52
%57 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.5, i64 0, i64 0))
br label %58
58: ; preds = %56, %52
%59 = load i32, i32* @O_WRONLY
%60 = and i32 %59, %23
%61 = icmp eq i32 %60, 0
br i1 %61, label %64, label %62
62: ; preds = %58
%63 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.6, i64 0, i64 0))
br label %64
64: ; preds = %62, %58
%65 = load i32, i32* @O_DSYNC
%66 = and i32 %65, %23
%67 = icmp eq i32 %66, 0
br i1 %67, label %70, label %68
68: ; preds = %64
%69 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @.str.7, i64 0, i64 0))
br label %70
70: ; preds = %68, %64
%71 = load i32, i32* @O_RSYNC
%72 = and i32 %71, %23
%73 = icmp eq i32 %72, 0
br i1 %73, label %76, label %74
74: ; preds = %70
%75 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @.str.8, i64 0, i64 0))
br label %76
76: ; preds = %74, %70
%77 = load i32, i32* @O_SYNC
%78 = and i32 %77, %23
%79 = icmp eq i32 %78, 0
br i1 %79, label %82, label %80
80: ; preds = %76
%81 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.9, i64 0, i64 0))
br label %82
82: ; preds = %80, %76
%83 = load i64, i64* @F_WRLCK
store i64 %83, i64* %11
%84 = load i32, i32* @F_GETLK
%85 = call i32 (i32, i32, ...) @fcntl(i32 %16, i32 %84, %struct.flock* %4)
%86 = load i64, i64* %11
%87 = load i64, i64* @F_UNLCK
%88 = icmp eq i64 %86, %87
br i1 %88, label %96, label %89
89: ; preds = %82
%90 = load i64, i64* @F_WRLCK
%91 = icmp eq i64 %86, %90
%92 = select i1 %91, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.10, i64 0, i64 0), i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.11, i64 0, i64 0)
%93 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) %92)
%94 = load i32, i32* %13
%95 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.12, i64 0, i64 0), i32 %94)
br label %96
96: ; preds = %89, %82
%97 = call i32 @putchar(i32 10)
br label %98
98: ; preds = %30, %96, %25
%99 = phi i32 [ %31, %96 ], [ %31, %30 ], [ %17, %25 ]
br label %100
100: ; preds = %21, %98, %15
%101 = phi i32 [ %17, %15 ], [ %99, %98 ], [ %17, %21 ]
%102 = add nuw nsw i32 %16, 1
%103 = icmp eq i32 %102, %5
br i1 %103, label %104, label %15
104: ; preds = %100, %1
%105 = phi i32 [ 0, %1 ], [ %101, %100 ]
ret i32 %105
}