#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_whence;
  int /*<<< orphan*/ l_start;
  int /*<<< orphan*/ l_type;
};

/* Variables and functions */
scalar_t__ EINTR;
int /*<<< orphan*/ F_RDLCK;
int /*<<< orphan*/ F_SETLKW;
int /*<<< orphan*/ SEEK_SET;
scalar_t__ errno;
int /*<<< orphan*/ exit(int);
int fcntl(int, int /*<<< orphan*/, struct flock *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, char *);
int /*<<< orphan*/ stderr;
char *strerror(scalar_t__);
void fatal_lock(char *name, int fd) {
  struct flock fl = {
      .l_type = F_RDLCK, .l_start = 0, .l_whence = SEEK_SET, .l_len = 0};
  int result = -1;
  do {
    result = fcntl(fd, F_SETLKW, &fl);
  } while (result == -1 && errno == EINTR);
  if (result != 0) {
    fprintf(stderr, "%s: locking failed: %s\n", name, strerror(errno));
    exit(2);
  }
}


x86 assembly (clang, -O0):
<fatal_lock>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0x0,-0x20(%rbp)
mov    0x2e6b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
movl   $0x0,-0x18(%rbp)
mov    0x2e40(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
movl   $0xffffffff,-0x24(%rbp)
mov    -0xc(%rbp),%edi
mov    0x2e62(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1030 <fcntl@plt>
mov    %eax,-0x24(%rbp)
xor    %eax,%eax
cmpl   $0xffffffff,-0x24(%rbp)
mov    %al,-0x25(%rbp)
jne    11bd <fatal_lock+0x7d>
mov    0x2e26(%rip),%rax
mov    (%rax),%rax
mov    0x2e0c(%rip),%rcx
cmp    (%rcx),%rax
sete   %al
mov    %al,-0x25(%rbp)
mov    -0x25(%rbp),%al
test   $0x1,%al
jne    117c <fatal_lock+0x3c>
cmpl   $0x0,-0x24(%rbp)
je     1215 <fatal_lock+0xd5>
mov    0x2dff(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    0x2de3(%rip),%rax
mov    (%rax),%rdi
call   1060 <strerror@plt>
mov    -0x34(%rbp),%edi
mov    -0x30(%rbp),%rdx
mov    %rax,%rcx
lea    0xdfa(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0x2,%edi
call   1050 <exit@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fatal_lock>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %esi,%ebp
mov    %rdi,%r14
movl   $0x0,0x8(%rsp)
mov    0x2e66(%rip),%rax
mov    (%rax),%eax
mov    %eax,0xc(%rsp)
movl   $0x0,0x10(%rsp)
mov    0x2e39(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x14(%rsp)
mov    0x2e64(%rip),%r15
lea    0x8(%rsp),%r12
mov    0x2e40(%rip),%r13
mov    0x2e29(%rip),%rbx
nopw   0x0(%rax,%rax,1)
mov    (%r15),%esi
mov    %ebp,%edi
mov    %r12,%rdx
call   1030 <fcntl@plt>
mov    0x0(%r13),%rdi
cmp    $0xffffffff,%eax
jne    11bb <fatal_lock+0x7b>
cmp    (%rbx),%rdi
je     11a0 <fatal_lock+0x60>
test   %eax,%eax
jne    11ce <fatal_lock+0x8e>
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2e03(%rip),%rax
mov    (%rax),%ebx
call   1060 <strerror@plt>
lea    0xe1d(%rip),%rsi
mov    %ebx,%edi
mov    %r14,%rdx
mov    %rax,%rcx
call   1040 <fprintf@plt>
mov    $0x2,%edi
call   1050 <exit@plt>

LLVM IR (-O0):
%struct.flock = type { i32, i32, i32, i32 }
@SEEK_SET = global i32 0
@F_RDLCK = global i32 0
@F_SETLKW = global i32 0
@errno = global i64 0
@EINTR = global i64 0
@stderr = global i32 0
@.str = [24 x i8] c"%s: locking failed: %s\0A\00"
define void @fatal_lock(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca %struct.flock
%6 = alloca i32
store i8* %0, i8** %3
store i32 %1, i32* %4
%7 = getelementptr inbounds %struct.flock, %struct.flock* %5, i32 0, i32 0
store i32 0, i32* %7
%8 = getelementptr inbounds %struct.flock, %struct.flock* %5, i32 0, i32 1
%9 = load i32, i32* @SEEK_SET
store i32 %9, i32* %8
%10 = getelementptr inbounds %struct.flock, %struct.flock* %5, i32 0, i32 2
store i32 0, i32* %10
%11 = getelementptr inbounds %struct.flock, %struct.flock* %5, i32 0, i32 3
%12 = load i32, i32* @F_RDLCK
store i32 %12, i32* %11
store i32 -1, i32* %6
br label %13
13: ; preds = %24, %2
%14 = load i32, i32* %4
%15 = load i32, i32* @F_SETLKW
%16 = call i32 @fcntl(i32 %14, i32 %15, %struct.flock* %5)
store i32 %16, i32* %6
br label %17
17: ; preds = %13
%18 = load i32, i32* %6
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %24
20: ; preds = %17
%21 = load i64, i64* @errno
%22 = load i64, i64* @EINTR
%23 = icmp eq i64 %21, %22
br label %24
24: ; preds = %20, %17
%25 = phi i1 [ false, %17 ], [ %23, %20 ]
br i1 %25, label %13, label %26
26: ; preds = %24
%27 = load i32, i32* %6
%28 = icmp ne i32 %27, 0
br i1 %28, label %29, label %36
29: ; preds = %26
%30 = load i32, i32* @stderr
%31 = load i8*, i8** %3
%32 = load i64, i64* @errno
%33 = call i8* @strerror(i64 %32)
%34 = call i32 @fprintf(i32 %30, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i8* %31, i8* %33)
%35 = call i32 @exit(i32 2)
unreachable
36: ; preds = %26
ret void
}

LLVM IR (-O2):
%struct.flock = type { i32, i32, i32, i32 }
@SEEK_SET = global i32 0
@F_RDLCK = global i32 0
@F_SETLKW = global i32 0
@errno = global i64 0
@EINTR = global i64 0
@stderr = global i32 0
@.str = [24 x i8] c"%s: locking failed: %s\0A\00"
define void @fatal_lock(i8* %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
%7 = load i32, i32* @SEEK_SET
store i32 %7, i32* %6
%8 = getelementptr inbounds %struct.flock, %struct.flock* %3, i64 0, i32 2
store i32 0, i32* %8
%9 = getelementptr inbounds %struct.flock, %struct.flock* %3, i64 0, i32 3
%10 = load i32, i32* @F_RDLCK
store i32 %10, i32* %9
br label %11
11: ; preds = %11, %2
%12 = load i32, i32* @F_SETLKW
%13 = call i32 @fcntl(i32 %1, i32 %12, %struct.flock* %3)
%14 = icmp eq i32 %13, -1
%15 = load i64, i64* @errno
%16 = load i64, i64* @EINTR
%17 = icmp eq i64 %15, %16
%18 = select i1 %14, i1 %17, i1 false
br i1 %18, label %11, label %19
19: ; preds = %11
%20 = icmp eq i32 %13, 0
br i1 %20, label %26, label %21
21: ; preds = %19
%22 = load i32, i32* @stderr
%23 = call i8* @strerror(i64 %15)
%24 = call i32 @fprintf(i32 %22, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %23)
%25 = call i32 @exit(i32 2)
unreachable
26: ; preds = %19
ret void
}