#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_SETLK;
int /*<<< orphan*/ F_UNLCK;
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int fcntl(int, int /*<<< orphan*/, struct flock *);
int /*<<< orphan*/ getpid();
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
void freemaster(char *masterfile, FILE *master, unsigned long start,
                unsigned long len) {
  struct flock fl = {F_UNLCK, SEEK_SET, start, len, 0};
  int fd;
  fl.l_pid = getpid();
  if ((fd = open(masterfile, O_RDWR)) == -1) {
    perror("freemaster: open");
    exit(-8);
  }
  if (fcntl(fd, F_SETLK, &fl) == -1) {
    perror("freemaster: fcntl");
    exit(1);
  }
  fclose(master);
  close(fd);
}


x86 assembly (clang, -O0):
<freemaster>:
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    0x2e49(%rip),%rax
movslq (%rax),%rax
mov    %rax,-0x40(%rbp)
mov    0x2e2b(%rip),%rax
movslq (%rax),%rax
mov    %rax,-0x38(%rbp)
mov    -0x18(%rbp),%rax
mov    %eax,-0x30(%rbp)
mov    -0x20(%rbp),%rax
mov    %eax,-0x2c(%rbp)
movl   $0x0,-0x28(%rbp)
movl   $0x0,-0x24(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,-0x30(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2e13(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x44(%rbp)
cmp    $0xffffffff,%eax
jne    11fe <freemaster+0x8e>
lea    0xe11(%rip),%rdi
call   1080 <perror@plt>
mov    $0xfffffff8,%edi
call   1090 <exit@plt>
mov    -0x44(%rbp),%edi
mov    0x2dd8(%rip),%rax
mov    (%rax),%esi
lea    -0x40(%rbp),%rdx
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
jne    1232 <freemaster+0xc2>
lea    0xdee(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x10(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x44(%rbp),%edi
call   1060 <close@plt>
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<freemaster>:
push   %rbp
push   %rbx
sub    $0x28,%rsp
mov    %rsi,%rbx
mov    %rdi,%rbp
mov    0x2e55(%rip),%rax
movslq (%rax),%rax
mov    %rax,0x8(%rsp)
mov    0x2e36(%rip),%rax
movslq (%rax),%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    0x2e2f(%rip),%rax
mov    (%rax),%esi
mov    %rbp,%rdi
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     11fa <freemaster+0x8a>
mov    %eax,%ebp
mov    0x2e0f(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
mov    %ebp,%edi
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
je     1210 <freemaster+0xa0>
mov    %rbx,%rdi
call   1050 <fclose@plt>
mov    %ebp,%edi
call   1060 <close@plt>
add    $0x28,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xdff(%rip),%rdi
call   1080 <perror@plt>
mov    $0xfffffff8,%edi
call   1090 <exit@plt>
lea    0xdfa(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

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

LLVM IR (-O2):
%struct.flock = type { i64, i64, i32, i32, i32, i32 }
@F_UNLCK = global i32 0
@SEEK_SET = global i32 0
@O_RDWR = global i32 0
@.str = [17 x i8] c"freemaster: open\00"
@F_SETLK = global i32 0
@.str.1 = [18 x i8] c"freemaster: fcntl\00"
define void @freemaster(i8* nocapture %0, i32* nocapture %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_UNLCK
%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 %22, label %25
22: ; preds = %4
%23 = tail call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
%24 = tail call i32 @exit(i32 -8)
unreachable
25: ; preds = %4
%26 = load i32, i32* @F_SETLK
%27 = call i32 @fcntl(i32 %20, i32 %26, %struct.flock* %5)
%28 = icmp eq i32 %27, -1
br i1 %28, label %29, label %32
29: ; preds = %25
%30 = call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
%31 = call i32 @exit(i32 1)
unreachable
32: ; preds = %25
%33 = call i32 @fclose(i32* %1)
%34 = call i32 @close(i32 %20)
ret void
}