#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 /*<<< orphan*/ l_len;
  int /*<<< orphan*/ l_start;
  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 write_lock_file(int fd, int wait) {
  struct flock lock = {
      .l_type = F_WRLCK,
      .l_whence = SEEK_SET,
      .l_start = 0,
      .l_len = 0,
  };
  if (wait) {
    return fcntl(fd, F_SETLKW, &lock);
  } else {
    return fcntl(fd, F_SETLK, &lock);
  }
}


x86 assembly (clang, -O0):
<write_lock_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0x0,-0x20(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    0x2e9d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    0x2e89(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0xc(%rbp)
je     116b <write_lock_file+0x5b>
mov    -0x8(%rbp),%edi
mov    0x2e90(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1030 <fcntl@plt>
mov    %eax,-0x4(%rbp)
jmp    1183 <write_lock_file+0x73>
mov    -0x8(%rbp),%edi
mov    0x2e6b(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1030 <fcntl@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_lock_file>:
sub    $0x18,%rsp
movq   $0x0,0x8(%rsp)
mov    0x2eac(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
mov    0x2e97(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x14(%rsp)
test   %esi,%esi
jne    1144 <write_lock_file+0x34>
mov    0x2e9e(%rip),%rax
jmp    114b <write_lock_file+0x3b>
mov    0x2e9d(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
call   1030 <fcntl@plt>
add    $0x18,%rsp
ret

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

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