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

/* Variables and functions */
int /*<<< orphan*/ F_GETLK;
int /*<<< orphan*/ F_OK;
scalar_t__ F_RDLCK;
int /*<<< orphan*/ F_SETLK;
scalar_t__ F_UNLCK;
scalar_t__ F_WRLCK;
int O_CREAT;
int O_RDWR;
int SEEK_SET;
int /*<<< orphan*/ access(char *, int /*<<< orphan*/);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fcntl(int, int /*<<< orphan*/, struct flock *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int open(char *, int, ...);
int /*<<< orphan*/ pause();
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ write(int, char *, int);
int main(int argc, char **argv) {
  int fd;
  if (!access("lock", F_OK))
    fd = open("lock", O_RDWR);
  else {
    fd = open("lock", O_RDWR | O_CREAT, 0644);
    write(fd, "abcde_12345", 12);
  }
  struct flock lock;
  lock.l_type = F_WRLCK;
  lock.l_whence = SEEK_SET;
  lock.l_start = 1;
  lock.l_len = 5;
  lock.l_pid = -1;
  fcntl(fd, F_GETLK, &lock);
  if (lock.l_type != F_UNLCK) {
    fprintf(stderr, "lock is bussy!\n");
    fprintf(stderr, "%d is holding the lock.\n", lock.l_pid);
    fprintf(stderr, "lock type: %s\n",
            lock.l_type == F_WRLCK
                ? "F_WRLCK"
                : (lock.l_type == F_RDLCK ? "F_RDLCK" : "unknown"));
    fprintf(stderr, "lock range: %ld ~ %ld (totally %d bytes)\n",
            lock.l_whence + lock.l_start,
            lock.l_whence + lock.l_start + lock.l_len, (int)lock.l_len);
    exit(0);
  } else {
    lock.l_type = F_WRLCK;
    fcntl(fd, F_SETLK, &lock);
    pause();
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e0b(%rip),%rax
mov    (%rax),%esi
lea    0xe6a(%rip),%rdi
call   1080 <access@plt>
cmp    $0x0,%eax
jne    11c3 <main+0x53>
mov    0x2e35(%rip),%rax
mov    (%rax),%esi
lea    0xe4c(%rip),%rdi
mov    $0x0,%al
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
jmp    11ff <main+0x8f>
mov    0x2e16(%rip),%rax
mov    (%rax),%esi
mov    0x2e25(%rip),%rax
or     (%rax),%esi
lea    0xe24(%rip),%rdi
mov    $0x1a4,%edx
mov    $0x0,%al
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%edi
lea    0xe10(%rip),%rsi
mov    $0xc,%edx
call   1040 <write@plt>
mov    0x2da2(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x30(%rbp)
mov    0x2d9c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
movl   $0x1,-0x24(%rbp)
movl   $0x5,-0x20(%rbp)
movl   $0xffffffff,-0x1c(%rbp)
mov    -0x14(%rbp),%edi
mov    0x2d98(%rip),%rax
mov    (%rax),%esi
lea    -0x30(%rbp),%rdx
call   1030 <fcntl@plt>
mov    -0x30(%rbp),%rax
mov    0x2d7a(%rip),%rcx
cmp    (%rcx),%rax
je     1325 <main+0x1b5>
mov    0x2d5a(%rip),%rax
mov    (%rax),%edi
lea    0xdaa(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    0x2d43(%rip),%rax
mov    (%rax),%edi
mov    -0x1c(%rbp),%edx
lea    0xda0(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    0x2d29(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    -0x30(%rbp),%rax
mov    0x2d09(%rip),%rcx
cmp    (%rcx),%rax
jne    12b8 <main+0x148>
lea    0xd9a(%rip),%rax
mov    %rax,-0x40(%rbp)
jmp    12df <main+0x16f>
mov    -0x30(%rbp),%rdx
mov    0x2ccd(%rip),%rax
mov    (%rax),%rsi
lea    0xd8c(%rip),%rax
lea    0xd7d(%rip),%rcx
cmp    %rsi,%rdx
cmove  %rcx,%rax
mov    %rax,-0x40(%rbp)
mov    -0x34(%rbp),%edi
mov    -0x40(%rbp),%rdx
lea    0xd4d(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    0x2cbd(%rip),%rax
mov    (%rax),%edi
mov    -0x28(%rbp),%edx
add    -0x24(%rbp),%edx
mov    -0x28(%rbp),%ecx
add    -0x24(%rbp),%ecx
add    -0x20(%rbp),%ecx
mov    -0x20(%rbp),%r8d
lea    0xd4a(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
xor    %edi,%edi
call   1090 <exit@plt>
mov    0x2c7c(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x30(%rbp)
mov    -0x14(%rbp),%edi
mov    0x2c9b(%rip),%rax
mov    (%rax),%esi
lea    -0x30(%rbp),%rdx
call   1030 <fcntl@plt>
mov    $0x0,%al
call   1060 <pause@plt>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    0x2e1a(%rip),%rax
mov    (%rax),%esi
lea    0xe79(%rip),%rdi
call   1080 <access@plt>
mov    0x2e4d(%rip),%rcx
mov    (%rcx),%esi
test   %eax,%eax
je     11cc <main+0x5c>
mov    0x2e58(%rip),%rax
or     (%rax),%esi
lea    0xe57(%rip),%rdi
mov    $0x1a4,%edx
xor    %eax,%eax
call   1070 <open@plt>
mov    %eax,%ebx
lea    0xe47(%rip),%rsi
mov    %eax,%edi
mov    $0xc,%edx
call   1040 <write@plt>
jmp    11dc <main+0x6c>
lea    0xe2d(%rip),%rdi
xor    %eax,%eax
call   1070 <open@plt>
mov    %eax,%ebx
mov    0x2dc5(%rip),%r14
mov    (%r14),%rax
mov    %rax,(%rsp)
mov    0x2dbf(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
movabs $0x500000001,%rax
mov    %rax,0xc(%rsp)
movl   $0xffffffff,0x14(%rsp)
ff
mov    0x2dbb(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
mov    %ebx,%edi
call   1030 <fcntl@plt>
mov    (%rsp),%rax
mov    0x2d9c(%rip),%rcx
cmp    (%rcx),%rax
jne    125c <main+0xec>
mov    (%r14),%rax
mov    %rax,(%rsp)
mov    0x2d99(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
mov    %ebx,%edi
call   1030 <fcntl@plt>
xor    %eax,%eax
call   1060 <pause@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret
mov    0x2d55(%rip),%rbx
mov    (%rbx),%edi
lea    0xda5(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    (%rbx),%edi
mov    0x14(%rsp),%edx
lea    0xda1(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    (%rsp),%rax
mov    0x2cfe(%rip),%rcx
cmp    (%rcx),%rax
lea    0xdb5(%rip),%rcx
lea    0xdb6(%rip),%rsi
cmove  %rcx,%rsi
cmp    (%r14),%rax
mov    (%rbx),%edi
lea    0xd96(%rip),%rdx
cmovne %rsi,%rdx
lea    0xd7c(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    (%rbx),%edi
mov    0xc(%rsp),%edx
mov    0x10(%rsp),%r8d
add    0x8(%rsp),%edx
lea    (%rdx,%r8,1),%ecx
lea    0xd82(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
xor    %edi,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
%struct.flock = type { i64, i32, i32, i32, i32 }
@.str = [5 x i8] c"lock\00"
@F_OK = global i32 0
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@.str.1 = [12 x i8] c"abcde_12345\00"
@F_WRLCK = global i64 0
@SEEK_SET = global i32 0
@F_GETLK = global i32 0
@F_UNLCK = global i64 0
@stderr = global i32 0
@.str.2 = [16 x i8] c"lock is bussy!\0A\00"
@.str.3 = [25 x i8] c"%d is holding the lock.\0A\00"
@.str.4 = [15 x i8] c"lock type: %s\0A\00"
@.str.5 = [8 x i8] c"F_WRLCK\00"
@F_RDLCK = global i64 0
@.str.6 = [8 x i8] c"F_RDLCK\00"
@.str.7 = [8 x i8] c"unknown\00"
@.str.8 = [42 x i8] c"lock range: %ld ~ %ld (totally %d bytes)\0A\00"
@F_SETLK = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca %struct.flock
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = load i32, i32* @F_OK
%9 = call i32 @access(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 %8)
%10 = icmp ne i32 %9, 0
br i1 %10, label %14, label %11
11: ; preds = %2
%12 = load i32, i32* @O_RDWR
%13 = call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 %12)
store i32 %13, i32* %6
br label %21
14: ; preds = %2
%15 = load i32, i32* @O_RDWR
%16 = load i32, i32* @O_CREAT
%17 = or i32 %15, %16
%18 = call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 %17, i32 420)
store i32 %18, i32* %6
%19 = load i32, i32* %6
%20 = call i32 @write(i32 %19, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 12)
br label %21
21: ; preds = %14, %11
%22 = load i64, i64* @F_WRLCK
%23 = getelementptr inbounds %struct.flock, %struct.flock* %7, i32 0, i32 0
store i64 %22, i64* %23
%24 = load i32, i32* @SEEK_SET
%25 = getelementptr inbounds %struct.flock, %struct.flock* %7, i32 0, i32 1
store i32 %24, i32* %25
%26 = getelementptr inbounds %struct.flock, %struct.flock* %7, i32 0, i32 2
store i32 1, i32* %26
%27 = getelementptr inbounds %struct.flock, %struct.flock* %7, i32 0, i32 3
store i32 5, i32* %27
%28 = getelementptr inbounds %struct.flock, %struct.flock* %7, i32 0, i32 4
store i32 -1, i32* %28
%29 = load i32, i32* %6
%30 = load i32, i32* @F_GETLK
%31 = call i32 @fcntl(i32 %29, i32 %30, %struct.flock* %7)
%32 = getelementptr inbounds %struct.flock, %struct.flock* %7, i32 0, i32 0
%33 = load i64, i64* %32
%34 = load i64, i64* @F_UNLCK
%35 = icmp ne i64 %33, %34
br i1 %35, label %36, label %77
36: ; preds = %21
%37 = load i32, i32* @stderr
%38 = call i32 (i32, i8*, ...) @fprintf(i32 %37, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
%39 = load i32, i32* @stderr
%40 = getelementptr inbounds %struct.flock, %struct.flock* %7, i32 0, i32 4
%41 = load i32, i32* %40
%42 = call i32 (i32, i8*, ...) @fprintf(i32 %39, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0), i32 %41)
%43 = load i32, i32* @stderr
%44 = getelementptr inbounds %struct.flock, %struct.flock* %7, i32 0, i32 0
%45 = load i64, i64* %44
%46 = load i64, i64* @F_WRLCK
%47 = icmp eq i64 %45, %46
br i1 %47, label %48, label %49
48: ; preds = %36
br label %56
49: ; preds = %36
%50 = getelementptr inbounds %struct.flock, %struct.flock* %7, i32 0, i32 0
%51 = load i64, i64* %50
%52 = load i64, i64* @F_RDLCK
%53 = icmp eq i64 %51, %52
%54 = zext i1 %53 to i64
%55 = select i1 %53, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.6, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.7, i64 0, i64 0)
br label %56
56: ; preds = %49, %48
%57 = phi i8* [ getelementptr inbounds ([8 x i8], [8 x i8]* @.str.5, i64 0, i64 0), %48 ], [ %55, %49 ]
%58 = call i32 (i32, i8*, ...) @fprintf(i32 %43, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0), i8* %57)
%59 = load i32, i32* @stderr
%60 = getelementptr inbounds %struct.flock, %struct.flock* %7, i32 0, i32 1
%61 = load i32, i32* %60
%62 = getelementptr inbounds %struct.flock, %struct.flock* %7, i32 0, i32 2
%63 = load i32, i32* %62
%64 = add nsw i32 %61, %63
%65 = getelementptr inbounds %struct.flock, %struct.flock* %7, i32 0, i32 1
%66 = load i32, i32* %65
%67 = getelementptr inbounds %struct.flock, %struct.flock* %7, i32 0, i32 2
%68 = load i32, i32* %67
%69 = add nsw i32 %66, %68
%70 = getelementptr inbounds %struct.flock, %struct.flock* %7, i32 0, i32 3
%71 = load i32, i32* %70
%72 = add nsw i32 %69, %71
%73 = getelementptr inbounds %struct.flock, %struct.flock* %7, i32 0, i32 3
%74 = load i32, i32* %73
%75 = call i32 (i32, i8*, ...) @fprintf(i32 %59, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.8, i64 0, i64 0), i32 %64, i32 %72, i32 %74)
%76 = call i32 @exit(i32 0)
unreachable
77: ; preds = %21
%78 = load i64, i64* @F_WRLCK
%79 = getelementptr inbounds %struct.flock, %struct.flock* %7, i32 0, i32 0
store i64 %78, i64* %79
%80 = load i32, i32* %6
%81 = load i32, i32* @F_SETLK
%82 = call i32 @fcntl(i32 %80, i32 %81, %struct.flock* %7)
%83 = call i32 (...) @pause()
br label %84
84: ; preds = %77
ret i32 0
}

LLVM IR (-O2):
%struct.flock = type { i64, i32, i32, i32, i32 }
@.str = [5 x i8] c"lock\00"
@F_OK = global i32 0
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@.str.1 = [12 x i8] c"abcde_12345\00"
@F_WRLCK = global i64 0
@SEEK_SET = global i32 0
@F_GETLK = global i32 0
@F_UNLCK = global i64 0
@stderr = global i32 0
@.str.2 = [16 x i8] c"lock is bussy!\0A\00"
@.str.3 = [25 x i8] c"%d is holding the lock.\0A\00"
@.str.4 = [15 x i8] c"lock type: %s\0A\00"
@.str.5 = [8 x i8] c"F_WRLCK\00"
@F_RDLCK = global i64 0
@.str.6 = [8 x i8] c"F_RDLCK\00"
@.str.7 = [8 x i8] c"unknown\00"
@.str.8 = [42 x i8] c"lock range: %ld ~ %ld (totally %d bytes)\0A\00"
@F_SETLK = global i32 0
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca %struct.flock
%4 = load i32, i32* @F_OK
%5 = tail call i32 @access(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 %4)
%6 = icmp eq i32 %5, 0
%7 = load i32, i32* @O_RDWR
br i1 %6, label %8, label %10
8: ; preds = %2
%9 = tail call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 %7)
br label %15
10: ; preds = %2
%11 = load i32, i32* @O_CREAT
%12 = or i32 %11, %7
%13 = tail call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 %12, i32 420)
%14 = tail call i32 @write(i32 %13, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 12)
br label %15
15: ; preds = %10, %8
%16 = phi i32 [ %13, %10 ], [ %9, %8 ]
%17 = bitcast %struct.flock* %3 to i8*
%18 = load i64, i64* @F_WRLCK
%19 = getelementptr inbounds %struct.flock, %struct.flock* %3, i64 0, i32 0
store i64 %18, i64* %19
%20 = load i32, i32* @SEEK_SET
%21 = getelementptr inbounds %struct.flock, %struct.flock* %3, i64 0, i32 1
store i32 %20, i32* %21
%22 = getelementptr inbounds %struct.flock, %struct.flock* %3, i64 0, i32 2
store i32 1, i32* %22
%23 = getelementptr inbounds %struct.flock, %struct.flock* %3, i64 0, i32 3
store i32 5, i32* %23
%24 = getelementptr inbounds %struct.flock, %struct.flock* %3, i64 0, i32 4
store i32 -1, i32* %24
%25 = load i32, i32* @F_GETLK
%26 = call i32 @fcntl(i32 %16, i32 %25, %struct.flock* %3)
%27 = load i64, i64* %19
%28 = load i64, i64* @F_UNLCK
%29 = icmp eq i64 %27, %28
br i1 %29, label %53, label %30
30: ; preds = %15
%31 = load i32, i32* @stderr
%32 = call i32 (i32, i8*, ...) @fprintf(i32 %31, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
%33 = load i32, i32* @stderr
%34 = load i32, i32* %24
%35 = call i32 (i32, i8*, ...) @fprintf(i32 %33, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0), i32 %34)
%36 = load i32, i32* @stderr
%37 = load i64, i64* %19
%38 = load i64, i64* @F_WRLCK
%39 = icmp eq i64 %37, %38
%40 = load i64, i64* @F_RDLCK
%41 = icmp eq i64 %37, %40
%42 = select i1 %41, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.6, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.7, i64 0, i64 0)
%43 = select i1 %39, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.5, i64 0, i64 0), i8* %42
%44 = call i32 (i32, i8*, ...) @fprintf(i32 %36, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0), i8* %43)
%45 = load i32, i32* @stderr
%46 = load i32, i32* %21
%47 = load i32, i32* %22
%48 = add nsw i32 %47, %46
%49 = load i32, i32* %23
%50 = add nsw i32 %48, %49
%51 = call i32 (i32, i8*, ...) @fprintf(i32 %45, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.8, i64 0, i64 0), i32 %48, i32 %50, i32 %49)
%52 = call i32 @exit(i32 0)
unreachable
53: ; preds = %15
%54 = load i64, i64* @F_WRLCK
store i64 %54, i64* %19
%55 = load i32, i32* @F_SETLK
%56 = call i32 @fcntl(i32 %16, i32 %55, %struct.flock* %3)
%57 = call i32 (...) @pause()
ret i32 0
}