#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 */
typedef int /*<<< orphan*/ ssize_t;

/* Variables and functions */
int /*<<< orphan*/ F_RDLCK;
int /*<<< orphan*/ F_UNLCK;
int O_CREAT;
int O_RDWR;
int /*<<< orphan*/ close(int);
int getpid();
int /*<<< orphan*/ lock_file(int, int /*<<< orphan*/);
int open(char *, int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ sleep(int);
int lock_demo() {
  int fd = open("1.txt", O_RDWR | O_CREAT, 0666);
  if (fd < 0) {
    perror("open");
    goto err;
  }
  lock_file(fd, F_RDLCK);
  printf("set write lock, %d\n", getpid());
  char buf[1024];
  ssize_t n;
  sleep(5);
  printf("write lock release, %d\n", getpid());
  lock_file(fd, F_UNLCK);
  close(fd);
err:
  return -1;
}


x86 assembly (clang, -O0):
<lock_demo>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
mov    0x2e5e(%rip),%rax
mov    (%rax),%esi
mov    0x2e6d(%rip),%rax
or     (%rax),%esi
lea    0xe6c(%rip),%rdi
mov    $0x1b6,%edx
call   1070 <open@plt>
mov    %eax,-0x4(%rbp)
cmpl   $0x0,-0x4(%rbp)
jge    11bc <lock_demo+0x4c>
lea    0xe54(%rip),%rdi
call   1080 <perror@plt>
jmp    121a <lock_demo+0xaa>
mov    -0x4(%rbp),%edi
mov    0x2dfa(%rip),%rax
mov    (%rax),%esi
call   1050 <lock_file@plt>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xe2e(%rip),%rdi
call   1040 <printf@plt>
mov    $0x5,%edi
call   1090 <sleep@plt>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xe23(%rip),%rdi
call   1040 <printf@plt>
mov    -0x4(%rbp),%edi
mov    0x2dcd(%rip),%rax
mov    (%rax),%esi
call   1050 <lock_file@plt>
mov    -0x4(%rbp),%edi
call   1060 <close@plt>
mov    $0xffffffff,%eax
add    $0x420,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<lock_demo>:
push   %rbx
mov    0x2e68(%rip),%rax
mov    0x2e79(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe76(%rip),%rdi
mov    $0x1b6,%edx
call   1070 <open@plt>
test   %eax,%eax
js     11fc <lock_demo+0x8c>
mov    %eax,%ebx
mov    0x2e1f(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1050 <lock_file@plt>
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xe53(%rip),%rdi
mov    %eax,%esi
call   1040 <printf@plt>
mov    $0x5,%edi
call   1090 <sleep@plt>
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xe48(%rip),%rdi
mov    %eax,%esi
call   1040 <printf@plt>
mov    0x2df3(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1050 <lock_file@plt>
mov    %ebx,%edi
call   1060 <close@plt>
mov    $0xffffffff,%eax
pop    %rbx
ret
lea    0xe03(%rip),%rdi
call   1080 <perror@plt>
jmp    11f5 <lock_demo+0x85>

LLVM IR (-O0):
@.str = [6 x i8] c"1.txt\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.2 = [20 x i8] c"set write lock, %d\0A\00"
@.str.3 = [24 x i8] c"write lock release, %d\0A\00"
@F_UNLCK = global i32 0
define i32 @lock_demo() {
%1 = alloca i32
%2 = alloca [1024 x i8]
%3 = alloca i32
%4 = load i32, i32* @O_RDWR
%5 = load i32, i32* @O_CREAT
%6 = or i32 %4, %5
%7 = call i32 @open(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32 %6, i32 438)
store i32 %7, i32* %1
%8 = load i32, i32* %1
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %0
%11 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %26
12: ; preds = %0
%13 = load i32, i32* %1
%14 = load i32, i32* @F_RDLCK
%15 = call i32 @lock_file(i32 %13, i32 %14)
%16 = call i32 (...) @getpid()
%17 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i32 %16)
%18 = call i32 @sleep(i32 5)
%19 = call i32 (...) @getpid()
%20 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0), i32 %19)
%21 = load i32, i32* %1
%22 = load i32, i32* @F_UNLCK
%23 = call i32 @lock_file(i32 %21, i32 %22)
%24 = load i32, i32* %1
%25 = call i32 @close(i32 %24)
br label %26
26: ; preds = %12, %10
ret i32 -1
}

LLVM IR (-O2):
@.str = [6 x i8] c"1.txt\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.2 = [20 x i8] c"set write lock, %d\0A\00"
@.str.3 = [24 x i8] c"write lock release, %d\0A\00"
@F_UNLCK = global i32 0
define i32 @lock_demo() {
%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 ([6 x i8], [6 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 %19
8: ; preds = %0
%9 = load i32, i32* @F_RDLCK
%10 = tail call i32 @lock_file(i32 %4, i32 %9)
%11 = tail call i32 (...) @getpid()
%12 = tail call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i32 %11)
%13 = tail call i32 @sleep(i32 5)
%14 = tail call i32 (...) @getpid()
%15 = tail call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0), i32 %14)
%16 = load i32, i32* @F_UNLCK
%17 = tail call i32 @lock_file(i32 %4, i32 %16)
%18 = tail call i32 @close(i32 %4)
br label %19
19: ; preds = %8, %6
ret i32 -1
}