#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;
  scalar_t__ l_len;
  scalar_t__ l_start;
  int /*<<< orphan*/ l_whence;
};

/* Variables and functions */
int /*<<< orphan*/ F_SETLK;
int /*<<< orphan*/ F_SETLKW;
int /*<<< orphan*/ F_UNLCK;
int /*<<< orphan*/ F_WRLCK;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ abort();
int /*<<< orphan*/ delay();
int fcntl(int, int /*<<< orphan*/, struct flock *);
int /*<<< orphan*/ message(char *);
int /*<<< orphan*/ perror(char *);
void test_fcntl_posix(int fd) {
  message("lock");
  struct flock fl;
  fl.l_type = F_WRLCK;
  fl.l_whence = SEEK_SET;
  fl.l_start = 0;
  fl.l_len = 0;
  if (fcntl(fd, F_SETLKW, &fl) == -1) {
    perror("fcntl");
    abort();
  }
  message("sleep");
  delay();
  message("unlock");
  fl.l_type = F_UNLCK;
  if (fcntl(fd, F_SETLK, &fl) == -1) {
    perror("fcntl");
    abort();
  }
}


x86 assembly (clang, -O0):
<test_fcntl_posix>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
lea    0xeac(%rip),%rdi
call   1030 <message@plt>
mov    0x2e52(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    0x2e4e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x4(%rbp),%edi
mov    0x2e4f(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdx
call   1050 <fcntl@plt>
cmp    $0xffffffff,%eax
jne    11c0 <test_fcntl_posix+0x70>
lea    0xe4c(%rip),%rdi
call   1070 <perror@plt>
mov    $0x0,%al
call   1040 <abort@plt>
lea    0xe3f(%rip),%rdi
call   1030 <message@plt>
mov    $0x0,%al
call   1060 <delay@plt>
lea    0xe32(%rip),%rdi
call   1030 <message@plt>
mov    0x2df2(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    -0x4(%rbp),%edi
mov    0x2deb(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdx
call   1050 <fcntl@plt>
cmp    $0xffffffff,%eax
jne    121c <test_fcntl_posix+0xcc>
lea    0xdf0(%rip),%rdi
call   1070 <perror@plt>
mov    $0x0,%al
call   1040 <abort@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_fcntl_posix>:
push   %rbx
sub    $0x20,%rsp
mov    %edi,%ebx
lea    0xeb0(%rip),%rdi
call   1030 <message@plt>
mov    0x2e56(%rip),%rax
mov    (%rax),%eax
mov    %eax,(%rsp)
mov    0x2e52(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x18(%rsp)
xorps  %xmm0,%xmm0
movups %xmm0,0x8(%rsp)
mov    0x2e5d(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
mov    %ebx,%edi
call   1050 <fcntl@plt>
cmp    $0xffffffff,%eax
je     11e5 <test_fcntl_posix+0x95>
lea    0xe63(%rip),%rdi
call   1030 <message@plt>
xor    %eax,%eax
call   1060 <delay@plt>
lea    0xe56(%rip),%rdi
call   1030 <message@plt>
mov    0x2e16(%rip),%rax
mov    (%rax),%eax
mov    %eax,(%rsp)
mov    0x2e12(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
mov    %ebx,%edi
call   1050 <fcntl@plt>
cmp    $0xffffffff,%eax
je     11e5 <test_fcntl_posix+0x95>
add    $0x20,%rsp
pop    %rbx
ret
lea    0xe14(%rip),%rdi
call   1070 <perror@plt>
xor    %eax,%eax
call   1040 <abort@plt>

LLVM IR (-O0):
%struct.flock = type { i32, i64, i64, i32 }
@.str = [5 x i8] c"lock\00"
@F_WRLCK = global i32 0
@SEEK_SET = global i32 0
@F_SETLKW = global i32 0
@.str.1 = [6 x i8] c"fcntl\00"
@.str.2 = [6 x i8] c"sleep\00"
@.str.3 = [7 x i8] c"unlock\00"
@F_UNLCK = global i32 0
@F_SETLK = global i32 0
define void @test_fcntl_posix(i32 %0) {
%2 = alloca i32
%3 = alloca %struct.flock
store i32 %0, i32* %2
%4 = call i32 @message(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%5 = load i32, i32* @F_WRLCK
%6 = getelementptr inbounds %struct.flock, %struct.flock* %3, i32 0, i32 0
store i32 %5, i32* %6
%7 = load i32, i32* @SEEK_SET
%8 = getelementptr inbounds %struct.flock, %struct.flock* %3, i32 0, i32 3
store i32 %7, i32* %8
%9 = getelementptr inbounds %struct.flock, %struct.flock* %3, i32 0, i32 2
store i64 0, i64* %9
%10 = getelementptr inbounds %struct.flock, %struct.flock* %3, i32 0, i32 1
store i64 0, i64* %10
%11 = load i32, i32* %2
%12 = load i32, i32* @F_SETLKW
%13 = call i32 @fcntl(i32 %11, i32 %12, %struct.flock* %3)
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %18
15: ; preds = %1
%16 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%17 = call i32 (...) @abort()
unreachable
18: ; preds = %1
%19 = call i32 @message(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%20 = call i32 (...) @delay()
%21 = call i32 @message(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
%22 = load i32, i32* @F_UNLCK
%23 = getelementptr inbounds %struct.flock, %struct.flock* %3, i32 0, i32 0
store i32 %22, i32* %23
%24 = load i32, i32* %2
%25 = load i32, i32* @F_SETLK
%26 = call i32 @fcntl(i32 %24, i32 %25, %struct.flock* %3)
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %31
28: ; preds = %18
%29 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%30 = call i32 (...) @abort()
unreachable
31: ; preds = %18
ret void
}

LLVM IR (-O2):
%struct.flock = type { i32, i64, i64, i32 }
@.str = [5 x i8] c"lock\00"
@F_WRLCK = global i32 0
@SEEK_SET = global i32 0
@F_SETLKW = global i32 0
@.str.1 = [6 x i8] c"fcntl\00"
@.str.2 = [6 x i8] c"sleep\00"
@.str.3 = [7 x i8] c"unlock\00"
@F_UNLCK = global i32 0
@F_SETLK = global i32 0
define void @test_fcntl_posix(i32 %0) {
%2 = alloca %struct.flock
%3 = tail call i32 @message(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%4 = bitcast %struct.flock* %2 to i8*
%5 = load i32, i32* @F_WRLCK
%6 = getelementptr inbounds %struct.flock, %struct.flock* %2, i64 0, i32 0
store i32 %5, i32* %6
%7 = load i32, i32* @SEEK_SET
%8 = getelementptr inbounds %struct.flock, %struct.flock* %2, i64 0, i32 3
store i32 %7, i32* %8
%9 = getelementptr inbounds %struct.flock, %struct.flock* %2, i64 0, i32 1
%10 = bitcast i64* %9 to i8*
%11 = load i32, i32* @F_SETLKW
%12 = call i32 @fcntl(i32 %0, i32 %11, %struct.flock* %2)
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %17
14: ; preds = %1
%15 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%16 = call i32 (...) @abort()
unreachable
17: ; preds = %1
%18 = call i32 @message(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%19 = call i32 (...) @delay()
%20 = call i32 @message(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
%21 = load i32, i32* @F_UNLCK
store i32 %21, i32* %6
%22 = load i32, i32* @F_SETLK
%23 = call i32 @fcntl(i32 %0, i32 %22, %struct.flock* %2)
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %28
25: ; preds = %17
%26 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%27 = call i32 (...) @abort()
unreachable
28: ; preds = %17
ret void
}