#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 scalar_t__ pid_t;

/* Variables and functions */
int /*<<< orphan*/ F_WRLCK;
int /*<<< orphan*/ close_pipes(int **);
int /*<<< orphan*/ err(int, char *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fork();
int /*<<< orphan*/ lock(int /*<<< orphan*/, int, int /*<<< orphan*/);
int /*<<< orphan*/ lock_check(int /*<<< orphan*/, int, int, int /*<<< orphan*/,
                              int, int /*<<< orphan*/);
int /*<<< orphan*/ open_pipes(int **);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ read_pipe(int *);
int /*<<< orphan*/ unlock(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ wait_for_child();
int /*<<< orphan*/ write_pipe(int *);
void test_range_with_eof() {
  printf("testing range with EOF...\n");
  unlock(0, 0);
  int pipes[2][2];
  open_pipes(pipes);
  pid_t pid = fork();
  if (pid < 0)
    err(1, "fork");
  if (pid == 0) {
    lock(F_WRLCK, 100, 0);
    write_pipe(pipes[0]);
    read_pipe(pipes[1]);
    exit(0);
  }
  read_pipe(pipes[0]);
  lock_check(F_WRLCK, 50, 100, F_WRLCK, 100, 0);
  write_pipe(pipes[1]);
  wait_for_child();
  close_pipes(pipes);
}


x86 assembly (clang, -O0):
<test_range_with_eof>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
lea    0xe31(%rip),%rdi
call   1050 <printf@plt>
xor    %esi,%esi
mov    %esi,%edi
call   1030 <unlock@plt>
lea    -0x10(%rbp),%rdi
call   1060 <open_pipes@plt>
mov    $0x0,%al
call   10d0 <fork@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jge    120d <test_range_with_eof+0x4d>
mov    $0x1,%edi
lea    0xe13(%rip),%rsi
call   1070 <err@plt>
cmpq   $0x0,-0x18(%rbp)
jne    124a <test_range_with_eof+0x8a>
mov    0x2dc1(%rip),%rax
mov    (%rax),%edi
mov    $0x64,%esi
xor    %edx,%edx
call   1040 <lock@plt>
lea    -0x10(%rbp),%rdi
call   1080 <write_pipe@plt>
lea    -0x10(%rbp),%rdi
add    $0x8,%rdi
call   10a0 <read_pipe@plt>
xor    %edi,%edi
call   1090 <exit@plt>
lea    -0x10(%rbp),%rdi
call   10a0 <read_pipe@plt>
mov    0x2d86(%rip),%rax
mov    (%rax),%edi
mov    0x2d7d(%rip),%rax
mov    (%rax),%ecx
mov    $0x32,%esi
mov    $0x64,%r8d
xor    %r9d,%r9d
mov    %r8d,%edx
call   10e0 <lock_check@plt>
lea    -0x10(%rbp),%rdi
add    $0x8,%rdi
call   1080 <write_pipe@plt>
mov    $0x0,%al
call   10c0 <wait_for_child@plt>
lea    -0x10(%rbp),%rdi
call   10b0 <close_pipes@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_range_with_eof>:
push   %rbx
sub    $0x10,%rsp
lea    0xe39(%rip),%rdi
call   1040 <puts@plt>
xor    %edi,%edi
xor    %esi,%esi
call   1030 <unlock@plt>
mov    %rsp,%rdi
call   1060 <open_pipes@plt>
xor    %eax,%eax
call   10d0 <fork@plt>
test   %rax,%rax
js     121e <test_range_with_eof+0x5e>
jne    122f <test_range_with_eof+0x6f>
mov    0x2de9(%rip),%rax
mov    (%rax),%edi
mov    $0x64,%esi
xor    %edx,%edx
call   1050 <lock@plt>
mov    %rsp,%rdi
call   1080 <write_pipe@plt>
lea    0x8(%rsp),%rdi
call   10a0 <read_pipe@plt>
xor    %edi,%edi
call   1090 <exit@plt>
lea    0xddb(%rip),%rsi
mov    $0x1,%edi
call   1070 <err@plt>
mov    %rsp,%rbx
mov    %rbx,%rdi
call   10a0 <read_pipe@plt>
mov    0x2d9f(%rip),%rax
mov    (%rax),%ecx
mov    %ecx,%edi
mov    $0x32,%esi
mov    $0x64,%edx
mov    $0x64,%r8d
xor    %r9d,%r9d
call   10e0 <lock_check@plt>
lea    0x8(%rsp),%rdi
call   1080 <write_pipe@plt>
xor    %eax,%eax
call   10c0 <wait_for_child@plt>
mov    %rbx,%rdi
call   10b0 <close_pipes@plt>
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@.str = [27 x i8] c"testing range with EOF...\0A\00"
@.str.1 = [5 x i8] c"fork\00"
@F_WRLCK = global i32 0
define void @test_range_with_eof() {
%1 = alloca [2 x [2 x i32]]
%2 = alloca i64
%3 = call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
%4 = call i32 @unlock(i32 0, i32 0)
%5 = getelementptr inbounds [2 x [2 x i32]], [2 x [2 x i32]]* %1, i64 0, i64 0
%6 = bitcast [2 x i32]* %5 to i32**
%7 = call i32 @open_pipes(i32** %6)
%8 = call i64 (...) @fork()
store i64 %8, i64* %2
%9 = load i64, i64* %2
%10 = icmp slt i64 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %0
%12 = call i32 @err(i32 1, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %13
13: ; preds = %11, %0
%14 = load i64, i64* %2
%15 = icmp eq i64 %14, 0
br i1 %15, label %16, label %26
16: ; preds = %13
%17 = load i32, i32* @F_WRLCK
%18 = call i32 @lock(i32 %17, i32 100, i32 0)
%19 = getelementptr inbounds [2 x [2 x i32]], [2 x [2 x i32]]* %1, i64 0, i64 0
%20 = getelementptr inbounds [2 x i32], [2 x i32]* %19, i64 0, i64 0
%21 = call i32 @write_pipe(i32* %20)
%22 = getelementptr inbounds [2 x [2 x i32]], [2 x [2 x i32]]* %1, i64 0, i64 1
%23 = getelementptr inbounds [2 x i32], [2 x i32]* %22, i64 0, i64 0
%24 = call i32 @read_pipe(i32* %23)
%25 = call i32 @exit(i32 0)
unreachable
26: ; preds = %13
%27 = getelementptr inbounds [2 x [2 x i32]], [2 x [2 x i32]]* %1, i64 0, i64 0
%28 = getelementptr inbounds [2 x i32], [2 x i32]* %27, i64 0, i64 0
%29 = call i32 @read_pipe(i32* %28)
%30 = load i32, i32* @F_WRLCK
%31 = load i32, i32* @F_WRLCK
%32 = call i32 @lock_check(i32 %30, i32 50, i32 100, i32 %31, i32 100, i32 0)
%33 = getelementptr inbounds [2 x [2 x i32]], [2 x [2 x i32]]* %1, i64 0, i64 1
%34 = getelementptr inbounds [2 x i32], [2 x i32]* %33, i64 0, i64 0
%35 = call i32 @write_pipe(i32* %34)
%36 = call i32 (...) @wait_for_child()
%37 = getelementptr inbounds [2 x [2 x i32]], [2 x [2 x i32]]* %1, i64 0, i64 0
%38 = bitcast [2 x i32]* %37 to i32**
%39 = call i32 @close_pipes(i32** %38)
ret void
}

LLVM IR (-O2):
@.str.1 = [5 x i8] c"fork\00"
@F_WRLCK = global i32 0
@str = [26 x i8] c"testing range with EOF...\00"
define void @test_range_with_eof() {
%1 = alloca [2 x [2 x i32]]
%2 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @str, i64 0, i64 0))
%3 = tail call i32 @unlock(i32 0, i32 0)
%4 = bitcast [2 x [2 x i32]]* %1 to i8*
%5 = bitcast [2 x [2 x i32]]* %1 to i32**
%6 = call i32 @open_pipes(i32** %5)
%7 = call i64 (...) @fork()
%8 = icmp slt i64 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %0
%10 = call i32 @err(i32 1, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %21
11: ; preds = %0
%12 = icmp eq i64 %7, 0
br i1 %12, label %13, label %21
13: ; preds = %11
%14 = load i32, i32* @F_WRLCK
%15 = call i32 @lock(i32 %14, i32 100, i32 0)
%16 = getelementptr inbounds [2 x [2 x i32]], [2 x [2 x i32]]* %1, i64 0, i64 0, i64 0
%17 = call i32 @write_pipe(i32* %16)
%18 = getelementptr inbounds [2 x [2 x i32]], [2 x [2 x i32]]* %1, i64 0, i64 1, i64 0
%19 = call i32 @read_pipe(i32* %18)
%20 = call i32 @exit(i32 0)
unreachable
21: ; preds = %9, %11
%22 = getelementptr inbounds [2 x [2 x i32]], [2 x [2 x i32]]* %1, i64 0, i64 0, i64 0
%23 = call i32 @read_pipe(i32* %22)
%24 = load i32, i32* @F_WRLCK
%25 = call i32 @lock_check(i32 %24, i32 50, i32 100, i32 %24, i32 100, i32 0)
%26 = getelementptr inbounds [2 x [2 x i32]], [2 x [2 x i32]]* %1, i64 0, i64 1, i64 0
%27 = call i32 @write_pipe(i32* %26)
%28 = call i32 (...) @wait_for_child()
%29 = call i32 @close_pipes(i32** %5)
ret void
}