#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*/ abort();
int /*<<< orphan*/ delay();
int flock(int, int /*<<< orphan*/);
int /*<<< orphan*/ message(char *);
int /*<<< orphan*/ perror(char *);
void test_flock(int fd) {
  message("lock");
  if (flock(fd, LOCK_EX) == -1) {
    perror("flock");
    abort();
  }
  message("sleep");
  delay();
  message("unlock");
  if (flock(fd, LOCK_UN) == -1) {
    perror("flock");
    abort();
  }
}


x86 assembly (clang, -O0):
<test_flock>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
lea    0xe9f(%rip),%rdi
call   1030 <message@plt>
mov    -0x4(%rbp),%edi
mov    0x2e77(%rip),%rax
mov    (%rax),%esi
call   1070 <flock@plt>
cmp    $0xffffffff,%eax
jne    1194 <test_flock+0x44>
lea    0xe78(%rip),%rdi
call   1060 <perror@plt>
mov    $0x0,%al
call   1040 <abort@plt>
lea    0xe6b(%rip),%rdi
call   1030 <message@plt>
mov    $0x0,%al
call   1050 <delay@plt>
lea    0xe5e(%rip),%rdi
call   1030 <message@plt>
mov    -0x4(%rbp),%edi
mov    0x2e1b(%rip),%rax
mov    (%rax),%esi
call   1070 <flock@plt>
cmp    $0xffffffff,%eax
jne    11e0 <test_flock+0x90>
lea    0xe2c(%rip),%rdi
call   1060 <perror@plt>
mov    $0x0,%al
call   1040 <abort@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_flock>:
push   %rbx
mov    %edi,%ebx
lea    0xea7(%rip),%rdi
call   1030 <message@plt>
mov    0x2e82(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1070 <flock@plt>
cmp    $0xffffffff,%eax
je     11aa <test_flock+0x5a>
lea    0xe8b(%rip),%rdi
call   1030 <message@plt>
xor    %eax,%eax
call   1050 <delay@plt>
lea    0xe7e(%rip),%rdi
call   1030 <message@plt>
mov    0x2e3e(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1070 <flock@plt>
cmp    $0xffffffff,%eax
je     11aa <test_flock+0x5a>
pop    %rbx
ret
lea    0xe4f(%rip),%rdi
call   1060 <perror@plt>
xor    %eax,%eax
call   1040 <abort@plt>

LLVM IR (-O0):
@.str = [5 x i8] c"lock\00"
@LOCK_EX = global i32 0
@.str.1 = [6 x i8] c"flock\00"
@.str.2 = [6 x i8] c"sleep\00"
@.str.3 = [7 x i8] c"unlock\00"
@LOCK_UN = global i32 0
define void @test_flock(i32 %0) {
%2 = alloca i32
store i32 %0, i32* %2
%3 = call i32 @message(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%4 = load i32, i32* %2
%5 = load i32, i32* @LOCK_EX
%6 = call i32 @flock(i32 %4, i32 %5)
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %11
8: ; preds = %1
%9 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%10 = call i32 (...) @abort()
unreachable
11: ; preds = %1
%12 = call i32 @message(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%13 = call i32 (...) @delay()
%14 = call i32 @message(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
%15 = load i32, i32* %2
%16 = load i32, i32* @LOCK_UN
%17 = call i32 @flock(i32 %15, i32 %16)
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %22
19: ; preds = %11
%20 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%21 = call i32 (...) @abort()
unreachable
22: ; preds = %11
ret void
}

LLVM IR (-O2):
@.str = [5 x i8] c"lock\00"
@LOCK_EX = global i32 0
@.str.1 = [6 x i8] c"flock\00"
@.str.2 = [6 x i8] c"sleep\00"
@.str.3 = [7 x i8] c"unlock\00"
@LOCK_UN = global i32 0
define void @test_flock(i32 %0) {
%2 = tail call i32 @message(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%3 = load i32, i32* @LOCK_EX
%4 = tail call i32 @flock(i32 %0, i32 %3)
%5 = icmp eq i32 %4, -1
br i1 %5, label %6, label %9
6: ; preds = %1
%7 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%8 = tail call i32 (...) @abort()
unreachable
9: ; preds = %1
%10 = tail call i32 @message(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%11 = tail call i32 (...) @delay()
%12 = tail call i32 @message(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
%13 = load i32, i32* @LOCK_UN
%14 = tail call i32 @flock(i32 %0, i32 %13)
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %19
16: ; preds = %9
%17 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%18 = tail call i32 (...) @abort()
unreachable
19: ; preds = %9
ret void
}