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

/* Variables and functions */
scalar_t__ EINTR;
int /*<<< orphan*/ F_SETLKW;
int /*<<< orphan*/ SEEK_CUR;
scalar_t__ errno;
int fcntl(int, int /*<<< orphan*/, struct flock *);
int /*<<< orphan*/ sleep(int);
void PttLock(int fd, int start, int size, int mode) {
  static struct flock lock_it;
  int ret;
  lock_it.l_whence = SEEK_CUR;
  lock_it.l_start = start;
  lock_it.l_len = size;
  lock_it.l_type = mode;
  lock_it.l_pid = 0;
  while ((ret = fcntl(fd, F_SETLKW, &lock_it)) < 0 && errno == EINTR)
    sleep(1);
}


x86 assembly (clang, -O0):
<PttLock>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %edx,-0xc(%rbp)
mov    %ecx,-0x10(%rbp)
mov    0x2ead(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x2f0d(%rip)
mov    -0x8(%rbp),%eax
mov    %eax,0x2eec(%rip)
mov    -0xc(%rbp),%eax
mov    %eax,0x2ee7(%rip)
mov    -0x10(%rbp),%eax
mov    %eax,0x2ee2(%rip)
movq   $0x0,0x2edf(%rip)
mov    -0x4(%rbp),%edi
mov    0x2e6d(%rip),%rax
mov    (%rax),%esi
lea    0x2ebc(%rip),%rdx
call   1030 <fcntl@plt>
mov    %eax,%ecx
mov    %ecx,-0x14(%rbp)
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0x15(%rbp)
jge    11ae <PttLock+0x8e>
mov    0x2e35(%rip),%rax
mov    (%rax),%rax
mov    0x2e23(%rip),%rcx
cmp    (%rcx),%rax
sete   %al
mov    %al,-0x15(%rbp)
mov    -0x15(%rbp),%al
test   $0x1,%al
jne    11be <PttLock+0x9e>
jmp    11cd <PttLock+0xad>
mov    $0x1,%edi
call   1040 <sleep@plt>
jmp    1169 <PttLock+0x49>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<PttLock>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %edi,%ebx
mov    0x2eb6(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x2f16(%rip)
lea    0x2ef7(%rip),%rax
mov    %esi,0x2ef1(%rip)
mov    %edx,0x2eef(%rip)
mov    %ecx,0x2eed(%rip)
movq   $0x0,0x2eea(%rip)
mov    0x2e7b(%rip),%r15
mov    (%r15),%esi
mov    %rax,%rdx
call   1030 <fcntl@plt>
test   %eax,%eax
jns    11c5 <PttLock+0xa5>
mov    0x2e55(%rip),%r12
mov    0x2e46(%rip),%r13
mov    0x0(%r13),%rax
cmp    %rax,(%r12)
jne    11c5 <PttLock+0xa5>
lea    0x2ea5(%rip),%r14
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    $0x1,%edi
call   1040 <sleep@plt>
mov    (%r15),%esi
mov    %ebx,%edi
mov    %r14,%rdx
call   1030 <fcntl@plt>
test   %eax,%eax
jns    11c5 <PttLock+0xa5>
mov    0x0(%r13),%rax
cmp    %rax,(%r12)
je     11a0 <PttLock+0x80>
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.flock = type { i32, i32, i32, i64, i32 }
@PttLock.lock_it = internal global %struct.flock zeroinitializer
@SEEK_CUR = global i32 0
@F_SETLKW = global i32 0
@errno = global i64 0
@EINTR = global i64 0
define void @PttLock(i32 %0, i32 %1, i32 %2, i32 %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i32 %0, i32* %5
store i32 %1, i32* %6
store i32 %2, i32* %7
store i32 %3, i32* %8
%10 = load i32, i32* @SEEK_CUR
store i32 %10, i32* getelementptr inbounds (%struct.flock, %struct.flock* @PttLock.lock_it, i32 0, i32 4)
%11 = load i32, i32* %6
store i32 %11, i32* getelementptr inbounds (%struct.flock, %struct.flock* @PttLock.lock_it, i32 0, i32 0)
%12 = load i32, i32* %7
store i32 %12, i32* getelementptr inbounds (%struct.flock, %struct.flock* @PttLock.lock_it, i32 0, i32 1)
%13 = load i32, i32* %8
store i32 %13, i32* getelementptr inbounds (%struct.flock, %struct.flock* @PttLock.lock_it, i32 0, i32 2)
store i64 0, i64* getelementptr inbounds (%struct.flock, %struct.flock* @PttLock.lock_it, i32 0, i32 3)
br label %14
14: ; preds = %25, %4
%15 = load i32, i32* %5
%16 = load i32, i32* @F_SETLKW
%17 = call i32 @fcntl(i32 %15, i32 %16, %struct.flock* @PttLock.lock_it)
store i32 %17, i32* %9
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %23
19: ; preds = %14
%20 = load i64, i64* @errno
%21 = load i64, i64* @EINTR
%22 = icmp eq i64 %20, %21
br label %23
23: ; preds = %19, %14
%24 = phi i1 [ false, %14 ], [ %22, %19 ]
br i1 %24, label %25, label %27
25: ; preds = %23
%26 = call i32 @sleep(i32 1)
br label %14
27: ; preds = %23
ret void
}

LLVM IR (-O2):
%struct.flock = type { i32, i32, i32, i64, i32 }
@PttLock.lock_it = internal global %struct.flock zeroinitializer
@SEEK_CUR = global i32 0
@F_SETLKW = global i32 0
@errno = global i64 0
@EINTR = global i64 0
define void @PttLock(i32 %0, i32 %1, i32 %2, i32 %3) {
%5 = load i32, i32* @SEEK_CUR
store i32 %5, i32* getelementptr inbounds (%struct.flock, %struct.flock* @PttLock.lock_it, i64 0, i32 4)
store i32 %1, i32* getelementptr inbounds (%struct.flock, %struct.flock* @PttLock.lock_it, i64 0, i32 0)
store i32 %2, i32* getelementptr inbounds (%struct.flock, %struct.flock* @PttLock.lock_it, i64 0, i32 1)
store i32 %3, i32* getelementptr inbounds (%struct.flock, %struct.flock* @PttLock.lock_it, i64 0, i32 2)
store i64 0, i64* getelementptr inbounds (%struct.flock, %struct.flock* @PttLock.lock_it, i64 0, i32 3)
%6 = load i32, i32* @F_SETLKW
%7 = tail call i32 @fcntl(i32 %0, i32 %6, %struct.flock* @PttLock.lock_it)
%8 = icmp slt i32 %7, 0
%9 = load i64, i64* @errno
%10 = load i64, i64* @EINTR
%11 = icmp eq i64 %9, %10
%12 = select i1 %8, i1 %11, i1 false
br i1 %12, label %13, label %22
13: ; preds = %4, %13
%14 = tail call i32 @sleep(i32 1)
%15 = load i32, i32* @F_SETLKW
%16 = tail call i32 @fcntl(i32 %0, i32 %15, %struct.flock* @PttLock.lock_it)
%17 = icmp slt i32 %16, 0
%18 = load i64, i64* @errno
%19 = load i64, i64* @EINTR
%20 = icmp eq i64 %18, %19
%21 = select i1 %17, i1 %20, i1 false
br i1 %21, label %13, label %22
22: ; preds = %13, %4
ret void
}