#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_whence;
  int /*<<< orphan*/ l_pid;
  void *l_len;
  void *l_start;
};
typedef void *off_t;

/* Variables and functions */
int F_GETLK;
int F_RDLCK;
int F_SETLK;
int F_SETLKW;
int F_UNLCK;
int F_WRLCK;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ O_WRONLY;
int SEEK_CUR;
int SEEK_END;
int SEEK_SET;
void *atoi(char *);
int /*<<< orphan*/ dump_lock(char *, struct flock *);
int /*<<< orphan*/ err_sys(char *, char *);
char *errno;
int fcntl(int, int, struct flock *);
int /*<<< orphan*/ getpid();
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ sleep(int);
scalar_t__ strcasecmp(char *, char *);
int /*<<< orphan*/ usage();
int main(int argc, char *argv[]) {
  if (argc != 7)
    usage();
  off_t offset = atoi(argv[4]);
  off_t len = atoi(argv[6]);
  int whence = 0;
  if (strcasecmp(argv[5], "SET") == 0)
    whence = SEEK_SET;
  else if (strcasecmp(argv[5], "CUR") == 0)
    whence = SEEK_CUR;
  else if (strcasecmp(argv[5], "END") == 0)
    whence = SEEK_END;
  else
    usage();
  int type = 0;
  if (strcasecmp(argv[3], "READ") == 0)
    type = F_RDLCK;
  else if (strcasecmp(argv[3], "WRITE") == 0)
    type = F_WRLCK;
  else
    usage();
  int fd = open(argv[1], type == F_RDLCK ? O_RDONLY : O_WRONLY);
  if (fd == -1)
    err_sys("open %s failed", argv[1]);
  int action = 0;
  if (strcasecmp(argv[2], "LOCK") == 0)
    action = F_SETLK;
  else if (strcasecmp(argv[2], "LOCKWAIT") == 0)
    action = F_SETLKW;
  else if (strcasecmp(argv[2], "UNLOCK") == 0) {
    action = F_SETLK;
    type = F_UNLCK;
  } else if (strcasecmp(argv[2], "TESTLOCK") == 0)
    action = F_GETLK;
  else
    usage();
  struct flock lock;
  lock.l_type = type;
  lock.l_start = offset;
  lock.l_whence = whence;
  lock.l_len = len;
  dump_lock(argv[2], &lock);
  int ret = fcntl(fd, action, &lock);
  if (ret == -1)
    err_sys("fcntl failed, errno %d", errno);
  if (action == F_GETLK) {
    if (lock.l_type == F_UNLCK)
      printf("no lock in destination found\n");
    else {
      printf("find a lock owning by %lu\n", lock.l_pid);
      dump_lock("FINDLOCK", &lock);
    }
  } else {
    printf("[%lu] got lock\n", getpid());
    sleep(10);
  }
  printf("%lu exit\n", getpid());
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x7,-0x8(%rbp)
je     11c7 <main+0x27>
mov    $0x0,%al
call   1080 <usage@plt>
mov    -0x10(%rbp),%rax
mov    0x20(%rax),%rdi
call   10a0 <atoi@plt>
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    0x30(%rax),%rdi
call   10a0 <atoi@plt>
mov    %rax,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x10(%rbp),%rax
mov    0x28(%rax),%rdi
lea    0xe01(%rip),%rsi
call   1030 <strcasecmp@plt>
cmp    $0x0,%rax
jne    121f <main+0x7f>
mov    0x2d8b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
jmp    128e <main+0xee>
mov    -0x10(%rbp),%rax
mov    0x28(%rax),%rdi
lea    0xdd6(%rip),%rsi
call   1030 <strcasecmp@plt>
cmp    $0x0,%rax
jne    124e <main+0xae>
mov    0x2d9c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
jmp    1289 <main+0xe9>
mov    -0x10(%rbp),%rax
mov    0x28(%rax),%rdi
lea    0xdab(%rip),%rsi
call   1030 <strcasecmp@plt>
cmp    $0x0,%rax
jne    127d <main+0xdd>
mov    0x2d75(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
jmp    1284 <main+0xe4>
mov    $0x0,%al
call   1080 <usage@plt>
jmp    1289 <main+0xe9>
jmp    128e <main+0xee>
movl   $0x0,-0x28(%rbp)
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rdi
lea    0xd68(%rip),%rsi
call   1030 <strcasecmp@plt>
cmp    $0x0,%rax
jne    12c4 <main+0x124>
mov    0x2cc6(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
jmp    12ff <main+0x15f>
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rdi
lea    0xd3e(%rip),%rsi
call   1030 <strcasecmp@plt>
cmp    $0x0,%rax
jne    12f3 <main+0x153>
mov    0x2caf(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
jmp    12fa <main+0x15a>
mov    $0x0,%al
call   1080 <usage@plt>
jmp    12ff <main+0x15f>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x60(%rbp)
mov    -0x28(%rbp),%eax
mov    0x2c6b(%rip),%rcx
cmp    (%rcx),%eax
jne    132e <main+0x18e>
mov    0x2cb4(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x64(%rbp)
jmp    133a <main+0x19a>
mov    0x2c53(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x64(%rbp)
mov    -0x60(%rbp),%rdi
mov    -0x64(%rbp),%esi
call   1090 <open@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0xffffffff,-0x2c(%rbp)
jne    1367 <main+0x1c7>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xcb5(%rip),%rdi
call   1040 <err_sys@plt>
movl   $0x0,-0x30(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
lea    0xd16(%rip),%rsi
call   1030 <strcasecmp@plt>
cmp    $0x0,%rax
jne    139d <main+0x1fd>
mov    0x2c35(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
jmp    144c <main+0x2ac>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
lea    0xc7a(%rip),%rsi
call   1030 <strcasecmp@plt>
cmp    $0x0,%rax
jne    13cc <main+0x22c>
mov    0x2c0e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
jmp    1447 <main+0x2a7>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
lea    0xc54(%rip),%rsi
call   1030 <strcasecmp@plt>
cmp    $0x0,%rax
jne    1407 <main+0x267>
mov    0x2bd7(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    0x2bbb(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
jmp    1442 <main+0x2a2>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
lea    0xc20(%rip),%rsi
call   1030 <strcasecmp@plt>
cmp    $0x0,%rax
jne    1436 <main+0x296>
mov    0x2b94(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
jmp    143d <main+0x29d>
mov    $0x0,%al
call   1080 <usage@plt>
jmp    1442 <main+0x2a2>
jmp    1447 <main+0x2a7>
jmp    144c <main+0x2ac>
mov    -0x28(%rbp),%eax
mov    %eax,-0x50(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x24(%rbp),%eax
mov    %eax,-0x4c(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
lea    -0x50(%rbp),%rsi
call   10c0 <dump_lock@plt>
mov    -0x2c(%rbp),%edi
mov    -0x30(%rbp),%esi
lea    -0x50(%rbp),%rdx
call   1050 <fcntl@plt>
mov    %eax,-0x54(%rbp)
cmpl   $0xffffffff,-0x54(%rbp)
jne    14ab <main+0x30b>
mov    0x2b0c(%rip),%rax
mov    (%rax),%rsi
lea    0xb99(%rip),%rdi
call   1040 <err_sys@plt>
mov    -0x30(%rbp),%eax
mov    0x2b0b(%rip),%rcx
cmp    (%rcx),%eax
jne    1508 <main+0x368>
mov    -0x50(%rbp),%eax
mov    0x2af1(%rip),%rcx
cmp    (%rcx),%eax
jne    14e2 <main+0x342>
lea    0xb80(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
jmp    1503 <main+0x363>
mov    -0x48(%rbp),%esi
lea    0xb88(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
lea    0xb95(%rip),%rdi
lea    -0x50(%rbp),%rsi
call   10c0 <dump_lock@plt>
jmp    1529 <main+0x389>
mov    $0x0,%al
call   1060 <getpid@plt>
mov    %eax,%esi
lea    0xb80(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    $0xa,%edi
call   10b0 <sleep@plt>
mov    $0x0,%al
call   1060 <getpid@plt>
mov    %eax,%esi
lea    0xb6f(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x4(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    %rsi,%r13
cmp    $0x7,%edi
je     11cd <main+0x1d>
xor    %eax,%eax
call   1090 <usage@plt>
mov    0x20(%r13),%rdi
call   10b0 <atoi@plt>
mov    %rax,0x10(%rsp)
mov    0x30(%r13),%rdi
call   10b0 <atoi@plt>
mov    %rax,0x8(%rsp)
mov    0x28(%r13),%rbx
lea    0xe0c(%rip),%rsi
mov    %rbx,%rdi
call   1030 <strcasecmp@plt>
test   %rax,%rax
je     1235 <main+0x85>
lea    0xdfc(%rip),%rsi
mov    %rbx,%rdi
call   1030 <strcasecmp@plt>
test   %rax,%rax
je     123e <main+0x8e>
lea    0xdec(%rip),%rsi
mov    %rbx,%rdi
call   1030 <strcasecmp@plt>
test   %rax,%rax
je     1247 <main+0x97>
xor    %r15d,%r15d
xor    %eax,%eax
call   1090 <usage@plt>
jmp    1251 <main+0xa1>
mov    0x2d64(%rip),%rax
jmp    124e <main+0x9e>
mov    0x2d9b(%rip),%rax
jmp    124e <main+0x9e>
mov    0x2d9a(%rip),%rax
mov    (%rax),%r15d
mov    0x18(%r13),%rbx
lea    0xdb0(%rip),%rsi
mov    %rbx,%rdi
call   1030 <strcasecmp@plt>
test   %rax,%rax
je     1288 <main+0xd8>
lea    0xda1(%rip),%rsi
mov    %rbx,%rdi
call   1030 <strcasecmp@plt>
test   %rax,%rax
je     1291 <main+0xe1>
xor    %ebx,%ebx
xor    %eax,%eax
call   1090 <usage@plt>
jmp    129a <main+0xea>
mov    0x2cf1(%rip),%rax
jmp    1298 <main+0xe8>
mov    0x2d00(%rip),%rax
mov    (%rax),%ebx
mov    0x8(%r13),%rdi
mov    0x2cdb(%rip),%rax
cmp    (%rax),%ebx
jne    12b2 <main+0x102>
mov    0x2d28(%rip),%rax
jmp    12b9 <main+0x109>
mov    0x2ccf(%rip),%rax
mov    (%rax),%esi
call   10a0 <open@plt>
mov    %eax,%r12d
cmp    $0xffffffff,%eax
jne    12d8 <main+0x128>
mov    0x8(%r13),%rsi
lea    0xd44(%rip),%rdi
call   1050 <err_sys@plt>
mov    0x10(%r13),%rbp
lea    0xd92(%rip),%rsi
mov    %rbp,%rdi
call   1030 <strcasecmp@plt>
test   %rax,%rax
je     133c <main+0x18c>
lea    0xd2f(%rip),%rsi
mov    %rbp,%rdi
call   1030 <strcasecmp@plt>
test   %rax,%rax
je     1345 <main+0x195>
lea    0xd24(%rip),%rsi
mov    %rbp,%rdi
call   1030 <strcasecmp@plt>
test   %rax,%rax
je     134e <main+0x19e>
lea    0xd17(%rip),%rsi
mov    %rbp,%rdi
call   1030 <strcasecmp@plt>
test   %rax,%rax
je     1363 <main+0x1b3>
xor    %r14d,%r14d
xor    %eax,%eax
call   1090 <usage@plt>
mov    0x10(%r13),%rbp
jmp    136d <main+0x1bd>
mov    0x2c85(%rip),%rax
jmp    136a <main+0x1ba>
mov    0x2c84(%rip),%rax
jmp    136a <main+0x1ba>
mov    0x2c73(%rip),%rax
mov    (%rax),%r14d
mov    0x2c59(%rip),%rax
mov    (%rax),%ebx
jmp    136d <main+0x1bd>
mov    0x2c56(%rip),%rax
mov    (%rax),%r14d
mov    0x10(%rsp),%rax
mov    0x8(%rsp),%rcx
mov    %ebx,0x18(%rsp)
mov    %rax,0x30(%rsp)
mov    %r15d,0x1c(%rsp)
mov    %rcx,0x28(%rsp)
lea    0x18(%rsp),%rbx
mov    %rbp,%rdi
mov    %rbx,%rsi
call   10d0 <dump_lock@plt>
mov    %r12d,%edi
mov    %r14d,%esi
mov    %rbx,%rdx
call   1060 <fcntl@plt>
cmp    $0xffffffff,%eax
jne    13c3 <main+0x213>
mov    0x2bf4(%rip),%rax
mov    (%rax),%rsi
lea    0xc81(%rip),%rdi
call   1050 <err_sys@plt>
mov    0x2bf6(%rip),%rax
cmp    (%rax),%r14d
jne    13ec <main+0x23c>
mov    0x18(%rsp),%eax
mov    0x2bde(%rip),%rcx
cmp    (%rcx),%eax
jne    140f <main+0x25f>
lea    0xcaf(%rip),%rdi
call   1040 <puts@plt>
jmp    1432 <main+0x282>
xor    %eax,%eax
call   1070 <getpid@plt>
lea    0xc80(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1080 <printf@plt>
mov    $0xa,%edi
call   10c0 <sleep@plt>
jmp    1432 <main+0x282>
mov    0x20(%rsp),%esi
lea    0xc3c(%rip),%rdi
xor    %eax,%eax
call   1080 <printf@plt>
lea    0xc49(%rip),%rdi
lea    0x18(%rsp),%rsi
call   10d0 <dump_lock@plt>
xor    %eax,%eax
call   1070 <getpid@plt>
lea    0xc4a(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1080 <printf@plt>
xor    %eax,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.flock = type { i32, i32, i32, i8*, i8* }
@.str = [4 x i8] c"SET\00"
@SEEK_SET = global i32 0
@.str.1 = [4 x i8] c"CUR\00"
@SEEK_CUR = global i32 0
@.str.2 = [4 x i8] c"END\00"
@SEEK_END = global i32 0
@.str.3 = [5 x i8] c"READ\00"
@F_RDLCK = global i32 0
@.str.4 = [6 x i8] c"WRITE\00"
@F_WRLCK = global i32 0
@O_RDONLY = global i32 0
@O_WRONLY = global i32 0
@.str.5 = [15 x i8] c"open %s failed\00"
@.str.6 = [5 x i8] c"LOCK\00"
@F_SETLK = global i32 0
@.str.7 = [9 x i8] c"LOCKWAIT\00"
@F_SETLKW = global i32 0
@.str.8 = [7 x i8] c"UNLOCK\00"
@F_UNLCK = global i32 0
@.str.9 = [9 x i8] c"TESTLOCK\00"
@F_GETLK = global i32 0
@.str.10 = [23 x i8] c"fcntl failed, errno %d\00"
@errno = global i8* null
@.str.11 = [30 x i8] c"no lock in destination found\0A\00"
@.str.12 = [27 x i8] c"find a lock owning by %lu\0A\00"
@.str.13 = [9 x i8] c"FINDLOCK\00"
@.str.14 = [16 x i8] c"[%lu] got lock\0A\00"
@.str.15 = [10 x i8] c"%lu exit\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca %struct.flock
%13 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%14 = load i32, i32* %4
%15 = icmp ne i32 %14, 7
br i1 %15, label %16, label %18
16: ; preds = %2
%17 = call i32 (...) @usage()
br label %18
18: ; preds = %16, %2
%19 = load i8**, i8*** %5
%20 = getelementptr inbounds i8*, i8** %19, i64 4
%21 = load i8*, i8** %20
%22 = call i8* @atoi(i8* %21)
store i8* %22, i8** %6
%23 = load i8**, i8*** %5
%24 = getelementptr inbounds i8*, i8** %23, i64 6
%25 = load i8*, i8** %24
%26 = call i8* @atoi(i8* %25)
store i8* %26, i8** %7
store i32 0, i32* %8
%27 = load i8**, i8*** %5
%28 = getelementptr inbounds i8*, i8** %27, i64 5
%29 = load i8*, i8** %28
%30 = call i64 @strcasecmp(i8* %29, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%31 = icmp eq i64 %30, 0
br i1 %31, label %32, label %34
32: ; preds = %18
%33 = load i32, i32* @SEEK_SET
store i32 %33, i32* %8
br label %54
34: ; preds = %18
%35 = load i8**, i8*** %5
%36 = getelementptr inbounds i8*, i8** %35, i64 5
%37 = load i8*, i8** %36
%38 = call i64 @strcasecmp(i8* %37, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%39 = icmp eq i64 %38, 0
br i1 %39, label %40, label %42
40: ; preds = %34
%41 = load i32, i32* @SEEK_CUR
store i32 %41, i32* %8
br label %53
42: ; preds = %34
%43 = load i8**, i8*** %5
%44 = getelementptr inbounds i8*, i8** %43, i64 5
%45 = load i8*, i8** %44
%46 = call i64 @strcasecmp(i8* %45, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0))
%47 = icmp eq i64 %46, 0
br i1 %47, label %48, label %50
48: ; preds = %42
%49 = load i32, i32* @SEEK_END
store i32 %49, i32* %8
br label %52
50: ; preds = %42
%51 = call i32 (...) @usage()
br label %52
52: ; preds = %50, %48
br label %53
53: ; preds = %52, %40
br label %54
54: ; preds = %53, %32
store i32 0, i32* %9
%55 = load i8**, i8*** %5
%56 = getelementptr inbounds i8*, i8** %55, i64 3
%57 = load i8*, i8** %56
%58 = call i64 @strcasecmp(i8* %57, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%59 = icmp eq i64 %58, 0
br i1 %59, label %60, label %62
60: ; preds = %54
%61 = load i32, i32* @F_RDLCK
store i32 %61, i32* %9
br label %73
62: ; preds = %54
%63 = load i8**, i8*** %5
%64 = getelementptr inbounds i8*, i8** %63, i64 3
%65 = load i8*, i8** %64
%66 = call i64 @strcasecmp(i8* %65, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0))
%67 = icmp eq i64 %66, 0
br i1 %67, label %68, label %70
68: ; preds = %62
%69 = load i32, i32* @F_WRLCK
store i32 %69, i32* %9
br label %72
70: ; preds = %62
%71 = call i32 (...) @usage()
br label %72
72: ; preds = %70, %68
br label %73
73: ; preds = %72, %60
%74 = load i8**, i8*** %5
%75 = getelementptr inbounds i8*, i8** %74, i64 1
%76 = load i8*, i8** %75
%77 = load i32, i32* %9
%78 = load i32, i32* @F_RDLCK
%79 = icmp eq i32 %77, %78
br i1 %79, label %80, label %82
80: ; preds = %73
%81 = load i32, i32* @O_RDONLY
br label %84
82: ; preds = %73
%83 = load i32, i32* @O_WRONLY
br label %84
84: ; preds = %82, %80
%85 = phi i32 [ %81, %80 ], [ %83, %82 ]
%86 = call i32 @open(i8* %76, i32 %85)
store i32 %86, i32* %10
%87 = load i32, i32* %10
%88 = icmp eq i32 %87, -1
br i1 %88, label %89, label %94
89: ; preds = %84
%90 = load i8**, i8*** %5
%91 = getelementptr inbounds i8*, i8** %90, i64 1
%92 = load i8*, i8** %91
%93 = call i32 @err_sys(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.5, i64 0, i64 0), i8* %92)
br label %94
94: ; preds = %89, %84
store i32 0, i32* %11
%95 = load i8**, i8*** %5
%96 = getelementptr inbounds i8*, i8** %95, i64 2
%97 = load i8*, i8** %96
%98 = call i64 @strcasecmp(i8* %97, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.6, i64 0, i64 0))
%99 = icmp eq i64 %98, 0
br i1 %99, label %100, label %102
100: ; preds = %94
%101 = load i32, i32* @F_SETLK
store i32 %101, i32* %11
br label %132
102: ; preds = %94
%103 = load i8**, i8*** %5
%104 = getelementptr inbounds i8*, i8** %103, i64 2
%105 = load i8*, i8** %104
%106 = call i64 @strcasecmp(i8* %105, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.7, i64 0, i64 0))
%107 = icmp eq i64 %106, 0
br i1 %107, label %108, label %110
108: ; preds = %102
%109 = load i32, i32* @F_SETLKW
store i32 %109, i32* %11
br label %131
110: ; preds = %102
%111 = load i8**, i8*** %5
%112 = getelementptr inbounds i8*, i8** %111, i64 2
%113 = load i8*, i8** %112
%114 = call i64 @strcasecmp(i8* %113, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.8, i64 0, i64 0))
%115 = icmp eq i64 %114, 0
br i1 %115, label %116, label %119
116: ; preds = %110
%117 = load i32, i32* @F_SETLK
store i32 %117, i32* %11
%118 = load i32, i32* @F_UNLCK
store i32 %118, i32* %9
br label %130
119: ; preds = %110
%120 = load i8**, i8*** %5
%121 = getelementptr inbounds i8*, i8** %120, i64 2
%122 = load i8*, i8** %121
%123 = call i64 @strcasecmp(i8* %122, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.9, i64 0, i64 0))
%124 = icmp eq i64 %123, 0
br i1 %124, label %125, label %127
125: ; preds = %119
%126 = load i32, i32* @F_GETLK
store i32 %126, i32* %11
br label %129
127: ; preds = %119
%128 = call i32 (...) @usage()
br label %129
129: ; preds = %127, %125
br label %130
130: ; preds = %129, %116
br label %131
131: ; preds = %130, %108
br label %132
132: ; preds = %131, %100
%133 = load i32, i32* %9
%134 = getelementptr inbounds %struct.flock, %struct.flock* %12, i32 0, i32 0
store i32 %133, i32* %134
%135 = load i8*, i8** %6
%136 = getelementptr inbounds %struct.flock, %struct.flock* %12, i32 0, i32 4
store i8* %135, i8** %136
%137 = load i32, i32* %8
%138 = getelementptr inbounds %struct.flock, %struct.flock* %12, i32 0, i32 1
store i32 %137, i32* %138
%139 = load i8*, i8** %7
%140 = getelementptr inbounds %struct.flock, %struct.flock* %12, i32 0, i32 3
store i8* %139, i8** %140
%141 = load i8**, i8*** %5
%142 = getelementptr inbounds i8*, i8** %141, i64 2
%143 = load i8*, i8** %142
%144 = call i32 @dump_lock(i8* %143, %struct.flock* %12)
%145 = load i32, i32* %10
%146 = load i32, i32* %11
%147 = call i32 @fcntl(i32 %145, i32 %146, %struct.flock* %12)
store i32 %147, i32* %13
%148 = load i32, i32* %13
%149 = icmp eq i32 %148, -1
br i1 %149, label %150, label %153
150: ; preds = %132
%151 = load i8*, i8** @errno
%152 = call i32 @err_sys(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.10, i64 0, i64 0), i8* %151)
br label %153
153: ; preds = %150, %132
%154 = load i32, i32* %11
%155 = load i32, i32* @F_GETLK
%156 = icmp eq i32 %154, %155
br i1 %156, label %157, label %170
157: ; preds = %153
%158 = getelementptr inbounds %struct.flock, %struct.flock* %12, i32 0, i32 0
%159 = load i32, i32* %158
%160 = load i32, i32* @F_UNLCK
%161 = icmp eq i32 %159, %160
br i1 %161, label %162, label %164
162: ; preds = %157
%163 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.11, i64 0, i64 0))
br label %169
164: ; preds = %157
%165 = getelementptr inbounds %struct.flock, %struct.flock* %12, i32 0, i32 2
%166 = load i32, i32* %165
%167 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.12, i64 0, i64 0), i32 %166)
%168 = call i32 @dump_lock(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.13, i64 0, i64 0), %struct.flock* %12)
br label %169
169: ; preds = %164, %162
br label %174
170: ; preds = %153
%171 = call i32 (...) @getpid()
%172 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.14, i64 0, i64 0), i32 %171)
%173 = call i32 @sleep(i32 10)
br label %174
174: ; preds = %170, %169
%175 = call i32 (...) @getpid()
%176 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.15, i64 0, i64 0), i32 %175)
%177 = load i32, i32* %3
ret i32 %177
}

LLVM IR (-O2):
%struct.flock = type { i32, i32, i32, i8*, i8* }
@.str = [4 x i8] c"SET\00"
@SEEK_SET = global i32 0
@.str.1 = [4 x i8] c"CUR\00"
@SEEK_CUR = global i32 0
@.str.2 = [4 x i8] c"END\00"
@SEEK_END = global i32 0
@.str.3 = [5 x i8] c"READ\00"
@F_RDLCK = global i32 0
@.str.4 = [6 x i8] c"WRITE\00"
@F_WRLCK = global i32 0
@O_RDONLY = global i32 0
@O_WRONLY = global i32 0
@.str.5 = [15 x i8] c"open %s failed\00"
@.str.6 = [5 x i8] c"LOCK\00"
@F_SETLK = global i32 0
@.str.7 = [9 x i8] c"LOCKWAIT\00"
@F_SETLKW = global i32 0
@.str.8 = [7 x i8] c"UNLOCK\00"
@F_UNLCK = global i32 0
@.str.9 = [9 x i8] c"TESTLOCK\00"
@F_GETLK = global i32 0
@.str.10 = [23 x i8] c"fcntl failed, errno %d\00"
@errno = global i8* null
@.str.12 = [27 x i8] c"find a lock owning by %lu\0A\00"
@.str.13 = [9 x i8] c"FINDLOCK\00"
@.str.14 = [16 x i8] c"[%lu] got lock\0A\00"
@.str.15 = [10 x i8] c"%lu exit\0A\00"
@str = [29 x i8] c"no lock in destination found\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.flock
%4 = icmp eq i32 %0, 7
br i1 %4, label %7, label %5
5: ; preds = %2
%6 = tail call i32 (...) @usage()
br label %7
7: ; preds = %5, %2
%8 = getelementptr inbounds i8*, i8** %1, i64 4
%9 = load i8*, i8** %8
%10 = tail call i8* @atoi(i8* %9)
%11 = getelementptr inbounds i8*, i8** %1, i64 6
%12 = load i8*, i8** %11
%13 = tail call i8* @atoi(i8* %12)
%14 = getelementptr inbounds i8*, i8** %1, i64 5
%15 = load i8*, i8** %14
%16 = tail call i64 @strcasecmp(i8* %15, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%17 = icmp eq i64 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %7
%19 = load i32, i32* @SEEK_SET
br label %32
20: ; preds = %7
%21 = tail call i64 @strcasecmp(i8* %15, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%22 = icmp eq i64 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %20
%24 = load i32, i32* @SEEK_CUR
br label %32
25: ; preds = %20
%26 = tail call i64 @strcasecmp(i8* %15, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0))
%27 = icmp eq i64 %26, 0
br i1 %27, label %28, label %30
28: ; preds = %25
%29 = load i32, i32* @SEEK_END
br label %32
30: ; preds = %25
%31 = tail call i32 (...) @usage()
br label %32
32: ; preds = %23, %30, %28, %18
%33 = phi i32 [ %19, %18 ], [ %24, %23 ], [ %29, %28 ], [ 0, %30 ]
%34 = getelementptr inbounds i8*, i8** %1, i64 3
%35 = load i8*, i8** %34
%36 = tail call i64 @strcasecmp(i8* %35, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%37 = icmp eq i64 %36, 0
br i1 %37, label %38, label %40
38: ; preds = %32
%39 = load i32, i32* @F_RDLCK
br label %47
40: ; preds = %32
%41 = tail call i64 @strcasecmp(i8* %35, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0))
%42 = icmp eq i64 %41, 0
br i1 %42, label %43, label %45
43: ; preds = %40
%44 = load i32, i32* @F_WRLCK
br label %47
45: ; preds = %40
%46 = tail call i32 (...) @usage()
br label %47
47: ; preds = %43, %45, %38
%48 = phi i32 [ %39, %38 ], [ %44, %43 ], [ 0, %45 ]
%49 = getelementptr inbounds i8*, i8** %1, i64 1
%50 = load i8*, i8** %49
%51 = load i32, i32* @F_RDLCK
%52 = icmp eq i32 %48, %51
%53 = load i32, i32* @O_RDONLY
%54 = load i32, i32* @O_WRONLY
%55 = select i1 %52, i32 %53, i32 %54
%56 = tail call i32 @open(i8* %50, i32 %55)
%57 = icmp eq i32 %56, -1
br i1 %57, label %58, label %61
58: ; preds = %47
%59 = load i8*, i8** %49
%60 = tail call i32 @err_sys(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.5, i64 0, i64 0), i8* %59)
br label %61
61: ; preds = %58, %47
%62 = getelementptr inbounds i8*, i8** %1, i64 2
%63 = load i8*, i8** %62
%64 = tail call i64 @strcasecmp(i8* %63, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.6, i64 0, i64 0))
%65 = icmp eq i64 %64, 0
br i1 %65, label %66, label %68
66: ; preds = %61
%67 = load i32, i32* @F_SETLK
br label %87
68: ; preds = %61
%69 = tail call i64 @strcasecmp(i8* %63, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.7, i64 0, i64 0))
%70 = icmp eq i64 %69, 0
br i1 %70, label %71, label %73
71: ; preds = %68
%72 = load i32, i32* @F_SETLKW
br label %87
73: ; preds = %68
%74 = tail call i64 @strcasecmp(i8* %63, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.8, i64 0, i64 0))
%75 = icmp eq i64 %74, 0
br i1 %75, label %76, label %79
76: ; preds = %73
%77 = load i32, i32* @F_SETLK
%78 = load i32, i32* @F_UNLCK
br label %87
79: ; preds = %73
%80 = tail call i64 @strcasecmp(i8* %63, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.9, i64 0, i64 0))
%81 = icmp eq i64 %80, 0
br i1 %81, label %82, label %84
82: ; preds = %79
%83 = load i32, i32* @F_GETLK
br label %87
84: ; preds = %79
%85 = tail call i32 (...) @usage()
%86 = load i8*, i8** %62
br label %87
87: ; preds = %71, %82, %84, %76, %66
%88 = phi i8* [ %63, %66 ], [ %63, %71 ], [ %63, %76 ], [ %63, %82 ], [ %86, %84 ]
%89 = phi i32 [ %48, %66 ], [ %48, %71 ], [ %78, %76 ], [ %48, %82 ], [ %48, %84 ]
%90 = phi i32 [ %67, %66 ], [ %72, %71 ], [ %77, %76 ], [ %83, %82 ], [ 0, %84 ]
%91 = bitcast %struct.flock* %3 to i8*
%92 = getelementptr inbounds %struct.flock, %struct.flock* %3, i64 0, i32 0
store i32 %89, i32* %92
%93 = getelementptr inbounds %struct.flock, %struct.flock* %3, i64 0, i32 4
store i8* %10, i8** %93
%94 = getelementptr inbounds %struct.flock, %struct.flock* %3, i64 0, i32 1
store i32 %33, i32* %94
%95 = getelementptr inbounds %struct.flock, %struct.flock* %3, i64 0, i32 3
store i8* %13, i8** %95
%96 = call i32 @dump_lock(i8* %88, %struct.flock* %3)
%97 = call i32 @fcntl(i32 %56, i32 %90, %struct.flock* %3)
%98 = icmp eq i32 %97, -1
br i1 %98, label %99, label %102
99: ; preds = %87
%100 = load i8*, i8** @errno
%101 = call i32 @err_sys(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.10, i64 0, i64 0), i8* %100)
br label %102
102: ; preds = %99, %87
%103 = load i32, i32* @F_GETLK
%104 = icmp eq i32 %90, %103
br i1 %104, label %105, label %116
105: ; preds = %102
%106 = load i32, i32* %92
%107 = load i32, i32* @F_UNLCK
%108 = icmp eq i32 %106, %107
br i1 %108, label %109, label %111
109: ; preds = %105
%110 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @str, i64 0, i64 0))
br label %120
111: ; preds = %105
%112 = getelementptr inbounds %struct.flock, %struct.flock* %3, i64 0, i32 2
%113 = load i32, i32* %112
%114 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @.str.12, i64 0, i64 0), i32 %113)
%115 = call i32 @dump_lock(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.13, i64 0, i64 0), %struct.flock* %3)
br label %120
116: ; preds = %102
%117 = call i32 (...) @getpid()
%118 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.14, i64 0, i64 0), i32 %117)
%119 = call i32 @sleep(i32 10)
br label %120
120: ; preds = %109, %111, %116
%121 = call i32 (...) @getpid()
%122 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str.15, i64 0, i64 0), i32 %121)
ret i32 0
}