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

/* Variables and functions */
int /*<<< orphan*/ F_GETLK;
int /*<<< orphan*/ F_SETLK;
int /*<<< orphan*/ F_WRLCK;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ SIGTERM;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ err(int, char *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fcntl(int, int /*<<< orphan*/, struct flock *);
int fork();
int /*<<< orphan*/ kill(int, int /*<<< orphan*/);
int /*<<< orphan*/ pause();
scalar_t__ pipe(int *);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
int /*<<< orphan*/ safe_waitpid(int);
scalar_t__ write(int, char *, int);
int test10(int fd, __attribute__((__unused__)) int argc,
           const __attribute__((__unused__)) char **argv) {
  int pid;
  int pfd[2];
  struct flock fl;
  char ch;
  if (pipe(pfd) < 0)
    err(1, "pipe");
  fl.l_start = 0;
  fl.l_len = 0;
  fl.l_type = F_WRLCK;
  fl.l_whence = SEEK_SET;
  fl.l_pid = 9999;
  pid = fork();
  if (pid < 0)
    err(1, "fork");
  if (pid == 0) {
    if (fcntl(fd, F_SETLK, &fl) < 0)
      err(1, "F_SETLK (child)");
    if (write(pfd[1], "a", 1) < 0)
      err(1, "writing to pipe (child)");
    pause();
    exit(0);
  }
  if (read(pfd[0], &ch, 1) != 1)
    err(1, "reading from pipe (child)");
  printf("10 - trying to set bogus pid or sysid values: ");
  if (fcntl(fd, F_GETLK, &fl) < 0)
    err(1, "F_GETLK");
  kill(pid, SIGTERM);
  safe_waitpid(pid);
  close(pfd[0]);
  close(pfd[1]);
  do {
    if (fl.l_pid != pid) {
      printf("FAIL (%s)\n", "fl.l_pid != pid");
      return -1;
    }
  } while (0);
  do {
    printf("SUCCEED\n");
    return 0;
  } while (0);
}


x86 assembly (clang, -O0):
<test10>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
lea    -0x24(%rbp),%rdi
call   1080 <pipe@plt>
cmp    $0x0,%rax
jge    11f6 <test10+0x36>
mov    $0x1,%edi
lea    0xe0f(%rip),%rsi
call   10c0 <err@plt>
movq   $0x0,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
mov    0x2db3(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x40(%rbp)
mov    0x2daf(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x44(%rbp)
movl   $0x270f,-0x48(%rbp)
mov    $0x0,%al
call   10e0 <fork@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    124a <test10+0x8a>
mov    $0x1,%edi
lea    0xdc0(%rip),%rsi
call   10c0 <err@plt>
cmpl   $0x0,-0x1c(%rbp)
jne    12c1 <test10+0x101>
mov    -0x8(%rbp),%edi
mov    0x2d82(%rip),%rax
mov    (%rax),%esi
lea    -0x48(%rbp),%rdx
call   1040 <fcntl@plt>
cmp    $0x0,%rax
jge    1284 <test10+0xc4>
mov    $0x1,%edi
lea    0xd8b(%rip),%rsi
call   10c0 <err@plt>
mov    -0x20(%rbp),%edi
lea    0xd8c(%rip),%rsi
mov    $0x1,%edx
call   1050 <write@plt>
cmp    $0x0,%rax
jge    12b3 <test10+0xf3>
mov    $0x1,%edi
lea    0xd6e(%rip),%rsi
call   10c0 <err@plt>
mov    $0x0,%al
call   10b0 <pause@plt>
xor    %edi,%edi
call   10d0 <exit@plt>
mov    -0x24(%rbp),%edi
lea    -0x49(%rbp),%rsi
mov    $0x1,%edx
call   1090 <read@plt>
cmp    $0x1,%eax
je     12ec <test10+0x12c>
mov    $0x1,%edi
lea    0xd4d(%rip),%rsi
call   10c0 <err@plt>
lea    0xd5b(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x8(%rbp),%edi
mov    0x2cd4(%rip),%rax
mov    (%rax),%esi
lea    -0x48(%rbp),%rdx
call   1040 <fcntl@plt>
cmp    $0x0,%rax
jge    132a <test10+0x16a>
mov    $0x1,%edi
lea    0xd58(%rip),%rsi
call   10c0 <err@plt>
mov    -0x1c(%rbp),%edi
mov    0x2cb4(%rip),%rax
mov    (%rax),%esi
call   10a0 <kill@plt>
mov    -0x1c(%rbp),%edi
call   1030 <safe_waitpid@plt>
mov    -0x24(%rbp),%edi
call   1070 <close@plt>
mov    -0x20(%rbp),%edi
call   1070 <close@plt>
mov    -0x48(%rbp),%eax
cmp    -0x1c(%rbp),%eax
je     1380 <test10+0x1c0>
lea    0xd1f(%rip),%rdi
lea    0xd23(%rip),%rsi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    139f <test10+0x1df>
jmp    1385 <test10+0x1c5>
jmp    138a <test10+0x1ca>
lea    0xd0f(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test10>:
push   %rbp
push   %rbx
sub    $0x38,%rsp
mov    %edi,%ebx
lea    0x10(%rsp),%rdi
call   1090 <pipe@plt>
test   %rax,%rax
jns    11f8 <test10+0x28>
lea    0xe12(%rip),%rsi
mov    $0x1,%edi
call   10d0 <err@plt>
xorps  %xmm0,%xmm0
movups %xmm0,0x28(%rsp)
mov    0x2db9(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x20(%rsp)
mov    0x2db4(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x1c(%rsp)
movl   $0x270f,0x18(%rsp)
xor    %eax,%eax
call   10f0 <fork@plt>
mov    %eax,%ebp
test   %eax,%eax
js     1295 <test10+0xc5>
jne    12a6 <test10+0xd6>
mov    0x2da8(%rip),%rax
mov    (%rax),%esi
lea    0x18(%rsp),%rdx
mov    %ebx,%edi
call   1050 <fcntl@plt>
test   %rax,%rax
jns    125c <test10+0x8c>
lea    0xdb8(%rip),%rsi
mov    $0x1,%edi
call   10d0 <err@plt>
mov    0x14(%rsp),%edi
lea    0xdb3(%rip),%rsi
mov    $0x1,%edx
call   1060 <write@plt>
test   %rax,%rax
jns    1287 <test10+0xb7>
lea    0xd9f(%rip),%rsi
mov    $0x1,%edi
call   10d0 <err@plt>
xor    %eax,%eax
call   10c0 <pause@plt>
xor    %edi,%edi
call   10e0 <exit@plt>
lea    0xd69(%rip),%rsi
mov    $0x1,%edi
call   10d0 <err@plt>
mov    0x10(%rsp),%edi
lea    0xf(%rsp),%rsi
mov    $0x1,%edx
call   10a0 <read@plt>
cmp    $0x1,%eax
je     12cf <test10+0xff>
lea    0xd6f(%rip),%rsi
mov    $0x1,%edi
call   10d0 <err@plt>
lea    0xd78(%rip),%rdi
xor    %eax,%eax
call   1070 <printf@plt>
mov    0x2cf4(%rip),%rax
mov    (%rax),%esi
lea    0x18(%rsp),%rdx
mov    %ebx,%edi
call   1050 <fcntl@plt>
test   %rax,%rax
jns    1308 <test10+0x138>
lea    0xd7f(%rip),%rsi
mov    $0x1,%edi
call   10d0 <err@plt>
mov    0x2cd9(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
call   10b0 <kill@plt>
mov    %ebp,%edi
call   1040 <safe_waitpid@plt>
mov    0x10(%rsp),%edi
call   1080 <close@plt>
mov    0x14(%rsp),%edi
call   1080 <close@plt>
cmp    %ebp,0x18(%rsp)
jne    1347 <test10+0x177>
lea    0xd62(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
jmp    1361 <test10+0x191>
lea    0xd37(%rip),%rdi
lea    0xd3b(%rip),%rsi
xor    %eax,%eax
call   1070 <printf@plt>
mov    $0xffffffff,%eax
add    $0x38,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.flock = type { i32, i32, i32, i64, i64 }
@.str = [5 x i8] c"pipe\00"
@F_WRLCK = global i32 0
@SEEK_SET = global i32 0
@.str.1 = [5 x i8] c"fork\00"
@F_SETLK = global i32 0
@.str.2 = [16 x i8] c"F_SETLK (child)\00"
@.str.3 = [2 x i8] c"a\00"
@.str.4 = [24 x i8] c"writing to pipe (child)\00"
@.str.5 = [26 x i8] c"reading from pipe (child)\00"
@.str.6 = [47 x i8] c"10 - trying to set bogus pid or sysid values: \00"
@F_GETLK = global i32 0
@.str.7 = [8 x i8] c"F_GETLK\00"
@SIGTERM = global i32 0
@.str.8 = [11 x i8] c"FAIL (%s)\0A\00"
@.str.9 = [16 x i8] c"fl.l_pid != pid\00"
@.str.10 = [9 x i8] c"SUCCEED\0A\00"
define i32 @test10(i32 %0, i32 %1, i8** %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8**
%8 = alloca i32
%9 = alloca [2 x i32]
%10 = alloca %struct.flock
%11 = alloca i8
store i32 %0, i32* %5
store i32 %1, i32* %6
store i8** %2, i8*** %7
%12 = getelementptr inbounds [2 x i32], [2 x i32]* %9, i64 0, i64 0
%13 = call i64 @pipe(i32* %12)
%14 = icmp slt i64 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %3
%16 = call i32 @err(i32 1, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %17
17: ; preds = %15, %3
%18 = getelementptr inbounds %struct.flock, %struct.flock* %10, i32 0, i32 4
store i64 0, i64* %18
%19 = getelementptr inbounds %struct.flock, %struct.flock* %10, i32 0, i32 3
store i64 0, i64* %19
%20 = load i32, i32* @F_WRLCK
%21 = getelementptr inbounds %struct.flock, %struct.flock* %10, i32 0, i32 2
store i32 %20, i32* %21
%22 = load i32, i32* @SEEK_SET
%23 = getelementptr inbounds %struct.flock, %struct.flock* %10, i32 0, i32 1
store i32 %22, i32* %23
%24 = getelementptr inbounds %struct.flock, %struct.flock* %10, i32 0, i32 0
store i32 9999, i32* %24
%25 = call i32 (...) @fork()
store i32 %25, i32* %8
%26 = load i32, i32* %8
%27 = icmp slt i32 %26, 0
br i1 %27, label %28, label %30
28: ; preds = %17
%29 = call i32 @err(i32 1, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %30
30: ; preds = %28, %17
%31 = load i32, i32* %8
%32 = icmp eq i32 %31, 0
br i1 %32, label %33, label %50
33: ; preds = %30
%34 = load i32, i32* %5
%35 = load i32, i32* @F_SETLK
%36 = call i64 @fcntl(i32 %34, i32 %35, %struct.flock* %10)
%37 = icmp slt i64 %36, 0
br i1 %37, label %38, label %40
38: ; preds = %33
%39 = call i32 @err(i32 1, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
br label %40
40: ; preds = %38, %33
%41 = getelementptr inbounds [2 x i32], [2 x i32]* %9, i64 0, i64 1
%42 = load i32, i32* %41
%43 = call i64 @write(i32 %42, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0), i32 1)
%44 = icmp slt i64 %43, 0
br i1 %44, label %45, label %47
45: ; preds = %40
%46 = call i32 @err(i32 1, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.4, i64 0, i64 0))
br label %47
47: ; preds = %45, %40
%48 = call i32 (...) @pause()
%49 = call i32 @exit(i32 0)
unreachable
50: ; preds = %30
%51 = getelementptr inbounds [2 x i32], [2 x i32]* %9, i64 0, i64 0
%52 = load i32, i32* %51
%53 = call i32 @read(i32 %52, i8* %11, i32 1)
%54 = icmp ne i32 %53, 1
br i1 %54, label %55, label %57
55: ; preds = %50
%56 = call i32 @err(i32 1, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.5, i64 0, i64 0))
br label %57
57: ; preds = %55, %50
%58 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.6, i64 0, i64 0))
%59 = load i32, i32* %5
%60 = load i32, i32* @F_GETLK
%61 = call i64 @fcntl(i32 %59, i32 %60, %struct.flock* %10)
%62 = icmp slt i64 %61, 0
br i1 %62, label %63, label %65
63: ; preds = %57
%64 = call i32 @err(i32 1, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.7, i64 0, i64 0))
br label %65
65: ; preds = %63, %57
%66 = load i32, i32* %8
%67 = load i32, i32* @SIGTERM
%68 = call i32 @kill(i32 %66, i32 %67)
%69 = load i32, i32* %8
%70 = call i32 @safe_waitpid(i32 %69)
%71 = getelementptr inbounds [2 x i32], [2 x i32]* %9, i64 0, i64 0
%72 = load i32, i32* %71
%73 = call i32 @close(i32 %72)
%74 = getelementptr inbounds [2 x i32], [2 x i32]* %9, i64 0, i64 1
%75 = load i32, i32* %74
%76 = call i32 @close(i32 %75)
br label %77
77: ; preds = %65
%78 = getelementptr inbounds %struct.flock, %struct.flock* %10, i32 0, i32 0
%79 = load i32, i32* %78
%80 = load i32, i32* %8
%81 = icmp ne i32 %79, %80
br i1 %81, label %82, label %84
82: ; preds = %77
%83 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.8, i64 0, i64 0), i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.9, i64 0, i64 0))
store i32 -1, i32* %4
br label %88
84: ; preds = %77
br label %85
85: ; preds = %84
br label %86
86: ; preds = %85
%87 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.10, i64 0, i64 0))
store i32 0, i32* %4
br label %88
88: ; preds = %82, %86
%89 = load i32, i32* %4
ret i32 %89
}

LLVM IR (-O2):
%struct.flock = type { i32, i32, i32, i64, i64 }
@.str = [5 x i8] c"pipe\00"
@F_WRLCK = global i32 0
@SEEK_SET = global i32 0
@.str.1 = [5 x i8] c"fork\00"
@F_SETLK = global i32 0
@.str.2 = [16 x i8] c"F_SETLK (child)\00"
@.str.3 = [2 x i8] c"a\00"
@.str.4 = [24 x i8] c"writing to pipe (child)\00"
@.str.5 = [26 x i8] c"reading from pipe (child)\00"
@.str.6 = [47 x i8] c"10 - trying to set bogus pid or sysid values: \00"
@F_GETLK = global i32 0
@.str.7 = [8 x i8] c"F_GETLK\00"
@SIGTERM = global i32 0
@.str.8 = [11 x i8] c"FAIL (%s)\0A\00"
@.str.9 = [16 x i8] c"fl.l_pid != pid\00"
@str = [8 x i8] c"SUCCEED\00"
define i32 @test10(i32 %0, i32 %1, i8** nocapture readnone %2) {
%4 = alloca [2 x i32]
%5 = alloca %struct.flock
%6 = alloca i8
%7 = bitcast [2 x i32]* %4 to i8*
%8 = bitcast %struct.flock* %5 to i8*
%9 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 0
%10 = call i64 @pipe(i32* %9)
%11 = icmp slt i64 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %3
%13 = call i32 @err(i32 1, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %14
14: ; preds = %12, %3
%15 = getelementptr inbounds %struct.flock, %struct.flock* %5, i64 0, i32 3
%16 = bitcast i64* %15 to i8*
%17 = load i32, i32* @F_WRLCK
%18 = getelementptr inbounds %struct.flock, %struct.flock* %5, i64 0, i32 2
store i32 %17, i32* %18
%19 = load i32, i32* @SEEK_SET
%20 = getelementptr inbounds %struct.flock, %struct.flock* %5, i64 0, i32 1
store i32 %19, i32* %20
%21 = getelementptr inbounds %struct.flock, %struct.flock* %5, i64 0, i32 0
store i32 9999, i32* %21
%22 = call i32 (...) @fork()
%23 = icmp slt i32 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %14
%25 = call i32 @err(i32 1, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %44
26: ; preds = %14
%27 = icmp eq i32 %22, 0
br i1 %27, label %28, label %44
28: ; preds = %26
%29 = load i32, i32* @F_SETLK
%30 = call i64 @fcntl(i32 %0, i32 %29, %struct.flock* %5)
%31 = icmp slt i64 %30, 0
br i1 %31, label %32, label %34
32: ; preds = %28
%33 = call i32 @err(i32 1, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
br label %34
34: ; preds = %32, %28
%35 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 1
%36 = load i32, i32* %35
%37 = call i64 @write(i32 %36, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0), i32 1)
%38 = icmp slt i64 %37, 0
br i1 %38, label %39, label %41
39: ; preds = %34
%40 = call i32 @err(i32 1, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.4, i64 0, i64 0))
br label %41
41: ; preds = %39, %34
%42 = call i32 (...) @pause()
%43 = call i32 @exit(i32 0)
unreachable
44: ; preds = %24, %26
%45 = load i32, i32* %9
%46 = call i32 @read(i32 %45, i8* %6, i32 1)
%47 = icmp eq i32 %46, 1
br i1 %47, label %50, label %48
48: ; preds = %44
%49 = call i32 @err(i32 1, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.5, i64 0, i64 0))
br label %50
50: ; preds = %48, %44
%51 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([47 x i8], [47 x i8]* @.str.6, i64 0, i64 0))
%52 = load i32, i32* @F_GETLK
%53 = call i64 @fcntl(i32 %0, i32 %52, %struct.flock* %5)
%54 = icmp slt i64 %53, 0
br i1 %54, label %55, label %57
55: ; preds = %50
%56 = call i32 @err(i32 1, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.7, i64 0, i64 0))
br label %57
57: ; preds = %55, %50
%58 = load i32, i32* @SIGTERM
%59 = call i32 @kill(i32 %22, i32 %58)
%60 = call i32 @safe_waitpid(i32 %22)
%61 = load i32, i32* %9
%62 = call i32 @close(i32 %61)
%63 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 1
%64 = load i32, i32* %63
%65 = call i32 @close(i32 %64)
%66 = load i32, i32* %21
%67 = icmp eq i32 %66, %22
br i1 %67, label %70, label %68
68: ; preds = %57
%69 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.8, i64 0, i64 0), i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.9, i64 0, i64 0))
br label %72
70: ; preds = %57
%71 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @str, i64 0, i64 0))
br label %72
72: ; preds = %70, %68
%73 = phi i32 [ -1, %68 ], [ 0, %70 ]
ret i32 %73
}