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

/* Variables and functions */
int /*<<< orphan*/ F_SETLKW;
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*/ *fopen(char *, char *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, unsigned long,
                         int /*<<< orphan*/);
int /*<<< orphan*/ getpid();
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ usleep(int);
FILE *waitwritemaster(char *masterfile, unsigned long loc, unsigned long start,
                      unsigned long len) {
  FILE *master;
  struct flock fl = {F_WRLCK, SEEK_SET, start, len, 0};
  int fd;
  fl.l_pid = getpid();
  if ((fd = open(masterfile, O_RDWR)) == -1) {
    perror("waitwritemaster: open");
    exit(-8);
  }
  while (fcntl(fd, F_SETLKW, &fl) == -1)
    usleep(1000);
  master = fopen(masterfile, "r+");
  if (!master) {
    perror("waitwritemaster: fopen");
    exit(-7);
  }
  fseek(master, loc, SEEK_SET);
  close(fd);
  return master;
}


x86 assembly (clang, -O0):
<waitwritemaster>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    0x2e19(%rip),%rax
movslq (%rax),%rax
mov    %rax,-0x48(%rbp)
mov    0x2e13(%rip),%rax
movslq (%rax),%rax
mov    %rax,-0x40(%rbp)
mov    -0x18(%rbp),%rax
mov    %eax,-0x38(%rbp)
mov    -0x20(%rbp),%rax
mov    %eax,-0x34(%rbp)
movl   $0x0,-0x30(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,-0x38(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2df3(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x4c(%rbp)
cmp    $0xffffffff,%eax
jne    121e <waitwritemaster+0x8e>
lea    0xdf1(%rip),%rdi
call   1090 <perror@plt>
mov    $0xfffffff8,%edi
call   10a0 <exit@plt>
jmp    1223 <waitwritemaster+0x93>
mov    -0x4c(%rbp),%edi
mov    0x2db3(%rip),%rax
mov    (%rax),%esi
lea    -0x48(%rbp),%rdx
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
jne    1250 <waitwritemaster+0xc0>
mov    $0x3e8,%edi
call   10b0 <usleep@plt>
jmp    1223 <waitwritemaster+0x93>
mov    -0x8(%rbp),%rdi
lea    0xdbb(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1285 <waitwritemaster+0xf5>
lea    0xda3(%rip),%rdi
call   1090 <perror@plt>
mov    $0xfffffff9,%edi
call   10a0 <exit@plt>
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    0x2d3c(%rip),%rax
mov    (%rax),%edx
call   1060 <fseek@plt>
mov    -0x4c(%rbp),%edi
call   1050 <close@plt>
mov    -0x28(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<waitwritemaster>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rsi,%r14
mov    %rdi,%r15
mov    0x2e1d(%rip),%rax
movslq (%rax),%rax
mov    %rax,0x8(%rsp)
mov    0x2e16(%rip),%r12
movslq (%r12),%rax
mov    %rax,0x10(%rsp)
mov    %ecx,0x1c(%rsp)
movq   $0x0,0x20(%rsp)
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %eax,0x18(%rsp)
mov    0x2e06(%rip),%rax
mov    (%rax),%esi
mov    %r15,%rdi
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     127c <waitwritemaster+0xec>
mov    %eax,%ebp
mov    0x2de2(%rip),%r13
mov    0x0(%r13),%esi
lea    0x8(%rsp),%rdx
mov    %eax,%edi
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
jne    123d <waitwritemaster+0xad>
lea    0x8(%rsp),%rbx
nopl   0x0(%rax,%rax,1)
mov    $0x3e8,%edi
call   10b0 <usleep@plt>
mov    0x0(%r13),%esi
mov    %ebp,%edi
mov    %rbx,%rdx
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
je     1220 <waitwritemaster+0x90>
lea    0xdd2(%rip),%rsi
mov    %r15,%rdi
call   1080 <fopen@plt>
test   %rax,%rax
je     1292 <waitwritemaster+0x102>
mov    %rax,%rbx
mov    (%r12),%edx
mov    %rax,%rdi
mov    %r14,%rsi
call   1060 <fseek@plt>
mov    %ebp,%edi
call   1050 <close@plt>
mov    %rbx,%rax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd7d(%rip),%rdi
call   1090 <perror@plt>
mov    $0xfffffff8,%edi
call   10a0 <exit@plt>
lea    0xd80(%rip),%rdi
call   1090 <perror@plt>
mov    $0xfffffff9,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
%struct.flock = type { i64, i64, i32, i32, i32, i32 }
@F_WRLCK = global i32 0
@SEEK_SET = global i32 0
@O_RDWR = global i32 0
@.str = [22 x i8] c"waitwritemaster: open\00"
@F_SETLKW = global i32 0
@.str.1 = [3 x i8] c"r+\00"
@.str.2 = [23 x i8] c"waitwritemaster: fopen\00"
define i32* @waitwritemaster(i8* %0, i64 %1, i64 %2, i64 %3) {
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i64
%8 = alloca i64
%9 = alloca i32*
%10 = alloca %struct.flock
%11 = alloca i32
store i8* %0, i8** %5
store i64 %1, i64* %6
store i64 %2, i64* %7
store i64 %3, i64* %8
%12 = getelementptr inbounds %struct.flock, %struct.flock* %10, i32 0, i32 0
%13 = load i32, i32* @F_WRLCK
%14 = sext i32 %13 to i64
store i64 %14, i64* %12
%15 = getelementptr inbounds %struct.flock, %struct.flock* %10, i32 0, i32 1
%16 = load i32, i32* @SEEK_SET
%17 = sext i32 %16 to i64
store i64 %17, i64* %15
%18 = getelementptr inbounds %struct.flock, %struct.flock* %10, i32 0, i32 2
%19 = load i64, i64* %7
%20 = trunc i64 %19 to i32
store i32 %20, i32* %18
%21 = getelementptr inbounds %struct.flock, %struct.flock* %10, i32 0, i32 3
%22 = load i64, i64* %8
%23 = trunc i64 %22 to i32
store i32 %23, i32* %21
%24 = getelementptr inbounds %struct.flock, %struct.flock* %10, i32 0, i32 4
store i32 0, i32* %24
%25 = getelementptr inbounds %struct.flock, %struct.flock* %10, i32 0, i32 5
store i32 0, i32* %25
%26 = call i32 (...) @getpid()
%27 = getelementptr inbounds %struct.flock, %struct.flock* %10, i32 0, i32 2
store i32 %26, i32* %27
%28 = load i8*, i8** %5
%29 = load i32, i32* @O_RDWR
%30 = call i32 @open(i8* %28, i32 %29)
store i32 %30, i32* %11
%31 = icmp eq i32 %30, -1
br i1 %31, label %32, label %35
32: ; preds = %4
%33 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
%34 = call i32 @exit(i32 -8)
unreachable
35: ; preds = %4
br label %36
36: ; preds = %41, %35
%37 = load i32, i32* %11
%38 = load i32, i32* @F_SETLKW
%39 = call i32 @fcntl(i32 %37, i32 %38, %struct.flock* %10)
%40 = icmp eq i32 %39, -1
br i1 %40, label %41, label %43
41: ; preds = %36
%42 = call i32 @usleep(i32 1000)
br label %36
43: ; preds = %36
%44 = load i8*, i8** %5
%45 = call i32* @fopen(i8* %44, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %45, i32** %9
%46 = load i32*, i32** %9
%47 = icmp ne i32* %46, null
br i1 %47, label %51, label %48
48: ; preds = %43
%49 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0))
%50 = call i32 @exit(i32 -7)
unreachable
51: ; preds = %43
%52 = load i32*, i32** %9
%53 = load i64, i64* %6
%54 = load i32, i32* @SEEK_SET
%55 = call i32 @fseek(i32* %52, i64 %53, i32 %54)
%56 = load i32, i32* %11
%57 = call i32 @close(i32 %56)
%58 = load i32*, i32** %9
ret i32* %58
}

LLVM IR (-O2):
%struct.flock = type { i64, i64, i32, i32, i32, i32 }
@F_WRLCK = global i32 0
@SEEK_SET = global i32 0
@O_RDWR = global i32 0
@.str = [22 x i8] c"waitwritemaster: open\00"
@F_SETLKW = global i32 0
@.str.1 = [3 x i8] c"r+\00"
@.str.2 = [23 x i8] c"waitwritemaster: fopen\00"
define noalias i32* @waitwritemaster(i8* nocapture %0, i64 %1, i64 %2, i64 %3) {
%5 = alloca %struct.flock
%6 = bitcast %struct.flock* %5 to i8*
%7 = getelementptr inbounds %struct.flock, %struct.flock* %5, i64 0, i32 0
%8 = load i32, i32* @F_WRLCK
%9 = sext i32 %8 to i64
store i64 %9, i64* %7
%10 = getelementptr inbounds %struct.flock, %struct.flock* %5, i64 0, i32 1
%11 = load i32, i32* @SEEK_SET
%12 = sext i32 %11 to i64
store i64 %12, i64* %10
%13 = getelementptr inbounds %struct.flock, %struct.flock* %5, i64 0, i32 2
%14 = getelementptr inbounds %struct.flock, %struct.flock* %5, i64 0, i32 3
%15 = trunc i64 %3 to i32
store i32 %15, i32* %14
%16 = getelementptr inbounds %struct.flock, %struct.flock* %5, i64 0, i32 4
store i32 0, i32* %16
%17 = getelementptr inbounds %struct.flock, %struct.flock* %5, i64 0, i32 5
store i32 0, i32* %17
%18 = tail call i32 (...) @getpid()
store i32 %18, i32* %13
%19 = load i32, i32* @O_RDWR
%20 = tail call i32 @open(i8* %0, i32 %19)
%21 = icmp eq i32 %20, -1
br i1 %21, label %26, label %22
22: ; preds = %4
%23 = load i32, i32* @F_SETLKW
%24 = call i32 @fcntl(i32 %20, i32 %23, %struct.flock* %5)
%25 = icmp eq i32 %24, -1
br i1 %25, label %29, label %34
26: ; preds = %4
%27 = tail call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
%28 = tail call i32 @exit(i32 -8)
unreachable
29: ; preds = %22, %29
%30 = call i32 @usleep(i32 1000)
%31 = load i32, i32* @F_SETLKW
%32 = call i32 @fcntl(i32 %20, i32 %31, %struct.flock* %5)
%33 = icmp eq i32 %32, -1
br i1 %33, label %29, label %34
34: ; preds = %29, %22
%35 = call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%36 = icmp eq i32* %35, null
br i1 %36, label %37, label %40
37: ; preds = %34
%38 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0))
%39 = call i32 @exit(i32 -7)
unreachable
40: ; preds = %34
%41 = load i32, i32* @SEEK_SET
%42 = call i32 @fseek(i32* %35, i64 %1, i32 %41)
%43 = call i32 @close(i32 %20)
ret i32* %35
}