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

/* Variables and functions */
int /*<<< orphan*/ F_SETLK;
int /*<<< orphan*/ F_SETLKW;
int /*<<< orphan*/ F_WRLCK;
int /*<<< orphan*/ SEEK_SET;
int fcntl(int, int /*<<< orphan*/, struct flock *);
int lock_range(int fd, int offset, int len, bool wait) {
  struct flock lock;
  lock.l_type = F_WRLCK;
  lock.l_whence = SEEK_SET;
  lock.l_start = offset;
  lock.l_len = len;
  lock.l_pid = 0;
  return fcntl(fd, wait ? F_SETLKW : F_SETLK, &lock);
}


x86 assembly (clang, -O0):
<lock_range>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %edx,-0xc(%rbp)
mov    %ecx,-0x10(%rbp)
mov    0x2e9d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    0x2e99(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x24(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x4(%rbp),%eax
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x10(%rbp)
je     1171 <lock_range+0x61>
mov    0x2e81(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
jmp    117d <lock_range+0x6d>
mov    0x2e68(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    -0x2c(%rbp),%edi
mov    -0x30(%rbp),%esi
lea    -0x28(%rbp),%rdx
call   1030 <fcntl@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<lock_range>:
sub    $0x18,%rsp
mov    0x2ead(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x14(%rsp)
mov    0x2ea8(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
mov    %esi,(%rsp)
mov    %edx,0x4(%rsp)
movq   $0x0,0x8(%rsp)
test   %ecx,%ecx
jne    114b <lock_range+0x3b>
mov    0x2e97(%rip),%rax
jmp    1152 <lock_range+0x42>
mov    0x2e96(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
call   1030 <fcntl@plt>
add    $0x18,%rsp
ret

LLVM IR (-O0):
%struct.flock = type { i32, i32, i64, i32, i32 }
@F_WRLCK = global i32 0
@SEEK_SET = global i32 0
@F_SETLKW = global i32 0
@F_SETLK = global i32 0
define i32 @lock_range(i32 %0, i32 %1, i32 %2, i32 %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca %struct.flock
store i32 %0, i32* %5
store i32 %1, i32* %6
store i32 %2, i32* %7
store i32 %3, i32* %8
%10 = load i32, i32* @F_WRLCK
%11 = getelementptr inbounds %struct.flock, %struct.flock* %9, i32 0, i32 4
store i32 %10, i32* %11
%12 = load i32, i32* @SEEK_SET
%13 = getelementptr inbounds %struct.flock, %struct.flock* %9, i32 0, i32 3
store i32 %12, i32* %13
%14 = load i32, i32* %6
%15 = getelementptr inbounds %struct.flock, %struct.flock* %9, i32 0, i32 0
store i32 %14, i32* %15
%16 = load i32, i32* %7
%17 = getelementptr inbounds %struct.flock, %struct.flock* %9, i32 0, i32 1
store i32 %16, i32* %17
%18 = getelementptr inbounds %struct.flock, %struct.flock* %9, i32 0, i32 2
store i64 0, i64* %18
%19 = load i32, i32* %5
%20 = load i32, i32* %8
%21 = icmp ne i32 %20, 0
br i1 %21, label %22, label %24
22: ; preds = %4
%23 = load i32, i32* @F_SETLKW
br label %26
24: ; preds = %4
%25 = load i32, i32* @F_SETLK
br label %26
26: ; preds = %24, %22
%27 = phi i32 [ %23, %22 ], [ %25, %24 ]
%28 = call i32 @fcntl(i32 %19, i32 %27, %struct.flock* %9)
ret i32 %28
}

LLVM IR (-O2):
%struct.flock = type { i32, i32, i64, i32, i32 }
@F_WRLCK = global i32 0
@SEEK_SET = global i32 0
@F_SETLKW = global i32 0
@F_SETLK = global i32 0
define i32 @lock_range(i32 %0, i32 %1, i32 %2, i32 %3) {
%5 = alloca %struct.flock
%6 = bitcast %struct.flock* %5 to i8*
%7 = load i32, i32* @F_WRLCK
%8 = getelementptr inbounds %struct.flock, %struct.flock* %5, i64 0, i32 4
store i32 %7, i32* %8
%9 = load i32, i32* @SEEK_SET
%10 = getelementptr inbounds %struct.flock, %struct.flock* %5, i64 0, i32 3
store i32 %9, i32* %10
%11 = getelementptr inbounds %struct.flock, %struct.flock* %5, i64 0, i32 0
store i32 %1, i32* %11
%12 = getelementptr inbounds %struct.flock, %struct.flock* %5, i64 0, i32 1
store i32 %2, i32* %12
%13 = getelementptr inbounds %struct.flock, %struct.flock* %5, i64 0, i32 2
store i64 0, i64* %13
%14 = icmp eq i32 %3, 0
%15 = load i32, i32* @F_SETLKW
%16 = load i32, i32* @F_SETLK
%17 = select i1 %14, i32 %16, i32 %15
%18 = call i32 @fcntl(i32 %0, i32 %17, %struct.flock* %5)
ret i32 %18
}