#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_type;
  int /*<<< orphan*/ l_pid;
  int /*<<< orphan*/ l_len;
  int /*<<< orphan*/ l_start;
  int /*<<< orphan*/ l_whence;
  int /*<<< orphan*/ member_4;
  int /*<<< orphan*/ member_3;
  int /*<<< orphan*/ member_2;
  int /*<<< orphan*/ member_1;
  int /*<<< orphan*/ member_0;
};

/* Variables and functions */
int /*<<< orphan*/ F_GETLK;
int /*<<< orphan*/ F_RDLCK;
int /*<<< orphan*/ F_SETLK;
int /*<<< orphan*/ F_UNLCK;
int /*<<< orphan*/ F_WRLCK;
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int fcntl(int, int /*<<< orphan*/, struct flock *);
int /*<<< orphan*/ getpid();
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int main(int argc, char *argv[]) {
  struct flock fl = {F_WRLCK, SEEK_SET, 0, 0, 0};
  int fd;
  if (argc != 2) {
    printf("Usage: unlock <file>\n");
    exit(1);
  }
  fl.l_pid = getpid();
  if ((fd = open(argv[1], O_RDWR)) == -1) {
    perror("open");
    exit(1);
  }
  if (fcntl(fd, F_GETLK, &fl) == -1) {
    perror("fcntl");
    exit(1);
  }
  printf("Lock found.\n");
  printf("%ld (READ = %ld, WRITE = %ld, UNLCK = %ld), %ld, %ld, %ld, %ld\n",
         fl.l_type, F_WRLCK, F_RDLCK, F_UNLCK, fl.l_whence, fl.l_start,
         fl.l_len, fl.l_pid);
  fl.l_type = F_UNLCK;
  if (fcntl(fd, F_SETLK, &fl) == -1) {
    perror("fcntl");
    exit(1);
  }
  printf("Unlocked.\n");
  close(fd);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x38(%rbp),%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1060 <memset@plt>
mov    0x2e12(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x38(%rbp)
mov    0x2e0e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
cmpl   $0x2,-0x4(%rbp)
je     11d9 <main+0x59>
lea    0xe38(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,-0x34(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2df6(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x3c(%rbp)
cmp    $0xffffffff,%eax
jne    121b <main+0x9b>
lea    0xe0a(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x3c(%rbp),%edi
mov    0x2db3(%rip),%rax
mov    (%rax),%esi
lea    -0x38(%rbp),%rdx
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
jne    124f <main+0xcf>
lea    0xddb(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
lea    0xdcb(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x38(%rbp),%esi
mov    0x2d51(%rip),%rax
mov    (%rax),%edx
mov    0x2d38(%rip),%rax
mov    (%rax),%ecx
mov    0x2d57(%rip),%rax
mov    (%rax),%r8d
mov    -0x28(%rbp),%r9d
mov    -0x2c(%rbp),%r11d
mov    -0x30(%rbp),%r10d
mov    -0x34(%rbp),%eax
lea    0xd9c(%rip),%rdi
mov    %r11d,(%rsp)
mov    %r10d,0x8(%rsp)
mov    %eax,0x10(%rsp)
mov    $0x0,%al
call   1050 <printf@plt>
mov    0x2d23(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x38(%rbp)
mov    -0x3c(%rbp),%edi
mov    0x2d24(%rip),%rax
mov    (%rax),%esi
lea    -0x38(%rbp),%rdx
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
jne    12e6 <main+0x166>
lea    0xd44(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
lea    0xd81(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x3c(%rbp),%edi
call   1070 <close@plt>
xor    %eax,%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x30,%rsp
xorps  %xmm0,%xmm0
movups %xmm0,0x20(%rsp)
movups %xmm0,0x10(%rsp)
mov    0x2e24(%rip),%r14
mov    (%r14),%eax
mov    %eax,0x8(%rsp)
cmp    $0x2,%edi
jne    1291 <main+0x111>
mov    %rsi,%rbx
xor    %eax,%eax
call   1050 <getpid@plt>
mov    %eax,0xc(%rsp)
mov    0x8(%rbx),%rdi
mov    0x2e23(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
cmp    $0xffffffff,%eax
je     12a7 <main+0x127>
mov    %eax,%ebp
mov    0x2dfa(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
mov    %ebp,%edi
call   1040 <fcntl@plt>
cmp    $0xffffffff,%eax
je     1288 <main+0x108>
lea    0xe4f(%rip),%rdi
call   1030 <puts@plt>
mov    (%r14),%edx
mov    0x2da5(%rip),%rax
mov    (%rax),%ecx
mov    0x2dbc(%rip),%r14
mov    (%r14),%r8d
mov    0x18(%rsp),%r9d
mov    0x14(%rsp),%r10d
mov    0x10(%rsp),%r11d
mov    0x8(%rsp),%esi
mov    0xc(%rsp),%ebx
sub    $0x8,%rsp
lea    0xdd2(%rip),%rdi
xor    %eax,%eax
push   %rbx
push   %r11
push   %r10
call   1060 <printf@plt>
add    $0x20,%rsp
mov    (%r14),%eax
mov    %eax,0x8(%rsp)
mov    0x2d89(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
mov    %ebp,%edi
call   1040 <fcntl@plt>
cmp    $0xffffffff,%eax
je     1288 <main+0x108>
lea    0xde6(%rip),%rdi
call   1030 <puts@plt>
mov    %ebp,%edi
call   1070 <close@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xd76(%rip),%rdi
jmp    12ae <main+0x12e>
lea    0xdc9(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
lea    0xd52(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
%struct.flock = type { i32, i32, i32, i32, i32, i32, i32, i32, i32, i32 }
@F_WRLCK = global i32 0
@SEEK_SET = global i32 0
@.str = [22 x i8] c"Usage: unlock <file>\0A\00"
@O_RDWR = global i32 0
@.str.1 = [5 x i8] c"open\00"
@F_GETLK = global i32 0
@.str.2 = [6 x i8] c"fcntl\00"
@.str.3 = [13 x i8] c"Lock found.\0A\00"
@.str.4 = [64 x i8] c"%ld (READ = %ld, WRITE = %ld, UNLCK = %ld), %ld, %ld, %ld, %ld\0A\00"
@F_RDLCK = global i32 0
@F_UNLCK = global i32 0
@F_SETLK = global i32 0
@.str.5 = [11 x i8] c"Unlocked.\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i8**
%5 = alloca %struct.flock
%6 = alloca i32
store i32 %0, i32* %3
store i8** %1, i8*** %4
%7 = bitcast %struct.flock* %5 to i8*
%8 = getelementptr inbounds %struct.flock, %struct.flock* %5, i32 0, i32 0
%9 = load i32, i32* @F_WRLCK
store i32 %9, i32* %8
%10 = getelementptr inbounds %struct.flock, %struct.flock* %5, i32 0, i32 1
%11 = load i32, i32* @SEEK_SET
store i32 %11, i32* %10
%12 = load i32, i32* %3
%13 = icmp ne i32 %12, 2
br i1 %13, label %14, label %17
14: ; preds = %2
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
%16 = call i32 @exit(i32 1)
unreachable
17: ; preds = %2
%18 = call i32 (...) @getpid()
%19 = getelementptr inbounds %struct.flock, %struct.flock* %5, i32 0, i32 1
store i32 %18, i32* %19
%20 = load i8**, i8*** %4
%21 = getelementptr inbounds i8*, i8** %20, i64 1
%22 = load i8*, i8** %21
%23 = load i32, i32* @O_RDWR
%24 = call i32 @open(i8* %22, i32 %23)
store i32 %24, i32* %6
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %29
26: ; preds = %17
%27 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%28 = call i32 @exit(i32 1)
unreachable
29: ; preds = %17
%30 = load i32, i32* %6
%31 = load i32, i32* @F_GETLK
%32 = call i32 @fcntl(i32 %30, i32 %31, %struct.flock* %5)
%33 = icmp eq i32 %32, -1
br i1 %33, label %34, label %37
34: ; preds = %29
%35 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%36 = call i32 @exit(i32 1)
unreachable
37: ; preds = %29
%38 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
%39 = getelementptr inbounds %struct.flock, %struct.flock* %5, i32 0, i32 0
%40 = load i32, i32* %39
%41 = load i32, i32* @F_WRLCK
%42 = load i32, i32* @F_RDLCK
%43 = load i32, i32* @F_UNLCK
%44 = getelementptr inbounds %struct.flock, %struct.flock* %5, i32 0, i32 4
%45 = load i32, i32* %44
%46 = getelementptr inbounds %struct.flock, %struct.flock* %5, i32 0, i32 3
%47 = load i32, i32* %46
%48 = getelementptr inbounds %struct.flock, %struct.flock* %5, i32 0, i32 2
%49 = load i32, i32* %48
%50 = getelementptr inbounds %struct.flock, %struct.flock* %5, i32 0, i32 1
%51 = load i32, i32* %50
%52 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([64 x i8], [64 x i8]* @.str.4, i64 0, i64 0), i32 %40, i32 %41, i32 %42, i32 %43, i32 %45, i32 %47, i32 %49, i32 %51)
%53 = load i32, i32* @F_UNLCK
%54 = getelementptr inbounds %struct.flock, %struct.flock* %5, i32 0, i32 0
store i32 %53, i32* %54
%55 = load i32, i32* %6
%56 = load i32, i32* @F_SETLK
%57 = call i32 @fcntl(i32 %55, i32 %56, %struct.flock* %5)
%58 = icmp eq i32 %57, -1
br i1 %58, label %59, label %62
59: ; preds = %37
%60 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%61 = call i32 @exit(i32 1)
unreachable
62: ; preds = %37
%63 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.5, i64 0, i64 0))
%64 = load i32, i32* %6
%65 = call i32 @close(i32 %64)
ret i32 0
}

LLVM IR (-O2):
%struct.flock = type { i32, i32, i32, i32, i32, i32, i32, i32, i32, i32 }
@F_WRLCK = global i32 0
@SEEK_SET = global i32 0
@O_RDWR = global i32 0
@.str.1 = [5 x i8] c"open\00"
@F_GETLK = global i32 0
@.str.2 = [6 x i8] c"fcntl\00"
@.str.4 = [64 x i8] c"%ld (READ = %ld, WRITE = %ld, UNLCK = %ld), %ld, %ld, %ld, %ld\0A\00"
@F_RDLCK = global i32 0
@F_UNLCK = global i32 0
@F_SETLK = global i32 0
@str = [12 x i8] c"Lock found.\00"
@str.6 = [10 x i8] c"Unlocked.\00"
@str.7 = [21 x i8] c"Usage: unlock <file>\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.flock
%4 = bitcast %struct.flock* %3 to i8*
%5 = getelementptr inbounds %struct.flock, %struct.flock* %3, i64 0, i32 2
%6 = bitcast i32* %5 to i8*
%7 = getelementptr inbounds %struct.flock, %struct.flock* %3, i64 0, i32 0
%8 = load i32, i32* @F_WRLCK
store i32 %8, i32* %7
%9 = getelementptr inbounds %struct.flock, %struct.flock* %3, i64 0, i32 1
%10 = icmp eq i32 %0, 2
br i1 %10, label %14, label %11
11: ; preds = %2
%12 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str.7, i64 0, i64 0))
%13 = tail call i32 @exit(i32 1)
unreachable
14: ; preds = %2
%15 = tail call i32 (...) @getpid()
store i32 %15, i32* %9
%16 = getelementptr inbounds i8*, i8** %1, i64 1
%17 = load i8*, i8** %16
%18 = load i32, i32* @O_RDWR
%19 = tail call i32 @open(i8* %17, i32 %18)
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %24
21: ; preds = %14
%22 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%23 = tail call i32 @exit(i32 1)
unreachable
24: ; preds = %14
%25 = load i32, i32* @F_GETLK
%26 = call i32 @fcntl(i32 %19, i32 %25, %struct.flock* %3)
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %31
28: ; preds = %24
%29 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%30 = call i32 @exit(i32 1)
unreachable
31: ; preds = %24
%32 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str, i64 0, i64 0))
%33 = load i32, i32* %7
%34 = load i32, i32* @F_WRLCK
%35 = load i32, i32* @F_RDLCK
%36 = load i32, i32* @F_UNLCK
%37 = getelementptr inbounds %struct.flock, %struct.flock* %3, i64 0, i32 4
%38 = load i32, i32* %37
%39 = getelementptr inbounds %struct.flock, %struct.flock* %3, i64 0, i32 3
%40 = load i32, i32* %39
%41 = getelementptr inbounds %struct.flock, %struct.flock* %3, i64 0, i32 2
%42 = load i32, i32* %41
%43 = load i32, i32* %9
%44 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([64 x i8], [64 x i8]* @.str.4, i64 0, i64 0), i32 %33, i32 %34, i32 %35, i32 %36, i32 %38, i32 %40, i32 %42, i32 %43)
%45 = load i32, i32* @F_UNLCK
store i32 %45, i32* %7
%46 = load i32, i32* @F_SETLK
%47 = call i32 @fcntl(i32 %19, i32 %46, %struct.flock* %3)
%48 = icmp eq i32 %47, -1
br i1 %48, label %49, label %52
49: ; preds = %31
%50 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%51 = call i32 @exit(i32 1)
unreachable
52: ; preds = %31
%53 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @str.6, i64 0, i64 0))
%54 = call i32 @close(i32 %19)
ret i32 0
}