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


x86 assembly (clang, -O0):
<waitreadtowrite>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
mov    0x2e0b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    0x2e07(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0x18(%rbp),%eax
mov    %eax,-0x24(%rbp)
movl   $0x0,-0x20(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
call   1070 <ftell@plt>
mov    %rax,-0x40(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,-0x28(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2dde(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
mov    %eax,-0x34(%rbp)
cmp    $0xffffffff,%eax
jne    1233 <waitreadtowrite+0x93>
lea    0xddc(%rip),%rdi
call   10a0 <perror@plt>
mov    $0xfffffff8,%edi
call   10b0 <exit@plt>
jmp    1238 <waitreadtowrite+0x98>
mov    -0x34(%rbp),%edi
mov    0x2d9e(%rip),%rax
mov    (%rax),%esi
lea    -0x30(%rbp),%rdx
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
jne    1265 <waitreadtowrite+0xc5>
mov    $0x3e8,%edi
call   10c0 <usleep@plt>
jmp    1238 <waitreadtowrite+0x98>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rdx
lea    0xda2(%rip),%rsi
call   1050 <freopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    129e <waitreadtowrite+0xfe>
lea    0xd8a(%rip),%rdi
call   10a0 <perror@plt>
mov    $0xfffffff9,%edi
call   10b0 <exit@plt>
mov    -0x10(%rbp),%rdi
mov    -0x40(%rbp),%rsi
mov    0x2d23(%rip),%rax
mov    (%rax),%edx
call   1080 <fseek@plt>
mov    -0x34(%rbp),%edi
call   1060 <close@plt>
mov    -0x10(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<waitreadtowrite>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%r15
mov    %rdi,%r12
mov    0x2e0d(%rip),%rax
mov    (%rax),%eax
mov    %eax,(%rsp)
mov    0x2e09(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x4(%rsp)
mov    %ecx,0xc(%rsp)
movq   $0x0,0x10(%rsp)
mov    %rsi,%rdi
call   1070 <ftell@plt>
mov    %rax,%r14
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %eax,0x8(%rsp)
mov    0x2df1(%rip),%rax
mov    (%rax),%esi
mov    %r12,%rdi
call   1090 <open@plt>
cmp    $0xffffffff,%eax
je     1294 <waitreadtowrite+0xf4>
mov    %eax,%ebp
mov    0x2dcd(%rip),%r13
mov    0x0(%r13),%esi
mov    %rsp,%rdx
mov    %eax,%edi
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
jne    124d <waitreadtowrite+0xad>
mov    %rsp,%rbx
nopl   0x0(%rax)
mov    $0x3e8,%edi
call   10c0 <usleep@plt>
mov    0x0(%r13),%esi
mov    %ebp,%edi
mov    %rbx,%rdx
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
je     1230 <waitreadtowrite+0x90>
lea    0xdc2(%rip),%rsi
mov    %r12,%rdi
mov    %r15,%rdx
call   1050 <freopen@plt>
test   %rax,%rax
je     12aa <waitreadtowrite+0x10a>
mov    %rax,%rbx
mov    0x2d62(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
mov    %r14,%rsi
call   1080 <fseek@plt>
mov    %ebp,%edi
call   1060 <close@plt>
mov    %rbx,%rax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd65(%rip),%rdi
call   10a0 <perror@plt>
mov    $0xfffffff8,%edi
call   10b0 <exit@plt>
lea    0xd68(%rip),%rdi
call   10a0 <perror@plt>
mov    $0xfffffff9,%edi
call   10b0 <exit@plt>

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

LLVM IR (-O2):
%struct.flock = type { i32, i32, i32, i32, i32, i32 }
@F_WRLCK = global i32 0
@SEEK_SET = global i32 0
@O_RDWR = global i32 0
@.str = [22 x i8] c"waitreadtowrite: 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* @waitreadtowrite(i8* %0, i32* %1, i32 %2, i32 %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
store i32 %8, i32* %7
%9 = getelementptr inbounds %struct.flock, %struct.flock* %5, i64 0, i32 1
%10 = load i32, i32* @SEEK_SET
store i32 %10, i32* %9
%11 = getelementptr inbounds %struct.flock, %struct.flock* %5, i64 0, i32 2
%12 = getelementptr inbounds %struct.flock, %struct.flock* %5, i64 0, i32 3
store i32 %3, i32* %12
%13 = getelementptr inbounds %struct.flock, %struct.flock* %5, i64 0, i32 4
store i32 0, i32* %13
%14 = getelementptr inbounds %struct.flock, %struct.flock* %5, i64 0, i32 5
store i32 0, i32* %14
%15 = tail call i64 @ftell(i32* %1)
%16 = tail call i32 (...) @getpid()
store i32 %16, i32* %11
%17 = load i32, i32* @O_RDWR
%18 = tail call i32 @open(i8* %0, i32 %17)
%19 = icmp eq i32 %18, -1
br i1 %19, label %24, label %20
20: ; preds = %4
%21 = load i32, i32* @F_SETLKW
%22 = call i32 @fcntl(i32 %18, i32 %21, %struct.flock* %5)
%23 = icmp eq i32 %22, -1
br i1 %23, label %27, label %32
24: ; preds = %4
%25 = tail call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
%26 = tail call i32 @exit(i32 -8)
unreachable
27: ; preds = %20, %27
%28 = call i32 @usleep(i32 1000)
%29 = load i32, i32* @F_SETLKW
%30 = call i32 @fcntl(i32 %18, i32 %29, %struct.flock* %5)
%31 = icmp eq i32 %30, -1
br i1 %31, label %27, label %32
32: ; preds = %27, %20
%33 = call i32* @freopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %1)
%34 = icmp eq i32* %33, null
br i1 %34, label %35, label %38
35: ; preds = %32
%36 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0))
%37 = call i32 @exit(i32 -7)
unreachable
38: ; preds = %32
%39 = load i32, i32* @SEEK_SET
%40 = call i32 @fseek(i32* %33, i64 %15, i32 %39)
%41 = call i32 @close(i32 %18)
ret i32* %33
}