#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 */

/* Variables and functions */
int /*<<< orphan*/ F_LOCK;
int /*<<< orphan*/ F_ULOCK;
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ getchar();
scalar_t__ lockf(int, int /*<<< orphan*/, int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int main(int argc, char *args[]) {
  int fd;
  if (argc != 2) {
    printf("Need to provide file argument.\n");
    return -1;
  }
  if ((fd = open(args[1], O_RDWR)) == -1) {
    perror("Failed to open file");
    return -1;
  }
  if (lockf(fd, F_LOCK, 0)) {
    perror("Failed to lock file");
    return -1;
  }
  printf("Press a key to unlock file\n");
  getchar();
  if (lockf(fd, F_ULOCK, 0)) {
    perror("Failed to unlock file");
    return -1;
  }
  printf("Press a key to exit\n");
  getchar();
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     1198 <main+0x38>
lea    0xe79(%rip),%rdi
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1271 <main+0x111>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e41(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    11d2 <main+0x72>
lea    0xe5f(%rip),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1271 <main+0x111>
mov    -0x14(%rbp),%edi
mov    0x2dfc(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1040 <lockf@plt>
cmp    $0x0,%rax
je     1207 <main+0xa7>
lea    0xe3e(%rip),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1271 <main+0x111>
lea    0xe3a(%rip),%rdi
call   1030 <printf@plt>
mov    $0x0,%al
call   1060 <getchar@plt>
mov    -0x14(%rbp),%edi
mov    0x2dbc(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1040 <lockf@plt>
cmp    $0x0,%rax
je     124f <main+0xef>
lea    0xe26(%rip),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1271 <main+0x111>
lea    0xe24(%rip),%rdi
call   1030 <printf@plt>
mov    $0x0,%al
call   1060 <getchar@plt>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
cmp    $0x2,%edi
jne    11e0 <main+0x80>
mov    0x8(%rsi),%rdi
mov    0x2e75(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     11fa <main+0x9a>
mov    %eax,%ebx
mov    0x2e50(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
xor    %edx,%edx
call   1040 <lockf@plt>
test   %rax,%rax
jne    1203 <main+0xa3>
lea    0xe9f(%rip),%rdi
call   1030 <puts@plt>
xor    %ebp,%ebp
xor    %eax,%eax
call   1060 <getchar@plt>
mov    0x2e2c(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
xor    %edx,%edx
call   1040 <lockf@plt>
test   %rax,%rax
jne    120c <main+0xac>
lea    0xe8e(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
call   1060 <getchar@plt>
mov    %ebx,%edi
call   1050 <close@plt>
jmp    11f1 <main+0x91>
lea    0xe86(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xdff(%rip),%rdi
jmp    1213 <main+0xb3>
lea    0xe0a(%rip),%rdi
jmp    1213 <main+0xb3>
lea    0xe15(%rip),%rdi
call   1080 <perror@plt>
jmp    11ec <main+0x8c>

LLVM IR (-O0):
@.str = [32 x i8] c"Need to provide file argument.\0A\00"
@O_RDWR = global i32 0
@.str.1 = [20 x i8] c"Failed to open file\00"
@F_LOCK = global i32 0
@.str.2 = [20 x i8] c"Failed to lock file\00"
@.str.3 = [28 x i8] c"Press a key to unlock file\0A\00"
@F_ULOCK = global i32 0
@.str.4 = [22 x i8] c"Failed to unlock file\00"
@.str.5 = [21 x i8] c"Press a key to exit\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%7 = load i32, i32* %4
%8 = icmp ne i32 %7, 2
br i1 %8, label %9, label %11
9: ; preds = %2
%10 = call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %41
11: ; preds = %2
%12 = load i8**, i8*** %5
%13 = getelementptr inbounds i8*, i8** %12, i64 1
%14 = load i8*, i8** %13
%15 = load i32, i32* @O_RDWR
%16 = call i32 @open(i8* %14, i32 %15)
store i32 %16, i32* %6
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %20
18: ; preds = %11
%19 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %3
br label %41
20: ; preds = %11
%21 = load i32, i32* %6
%22 = load i32, i32* @F_LOCK
%23 = call i64 @lockf(i32 %21, i32 %22, i32 0)
%24 = icmp ne i64 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %20
%26 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %3
br label %41
27: ; preds = %20
%28 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0))
%29 = call i32 (...) @getchar()
%30 = load i32, i32* %6
%31 = load i32, i32* @F_ULOCK
%32 = call i64 @lockf(i32 %30, i32 %31, i32 0)
%33 = icmp ne i64 %32, 0
br i1 %33, label %34, label %36
34: ; preds = %27
%35 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0))
store i32 -1, i32* %3
br label %41
36: ; preds = %27
%37 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.5, i64 0, i64 0))
%38 = call i32 (...) @getchar()
%39 = load i32, i32* %6
%40 = call i32 @close(i32 %39)
store i32 0, i32* %3
br label %41
41: ; preds = %36, %34, %25, %18, %9
%42 = load i32, i32* %3
ret i32 %42
}

LLVM IR (-O2):
@O_RDWR = global i32 0
@.str.1 = [20 x i8] c"Failed to open file\00"
@F_LOCK = global i32 0
@.str.2 = [20 x i8] c"Failed to lock file\00"
@F_ULOCK = global i32 0
@.str.4 = [22 x i8] c"Failed to unlock file\00"
@str = [27 x i8] c"Press a key to unlock file\00"
@str.6 = [20 x i8] c"Press a key to exit\00"
@str.7 = [31 x i8] c"Need to provide file argument.\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp eq i32 %0, 2
br i1 %3, label %6, label %4
4: ; preds = %2
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @str.7, i64 0, i64 0))
br label %32
6: ; preds = %2
%7 = getelementptr inbounds i8*, i8** %1, i64 1
%8 = load i8*, i8** %7
%9 = load i32, i32* @O_RDWR
%10 = tail call i32 @open(i8* %8, i32 %9)
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %14
12: ; preds = %6
%13 = tail call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
br label %32
14: ; preds = %6
%15 = load i32, i32* @F_LOCK
%16 = tail call i64 @lockf(i32 %10, i32 %15, i32 0)
%17 = icmp eq i64 %16, 0
br i1 %17, label %20, label %18
18: ; preds = %14
%19 = tail call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0))
br label %32
20: ; preds = %14
%21 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @str, i64 0, i64 0))
%22 = tail call i32 (...) @getchar()
%23 = load i32, i32* @F_ULOCK
%24 = tail call i64 @lockf(i32 %10, i32 %23, i32 0)
%25 = icmp eq i64 %24, 0
br i1 %25, label %28, label %26
26: ; preds = %20
%27 = tail call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0))
br label %32
28: ; preds = %20
%29 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str.6, i64 0, i64 0))
%30 = tail call i32 (...) @getchar()
%31 = tail call i32 @close(i32 %10)
br label %32
32: ; preds = %28, %26, %18, %12, %4
%33 = phi i32 [ -1, %4 ], [ -1, %12 ], [ -1, %18 ], [ -1, %26 ], [ 0, %28 ]
ret i32 %33
}