#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*/ abort();
int /*<<< orphan*/ delay();
int lockf(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ message(char *);
int /*<<< orphan*/ perror(char *);
void test_lockf(int fd) {
  message("lock");
  if (lockf(fd, F_LOCK, 0) == -1) {
    perror("lockf");
    abort();
  }
  message("sleep");
  delay();
  message("unlock");
  if (lockf(fd, F_ULOCK, 0) == -1) {
    perror("lockf");
    abort();
  }
}


x86 assembly (clang, -O0):
<test_lockf>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
lea    0xeac(%rip),%rdi
call   1030 <message@plt>
mov    -0x4(%rbp),%edi
mov    0x2e6f(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1050 <lockf@plt>
cmp    $0xffffffff,%eax
jne    1196 <test_lockf+0x46>
lea    0xe76(%rip),%rdi
call   1070 <perror@plt>
mov    $0x0,%al
call   1040 <abort@plt>
lea    0xe69(%rip),%rdi
call   1030 <message@plt>
mov    $0x0,%al
call   1060 <delay@plt>
lea    0xe5c(%rip),%rdi
call   1030 <message@plt>
mov    -0x4(%rbp),%edi
mov    0x2e29(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1050 <lockf@plt>
cmp    $0xffffffff,%eax
jne    11e4 <test_lockf+0x94>
lea    0xe28(%rip),%rdi
call   1070 <perror@plt>
mov    $0x0,%al
call   1040 <abort@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_lockf>:
push   %rbx
mov    %edi,%ebx
lea    0xeb4(%rip),%rdi
call   1030 <message@plt>
mov    0x2e7a(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
xor    %edx,%edx
call   1050 <lockf@plt>
cmp    $0xffffffff,%eax
je     11ae <test_lockf+0x5e>
lea    0xe89(%rip),%rdi
call   1030 <message@plt>
xor    %eax,%eax
call   1060 <delay@plt>
lea    0xe7c(%rip),%rdi
call   1030 <message@plt>
mov    0x2e4c(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
xor    %edx,%edx
call   1050 <lockf@plt>
cmp    $0xffffffff,%eax
je     11ae <test_lockf+0x5e>
pop    %rbx
ret
lea    0xe4b(%rip),%rdi
call   1070 <perror@plt>
xor    %eax,%eax
call   1040 <abort@plt>

LLVM IR (-O0):
@.str = [5 x i8] c"lock\00"
@F_LOCK = global i32 0
@.str.1 = [6 x i8] c"lockf\00"
@.str.2 = [6 x i8] c"sleep\00"
@.str.3 = [7 x i8] c"unlock\00"
@F_ULOCK = global i32 0
define void @test_lockf(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* @F_LOCK
%6 = call i32 @lockf(i32 %4, i32 %5, i32 0)
%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* @F_ULOCK
%17 = call i32 @lockf(i32 %15, i32 %16, i32 0)
%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"
@F_LOCK = global i32 0
@.str.1 = [6 x i8] c"lockf\00"
@.str.2 = [6 x i8] c"sleep\00"
@.str.3 = [7 x i8] c"unlock\00"
@F_ULOCK = global i32 0
define void @test_lockf(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* @F_LOCK
%4 = tail call i32 @lockf(i32 %0, i32 %3, i32 0)
%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* @F_ULOCK
%14 = tail call i32 @lockf(i32 %0, i32 %13, i32 0)
%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
}