#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;
  scalar_t__ l_start;
  scalar_t__ l_len;
  int l_pid;
  int /*<<< orphan*/ l_whence;
};
typedef scalar_t__ off_t;

/* Variables and functions */
int /*<<< orphan*/ F_GETLK;
scalar_t__ F_UNLCK;
scalar_t__ F_WRLCK;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ fcntl(int, int /*<<< orphan*/, struct flock *);
int /*<<< orphan*/ printf(char *, int, int, int, char *);
int showLocksInRange(int fd, off_t lwr, off_t upr) {
  int cnt = 0;
  struct flock x;
  x.l_type = F_WRLCK;
  x.l_whence = SEEK_SET;
  x.l_start = lwr;
  x.l_len = upr - lwr;
  fcntl(fd, F_GETLK, &x);
  if (x.l_type == F_UNLCK)
    return 0;
  printf("start: %-12d len: %-5d pid: %-5d type: %s\n", (int)x.l_start,
         (int)x.l_len, x.l_pid, x.l_type == F_WRLCK ? "WRLCK" : "RDLCK");
  cnt++;
  if (x.l_start > lwr) {
    cnt += showLocksInRange(fd, lwr, x.l_start - 1);
  }
  if (x.l_start + x.l_len < upr) {
    cnt += showLocksInRange(fd, x.l_start + x.l_len + 1, upr);
  }
  return cnt;
}


x86 assembly (clang, -O0):
<showLocksInRange>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    0x2e77(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x40(%rbp)
mov    0x2e71(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x18(%rbp),%rax
sub    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e66(%rip),%rax
mov    (%rax),%esi
lea    -0x40(%rbp),%rdx
call   1040 <fcntl@plt>
mov    -0x40(%rbp),%rax
mov    0x2e48(%rip),%rcx
cmp    (%rcx),%rax
jne    11ad <showLocksInRange+0x7d>
movl   $0x0,-0x4(%rbp)
jmp    1252 <showLocksInRange+0x122>
mov    -0x38(%rbp),%rax
mov    %eax,%esi
mov    -0x30(%rbp),%rax
mov    %eax,%edx
mov    -0x28(%rbp),%ecx
mov    -0x40(%rbp),%rdi
mov    0x2e01(%rip),%rax
mov    (%rax),%r9
lea    0xe60(%rip),%r8
lea    0xe53(%rip),%rax
cmp    %r9,%rdi
cmove  %rax,%r8
lea    0xe1a(%rip),%rdi
call   1050 <printf@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x38(%rbp),%rax
cmp    -0x10(%rbp),%rax
jle    121c <showLocksInRange+0xec>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x38(%rbp),%rdx
sub    $0x1,%rdx
call   1030 <showLocksInRange@plt>
add    -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x38(%rbp),%rax
add    -0x30(%rbp),%rax
cmp    -0x18(%rbp),%rax
jge    124c <showLocksInRange+0x11c>
mov    -0x8(%rbp),%edi
mov    -0x38(%rbp),%rsi
add    -0x30(%rbp),%rsi
add    $0x1,%rsi
mov    -0x18(%rbp),%rdx
call   1030 <showLocksInRange@plt>
add    -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<showLocksInRange>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x20,%rsp
mov    %rdx,%r15
mov    %rsi,%rbp
mov    %edi,%r14d
mov    0x2e7c(%rip),%r12
mov    (%r12),%rax
mov    %rax,(%rsp)
mov    0x2e75(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x1c(%rsp)
mov    %rsi,0x8(%rsp)
mov    %rdx,%rax
sub    %rsi,%rax
mov    %rax,0x10(%rsp)
mov    0x2e70(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
call   1040 <fcntl@plt>
mov    (%rsp),%rax
mov    0x2e53(%rip),%rcx
xor    %ebx,%ebx
cmp    (%rcx),%rax
je     1208 <showLocksInRange+0xd8>
mov    0x8(%rsp),%esi
mov    0x10(%rsp),%edx
mov    0x18(%rsp),%ecx
cmp    (%r12),%rax
lea    0xe80(%rip),%rax
lea    0xe7f(%rip),%r8
cmove  %rax,%r8
lea    0xe43(%rip),%rdi
call   1050 <printf@plt>
mov    0x8(%rsp),%rdx
mov    $0x1,%ebx
cmp    %rbp,%rdx
jle    11ea <showLocksInRange+0xba>
add    $0xffffffffffffffff,%rdx
mov    %r14d,%edi
mov    %rbp,%rsi
call   1030 <showLocksInRange@plt>
mov    %eax,%ebx
add    $0x1,%ebx
mov    0x8(%rsp),%rdx
add    0x10(%rsp),%rdx
cmp    %r15,%rdx
jge    1208 <showLocksInRange+0xd8>
add    $0x1,%rdx
mov    %r14d,%edi
mov    %rdx,%rsi
mov    %r15,%rdx
call   1030 <showLocksInRange@plt>
add    %eax,%ebx
mov    %ebx,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.flock = type { i64, i64, i64, i32, i32 }
@F_WRLCK = global i64 0
@SEEK_SET = global i32 0
@F_GETLK = global i32 0
@F_UNLCK = global i64 0
@.str = [43 x i8] c"start: %-12d len: %-5d pid: %-5d type: %s\0A\00"
@.str.1 = [6 x i8] c"WRLCK\00"
@.str.2 = [6 x i8] c"RDLCK\00"
define i32 @showLocksInRange(i32 %0, i64 %1, i64 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i64
%7 = alloca i64
%8 = alloca i32
%9 = alloca %struct.flock
store i32 %0, i32* %5
store i64 %1, i64* %6
store i64 %2, i64* %7
store i32 0, i32* %8
%10 = load i64, i64* @F_WRLCK
%11 = getelementptr inbounds %struct.flock, %struct.flock* %9, i32 0, i32 0
store i64 %10, i64* %11
%12 = load i32, i32* @SEEK_SET
%13 = getelementptr inbounds %struct.flock, %struct.flock* %9, i32 0, i32 4
store i32 %12, i32* %13
%14 = load i64, i64* %6
%15 = getelementptr inbounds %struct.flock, %struct.flock* %9, i32 0, i32 1
store i64 %14, i64* %15
%16 = load i64, i64* %7
%17 = load i64, i64* %6
%18 = sub nsw i64 %16, %17
%19 = getelementptr inbounds %struct.flock, %struct.flock* %9, i32 0, i32 2
store i64 %18, i64* %19
%20 = load i32, i32* %5
%21 = load i32, i32* @F_GETLK
%22 = call i32 @fcntl(i32 %20, i32 %21, %struct.flock* %9)
%23 = getelementptr inbounds %struct.flock, %struct.flock* %9, i32 0, i32 0
%24 = load i64, i64* %23
%25 = load i64, i64* @F_UNLCK
%26 = icmp eq i64 %24, %25
br i1 %26, label %27, label %28
27: ; preds = %3
store i32 0, i32* %4
br label %81
28: ; preds = %3
%29 = getelementptr inbounds %struct.flock, %struct.flock* %9, i32 0, i32 1
%30 = load i64, i64* %29
%31 = trunc i64 %30 to i32
%32 = getelementptr inbounds %struct.flock, %struct.flock* %9, i32 0, i32 2
%33 = load i64, i64* %32
%34 = trunc i64 %33 to i32
%35 = getelementptr inbounds %struct.flock, %struct.flock* %9, i32 0, i32 3
%36 = load i32, i32* %35
%37 = getelementptr inbounds %struct.flock, %struct.flock* %9, i32 0, i32 0
%38 = load i64, i64* %37
%39 = load i64, i64* @F_WRLCK
%40 = icmp eq i64 %38, %39
%41 = zext i1 %40 to i64
%42 = select i1 %40, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0)
%43 = call i32 @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str, i64 0, i64 0), i32 %31, i32 %34, i32 %36, i8* %42)
%44 = load i32, i32* %8
%45 = add nsw i32 %44, 1
store i32 %45, i32* %8
%46 = getelementptr inbounds %struct.flock, %struct.flock* %9, i32 0, i32 1
%47 = load i64, i64* %46
%48 = load i64, i64* %6
%49 = icmp sgt i64 %47, %48
br i1 %49, label %50, label %59
50: ; preds = %28
%51 = load i32, i32* %5
%52 = load i64, i64* %6
%53 = getelementptr inbounds %struct.flock, %struct.flock* %9, i32 0, i32 1
%54 = load i64, i64* %53
%55 = sub nsw i64 %54, 1
%56 = call i32 @showLocksInRange(i32 %51, i64 %52, i64 %55)
%57 = load i32, i32* %8
%58 = add nsw i32 %57, %56
store i32 %58, i32* %8
br label %59
59: ; preds = %50, %28
%60 = getelementptr inbounds %struct.flock, %struct.flock* %9, i32 0, i32 1
%61 = load i64, i64* %60
%62 = getelementptr inbounds %struct.flock, %struct.flock* %9, i32 0, i32 2
%63 = load i64, i64* %62
%64 = add nsw i64 %61, %63
%65 = load i64, i64* %7
%66 = icmp slt i64 %64, %65
br i1 %66, label %67, label %79
67: ; preds = %59
%68 = load i32, i32* %5
%69 = getelementptr inbounds %struct.flock, %struct.flock* %9, i32 0, i32 1
%70 = load i64, i64* %69
%71 = getelementptr inbounds %struct.flock, %struct.flock* %9, i32 0, i32 2
%72 = load i64, i64* %71
%73 = add nsw i64 %70, %72
%74 = add nsw i64 %73, 1
%75 = load i64, i64* %7
%76 = call i32 @showLocksInRange(i32 %68, i64 %74, i64 %75)
%77 = load i32, i32* %8
%78 = add nsw i32 %77, %76
store i32 %78, i32* %8
br label %79
79: ; preds = %67, %59
%80 = load i32, i32* %8
store i32 %80, i32* %4
br label %81
81: ; preds = %79, %27
%82 = load i32, i32* %4
ret i32 %82
}

LLVM IR (-O2):
%struct.flock = type { i64, i64, i64, i32, i32 }
@F_WRLCK = global i64 0
@SEEK_SET = global i32 0
@F_GETLK = global i32 0
@F_UNLCK = global i64 0
@.str = [43 x i8] c"start: %-12d len: %-5d pid: %-5d type: %s\0A\00"
@.str.1 = [6 x i8] c"WRLCK\00"
@.str.2 = [6 x i8] c"RDLCK\00"
define i32 @showLocksInRange(i32 %0, i64 %1, i64 %2) {
%4 = alloca %struct.flock
%5 = bitcast %struct.flock* %4 to i8*
%6 = load i64, i64* @F_WRLCK
%7 = getelementptr inbounds %struct.flock, %struct.flock* %4, i64 0, i32 0
store i64 %6, i64* %7
%8 = load i32, i32* @SEEK_SET
%9 = getelementptr inbounds %struct.flock, %struct.flock* %4, i64 0, i32 4
store i32 %8, i32* %9
%10 = getelementptr inbounds %struct.flock, %struct.flock* %4, i64 0, i32 1
store i64 %1, i64* %10
%11 = sub nsw i64 %2, %1
%12 = getelementptr inbounds %struct.flock, %struct.flock* %4, i64 0, i32 2
store i64 %11, i64* %12
%13 = load i32, i32* @F_GETLK
%14 = call i32 @fcntl(i32 %0, i32 %13, %struct.flock* %4)
%15 = load i64, i64* %7
%16 = load i64, i64* @F_UNLCK
%17 = icmp eq i64 %15, %16
br i1 %17, label %46, label %18
18: ; preds = %3
%19 = load i64, i64* %10
%20 = trunc i64 %19 to i32
%21 = load i64, i64* %12
%22 = trunc i64 %21 to i32
%23 = getelementptr inbounds %struct.flock, %struct.flock* %4, i64 0, i32 3
%24 = load i32, i32* %23
%25 = load i64, i64* @F_WRLCK
%26 = icmp eq i64 %15, %25
%27 = select i1 %26, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0)
%28 = call i32 @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str, i64 0, i64 0), i32 %20, i32 %22, i32 %24, i8* %27)
%29 = load i64, i64* %10
%30 = icmp sgt i64 %29, %1
br i1 %30, label %31, label %36
31: ; preds = %18
%32 = add nsw i64 %29, -1
%33 = call i32 @showLocksInRange(i32 %0, i64 %1, i64 %32)
%34 = add nsw i32 %33, 1
%35 = load i64, i64* %10
br label %36
36: ; preds = %31, %18
%37 = phi i64 [ %35, %31 ], [ %29, %18 ]
%38 = phi i32 [ %34, %31 ], [ 1, %18 ]
%39 = load i64, i64* %12
%40 = add nsw i64 %39, %37
%41 = icmp slt i64 %40, %2
br i1 %41, label %42, label %46
42: ; preds = %36
%43 = add nsw i64 %40, 1
%44 = call i32 @showLocksInRange(i32 %0, i64 %43, i64 %2)
%45 = add nsw i32 %44, %38
br label %46
46: ; preds = %36, %42, %3
%47 = phi i32 [ 0, %3 ], [ %45, %42 ], [ %38, %36 ]
ret i32 %47
}