#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;
  int /*<<< orphan*/ l_pid;
  int /*<<< orphan*/ member_4;
  int /*<<< orphan*/ member_3;
  int /*<<< orphan*/ member_2;
  int /*<<< orphan*/ member_1;
  int /*<<< orphan*/ member_0;
};

/* Variables and functions */
int /*<<< orphan*/ F_SETLK;
int /*<<< orphan*/ F_SETLKW;
int /*<<< orphan*/ F_UNLCK;
int /*<<< orphan*/ F_WRLCK;
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int fcntl(int, int /*<<< orphan*/, struct flock *);
int /*<<< orphan*/ getchar();
int /*<<< orphan*/ getpid();
int open(char const *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
void test_locking(const char *filepath) {
  struct flock fl = {F_WRLCK, SEEK_SET, 0, 0, 0};
  int fd;
  fl.l_pid = getpid();
  if ((fd = open(filepath, O_RDWR)) == -1) {
    perror("open");
    exit(1);
  }
  printf("Attempting to get write lock...\n");
  if (fcntl(fd, F_SETLKW, &fl) == -1) {
    perror("fcntl");
    close(fd);
    exit(1);
  }
  printf("LOCKED - %s\n", filepath);
  printf("Press <RETURN> to release lock: ");
  getchar();
  fl.l_type = F_UNLCK;
  if (fcntl(fd, F_SETLK, &fl) == -1) {
    perror("fcntl");
    close(fd);
    exit(1);
  }
  printf("UNLOCKED - %s\n", filepath);
  printf("success!\n");
  close(fd);
}


x86 assembly (clang, -O0):
<test_locking>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    0x2e25(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    0x2e21(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
movl   $0x0,-0x20(%rbp)
movl   $0x0,-0x1c(%rbp)
movl   $0x0,-0x18(%rbp)
movl   $0x0,-0x14(%rbp)
movl   $0x0,-0x10(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,-0x24(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2e0c(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x2c(%rbp)
cmp    $0xffffffff,%eax
jne    1205 <test_locking+0x85>
lea    0xe0a(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
lea    0xdf9(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x2c(%rbp),%edi
mov    0x2dc3(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdx
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
jne    124f <test_locking+0xcf>
lea    0xdee(%rip),%rdi
call   1090 <perror@plt>
mov    -0x2c(%rbp),%edi
call   1060 <close@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x8(%rbp),%rsi
lea    0xdf5(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xdc4(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x0,%al
call   1070 <getchar@plt>
mov    0x2d53(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    -0x2c(%rbp),%edi
mov    0x2d4c(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdx
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
jne    12be <test_locking+0x13e>
lea    0xd7f(%rip),%rdi
call   1090 <perror@plt>
mov    -0x2c(%rbp),%edi
call   1060 <close@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x8(%rbp),%rsi
lea    0xd84(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xd85(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x2c(%rbp),%edi
call   1060 <close@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_locking>:
push   %r14
push   %rbx
sub    $0x28,%rsp
mov    %rdi,%r14
mov    0x2e1f(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
xorps  %xmm0,%xmm0
movups %xmm0,0x10(%rsp)
movl   $0x0,0x20(%rsp)
xor    %eax,%eax
call   1050 <getpid@plt>
mov    %eax,0xc(%rsp)
mov    0x2e1f(%rip),%rax
mov    (%rax),%esi
mov    %r14,%rdi
call   1090 <open@plt>
cmp    $0xffffffff,%eax
je     129a <test_locking+0x10a>
mov    %eax,%ebx
lea    0xe56(%rip),%rdi
call   1030 <puts@plt>
mov    0x2def(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
mov    %ebx,%edi
call   1040 <fcntl@plt>
cmp    $0xffffffff,%eax
je     127d <test_locking+0xed>
lea    0xe23(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xdef(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
xor    %eax,%eax
call   1080 <getchar@plt>
mov    0x2d9f(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
mov    0x2d9a(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
mov    %ebx,%edi
call   1040 <fcntl@plt>
cmp    $0xffffffff,%eax
je     127d <test_locking+0xed>
lea    0xdd4(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xdf2(%rip),%rdi
call   1030 <puts@plt>
mov    %ebx,%edi
call   1070 <close@plt>
add    $0x28,%rsp
pop    %rbx
pop    %r14
ret
lea    0xd81(%rip),%rdi
call   10a0 <perror@plt>
mov    %ebx,%edi
call   1070 <close@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
lea    0xd5f(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
%struct.flock = type { i32, i32, i32, i32, i32, i32, i32 }
@F_WRLCK = global i32 0
@SEEK_SET = global i32 0
@O_RDWR = global i32 0
@.str = [5 x i8] c"open\00"
@.str.1 = [33 x i8] c"Attempting to get write lock...\0A\00"
@F_SETLKW = global i32 0
@.str.2 = [6 x i8] c"fcntl\00"
@.str.3 = [13 x i8] c"LOCKED - %s\0A\00"
@.str.4 = [33 x i8] c"Press <RETURN> to release lock: \00"
@F_UNLCK = global i32 0
@F_SETLK = global i32 0
@.str.5 = [15 x i8] c"UNLOCKED - %s\0A\00"
@.str.6 = [10 x i8] c"success!\0A\00"
define void @test_locking(i8* %0) {
%2 = alloca i8*
%3 = alloca %struct.flock
%4 = alloca i32
store i8* %0, i8** %2
%5 = getelementptr inbounds %struct.flock, %struct.flock* %3, i32 0, i32 0
%6 = load i32, i32* @F_WRLCK
store i32 %6, i32* %5
%7 = getelementptr inbounds %struct.flock, %struct.flock* %3, i32 0, i32 1
%8 = load i32, i32* @SEEK_SET
store i32 %8, i32* %7
%9 = getelementptr inbounds %struct.flock, %struct.flock* %3, i32 0, i32 2
store i32 0, i32* %9
%10 = getelementptr inbounds %struct.flock, %struct.flock* %3, i32 0, i32 3
store i32 0, i32* %10
%11 = getelementptr inbounds %struct.flock, %struct.flock* %3, i32 0, i32 4
store i32 0, i32* %11
%12 = getelementptr inbounds %struct.flock, %struct.flock* %3, i32 0, i32 5
store i32 0, i32* %12
%13 = getelementptr inbounds %struct.flock, %struct.flock* %3, i32 0, i32 6
store i32 0, i32* %13
%14 = call i32 (...) @getpid()
%15 = getelementptr inbounds %struct.flock, %struct.flock* %3, i32 0, i32 1
store i32 %14, i32* %15
%16 = load i8*, i8** %2
%17 = load i32, i32* @O_RDWR
%18 = call i32 @open(i8* %16, i32 %17)
store i32 %18, i32* %4
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %23
20: ; preds = %1
%21 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%22 = call i32 @exit(i32 1)
unreachable
23: ; preds = %1
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0))
%25 = load i32, i32* %4
%26 = load i32, i32* @F_SETLKW
%27 = call i32 @fcntl(i32 %25, i32 %26, %struct.flock* %3)
%28 = icmp eq i32 %27, -1
br i1 %28, label %29, label %34
29: ; preds = %23
%30 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%31 = load i32, i32* %4
%32 = call i32 @close(i32 %31)
%33 = call i32 @exit(i32 1)
unreachable
34: ; preds = %23
%35 = load i8*, i8** %2
%36 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0), i8* %35)
%37 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.4, i64 0, i64 0))
%38 = call i32 (...) @getchar()
%39 = load i32, i32* @F_UNLCK
%40 = getelementptr inbounds %struct.flock, %struct.flock* %3, i32 0, i32 0
store i32 %39, i32* %40
%41 = load i32, i32* %4
%42 = load i32, i32* @F_SETLK
%43 = call i32 @fcntl(i32 %41, i32 %42, %struct.flock* %3)
%44 = icmp eq i32 %43, -1
br i1 %44, label %45, label %50
45: ; preds = %34
%46 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%47 = load i32, i32* %4
%48 = call i32 @close(i32 %47)
%49 = call i32 @exit(i32 1)
unreachable
50: ; preds = %34
%51 = load i8*, i8** %2
%52 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.5, i64 0, i64 0), i8* %51)
%53 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.6, i64 0, i64 0))
%54 = load i32, i32* %4
%55 = call i32 @close(i32 %54)
ret void
}

LLVM IR (-O2):
%struct.flock = type { i32, i32, i32, i32, i32, i32, i32 }
@F_WRLCK = global i32 0
@SEEK_SET = global i32 0
@O_RDWR = global i32 0
@.str = [5 x i8] c"open\00"
@F_SETLKW = global i32 0
@.str.2 = [6 x i8] c"fcntl\00"
@.str.3 = [13 x i8] c"LOCKED - %s\0A\00"
@.str.4 = [33 x i8] c"Press <RETURN> to release lock: \00"
@F_UNLCK = global i32 0
@F_SETLK = global i32 0
@.str.5 = [15 x i8] c"UNLOCKED - %s\0A\00"
@str = [32 x i8] c"Attempting to get write lock...\00"
@str.7 = [9 x i8] c"success!\00"
define void @test_locking(i8* %0) {
%2 = alloca %struct.flock
%3 = bitcast %struct.flock* %2 to i8*
%4 = getelementptr inbounds %struct.flock, %struct.flock* %2, i64 0, i32 0
%5 = load i32, i32* @F_WRLCK
store i32 %5, i32* %4
%6 = getelementptr inbounds %struct.flock, %struct.flock* %2, i64 0, i32 1
%7 = getelementptr inbounds %struct.flock, %struct.flock* %2, i64 0, i32 2
%8 = bitcast i32* %7 to i8*
%9 = tail call i32 (...) @getpid()
store i32 %9, i32* %6
%10 = load i32, i32* @O_RDWR
%11 = tail call i32 @open(i8* %0, i32 %10)
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %16
13: ; preds = %1
%14 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%15 = tail call i32 @exit(i32 1)
unreachable
16: ; preds = %1
%17 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @str, i64 0, i64 0))
%18 = load i32, i32* @F_SETLKW
%19 = call i32 @fcntl(i32 %11, i32 %18, %struct.flock* %2)
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %25
21: ; preds = %16
%22 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%23 = call i32 @close(i32 %11)
%24 = call i32 @exit(i32 1)
unreachable
25: ; preds = %16
%26 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0), i8* %0)
%27 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @.str.4, i64 0, i64 0))
%28 = call i32 (...) @getchar()
%29 = load i32, i32* @F_UNLCK
store i32 %29, i32* %4
%30 = load i32, i32* @F_SETLK
%31 = call i32 @fcntl(i32 %11, i32 %30, %struct.flock* %2)
%32 = icmp eq i32 %31, -1
br i1 %32, label %33, label %37
33: ; preds = %25
%34 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%35 = call i32 @close(i32 %11)
%36 = call i32 @exit(i32 1)
unreachable
37: ; preds = %25
%38 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.5, i64 0, i64 0), i8* %0)
%39 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @str.7, i64 0, i64 0))
%40 = call i32 @close(i32 %11)
ret void
}