#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*/ LOCK_EX;
int /*<<< orphan*/ LOCK_UN;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
int flock(int, int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdout;
int main(int argc, char **argv) {
  if (argc == 2) {
    int fd = open(argv[1], O_RDONLY);
    if (-1 == fd) {
      perror("open");
      return 1;
    }
    if (-1 == flock(fd, LOCK_EX)) {
      perror("flock");
      return 1;
    }
    printf("Locked file, sleeping...");
    fflush(stdout);
    sleep(10);
    printf("done\n");
    if (-1 == flock(fd, LOCK_UN)) {
      perror("flock");
      return 1;
    }
    printf("Unlocked file.\n");
    close(fd);
  } else {
    fprintf(stderr, "Usage: \n\t%s [file to lock]\n", argv[0]);
    return 1;
  }
  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)
jne    1298 <main+0x118>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e31(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
mov    $0xffffffff,%eax
cmp    -0x14(%rbp),%eax
jne    11df <main+0x5f>
lea    0xe32(%rip),%rdi
call   1080 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    12c7 <main+0x147>
mov    -0x14(%rbp),%edi
mov    0x2dff(%rip),%rax
mov    (%rax),%esi
call   1090 <flock@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
cmp    %ecx,%eax
jne    1217 <main+0x97>
lea    0xdff(%rip),%rdi
call   1080 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    12c7 <main+0x147>
lea    0xded(%rip),%rdi
call   1030 <printf@plt>
mov    0x2d9e(%rip),%rax
mov    (%rax),%edi
call   1060 <fflush@plt>
mov    $0xa,%edi
call   10a0 <sleep@plt>
lea    0xde2(%rip),%rdi
call   1030 <printf@plt>
mov    -0x14(%rbp),%edi
mov    0x2d6f(%rip),%rax
mov    (%rax),%esi
call   1090 <flock@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
cmp    %ecx,%eax
jne    127f <main+0xff>
lea    0xd97(%rip),%rdi
call   1080 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    12c7 <main+0x147>
lea    0xda4(%rip),%rdi
call   1030 <printf@plt>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
jmp    12c0 <main+0x140>
mov    0x2d31(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xd8b(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12c7 <main+0x147>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
cmp    $0x2,%edi
jne    1228 <main+0x98>
mov    0x8(%rsi),%rdi
mov    0x2e3b(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
cmp    $0xffffffff,%eax
je     1255 <main+0xc5>
mov    %eax,%ebx
mov    0x2e2a(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   10a0 <flock@plt>
cmp    $0xffffffff,%eax
je     1247 <main+0xb7>
lea    0xe38(%rip),%rdi
call   1040 <printf@plt>
mov    0x2de9(%rip),%rax
mov    (%rax),%edi
call   1070 <fflush@plt>
mov    $0xa,%edi
call   10b0 <sleep@plt>
lea    0xe49(%rip),%rdi
call   1030 <puts@plt>
mov    0x2dbd(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   10a0 <flock@plt>
cmp    $0xffffffff,%eax
je     1247 <main+0xb7>
lea    0xe2d(%rip),%rdi
call   1030 <puts@plt>
mov    %ebx,%edi
call   1050 <close@plt>
xor    %eax,%eax
pop    %rbx
ret
mov    0x2da1(%rip),%rax
mov    (%rax),%edi
mov    (%rsi),%rdx
lea    0xde9(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%eax
pop    %rbx
ret
lea    0xdb7(%rip),%rdi
call   1090 <perror@plt>
jmp    1240 <main+0xb0>
lea    0xda4(%rip),%rdi
call   1090 <perror@plt>
jmp    1240 <main+0xb0>

LLVM IR (-O0):
@O_RDONLY = global i32 0
@.str = [5 x i8] c"open\00"
@LOCK_EX = global i32 0
@.str.1 = [6 x i8] c"flock\00"
@.str.2 = [25 x i8] c"Locked file, sleeping...\00"
@stdout = global i32 0
@.str.3 = [6 x i8] c"done\0A\00"
@LOCK_UN = global i32 0
@.str.4 = [16 x i8] c"Unlocked file.\0A\00"
@stderr = global i32 0
@.str.5 = [28 x i8] c"Usage: \0A\09%s [file to lock]\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 eq i32 %7, 2
br i1 %8, label %9, label %42
9: ; preds = %2
%10 = load i8**, i8*** %5
%11 = getelementptr inbounds i8*, i8** %10, i64 1
%12 = load i8*, i8** %11
%13 = load i32, i32* @O_RDONLY
%14 = call i32 @open(i8* %12, i32 %13)
store i32 %14, i32* %6
%15 = load i32, i32* %6
%16 = icmp eq i32 -1, %15
br i1 %16, label %17, label %19
17: ; preds = %9
%18 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %49
19: ; preds = %9
%20 = load i32, i32* %6
%21 = load i32, i32* @LOCK_EX
%22 = call i32 @flock(i32 %20, i32 %21)
%23 = icmp eq i32 -1, %22
br i1 %23, label %24, label %26
24: ; preds = %19
%25 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %3
br label %49
26: ; preds = %19
%27 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0))
%28 = load i32, i32* @stdout
%29 = call i32 @fflush(i32 %28)
%30 = call i32 @sleep(i32 10)
%31 = call i32 @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
%32 = load i32, i32* %6
%33 = load i32, i32* @LOCK_UN
%34 = call i32 @flock(i32 %32, i32 %33)
%35 = icmp eq i32 -1, %34
br i1 %35, label %36, label %38
36: ; preds = %26
%37 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %3
br label %49
38: ; preds = %26
%39 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0))
%40 = load i32, i32* %6
%41 = call i32 @close(i32 %40)
br label %48
42: ; preds = %2
%43 = load i32, i32* @stderr
%44 = load i8**, i8*** %5
%45 = getelementptr inbounds i8*, i8** %44, i64 0
%46 = load i8*, i8** %45
%47 = call i32 @fprintf(i32 %43, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.5, i64 0, i64 0), i8* %46)
store i32 1, i32* %3
br label %49
48: ; preds = %38
store i32 0, i32* %3
br label %49
49: ; preds = %48, %42, %36, %24, %17
%50 = load i32, i32* %3
ret i32 %50
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@.str = [5 x i8] c"open\00"
@LOCK_EX = global i32 0
@.str.1 = [6 x i8] c"flock\00"
@.str.2 = [25 x i8] c"Locked file, sleeping...\00"
@stdout = global i32 0
@LOCK_UN = global i32 0
@stderr = global i32 0
@.str.5 = [28 x i8] c"Usage: \0A\09%s [file to lock]\0A\00"
@str = [5 x i8] c"done\00"
@str.6 = [15 x i8] c"Unlocked file.\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp eq i32 %0, 2
br i1 %3, label %4, label %32
4: ; preds = %2
%5 = getelementptr inbounds i8*, i8** %1, i64 1
%6 = load i8*, i8** %5
%7 = load i32, i32* @O_RDONLY
%8 = tail call i32 @open(i8* %6, i32 %7)
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %12
10: ; preds = %4
%11 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %36
12: ; preds = %4
%13 = load i32, i32* @LOCK_EX
%14 = tail call i32 @flock(i32 %8, i32 %13)
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %18
16: ; preds = %12
%17 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
br label %36
18: ; preds = %12
%19 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0))
%20 = load i32, i32* @stdout
%21 = tail call i32 @fflush(i32 %20)
%22 = tail call i32 @sleep(i32 10)
%23 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @str, i64 0, i64 0))
%24 = load i32, i32* @LOCK_UN
%25 = tail call i32 @flock(i32 %8, i32 %24)
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %29
27: ; preds = %18
%28 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
br label %36
29: ; preds = %18
%30 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str.6, i64 0, i64 0))
%31 = tail call i32 @close(i32 %8)
br label %36
32: ; preds = %2
%33 = load i32, i32* @stderr
%34 = load i8*, i8** %1
%35 = tail call i32 @fprintf(i32 %33, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.5, i64 0, i64 0), i8* %34)
br label %36
36: ; preds = %10, %16, %27, %29, %32
%37 = phi i32 [ 1, %32 ], [ 0, %29 ], [ 1, %27 ], [ 1, %16 ], [ 1, %10 ]
ret i32 %37
}