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

/* Variables and functions */
int /*<<< orphan*/ F_RDLCK;
int /*<<< orphan*/ F_SETLK;
int /*<<< orphan*/ F_UNLCK;
int O_APPEND;
int O_RDWR;
void *SEEK_END;
int /*<<< orphan*/ dump_lock(char *, struct flock *);
int /*<<< orphan*/ err_sys(char *, char *);
char *errno;
int fcntl(int, int /*<<< orphan*/, struct flock *);
int /*<<< orphan*/ getpid();
int open(char *, int);
int /*<<< orphan*/ printf(char *, int /*<<< orphan*/, ...);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ usage();
int write(int, char *, int);
int main(int argc, char *argv[]) {
  if (argc != 2)
    usage();
  int fd = open(argv[1], O_RDWR | O_APPEND);
  if (fd == -1)
    err_sys("open %s failed", argv[1]);
  struct flock lock;
  lock.l_type = F_RDLCK;
  lock.l_start = 0;
  lock.l_whence = SEEK_END;
  lock.l_len = 0;
  dump_lock("LOCK", &lock);
  int ret = fcntl(fd, F_SETLK, &lock);
  if (ret == -1)
    err_sys("fcntl failed, errno %d", errno);
  printf("[%lu] got lock\n", getpid());
  sleep(3);
  ret = write(fd, "helo", 1);
  printf("[%lu] increase file by write %d byte\n", getpid(), ret);
  lock.l_type = F_UNLCK;
  lock.l_start = 0;
  lock.l_whence = SEEK_END;
  lock.l_len = 0;
  dump_lock("UNLOCK", &lock);
  ret = fcntl(fd, F_SETLK, &lock);
  if (ret == -1)
    err_sys("fcntl failed, errno %d", errno);
  printf("[%lu] release lock\n", getpid());
  sleep(2);
  printf("%lu exit\n", getpid());
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     11b7 <main+0x27>
mov    $0x0,%al
call   1080 <usage@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e22(%rip),%rax
mov    (%rax),%esi
mov    0x2de1(%rip),%rax
or     (%rax),%esi
call   1090 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11f7 <main+0x67>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xe0e(%rip),%rdi
call   1030 <err_sys@plt>
mov    0x2daa(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
mov    0x2dce(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
lea    0xe3b(%rip),%rdi
lea    -0x38(%rbp),%rsi
call   10b0 <dump_lock@plt>
mov    -0x14(%rbp),%edi
mov    0x2d9d(%rip),%rax
mov    (%rax),%esi
lea    -0x38(%rbp),%rdx
call   1040 <fcntl@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0xffffffff,-0x3c(%rbp)
jne    1269 <main+0xd9>
mov    0x2d66(%rip),%rax
mov    (%rax),%rsi
lea    0xdab(%rip),%rdi
call   1030 <err_sys@plt>
mov    $0x0,%al
call   1060 <getpid@plt>
mov    %eax,%esi
lea    0xdad(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    $0x3,%edi
call   10a0 <sleep@plt>
mov    -0x14(%rbp),%edi
lea    0xda2(%rip),%rsi
mov    $0x1,%edx
call   1050 <write@plt>
mov    %eax,-0x3c(%rbp)
mov    $0x0,%al
call   1060 <getpid@plt>
mov    %eax,%esi
mov    -0x3c(%rbp),%edx
lea    0xd87(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    0x2d0e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
mov    0x2d0a(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
lea    0xd75(%rip),%rdi
lea    -0x38(%rbp),%rsi
call   10b0 <dump_lock@plt>
mov    -0x14(%rbp),%edi
mov    0x2cd9(%rip),%rax
mov    (%rax),%esi
lea    -0x38(%rbp),%rdx
call   1040 <fcntl@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0xffffffff,-0x3c(%rbp)
jne    132d <main+0x19d>
mov    0x2ca2(%rip),%rax
mov    (%rax),%rsi
lea    0xce7(%rip),%rdi
call   1030 <err_sys@plt>
mov    $0x0,%al
call   1060 <getpid@plt>
mov    %eax,%esi
lea    0xd2b(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    $0x2,%edi
call   10a0 <sleep@plt>
mov    $0x0,%al
call   1060 <getpid@plt>
mov    %eax,%esi
lea    0xd1e(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x28,%rsp
mov    %rsi,%rbx
cmp    $0x2,%edi
je     11a9 <main+0x19>
xor    %eax,%eax
call   1080 <usage@plt>
mov    0x8(%rbx),%rdi
mov    0x2e34(%rip),%rax
mov    0x2df5(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
call   1090 <open@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
jne    11db <main+0x4b>
mov    0x8(%rbx),%rsi
lea    0xe2a(%rip),%rdi
call   1030 <err_sys@plt>
mov    0x2dc6(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x20(%rsp)
movq   $0x0,0x18(%rsp)
mov    0x2de8(%rip),%r14
mov    (%r14),%rax
mov    %rax,0x10(%rsp)
movq   $0x0,0x8(%rsp)
lea    0xe53(%rip),%rdi
lea    0x8(%rsp),%rbx
mov    %rbx,%rsi
call   10b0 <dump_lock@plt>
mov    0x2db4(%rip),%r15
mov    (%r15),%esi
mov    %ebp,%edi
mov    %rbx,%rdx
call   1040 <fcntl@plt>
cmp    $0xffffffff,%eax
jne    124c <main+0xbc>
mov    0x2d83(%rip),%rax
mov    (%rax),%rsi
lea    0xdc8(%rip),%rdi
call   1030 <err_sys@plt>
xor    %eax,%eax
call   1060 <getpid@plt>
lea    0xdcc(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1070 <printf@plt>
mov    $0x3,%edi
call   10a0 <sleep@plt>
lea    0xdc2(%rip),%rsi
mov    %ebp,%edi
mov    $0x1,%edx
call   1050 <write@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1060 <getpid@plt>
lea    0xdab(%rip),%rdi
mov    %eax,%esi
mov    %ebx,%edx
xor    %eax,%eax
call   1070 <printf@plt>
mov    0x2d2e(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x20(%rsp)
movq   $0x0,0x18(%rsp)
mov    (%r14),%rax
mov    %rax,0x10(%rsp)
movq   $0x0,0x8(%rsp)
lea    0xd98(%rip),%rdi
lea    0x8(%rsp),%rbx
mov    %rbx,%rsi
call   10b0 <dump_lock@plt>
mov    (%r15),%esi
mov    %ebp,%edi
mov    %rbx,%rdx
call   1040 <fcntl@plt>
cmp    $0xffffffff,%eax
jne    12fe <main+0x16e>
mov    0x2cd1(%rip),%rax
mov    (%rax),%rsi
lea    0xd16(%rip),%rdi
call   1030 <err_sys@plt>
xor    %eax,%eax
call   1060 <getpid@plt>
lea    0xd5c(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1070 <printf@plt>
mov    $0x2,%edi
call   10a0 <sleep@plt>
xor    %eax,%eax
call   1060 <getpid@plt>
lea    0xd4f(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1070 <printf@plt>
xor    %eax,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.flock = type { i64, i8*, i64, i32 }
@O_RDWR = global i32 0
@O_APPEND = global i32 0
@.str = [15 x i8] c"open %s failed\00"
@F_RDLCK = global i32 0
@SEEK_END = global i8* null
@.str.1 = [5 x i8] c"LOCK\00"
@F_SETLK = global i32 0
@.str.2 = [23 x i8] c"fcntl failed, errno %d\00"
@errno = global i8* null
@.str.3 = [16 x i8] c"[%lu] got lock\0A\00"
@.str.4 = [5 x i8] c"helo\00"
@.str.5 = [38 x i8] c"[%lu] increase file by write %d byte\0A\00"
@F_UNLCK = global i32 0
@.str.6 = [7 x i8] c"UNLOCK\00"
@.str.7 = [20 x i8] c"[%lu] release lock\0A\00"
@.str.8 = [10 x i8] c"%lu exit\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca %struct.flock
%8 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = load i32, i32* %4
%10 = icmp ne i32 %9, 2
br i1 %10, label %11, label %13
11: ; preds = %2
%12 = call i32 (...) @usage()
br label %13
13: ; preds = %11, %2
%14 = load i8**, i8*** %5
%15 = getelementptr inbounds i8*, i8** %14, i64 1
%16 = load i8*, i8** %15
%17 = load i32, i32* @O_RDWR
%18 = load i32, i32* @O_APPEND
%19 = or i32 %17, %18
%20 = call i32 @open(i8* %16, i32 %19)
store i32 %20, i32* %6
%21 = load i32, i32* %6
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %28
23: ; preds = %13
%24 = load i8**, i8*** %5
%25 = getelementptr inbounds i8*, i8** %24, i64 1
%26 = load i8*, i8** %25
%27 = call i32 @err_sys(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* %26)
br label %28
28: ; preds = %23, %13
%29 = load i32, i32* @F_RDLCK
%30 = getelementptr inbounds %struct.flock, %struct.flock* %7, i32 0, i32 3
store i32 %29, i32* %30
%31 = getelementptr inbounds %struct.flock, %struct.flock* %7, i32 0, i32 2
store i64 0, i64* %31
%32 = load i8*, i8** @SEEK_END
%33 = getelementptr inbounds %struct.flock, %struct.flock* %7, i32 0, i32 1
store i8* %32, i8** %33
%34 = getelementptr inbounds %struct.flock, %struct.flock* %7, i32 0, i32 0
store i64 0, i64* %34
%35 = call i32 @dump_lock(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), %struct.flock* %7)
%36 = load i32, i32* %6
%37 = load i32, i32* @F_SETLK
%38 = call i32 @fcntl(i32 %36, i32 %37, %struct.flock* %7)
store i32 %38, i32* %8
%39 = load i32, i32* %8
%40 = icmp eq i32 %39, -1
br i1 %40, label %41, label %44
41: ; preds = %28
%42 = load i8*, i8** @errno
%43 = call i32 @err_sys(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i8* %42)
br label %44
44: ; preds = %41, %28
%45 = call i32 (...) @getpid()
%46 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0), i32 %45)
%47 = call i32 @sleep(i32 3)
%48 = load i32, i32* %6
%49 = call i32 @write(i32 %48, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0), i32 1)
store i32 %49, i32* %8
%50 = call i32 (...) @getpid()
%51 = load i32, i32* %8
%52 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.5, i64 0, i64 0), i32 %50, i32 %51)
%53 = load i32, i32* @F_UNLCK
%54 = getelementptr inbounds %struct.flock, %struct.flock* %7, i32 0, i32 3
store i32 %53, i32* %54
%55 = getelementptr inbounds %struct.flock, %struct.flock* %7, i32 0, i32 2
store i64 0, i64* %55
%56 = load i8*, i8** @SEEK_END
%57 = getelementptr inbounds %struct.flock, %struct.flock* %7, i32 0, i32 1
store i8* %56, i8** %57
%58 = getelementptr inbounds %struct.flock, %struct.flock* %7, i32 0, i32 0
store i64 0, i64* %58
%59 = call i32 @dump_lock(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.6, i64 0, i64 0), %struct.flock* %7)
%60 = load i32, i32* %6
%61 = load i32, i32* @F_SETLK
%62 = call i32 @fcntl(i32 %60, i32 %61, %struct.flock* %7)
store i32 %62, i32* %8
%63 = load i32, i32* %8
%64 = icmp eq i32 %63, -1
br i1 %64, label %65, label %68
65: ; preds = %44
%66 = load i8*, i8** @errno
%67 = call i32 @err_sys(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i8* %66)
br label %68
68: ; preds = %65, %44
%69 = call i32 (...) @getpid()
%70 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.7, i64 0, i64 0), i32 %69)
%71 = call i32 @sleep(i32 2)
%72 = call i32 (...) @getpid()
%73 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.8, i64 0, i64 0), i32 %72)
%74 = load i32, i32* %3
ret i32 %74
}

LLVM IR (-O2):
%struct.flock = type { i64, i8*, i64, i32 }
@O_RDWR = global i32 0
@O_APPEND = global i32 0
@.str = [15 x i8] c"open %s failed\00"
@F_RDLCK = global i32 0
@SEEK_END = global i8* null
@.str.1 = [5 x i8] c"LOCK\00"
@F_SETLK = global i32 0
@.str.2 = [23 x i8] c"fcntl failed, errno %d\00"
@errno = global i8* null
@.str.3 = [16 x i8] c"[%lu] got lock\0A\00"
@.str.4 = [5 x i8] c"helo\00"
@.str.5 = [38 x i8] c"[%lu] increase file by write %d byte\0A\00"
@F_UNLCK = global i32 0
@.str.6 = [7 x i8] c"UNLOCK\00"
@.str.7 = [20 x i8] c"[%lu] release lock\0A\00"
@.str.8 = [10 x i8] c"%lu exit\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.flock
%4 = icmp eq i32 %0, 2
br i1 %4, label %7, label %5
5: ; preds = %2
%6 = tail call i32 (...) @usage()
br label %7
7: ; preds = %5, %2
%8 = getelementptr inbounds i8*, i8** %1, i64 1
%9 = load i8*, i8** %8
%10 = load i32, i32* @O_RDWR
%11 = load i32, i32* @O_APPEND
%12 = or i32 %11, %10
%13 = tail call i32 @open(i8* %9, i32 %12)
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %18
15: ; preds = %7
%16 = load i8*, i8** %8
%17 = tail call i32 @err_sys(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* %16)
br label %18
18: ; preds = %15, %7
%19 = bitcast %struct.flock* %3 to i8*
%20 = load i32, i32* @F_RDLCK
%21 = getelementptr inbounds %struct.flock, %struct.flock* %3, i64 0, i32 3
store i32 %20, i32* %21
%22 = getelementptr inbounds %struct.flock, %struct.flock* %3, i64 0, i32 2
store i64 0, i64* %22
%23 = load i8*, i8** @SEEK_END
%24 = getelementptr inbounds %struct.flock, %struct.flock* %3, i64 0, i32 1
store i8* %23, i8** %24
%25 = getelementptr inbounds %struct.flock, %struct.flock* %3, i64 0, i32 0
store i64 0, i64* %25
%26 = call i32 @dump_lock(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), %struct.flock* %3)
%27 = load i32, i32* @F_SETLK
%28 = call i32 @fcntl(i32 %13, i32 %27, %struct.flock* %3)
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %33
30: ; preds = %18
%31 = load i8*, i8** @errno
%32 = call i32 @err_sys(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i8* %31)
br label %33
33: ; preds = %30, %18
%34 = call i32 (...) @getpid()
%35 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0), i32 %34)
%36 = call i32 @sleep(i32 3)
%37 = call i32 @write(i32 %13, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0), i32 1)
%38 = call i32 (...) @getpid()
%39 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.5, i64 0, i64 0), i32 %38, i32 %37)
%40 = load i32, i32* @F_UNLCK
store i32 %40, i32* %21
store i64 0, i64* %22
%41 = load i8*, i8** @SEEK_END
store i8* %41, i8** %24
store i64 0, i64* %25
%42 = call i32 @dump_lock(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.6, i64 0, i64 0), %struct.flock* %3)
%43 = load i32, i32* @F_SETLK
%44 = call i32 @fcntl(i32 %13, i32 %43, %struct.flock* %3)
%45 = icmp eq i32 %44, -1
br i1 %45, label %46, label %49
46: ; preds = %33
%47 = load i8*, i8** @errno
%48 = call i32 @err_sys(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i8* %47)
br label %49
49: ; preds = %46, %33
%50 = call i32 (...) @getpid()
%51 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.7, i64 0, i64 0), i32 %50)
%52 = call i32 @sleep(i32 2)
%53 = call i32 (...) @getpid()
%54 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.8, i64 0, i64 0), i32 %53)
ret i32 0
}