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

/* Variables and functions */
int /*<<< orphan*/ F_SETLK;
int /*<<< orphan*/ F_WRLCK;
scalar_t__ MAP_FAILED;
int /*<<< orphan*/ MAP_PRIVATE;
int /*<<< orphan*/ O_RDWR;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ SEEK_CUR;
int /*<<< orphan*/ SEEK_SET;
int WEXITSTATUS(int);
int /*<<< orphan*/ WIFEXITED(int);
int WTERMSIG(int);
int /*<<< orphan*/ err(int, char *, ...);
int /*<<< orphan*/ errx(int, char *, ...);
int /*<<< orphan*/ exit(int);
scalar_t__ fcntl(int, int /*<<< orphan*/, struct flock *);
int fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ getpagesize();
int lseek(int, int, int /*<<< orphan*/);
scalar_t__ mmap(int /*<<< orphan*/ *, int /*<<< orphan*/, int,
                int /*<<< orphan*/, int, int /*<<< orphan*/);
int open(char const *, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int wait(int *);
int main(int argc, char *argv[]) {
  struct flock fl;
  const char *file = "mmap_fcntl_bug.c";
  int fd, status;
  off_t initial;
  if (!file)
    errx(1, "Usage: %s <normal-file>", argv[0]);
  fd = open(file, O_RDWR);
  if (fd < 0)
    err(1, "Opening %s", file);
  initial = lseek(fd, 123, SEEK_SET);
  if (123 != initial)
    err(1, "initial off_t differs from 123 (TEST FAILED)");
  if (lseek(fd, 0, SEEK_CUR) != 123)
    err(1, "zero offset from initial differs from 123 (TEST FAILED)");
  fl.l_type = F_WRLCK;
  fl.l_whence = SEEK_SET;
  fl.l_start = 0;
  fl.l_len = 1;
  if (fcntl(fd, F_SETLK, &fl) != 0)
    err(1, "Locking %s", file);
  if (mmap(NULL, getpagesize(), PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0) ==
      MAP_FAILED)
    err(1, "mmap of %s", file);
  if (lseek(fd, 0, SEEK_CUR) != 123)
    errx(1,
         "zero offset from initial after mmap differs from 123 (TEST FAILED)");
  switch (fork()) {
  case 0:
    if (fcntl(fd, F_SETLK, &fl) == 0)
      exit(1);
    exit(0);
  case -1:
    err(1, "Fork failed");
  }
  if (wait(&status) == -1)
    err(1, "Child vanished?");
  if (!WIFEXITED(status))
    errx(1, "Child died with signal %i", WTERMSIG(status));
  switch (WEXITSTATUS(status)) {
  case 1:
    errx(1, "Child got lock, we must have dropped it (TEST FAILED)");
  case 0:
    fprintf(stderr, "Child exited with zero (TEST PASSED).\n");
    return 0;
  default:
    errx(1, "Child weird exit status %i", WEXITSTATUS(status));
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xe03(%rip),%rax
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1226 <main+0x46>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
mov    $0x1,%edi
lea    0xdf2(%rip),%rsi
mov    $0x0,%al
call   1050 <errx@plt>
mov    -0x30(%rbp),%rdi
mov    0x2daf(%rip),%rax
mov    (%rax),%esi
call   10b0 <open@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
jge    125c <main+0x7c>
mov    -0x30(%rbp),%rdx
mov    $0x1,%edi
lea    0xdd4(%rip),%rsi
mov    $0x0,%al
call   1080 <err@plt>
mov    -0x34(%rbp),%edi
mov    0x2d52(%rip),%rax
mov    (%rax),%edx
mov    $0x7b,%esi
call   1060 <lseek@plt>
mov    %eax,-0x3c(%rbp)
mov    $0x7b,%eax
cmp    -0x3c(%rbp),%eax
je     1296 <main+0xb6>
mov    $0x1,%edi
lea    0xda5(%rip),%rsi
mov    $0x0,%al
call   1080 <err@plt>
mov    -0x34(%rbp),%edi
mov    0x2d38(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1060 <lseek@plt>
cmp    $0x7b,%eax
je     12c5 <main+0xe5>
mov    $0x1,%edi
lea    0xda3(%rip),%rsi
mov    $0x0,%al
call   1080 <err@plt>
mov    0x2cdc(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    0x2ce0(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
movl   $0x1,-0x28(%rbp)
mov    -0x34(%rbp),%edi
mov    0x2cda(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdx
call   1030 <fcntl@plt>
cmp    $0x0,%rax
je     1322 <main+0x142>
mov    -0x30(%rbp),%rdx
mov    $0x1,%edi
lea    0xd7e(%rip),%rsi
mov    $0x0,%al
call   1080 <err@plt>
mov    $0x0,%al
call   10c0 <getpagesize@plt>
mov    %eax,%esi
mov    0x2cc6(%rip),%rax
mov    (%rax),%edx
mov    0x2c75(%rip),%rax
or     (%rax),%edx
mov    0x2c5c(%rip),%rax
mov    (%rax),%ecx
mov    -0x34(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    0x2c3b(%rip),%rcx
cmp    (%rcx),%rax
jne    137d <main+0x19d>
mov    -0x30(%rbp),%rdx
mov    $0x1,%edi
lea    0xd2e(%rip),%rsi
mov    $0x0,%al
call   1080 <err@plt>
mov    -0x34(%rbp),%edi
mov    0x2c51(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1060 <lseek@plt>
cmp    $0x7b,%eax
je     13ac <main+0x1cc>
mov    $0x1,%edi
lea    0xd0a(%rip),%rsi
mov    $0x0,%al
call   1050 <errx@plt>
xor    %eax,%eax
call   1100 <fork@plt>
mov    %eax,%ecx
mov    %ecx,-0x40(%rbp)
sub    $0xffffffff,%eax
je     1406 <main+0x226>
jmp    13c6 <main+0x1e6>
mov    -0x40(%rbp),%eax
test   %eax,%eax
jne    1419 <main+0x239>
jmp    13d6 <main+0x1f6>
mov    -0x34(%rbp),%edi
mov    0x2bf0(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdx
call   1030 <fcntl@plt>
cmp    $0x0,%rax
jne    13ff <main+0x21f>
mov    $0x1,%edi
call   10e0 <exit@plt>
xor    %edi,%edi
call   10e0 <exit@plt>
mov    $0x1,%edi
lea    0xce0(%rip),%rsi
mov    $0x0,%al
call   1080 <err@plt>
lea    -0x38(%rbp),%rdi
call   10f0 <wait@plt>
cmp    $0xffffffff,%eax
jne    143e <main+0x25e>
mov    $0x1,%edi
lea    0xcc7(%rip),%rsi
mov    $0x0,%al
call   1080 <err@plt>
mov    -0x38(%rbp),%edi
call   10d0 <WIFEXITED@plt>
cmp    $0x0,%eax
jne    146c <main+0x28c>
mov    -0x38(%rbp),%edi
call   1090 <WTERMSIG@plt>
mov    %eax,%edx
mov    $0x1,%edi
lea    0xca9(%rip),%rsi
mov    $0x0,%al
call   1050 <errx@plt>
mov    -0x38(%rbp),%edi
call   10a0 <WEXITSTATUS@plt>
mov    %eax,%ecx
mov    %ecx,-0x44(%rbp)
test   %eax,%eax
je     14aa <main+0x2ca>
jmp    1486 <main+0x2a6>
mov    -0x44(%rbp),%eax
sub    $0x1,%eax
jne    14cb <main+0x2eb>
jmp    1497 <main+0x2b7>
mov    $0x1,%edi
lea    0xc85(%rip),%rsi
mov    $0x0,%al
call   1050 <errx@plt>
mov    0x2b0f(%rip),%rax
mov    (%rax),%edi
lea    0xca4(%rip),%rsi
call   1070 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    14e8 <main+0x308>
mov    -0x38(%rbp),%edi
call   10a0 <WEXITSTATUS@plt>
mov    %eax,%edx
mov    $0x1,%edi
lea    0xca4(%rip),%rsi
mov    $0x0,%al
call   1050 <errx@plt>
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    0x2df0(%rip),%rax
mov    (%rax),%esi
lea    0xe07(%rip),%rdi
call   10b0 <open@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    121e <main+0x3e>
lea    0xe06(%rip),%rsi
lea    0xdee(%rip),%rdx
mov    $0x1,%edi
xor    %eax,%eax
call   1080 <err@plt>
mov    0x2d93(%rip),%r14
mov    (%r14),%edx
mov    %ebx,%edi
mov    $0x7b,%esi
call   1060 <lseek@plt>
cmp    $0x7b,%eax
je     124c <main+0x6c>
lea    0xddc(%rip),%rsi
mov    $0x1,%edi
xor    %eax,%eax
call   1080 <err@plt>
mov    0x2d85(%rip),%r15
mov    (%r15),%edx
mov    %ebx,%edi
xor    %esi,%esi
call   1060 <lseek@plt>
cmp    $0x7b,%eax
je     1277 <main+0x97>
lea    0xdde(%rip),%rsi
mov    $0x1,%edi
xor    %eax,%eax
call   1080 <err@plt>
mov    0x2d2a(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x1c(%rsp)
mov    (%r14),%eax
mov    %eax,0x18(%rsp)
movq   $0x0,0x10(%rsp)
movl   $0x1,0x8(%rsp)
mov    0x2d2d(%rip),%r14
mov    (%r14),%esi
lea    0x8(%rsp),%rdx
mov    %ebx,%edi
call   1030 <fcntl@plt>
test   %rax,%rax
je     12d1 <main+0xf1>
lea    0xdc3(%rip),%rsi
lea    0xd3b(%rip),%rdx
mov    $0x1,%edi
xor    %eax,%eax
call   1080 <err@plt>
xor    %eax,%eax
call   10c0 <getpagesize@plt>
mov    0x2d19(%rip),%rcx
mov    0x2cca(%rip),%rdx
mov    (%rdx),%edx
or     (%rcx),%edx
mov    0x2caf(%rip),%rcx
mov    (%rcx),%ecx
xor    %edi,%edi
mov    %eax,%esi
mov    %ebx,%r8d
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    0x2c8f(%rip),%rcx
cmp    (%rcx),%rax
jne    1328 <main+0x148>
lea    0xd77(%rip),%rsi
lea    0xce4(%rip),%rdx
mov    $0x1,%edi
xor    %eax,%eax
call   1080 <err@plt>
mov    (%r15),%edx
mov    %ebx,%edi
xor    %esi,%esi
call   1060 <lseek@plt>
cmp    $0x7b,%eax
je     134c <main+0x16c>
lea    0xd57(%rip),%rsi
mov    $0x1,%edi
xor    %eax,%eax
call   1050 <errx@plt>
xor    %eax,%eax
call   1100 <fork@plt>
cmp    $0xffffffff,%eax
je     137e <main+0x19e>
test   %eax,%eax
jne    1391 <main+0x1b1>
mov    (%r14),%esi
lea    0x8(%rsp),%rdx
mov    %ebx,%edi
call   1030 <fcntl@plt>
test   %rax,%rax
jne    1444 <main+0x264>
mov    $0x1,%edi
call   10e0 <exit@plt>
lea    0xd55(%rip),%rsi
mov    $0x1,%edi
xor    %eax,%eax
call   1080 <err@plt>
lea    0x4(%rsp),%rdi
call   10f0 <wait@plt>
cmp    $0xffffffff,%eax
jne    13b3 <main+0x1d3>
lea    0xd3f(%rip),%rsi
mov    $0x1,%edi
xor    %eax,%eax
call   1080 <err@plt>
mov    0x4(%rsp),%edi
call   10d0 <WIFEXITED@plt>
test   %eax,%eax
jne    13de <main+0x1fe>
mov    0x4(%rsp),%edi
call   1090 <WTERMSIG@plt>
lea    0xd26(%rip),%rsi
mov    $0x1,%edi
mov    %eax,%edx
xor    %eax,%eax
call   1050 <errx@plt>
mov    0x4(%rsp),%edi
call   10a0 <WEXITSTATUS@plt>
test   %eax,%eax
je     1403 <main+0x223>
cmp    $0x1,%eax
jne    141a <main+0x23a>
lea    0xd19(%rip),%rsi
mov    $0x1,%edi
xor    %eax,%eax
call   1050 <errx@plt>
mov    0x2bb6(%rip),%rax
mov    (%rax),%edi
lea    0xd33(%rip),%rsi
call   1070 <fprintf@plt>
jmp    1438 <main+0x258>
mov    0x4(%rsp),%edi
call   10a0 <WEXITSTATUS@plt>
lea    0xd43(%rip),%rsi
mov    $0x1,%edi
mov    %eax,%edx
xor    %eax,%eax
call   1050 <errx@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
xor    %edi,%edi
call   10e0 <exit@plt>

LLVM IR (-O0):
%struct.flock = type { i32, i64, i32, i32 }
@.str = [17 x i8] c"mmap_fcntl_bug.c\00"
@.str.1 = [24 x i8] c"Usage: %s <normal-file>\00"
@O_RDWR = global i32 0
@.str.2 = [11 x i8] c"Opening %s\00"
@SEEK_SET = global i32 0
@.str.3 = [45 x i8] c"initial off_t differs from 123 (TEST FAILED)\00"
@SEEK_CUR = global i32 0
@.str.4 = [56 x i8] c"zero offset from initial differs from 123 (TEST FAILED)\00"
@F_WRLCK = global i32 0
@F_SETLK = global i32 0
@.str.5 = [11 x i8] c"Locking %s\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_FAILED = global i64 0
@.str.6 = [11 x i8] c"mmap of %s\00"
@.str.7 = [67 x i8] c"zero offset from initial after mmap differs from 123 (TEST FAILED)\00"
@.str.8 = [12 x i8] c"Fork failed\00"
@.str.9 = [16 x i8] c"Child vanished?\00"
@.str.10 = [26 x i8] c"Child died with signal %i\00"
@.str.11 = [54 x i8] c"Child got lock, we must have dropped it (TEST FAILED)\00"
@stderr = global i32 0
@.str.12 = [39 x i8] c"Child exited with zero (TEST PASSED).\0A\00"
@.str.13 = [27 x i8] c"Child weird exit status %i\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca %struct.flock
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8** %7
%11 = load i8*, i8** %7
%12 = icmp ne i8* %11, null
br i1 %12, label %18, label %13
13: ; preds = %2
%14 = load i8**, i8*** %5
%15 = getelementptr inbounds i8*, i8** %14, i64 0
%16 = load i8*, i8** %15
%17 = call i32 (i32, i8*, ...) @errx(i32 1, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i8* %16)
br label %18
18: ; preds = %13, %2
%19 = load i8*, i8** %7
%20 = load i32, i32* @O_RDWR
%21 = call i32 @open(i8* %19, i32 %20)
store i32 %21, i32* %8
%22 = load i32, i32* %8
%23 = icmp slt i32 %22, 0
br i1 %23, label %24, label %27
24: ; preds = %18
%25 = load i8*, i8** %7
%26 = call i32 (i32, i8*, ...) @err(i32 1, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i8* %25)
br label %27
27: ; preds = %24, %18
%28 = load i32, i32* %8
%29 = load i32, i32* @SEEK_SET
%30 = call i32 @lseek(i32 %28, i32 123, i32 %29)
store i32 %30, i32* %10
%31 = load i32, i32* %10
%32 = icmp ne i32 123, %31
br i1 %32, label %33, label %35
33: ; preds = %27
%34 = call i32 (i32, i8*, ...) @err(i32 1, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.3, i64 0, i64 0))
br label %35
35: ; preds = %33, %27
%36 = load i32, i32* %8
%37 = load i32, i32* @SEEK_CUR
%38 = call i32 @lseek(i32 %36, i32 0, i32 %37)
%39 = icmp ne i32 %38, 123
br i1 %39, label %40, label %42
40: ; preds = %35
%41 = call i32 (i32, i8*, ...) @err(i32 1, i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str.4, i64 0, i64 0))
br label %42
42: ; preds = %40, %35
%43 = load i32, i32* @F_WRLCK
%44 = getelementptr inbounds %struct.flock, %struct.flock* %6, i32 0, i32 3
store i32 %43, i32* %44
%45 = load i32, i32* @SEEK_SET
%46 = getelementptr inbounds %struct.flock, %struct.flock* %6, i32 0, i32 2
store i32 %45, i32* %46
%47 = getelementptr inbounds %struct.flock, %struct.flock* %6, i32 0, i32 1
store i64 0, i64* %47
%48 = getelementptr inbounds %struct.flock, %struct.flock* %6, i32 0, i32 0
store i32 1, i32* %48
%49 = load i32, i32* %8
%50 = load i32, i32* @F_SETLK
%51 = call i64 @fcntl(i32 %49, i32 %50, %struct.flock* %6)
%52 = icmp ne i64 %51, 0
br i1 %52, label %53, label %56
53: ; preds = %42
%54 = load i8*, i8** %7
%55 = call i32 (i32, i8*, ...) @err(i32 1, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.5, i64 0, i64 0), i8* %54)
br label %56
56: ; preds = %53, %42
%57 = call i32 (...) @getpagesize()
%58 = load i32, i32* @PROT_READ
%59 = load i32, i32* @PROT_WRITE
%60 = or i32 %58, %59
%61 = load i32, i32* @MAP_PRIVATE
%62 = load i32, i32* %8
%63 = call i64 @mmap(i32* null, i32 %57, i32 %60, i32 %61, i32 %62, i32 0)
%64 = load i64, i64* @MAP_FAILED
%65 = icmp eq i64 %63, %64
br i1 %65, label %66, label %69
66: ; preds = %56
%67 = load i8*, i8** %7
%68 = call i32 (i32, i8*, ...) @err(i32 1, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.6, i64 0, i64 0), i8* %67)
br label %69
69: ; preds = %66, %56
%70 = load i32, i32* %8
%71 = load i32, i32* @SEEK_CUR
%72 = call i32 @lseek(i32 %70, i32 0, i32 %71)
%73 = icmp ne i32 %72, 123
br i1 %73, label %74, label %76
74: ; preds = %69
%75 = call i32 (i32, i8*, ...) @errx(i32 1, i8* getelementptr inbounds ([67 x i8], [67 x i8]* @.str.7, i64 0, i64 0))
br label %76
76: ; preds = %74, %69
%77 = call i32 (...) @fork()
switch i32 %77, label %89 [
i32 0, label %78
i32 -1, label %87
]
78: ; preds = %76
%79 = load i32, i32* %8
%80 = load i32, i32* @F_SETLK
%81 = call i64 @fcntl(i32 %79, i32 %80, %struct.flock* %6)
%82 = icmp eq i64 %81, 0
br i1 %82, label %83, label %85
83: ; preds = %78
%84 = call i32 @exit(i32 1)
unreachable
85: ; preds = %78
%86 = call i32 @exit(i32 0)
unreachable
87: ; preds = %76
%88 = call i32 (i32, i8*, ...) @err(i32 1, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.8, i64 0, i64 0))
br label %89
89: ; preds = %87, %76
%90 = call i32 @wait(i32* %9)
%91 = icmp eq i32 %90, -1
br i1 %91, label %92, label %94
92: ; preds = %89
%93 = call i32 (i32, i8*, ...) @err(i32 1, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.9, i64 0, i64 0))
br label %94
94: ; preds = %92, %89
%95 = load i32, i32* %9
%96 = call i32 @WIFEXITED(i32 %95)
%97 = icmp ne i32 %96, 0
br i1 %97, label %102, label %98
98: ; preds = %94
%99 = load i32, i32* %9
%100 = call i32 @WTERMSIG(i32 %99)
%101 = call i32 (i32, i8*, ...) @errx(i32 1, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.10, i64 0, i64 0), i32 %100)
br label %102
102: ; preds = %98, %94
%103 = load i32, i32* %9
%104 = call i32 @WEXITSTATUS(i32 %103)
switch i32 %104, label %110 [
i32 1, label %105
i32 0, label %107
]
105: ; preds = %102
%106 = call i32 (i32, i8*, ...) @errx(i32 1, i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str.11, i64 0, i64 0))
br label %107
107: ; preds = %102, %105
%108 = load i32, i32* @stderr
%109 = call i32 @fprintf(i32 %108, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.12, i64 0, i64 0))
store i32 0, i32* %3
br label %114
110: ; preds = %102
%111 = load i32, i32* %9
%112 = call i32 @WEXITSTATUS(i32 %111)
%113 = call i32 (i32, i8*, ...) @errx(i32 1, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.13, i64 0, i64 0), i32 %112)
br label %114
114: ; preds = %107, %110
%115 = load i32, i32* %3
ret i32 %115
}

LLVM IR (-O2):
%struct.flock = type { i32, i64, i32, i32 }
@.str = [17 x i8] c"mmap_fcntl_bug.c\00"
@O_RDWR = global i32 0
@.str.2 = [11 x i8] c"Opening %s\00"
@SEEK_SET = global i32 0
@.str.3 = [45 x i8] c"initial off_t differs from 123 (TEST FAILED)\00"
@SEEK_CUR = global i32 0
@.str.4 = [56 x i8] c"zero offset from initial differs from 123 (TEST FAILED)\00"
@F_WRLCK = global i32 0
@F_SETLK = global i32 0
@.str.5 = [11 x i8] c"Locking %s\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_FAILED = global i64 0
@.str.6 = [11 x i8] c"mmap of %s\00"
@.str.7 = [67 x i8] c"zero offset from initial after mmap differs from 123 (TEST FAILED)\00"
@.str.8 = [12 x i8] c"Fork failed\00"
@.str.9 = [16 x i8] c"Child vanished?\00"
@.str.10 = [26 x i8] c"Child died with signal %i\00"
@.str.11 = [54 x i8] c"Child got lock, we must have dropped it (TEST FAILED)\00"
@stderr = global i32 0
@.str.12 = [39 x i8] c"Child exited with zero (TEST PASSED).\0A\00"
@.str.13 = [27 x i8] c"Child weird exit status %i\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca %struct.flock
%4 = alloca i32
%5 = bitcast %struct.flock* %3 to i8*
%6 = bitcast i32* %4 to i8*
%7 = load i32, i32* @O_RDWR
%8 = tail call i32 @open(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %7)
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %2
%11 = tail call i32 (i32, i8*, ...) @err(i32 1, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
br label %12
12: ; preds = %10, %2
%13 = load i32, i32* @SEEK_SET
%14 = tail call i32 @lseek(i32 %8, i32 123, i32 %13)
%15 = icmp eq i32 %14, 123
br i1 %15, label %18, label %16
16: ; preds = %12
%17 = tail call i32 (i32, i8*, ...) @err(i32 1, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.3, i64 0, i64 0))
br label %18
18: ; preds = %16, %12
%19 = load i32, i32* @SEEK_CUR
%20 = tail call i32 @lseek(i32 %8, i32 0, i32 %19)
%21 = icmp eq i32 %20, 123
br i1 %21, label %24, label %22
22: ; preds = %18
%23 = tail call i32 (i32, i8*, ...) @err(i32 1, i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str.4, i64 0, i64 0))
br label %24
24: ; preds = %22, %18
%25 = load i32, i32* @F_WRLCK
%26 = getelementptr inbounds %struct.flock, %struct.flock* %3, i64 0, i32 3
store i32 %25, i32* %26
%27 = load i32, i32* @SEEK_SET
%28 = getelementptr inbounds %struct.flock, %struct.flock* %3, i64 0, i32 2
store i32 %27, i32* %28
%29 = getelementptr inbounds %struct.flock, %struct.flock* %3, i64 0, i32 1
store i64 0, i64* %29
%30 = getelementptr inbounds %struct.flock, %struct.flock* %3, i64 0, i32 0
store i32 1, i32* %30
%31 = load i32, i32* @F_SETLK
%32 = call i64 @fcntl(i32 %8, i32 %31, %struct.flock* %3)
%33 = icmp eq i64 %32, 0
br i1 %33, label %36, label %34
34: ; preds = %24
%35 = call i32 (i32, i8*, ...) @err(i32 1, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
br label %36
36: ; preds = %34, %24
%37 = call i32 (...) @getpagesize()
%38 = load i32, i32* @PROT_READ
%39 = load i32, i32* @PROT_WRITE
%40 = or i32 %39, %38
%41 = load i32, i32* @MAP_PRIVATE
%42 = call i64 @mmap(i32* null, i32 %37, i32 %40, i32 %41, i32 %8, i32 0)
%43 = load i64, i64* @MAP_FAILED
%44 = icmp eq i64 %42, %43
br i1 %44, label %45, label %47
45: ; preds = %36
%46 = call i32 (i32, i8*, ...) @err(i32 1, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.6, i64 0, i64 0), i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
br label %47
47: ; preds = %45, %36
%48 = load i32, i32* @SEEK_CUR
%49 = call i32 @lseek(i32 %8, i32 0, i32 %48)
%50 = icmp eq i32 %49, 123
br i1 %50, label %53, label %51
51: ; preds = %47
%52 = call i32 (i32, i8*, ...) @errx(i32 1, i8* getelementptr inbounds ([67 x i8], [67 x i8]* @.str.7, i64 0, i64 0))
br label %53
53: ; preds = %51, %47
%54 = call i32 (...) @fork()
switch i32 %54, label %65 [
i32 0, label %55
i32 -1, label %63
]
55: ; preds = %53
%56 = load i32, i32* @F_SETLK
%57 = call i64 @fcntl(i32 %8, i32 %56, %struct.flock* %3)
%58 = icmp eq i64 %57, 0
br i1 %58, label %59, label %61
59: ; preds = %55
%60 = call i32 @exit(i32 1)
unreachable
61: ; preds = %55
%62 = call i32 @exit(i32 0)
unreachable
63: ; preds = %53
%64 = call i32 (i32, i8*, ...) @err(i32 1, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.8, i64 0, i64 0))
br label %65
65: ; preds = %63, %53
%66 = call i32 @wait(i32* %4)
%67 = icmp eq i32 %66, -1
br i1 %67, label %68, label %70
68: ; preds = %65
%69 = call i32 (i32, i8*, ...) @err(i32 1, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.9, i64 0, i64 0))
br label %70
70: ; preds = %68, %65
%71 = load i32, i32* %4
%72 = call i32 @WIFEXITED(i32 %71)
%73 = icmp eq i32 %72, 0
br i1 %73, label %74, label %78
74: ; preds = %70
%75 = load i32, i32* %4
%76 = call i32 @WTERMSIG(i32 %75)
%77 = call i32 (i32, i8*, ...) @errx(i32 1, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.10, i64 0, i64 0), i32 %76)
br label %78
78: ; preds = %74, %70
%79 = load i32, i32* %4
%80 = call i32 @WEXITSTATUS(i32 %79)
switch i32 %80, label %86 [
i32 1, label %81
i32 0, label %83
]
81: ; preds = %78
%82 = call i32 (i32, i8*, ...) @errx(i32 1, i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str.11, i64 0, i64 0))
br label %83
83: ; preds = %78, %81
%84 = load i32, i32* @stderr
%85 = call i32 @fprintf(i32 %84, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.12, i64 0, i64 0))
br label %90
86: ; preds = %78
%87 = load i32, i32* %4
%88 = call i32 @WEXITSTATUS(i32 %87)
%89 = call i32 (i32, i8*, ...) @errx(i32 1, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.13, i64 0, i64 0), i32 %88)
br label %90
90: ; preds = %86, %83
ret i32 0
}