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

/* Variables and functions */
int /*<<< orphan*/ F_GETLK;
int /*<<< orphan*/ F_SETLK;
int F_UNLCK;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ fcntl(int, int /*<<< orphan*/, struct flock *);
int /*<<< orphan*/ sprintf(char *, char *, int);
int /*<<< orphan*/ waitforuser(char *);
void getlock(int fd, int type) {
  struct flock lockinfo;
  char message[80];
  lockinfo.l_whence = SEEK_SET;
  lockinfo.l_start = 0;
  lockinfo.l_len = 0;
  while (1) {
    lockinfo.l_type = type;
    if (!fcntl(fd, F_SETLK, &lockinfo))
      return;
    fcntl(fd, F_GETLK, &lockinfo);
    if (lockinfo.l_type != F_UNLCK) {
      sprintf(message,
              "conflict with process %d... press "
              "<return> to retry:",
              lockinfo.l_pid);
      waitforuser(message);
    }
  }
}


x86 assembly (clang, -O0):
<getlock>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    0x2e80(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0x4(%rbp),%edi
mov    0x2e7b(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdx
call   1030 <fcntl@plt>
cmp    $0x0,%eax
jne    118a <getlock+0x5a>
add    $0x80,%rsp
pop    %rbp
ret
mov    -0x4(%rbp),%edi
mov    0x2e4c(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdx
call   1030 <fcntl@plt>
mov    -0x28(%rbp),%eax
mov    0x2e2f(%rip),%rcx
cmp    (%rcx),%eax
je     11cd <getlock+0x9d>
lea    -0x80(%rbp),%rdi
mov    -0x24(%rbp),%edx
lea    0xe41(%rip),%rsi
call   1050 <sprintf@plt>
lea    -0x80(%rbp),%rdi
call   1040 <waitforuser@plt>
jmp    115d <getlock+0x2d>

x86 assembly (clang, -O2):
<getlock>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x78,%rsp
mov    %esi,%r14d
mov    %edi,%ebp
mov    0x2e7e(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x18(%rsp)
xorps  %xmm0,%xmm0
movups %xmm0,0x8(%rsp)
mov    %esi,(%rsp)
mov    0x2e86(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
call   1030 <fcntl@plt>
test   %eax,%eax
je     11de <getlock+0xae>
mov    0x2e69(%rip),%r15
mov    %rsp,%rbx
mov    0x2e57(%rip),%r13
lea    0x20(%rsp),%r12
jmp    11ab <getlock+0x7b>
nopl   0x0(%rax,%rax,1)
mov    %r14d,(%rsp)
mov    0x2e4d(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
mov    %rbx,%rdx
call   1030 <fcntl@plt>
test   %eax,%eax
je     11de <getlock+0xae>
mov    (%r15),%esi
mov    %ebp,%edi
mov    %rbx,%rdx
call   1030 <fcntl@plt>
mov    (%rsp),%eax
cmp    0x0(%r13),%eax
je     1190 <getlock+0x60>
mov    0x4(%rsp),%edx
mov    %r12,%rdi
lea    0xe31(%rip),%rsi
call   1050 <sprintf@plt>
mov    %r12,%rdi
call   1040 <waitforuser@plt>
jmp    1190 <getlock+0x60>
add    $0x78,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.flock = type { i32, i32, i64, i64, i32 }
@SEEK_SET = global i32 0
@F_SETLK = global i32 0
@F_GETLK = global i32 0
@F_UNLCK = global i32 0
@.str = [53 x i8] c"conflict with process %d... press <return> to retry:\00"
define void @getlock(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.flock
%6 = alloca [80 x i8]
store i32 %0, i32* %3
store i32 %1, i32* %4
%7 = load i32, i32* @SEEK_SET
%8 = getelementptr inbounds %struct.flock, %struct.flock* %5, i32 0, i32 4
store i32 %7, i32* %8
%9 = getelementptr inbounds %struct.flock, %struct.flock* %5, i32 0, i32 3
store i64 0, i64* %9
%10 = getelementptr inbounds %struct.flock, %struct.flock* %5, i32 0, i32 2
store i64 0, i64* %10
br label %11
11: ; preds = %2, %34
%12 = load i32, i32* %4
%13 = getelementptr inbounds %struct.flock, %struct.flock* %5, i32 0, i32 0
store i32 %12, i32* %13
%14 = load i32, i32* %3
%15 = load i32, i32* @F_SETLK
%16 = call i32 @fcntl(i32 %14, i32 %15, %struct.flock* %5)
%17 = icmp ne i32 %16, 0
br i1 %17, label %19, label %18
18: ; preds = %11
ret void
19: ; preds = %11
%20 = load i32, i32* %3
%21 = load i32, i32* @F_GETLK
%22 = call i32 @fcntl(i32 %20, i32 %21, %struct.flock* %5)
%23 = getelementptr inbounds %struct.flock, %struct.flock* %5, i32 0, i32 0
%24 = load i32, i32* %23
%25 = load i32, i32* @F_UNLCK
%26 = icmp ne i32 %24, %25
br i1 %26, label %27, label %34
27: ; preds = %19
%28 = getelementptr inbounds [80 x i8], [80 x i8]* %6, i64 0, i64 0
%29 = getelementptr inbounds %struct.flock, %struct.flock* %5, i32 0, i32 1
%30 = load i32, i32* %29
%31 = call i32 @sprintf(i8* %28, i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str, i64 0, i64 0), i32 %30)
%32 = getelementptr inbounds [80 x i8], [80 x i8]* %6, i64 0, i64 0
%33 = call i32 @waitforuser(i8* %32)
br label %34
34: ; preds = %27, %19
br label %11
}

LLVM IR (-O2):
%struct.flock = type { i32, i32, i64, i64, i32 }
@SEEK_SET = global i32 0
@F_SETLK = global i32 0
@F_GETLK = global i32 0
@F_UNLCK = global i32 0
@.str = [53 x i8] c"conflict with process %d... press <return> to retry:\00"
define void @getlock(i32 %0, i32 %1) {
%3 = alloca %struct.flock
%4 = alloca [80 x i8]
%5 = bitcast %struct.flock* %3 to i8*
%6 = getelementptr inbounds [80 x i8], [80 x i8]* %4, i64 0, i64 0
%7 = load i32, i32* @SEEK_SET
%8 = getelementptr inbounds %struct.flock, %struct.flock* %3, i64 0, i32 4
store i32 %7, i32* %8
%9 = getelementptr inbounds %struct.flock, %struct.flock* %3, i64 0, i32 2
%10 = getelementptr inbounds %struct.flock, %struct.flock* %3, i64 0, i32 0
%11 = bitcast i64* %9 to i8*
store i32 %1, i32* %10
%12 = load i32, i32* @F_SETLK
%13 = call i32 @fcntl(i32 %0, i32 %12, %struct.flock* %3)
%14 = icmp eq i32 %13, 0
br i1 %14, label %17, label %15
15: ; preds = %2
%16 = getelementptr inbounds %struct.flock, %struct.flock* %3, i64 0, i32 1
br label %18
17: ; preds = %28, %2
ret void
18: ; preds = %15, %28
%19 = load i32, i32* @F_GETLK
%20 = call i32 @fcntl(i32 %0, i32 %19, %struct.flock* %3)
%21 = load i32, i32* %10
%22 = load i32, i32* @F_UNLCK
%23 = icmp eq i32 %21, %22
br i1 %23, label %28, label %24
24: ; preds = %18
%25 = load i32, i32* %16
%26 = call i32 @sprintf(i8* %6, i8* dereferenceable(1) getelementptr inbounds ([53 x i8], [53 x i8]* @.str, i64 0, i64 0), i32 %25)
%27 = call i32 @waitforuser(i8* %6)
br label %28
28: ; preds = %24, %18
store i32 %1, i32* %10
%29 = load i32, i32* @F_SETLK
%30 = call i32 @fcntl(i32 %0, i32 %29, %struct.flock* %3)
%31 = icmp eq i32 %30, 0
br i1 %31, label %17, label %18
}