#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 */
typedef int pid_t;
typedef int /*<<< orphan*/ filename;

/* Variables and functions */
int /*<<< orphan*/ assert(int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int getpid();
int open_lock_and_map(char *, char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ unlink(char *);
int wait4(int, int *, int /*<<< orphan*/, int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  int fd1;
  int fd2;
  int exitcode = 1;
  char filename[256];
  snprintf(filename, sizeof(filename), "/tmp/valgrind-file-locking-test.%d",
           getpid());
  unlink(filename);
  if ((fd1 = open_lock_and_map("parent", filename)) >= 0) {
    pid_t fork_result;
    fork_result = fork();
    switch (fork_result) {
    case -1:
      perror("fork");
      break;
    case 0:
      fd2 = open_lock_and_map("child", filename);
      if (fd2 >= 0) {
        close(fd2);
      }
      exit(0);
      break;
    default: {
      int child_status;
      int wait_result;
      wait_result = wait4(fork_result, &child_status, 0, 0);
      assert(wait_result >= 0);
    }
    }
  }
  close(fd1);
  unlink(filename);
  fprintf(stderr, "Test finished.\n");
  return exitcode;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x140,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x1,-0x1c(%rbp)
lea    -0x120(%rbp),%rax
mov    %rax,-0x138(%rbp)
mov    $0x0,%al
call   1050 <getpid@plt>
mov    -0x138(%rbp),%rdi
mov    %eax,%ecx
mov    $0x100,%esi
lea    0xe06(%rip),%rdx
call   1060 <snprintf@plt>
lea    -0x120(%rbp),%rdi
call   1030 <unlink@plt>
lea    -0x120(%rbp),%rsi
lea    0xe0a(%rip),%rdi
call   1040 <open_lock_and_map@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jl     12d9 <main+0x129>
xor    %eax,%eax
call   10d0 <fork@plt>
mov    %eax,-0x124(%rbp)
mov    -0x124(%rbp),%eax
mov    %eax,-0x13c(%rbp)
sub    $0xffffffff,%eax
je     1264 <main+0xb4>
jmp    1251 <main+0xa1>
mov    -0x13c(%rbp),%eax
test   %eax,%eax
je     1275 <main+0xc5>
jmp    12a4 <main+0xf4>
lea    0xdbf(%rip),%rdi
call   10b0 <perror@plt>
jmp    12d4 <main+0x124>
lea    -0x120(%rbp),%rsi
lea    0xdac(%rip),%rdi
call   1040 <open_lock_and_map@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jl     129d <main+0xed>
mov    -0x18(%rbp),%edi
call   1070 <close@plt>
xor    %edi,%edi
call   10c0 <exit@plt>
mov    -0x124(%rbp),%edi
lea    -0x128(%rbp),%rsi
xor    %ecx,%ecx
mov    %ecx,%edx
call   10a0 <wait4@plt>
mov    %eax,-0x12c(%rbp)
cmpl   $0x0,-0x12c(%rbp)
setge  %al
and    $0x1,%al
movzbl %al,%edi
call   1080 <assert@plt>
jmp    12d9 <main+0x129>
mov    -0x14(%rbp),%edi
call   1070 <close@plt>
lea    -0x120(%rbp),%rdi
call   1030 <unlink@plt>
mov    0x2cec(%rip),%rax
mov    (%rax),%edi
lea    0xd38(%rip),%rsi
call   1090 <fprintf@plt>
mov    -0x1c(%rbp),%eax
add    $0x140,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x110,%rsp
xor    %eax,%eax
call   1050 <getpid@plt>
lea    0xe3a(%rip),%rdx
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x100,%esi
mov    %eax,%ecx
call   1060 <snprintf@plt>
mov    %rbx,%rdi
call   1030 <unlink@plt>
lea    0xe3a(%rip),%rdi
mov    %rbx,%rsi
call   1040 <open_lock_and_map@plt>
mov    %eax,%ebx
test   %eax,%eax
js     1223 <main+0x73>
xor    %eax,%eax
call   10d0 <fork@plt>
cmp    $0xffffffff,%eax
je     1257 <main+0xa7>
test   %eax,%eax
je     1265 <main+0xb5>
lea    0xc(%rsp),%rsi
mov    %eax,%edi
xor    %edx,%edx
xor    %ecx,%ecx
call   10a0 <wait4@plt>
not    %eax
shr    $0x1f,%eax
mov    %eax,%edi
call   1080 <assert@plt>
mov    %ebx,%edi
call   1070 <close@plt>
lea    0x10(%rsp),%rdi
call   1030 <unlink@plt>
mov    0x2da5(%rip),%rax
mov    (%rax),%edi
lea    0xdf1(%rip),%rsi
call   1090 <fprintf@plt>
mov    $0x1,%eax
add    $0x110,%rsp
pop    %rbx
ret
lea    0xdcc(%rip),%rdi
call   10b0 <perror@plt>
jmp    1223 <main+0x73>
lea    0xdc3(%rip),%rdi
lea    0x10(%rsp),%rsi
call   1040 <open_lock_and_map@plt>
test   %eax,%eax
js     1281 <main+0xd1>
mov    %eax,%edi
call   1070 <close@plt>
xor    %edi,%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
@.str = [35 x i8] c"/tmp/valgrind-file-locking-test.%d\00"
@.str.1 = [7 x i8] c"parent\00"
@.str.2 = [5 x i8] c"fork\00"
@.str.3 = [6 x i8] c"child\00"
@stderr = global i32 0
@.str.4 = [16 x i8] c"Test finished.\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca [256 x i8]
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 1, i32* %8
%13 = getelementptr inbounds [256 x i8], [256 x i8]* %9, i64 0, i64 0
%14 = call i32 (...) @getpid()
%15 = call i32 @snprintf(i8* %13, i32 256, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0), i32 %14)
%16 = getelementptr inbounds [256 x i8], [256 x i8]* %9, i64 0, i64 0
%17 = call i32 @unlink(i8* %16)
%18 = getelementptr inbounds [256 x i8], [256 x i8]* %9, i64 0, i64 0
%19 = call i32 @open_lock_and_map(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i8* %18)
store i32 %19, i32* %6
%20 = icmp sge i32 %19, 0
br i1 %20, label %21, label %44
21: ; preds = %2
%22 = call i32 (...) @fork()
store i32 %22, i32* %10
%23 = load i32, i32* %10
switch i32 %23, label %36 [
i32 -1, label %24
i32 0, label %26
]
24: ; preds = %21
%25 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
br label %43
26: ; preds = %21
%27 = getelementptr inbounds [256 x i8], [256 x i8]* %9, i64 0, i64 0
%28 = call i32 @open_lock_and_map(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i8* %27)
store i32 %28, i32* %7
%29 = load i32, i32* %7
%30 = icmp sge i32 %29, 0
br i1 %30, label %31, label %34
31: ; preds = %26
%32 = load i32, i32* %7
%33 = call i32 @close(i32 %32)
br label %34
34: ; preds = %31, %26
%35 = call i32 @exit(i32 0)
unreachable
36: ; preds = %21
%37 = load i32, i32* %10
%38 = call i32 @wait4(i32 %37, i32* %11, i32 0, i32 0)
store i32 %38, i32* %12
%39 = load i32, i32* %12
%40 = icmp sge i32 %39, 0
%41 = zext i1 %40 to i32
%42 = call i32 @assert(i32 %41)
br label %43
43: ; preds = %36, %24
br label %44
44: ; preds = %43, %2
%45 = load i32, i32* %6
%46 = call i32 @close(i32 %45)
%47 = getelementptr inbounds [256 x i8], [256 x i8]* %9, i64 0, i64 0
%48 = call i32 @unlink(i8* %47)
%49 = load i32, i32* @stderr
%50 = call i32 @fprintf(i32 %49, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0))
%51 = load i32, i32* %8
ret i32 %51
}

LLVM IR (-O2):
@.str = [35 x i8] c"/tmp/valgrind-file-locking-test.%d\00"
@.str.1 = [7 x i8] c"parent\00"
@.str.2 = [5 x i8] c"fork\00"
@.str.3 = [6 x i8] c"child\00"
@stderr = global i32 0
@.str.4 = [16 x i8] c"Test finished.\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [256 x i8]
%4 = alloca i32
%5 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%6 = tail call i32 (...) @getpid()
%7 = call i32 @snprintf(i8* %5, i32 256, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0), i32 %6)
%8 = call i32 @unlink(i8* %5)
%9 = call i32 @open_lock_and_map(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i8* %5)
%10 = icmp sgt i32 %9, -1
br i1 %10, label %11, label %28
11: ; preds = %2
%12 = call i32 (...) @fork()
switch i32 %12, label %22 [
i32 -1, label %13
i32 0, label %15
]
13: ; preds = %11
%14 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
br label %28
15: ; preds = %11
%16 = call i32 @open_lock_and_map(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i8* %5)
%17 = icmp sgt i32 %16, -1
br i1 %17, label %18, label %20
18: ; preds = %15
%19 = call i32 @close(i32 %16)
br label %20
20: ; preds = %18, %15
%21 = call i32 @exit(i32 0)
unreachable
22: ; preds = %11
%23 = bitcast i32* %4 to i8*
%24 = call i32 @wait4(i32 %12, i32* %4, i32 0, i32 0)
%25 = xor i32 %24, -1
%26 = lshr i32 %25, 31
%27 = call i32 @assert(i32 %26)
br label %28
28: ; preds = %13, %22, %2
%29 = call i32 @close(i32 %9)
%30 = call i32 @unlink(i8* %5)
%31 = load i32, i32* @stderr
%32 = call i32 @fprintf(i32 %31, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0))
ret i32 1
}