#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_RDLCK;
int /*<<< orphan*/ F_UNLCK;
int /*<<< orphan*/ F_WRLCK;
int O_CREAT;
int O_RDWR;
int /*<<< orphan*/ getlock(int, int /*<<< orphan*/);
int open(char *, int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ waitforuser(char *);
int main(void) {
  int fd;
  fd = open("testlockfile", O_RDWR | O_CREAT, 0666);
  if (fd < 0) {
    perror("open");
    return 1;
  }
  printf("getting read lock\n");
  getlock(fd, F_RDLCK);
  printf("got read lock\n");
  waitforuser("\npress <return> to continue:");
  printf("releasing lock\n");
  getlock(fd, F_UNLCK);
  printf("getting write lock\n");
  getlock(fd, F_WRLCK);
  printf("got write lock\n");
  waitforuser("\npress <return> to exit:");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e7a(%rip),%rax
mov    (%rax),%esi
mov    0x2e89(%rip),%rax
or     (%rax),%esi
lea    0xe88(%rip),%rdi
mov    $0x1b6,%edx
call   1050 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    11a7 <main+0x57>
lea    0xe77(%rip),%rdi
call   1060 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1235 <main+0xe5>
lea    0xe64(%rip),%rdi
call   1030 <printf@plt>
mov    -0x8(%rbp),%edi
mov    0x2dfb(%rip),%rax
mov    (%rax),%esi
call   1070 <getlock@plt>
lea    0xe5a(%rip),%rdi
call   1030 <printf@plt>
lea    0xe5d(%rip),%rdi
call   1040 <waitforuser@plt>
lea    0xe6e(%rip),%rdi
call   1030 <printf@plt>
mov    -0x8(%rbp),%edi
mov    0x2de6(%rip),%rax
mov    (%rax),%esi
call   1070 <getlock@plt>
lea    0xe61(%rip),%rdi
call   1030 <printf@plt>
mov    -0x8(%rbp),%edi
mov    0x2db9(%rip),%rax
mov    (%rax),%esi
call   1070 <getlock@plt>
lea    0xe58(%rip),%rdi
call   1030 <printf@plt>
lea    0xe5c(%rip),%rdi
call   1040 <waitforuser@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
mov    0x2e88(%rip),%rax
mov    0x2e99(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe96(%rip),%rdi
mov    $0x1b6,%edx
call   1050 <open@plt>
test   %eax,%eax
js     1206 <main+0xb6>
mov    %eax,%ebx
lea    0xec3(%rip),%rdi
call   1030 <puts@plt>
mov    0x2e27(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1070 <getlock@plt>
lea    0xeb9(%rip),%rdi
call   1030 <puts@plt>
lea    0xe65(%rip),%rdi
call   1040 <waitforuser@plt>
lea    0xeaf(%rip),%rdi
call   1030 <puts@plt>
mov    0x2e13(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1070 <getlock@plt>
lea    0xea2(%rip),%rdi
call   1030 <puts@plt>
mov    0x2de7(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1070 <getlock@plt>
lea    0xe99(%rip),%rdi
call   1030 <puts@plt>
lea    0xe32(%rip),%rdi
call   1040 <waitforuser@plt>
xor    %eax,%eax
pop    %rbx
ret
lea    0xe00(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%eax
pop    %rbx
ret

LLVM IR (-O0):
@.str = [13 x i8] c"testlockfile\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@.str.1 = [5 x i8] c"open\00"
@.str.2 = [19 x i8] c"getting read lock\0A\00"
@F_RDLCK = global i32 0
@.str.3 = [15 x i8] c"got read lock\0A\00"
@.str.4 = [29 x i8] c"\0Apress <return> to continue:\00"
@.str.5 = [16 x i8] c"releasing lock\0A\00"
@F_UNLCK = global i32 0
@.str.6 = [20 x i8] c"getting write lock\0A\00"
@F_WRLCK = global i32 0
@.str.7 = [16 x i8] c"got write lock\0A\00"
@.str.8 = [25 x i8] c"\0Apress <return> to exit:\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
store i32 0, i32* %1
%3 = load i32, i32* @O_RDWR
%4 = load i32, i32* @O_CREAT
%5 = or i32 %3, %4
%6 = call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %5, i32 438)
store i32 %6, i32* %2
%7 = load i32, i32* %2
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %0
%10 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %1
br label %28
11: ; preds = %0
%12 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0))
%13 = load i32, i32* %2
%14 = load i32, i32* @F_RDLCK
%15 = call i32 @getlock(i32 %13, i32 %14)
%16 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0))
%17 = call i32 @waitforuser(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.4, i64 0, i64 0))
%18 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.5, i64 0, i64 0))
%19 = load i32, i32* %2
%20 = load i32, i32* @F_UNLCK
%21 = call i32 @getlock(i32 %19, i32 %20)
%22 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.6, i64 0, i64 0))
%23 = load i32, i32* %2
%24 = load i32, i32* @F_WRLCK
%25 = call i32 @getlock(i32 %23, i32 %24)
%26 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.7, i64 0, i64 0))
%27 = call i32 @waitforuser(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.8, i64 0, i64 0))
store i32 0, i32* %1
br label %28
28: ; preds = %11, %9
%29 = load i32, i32* %1
ret i32 %29
}

LLVM IR (-O2):
@.str = [13 x i8] c"testlockfile\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@.str.1 = [5 x i8] c"open\00"
@F_RDLCK = global i32 0
@.str.4 = [29 x i8] c"\0Apress <return> to continue:\00"
@F_UNLCK = global i32 0
@F_WRLCK = global i32 0
@.str.8 = [25 x i8] c"\0Apress <return> to exit:\00"
@str = [18 x i8] c"getting read lock\00"
@str.9 = [14 x i8] c"got read lock\00"
@str.10 = [15 x i8] c"releasing lock\00"
@str.11 = [19 x i8] c"getting write lock\00"
@str.12 = [15 x i8] c"got write lock\00"
define i32 @main() {
%1 = load i32, i32* @O_RDWR
%2 = load i32, i32* @O_CREAT
%3 = or i32 %2, %1
%4 = tail call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %3, i32 438)
%5 = icmp slt i32 %4, 0
br i1 %5, label %6, label %8
6: ; preds = %0
%7 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %22
8: ; preds = %0
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str, i64 0, i64 0))
%10 = load i32, i32* @F_RDLCK
%11 = tail call i32 @getlock(i32 %4, i32 %10)
%12 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str.9, i64 0, i64 0))
%13 = tail call i32 @waitforuser(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.4, i64 0, i64 0))
%14 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str.10, i64 0, i64 0))
%15 = load i32, i32* @F_UNLCK
%16 = tail call i32 @getlock(i32 %4, i32 %15)
%17 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str.11, i64 0, i64 0))
%18 = load i32, i32* @F_WRLCK
%19 = tail call i32 @getlock(i32 %4, i32 %18)
%20 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str.12, i64 0, i64 0))
%21 = tail call i32 @waitforuser(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.8, i64 0, i64 0))
br label %22
22: ; preds = %8, %6
%23 = phi i32 [ 1, %6 ], [ 0, %8 ]
ret i32 %23
}