#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;
};
typedef int /*<<< orphan*/ lock;

/* Variables and functions */
int /*<<< orphan*/ F_SETLK;
int /*<<< orphan*/ F_UNLCK;
int /*<<< orphan*/ F_WRLCK;
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ close(int);
scalar_t__ fcntl(int, int /*<<< orphan*/, struct flock *);
int /*<<< orphan*/ getchar();
int /*<<< orphan*/ memset(struct flock *, int /*<<< orphan*/, int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int main(int argc, char *args[]) {
  int fd;
  struct flock lock;
  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;
  }
  memset(&lock, 0, sizeof(lock));
  lock.l_type = F_WRLCK;
  if (fcntl(fd, F_SETLK, &lock)) {
    perror("Failed to lock file");
    return -1;
  }
  printf("Press a key to unlock file\n");
  getchar();
  lock.l_type = F_UNLCK;
  if (fcntl(fd, F_SETLK, &lock)) {
    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     11a8 <main+0x38>
lea    0xe69(%rip),%rdi
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12ad <main+0x13d>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e31(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    11e2 <main+0x72>
lea    0xe4f(%rip),%rdi
call   1090 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12ad <main+0x13d>
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1050 <memset@plt>
mov    0x2dcf(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%edi
mov    0x2dd8(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rdx
call   1030 <fcntl@plt>
cmp    $0x0,%rax
je     1235 <main+0xc5>
lea    0xe10(%rip),%rdi
call   1090 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12ad <main+0x13d>
lea    0xe0c(%rip),%rdi
call   1040 <printf@plt>
mov    $0x0,%al
call   1070 <getchar@plt>
mov    0x2d89(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%edi
mov    0x2d82(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rdx
call   1030 <fcntl@plt>
cmp    $0x0,%rax
je     128b <main+0x11b>
lea    0xdea(%rip),%rdi
call   1090 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12ad <main+0x13d>
lea    0xde8(%rip),%rdi
call   1040 <printf@plt>
mov    $0x0,%al
call   1070 <getchar@plt>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
cmp    $0x2,%edi
jne    122a <main+0xba>
mov    0x8(%rsi),%rdi
mov    0x2e5c(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
cmp    $0xffffffff,%eax
je     1246 <main+0xd6>
mov    %eax,%ebx
lea    0x8(%rsp),%rbp
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1050 <memset@plt>
mov    0x2e0f(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
mov    0x2e1a(%rip),%r14
mov    (%r14),%esi
mov    %ebx,%edi
mov    %rbp,%rdx
call   1040 <fcntl@plt>
test   %rax,%rax
jne    124f <main+0xdf>
lea    0xe5f(%rip),%rdi
call   1030 <puts@plt>
xor    %ebp,%ebp
xor    %eax,%eax
call   1070 <getchar@plt>
mov    0x2de4(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
mov    (%r14),%esi
lea    0x8(%rsp),%rdx
mov    %ebx,%edi
call   1040 <fcntl@plt>
test   %rax,%rax
jne    1258 <main+0xe8>
lea    0xe44(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
call   1070 <getchar@plt>
mov    %ebx,%edi
call   1060 <close@plt>
jmp    123b <main+0xcb>
lea    0xe3c(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xdb3(%rip),%rdi
jmp    125f <main+0xef>
lea    0xdbe(%rip),%rdi
jmp    125f <main+0xef>
lea    0xdc9(%rip),%rdi
call   1090 <perror@plt>
jmp    1236 <main+0xc6>

LLVM IR (-O0):
%struct.flock = type { i32 }
@.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_WRLCK = global i32 0
@F_SETLK = 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_UNLCK = 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
%7 = alloca %struct.flock
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = load i32, i32* %4
%9 = icmp ne i32 %8, 2
br i1 %9, label %10, label %12
10: ; preds = %2
%11 = call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %47
12: ; preds = %2
%13 = load i8**, i8*** %5
%14 = getelementptr inbounds i8*, i8** %13, i64 1
%15 = load i8*, i8** %14
%16 = load i32, i32* @O_RDWR
%17 = call i32 @open(i8* %15, i32 %16)
store i32 %17, i32* %6
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %21
19: ; preds = %12
%20 = 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 %47
21: ; preds = %12
%22 = call i32 @memset(%struct.flock* %7, i32 0, i32 4)
%23 = load i32, i32* @F_WRLCK
%24 = getelementptr inbounds %struct.flock, %struct.flock* %7, i32 0, i32 0
store i32 %23, i32* %24
%25 = load i32, i32* %6
%26 = load i32, i32* @F_SETLK
%27 = call i64 @fcntl(i32 %25, i32 %26, %struct.flock* %7)
%28 = icmp ne i64 %27, 0
br i1 %28, label %29, label %31
29: ; preds = %21
%30 = 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 %47
31: ; preds = %21
%32 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0))
%33 = call i32 (...) @getchar()
%34 = load i32, i32* @F_UNLCK
%35 = getelementptr inbounds %struct.flock, %struct.flock* %7, i32 0, i32 0
store i32 %34, i32* %35
%36 = load i32, i32* %6
%37 = load i32, i32* @F_SETLK
%38 = call i64 @fcntl(i32 %36, i32 %37, %struct.flock* %7)
%39 = icmp ne i64 %38, 0
br i1 %39, label %40, label %42
40: ; preds = %31
%41 = 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 %47
42: ; preds = %31
%43 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.5, i64 0, i64 0))
%44 = call i32 (...) @getchar()
%45 = load i32, i32* %6
%46 = call i32 @close(i32 %45)
store i32 0, i32* %3
br label %47
47: ; preds = %42, %40, %29, %19, %10
%48 = load i32, i32* %3
ret i32 %48
}

LLVM IR (-O2):
%struct.flock = type { i32 }
@O_RDWR = global i32 0
@.str.1 = [20 x i8] c"Failed to open file\00"
@F_WRLCK = global i32 0
@F_SETLK = global i32 0
@.str.2 = [20 x i8] c"Failed to lock file\00"
@F_UNLCK = 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 = alloca %struct.flock
%4 = bitcast %struct.flock* %3 to i8*
%5 = icmp eq i32 %0, 2
br i1 %5, label %8, label %6
6: ; preds = %2
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @str.7, i64 0, i64 0))
br label %38
8: ; preds = %2
%9 = getelementptr inbounds i8*, i8** %1, i64 1
%10 = load i8*, i8** %9
%11 = load i32, i32* @O_RDWR
%12 = tail call i32 @open(i8* %10, i32 %11)
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %16
14: ; preds = %8
%15 = tail call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
br label %38
16: ; preds = %8
%17 = call i32 @memset(%struct.flock* %3, i32 0, i32 4)
%18 = load i32, i32* @F_WRLCK
%19 = getelementptr inbounds %struct.flock, %struct.flock* %3, i64 0, i32 0
store i32 %18, i32* %19
%20 = load i32, i32* @F_SETLK
%21 = call i64 @fcntl(i32 %12, i32 %20, %struct.flock* %3)
%22 = icmp eq i64 %21, 0
br i1 %22, label %25, label %23
23: ; preds = %16
%24 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0))
br label %38
25: ; preds = %16
%26 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @str, i64 0, i64 0))
%27 = call i32 (...) @getchar()
%28 = load i32, i32* @F_UNLCK
store i32 %28, i32* %19
%29 = load i32, i32* @F_SETLK
%30 = call i64 @fcntl(i32 %12, i32 %29, %struct.flock* %3)
%31 = icmp eq i64 %30, 0
br i1 %31, label %34, label %32
32: ; preds = %25
%33 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0))
br label %38
34: ; preds = %25
%35 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str.6, i64 0, i64 0))
%36 = call i32 (...) @getchar()
%37 = call i32 @close(i32 %12)
br label %38
38: ; preds = %34, %32, %23, %14, %6
%39 = phi i32 [ -1, %6 ], [ -1, %14 ], [ -1, %23 ], [ -1, %32 ], [ 0, %34 ]
ret i32 %39
}