#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 */
typedef int pid_t;

/* Variables and functions */
int /*<<< orphan*/ EXIT_SUCCESS;
int /*<<< orphan*/ F_TLOCK;
int /*<<< orphan*/ F_ULOCK;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int getpid();
scalar_t__ lockf(int, int /*<<< orphan*/, int /*<<< orphan*/);
int open_lockfile(char *);
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ sleep(int);
int main(void) {
  int fd;
  pid_t pid;
  pid = getpid();
  fd = open_lockfile("test_lock");
  for (;;) {
    if (lockf(fd, F_TLOCK, 0) == 0) {
      printf("%d has locked the file\n", pid);
      sleep(5);
      if (lockf(fd, F_ULOCK, 0) == 0) {
        printf("%d has unlocked the file\n", pid);
      }
      exit(EXIT_SUCCESS);
    } else {
      printf("%d found the file already locked, try again...\n", pid);
      sleep(2);
    }
  }
  exit(EXIT_SUCCESS);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,-0xc(%rbp)
lea    0xe80(%rip),%rdi
call   1030 <open_lockfile@plt>
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e5e(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1060 <lockf@plt>
cmp    $0x0,%rax
jne    11f8 <main+0x98>
mov    -0xc(%rbp),%esi
lea    0xe5b(%rip),%rdi
call   1050 <printf@plt>
mov    $0x5,%edi
call   1080 <sleep@plt>
mov    -0x8(%rbp),%edi
mov    0x2e18(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1060 <lockf@plt>
cmp    $0x0,%rax
jne    11ea <main+0x8a>
mov    -0xc(%rbp),%esi
lea    0xe3d(%rip),%rdi
call   1050 <printf@plt>
mov    0x2ddf(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
mov    -0xc(%rbp),%esi
lea    0xe3a(%rip),%rdi
call   1050 <printf@plt>
mov    $0x2,%edi
call   1080 <sleep@plt>
jmp    1188 <main+0x28>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %eax,%r14d
lea    0xe88(%rip),%rdi
call   1030 <open_lockfile@plt>
mov    %eax,%ebp
mov    0x2e6a(%rip),%r15
mov    (%r15),%esi
mov    %eax,%edi
xor    %edx,%edx
call   1060 <lockf@plt>
test   %rax,%rax
je     11c6 <main+0x66>
lea    0xe9e(%rip),%rbx
xchg   %ax,%ax
mov    %rbx,%rdi
mov    %r14d,%esi
call   1050 <printf@plt>
mov    $0x2,%edi
call   1080 <sleep@plt>
mov    (%r15),%esi
mov    %ebp,%edi
xor    %edx,%edx
call   1060 <lockf@plt>
test   %rax,%rax
jne    11a0 <main+0x40>
lea    0xe3d(%rip),%rdi
mov    %r14d,%esi
call   1050 <printf@plt>
mov    $0x5,%edi
call   1080 <sleep@plt>
mov    0x2dfa(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
xor    %edx,%edx
call   1060 <lockf@plt>
test   %rax,%rax
jne    1205 <main+0xa5>
lea    0xe25(%rip),%rdi
mov    %r14d,%esi
call   1050 <printf@plt>
mov    0x2dc4(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [10 x i8] c"test_lock\00"
@F_TLOCK = global i32 0
@.str.1 = [24 x i8] c"%d has locked the file\0A\00"
@F_ULOCK = global i32 0
@.str.2 = [26 x i8] c"%d has unlocked the file\0A\00"
@EXIT_SUCCESS = global i32 0
@.str.3 = [48 x i8] c"%d found the file already locked, try again...\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
store i32 0, i32* %1
%4 = call i32 (...) @getpid()
store i32 %4, i32* %3
%5 = call i32 @open_lockfile(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
store i32 %5, i32* %2
br label %6
6: ; preds = %29, %0
%7 = load i32, i32* %2
%8 = load i32, i32* @F_TLOCK
%9 = call i64 @lockf(i32 %7, i32 %8, i32 0)
%10 = icmp eq i64 %9, 0
br i1 %10, label %11, label %25
11: ; preds = %6
%12 = load i32, i32* %3
%13 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i32 %12)
%14 = call i32 @sleep(i32 5)
%15 = load i32, i32* %2
%16 = load i32, i32* @F_ULOCK
%17 = call i64 @lockf(i32 %15, i32 %16, i32 0)
%18 = icmp eq i64 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %11
%20 = load i32, i32* %3
%21 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i32 %20)
br label %22
22: ; preds = %19, %11
%23 = load i32, i32* @EXIT_SUCCESS
%24 = call i32 @exit(i32 %23)
unreachable
25: ; preds = %6
%26 = load i32, i32* %3
%27 = call i32 @printf(i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.3, i64 0, i64 0), i32 %26)
%28 = call i32 @sleep(i32 2)
br label %29
29: ; preds = %25
br label %6
}

LLVM IR (-O2):
@.str = [10 x i8] c"test_lock\00"
@F_TLOCK = global i32 0
@.str.1 = [24 x i8] c"%d has locked the file\0A\00"
@F_ULOCK = global i32 0
@.str.2 = [26 x i8] c"%d has unlocked the file\0A\00"
@EXIT_SUCCESS = global i32 0
@.str.3 = [48 x i8] c"%d found the file already locked, try again...\0A\00"
define i32 @main() {
%1 = tail call i32 (...) @getpid()
%2 = tail call i32 @open_lockfile(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%3 = load i32, i32* @F_TLOCK
%4 = tail call i64 @lockf(i32 %2, i32 %3, i32 0)
%5 = icmp eq i64 %4, 0
br i1 %5, label %6, label %17
6: ; preds = %17, %0
%7 = tail call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i32 %1)
%8 = tail call i32 @sleep(i32 5)
%9 = load i32, i32* @F_ULOCK
%10 = tail call i64 @lockf(i32 %2, i32 %9, i32 0)
%11 = icmp eq i64 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %6
%13 = tail call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i32 %1)
br label %14
14: ; preds = %12, %6
%15 = load i32, i32* @EXIT_SUCCESS
%16 = tail call i32 @exit(i32 %15)
unreachable
17: ; preds = %0, %17
%18 = tail call i32 @printf(i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.3, i64 0, i64 0), i32 %1)
%19 = tail call i32 @sleep(i32 2)
%20 = load i32, i32* @F_TLOCK
%21 = tail call i64 @lockf(i32 %2, i32 %20, i32 0)
%22 = icmp eq i64 %21, 0
br i1 %22, label %6, label %17
}