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

/* Variables and functions */
scalar_t__ EACCES;
scalar_t__ EAGAIN;
int /*<<< orphan*/ F_SETLK;
int /*<<< orphan*/ F_WRLCK;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ SIGTERM;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ err(int, char *);
scalar_t__ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int 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 test2(int fd, int argc, const char **argv) {
  int pid;
  int pfd[2];
  struct flock fl;
  char ch;
  int res;
  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;
  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("2 - F_SETLK on locked region: ");
  res = fcntl(fd, F_SETLK, &fl);
  kill(pid, SIGTERM);
  safe_waitpid(pid);
  close(pfd[0]);
  close(pfd[1]);
  do {
    if (res == 0) {
      printf("FAIL (%s)\n", "res == 0");
      return -1;
    }
  } while (0);
  do {
    if (errno != EACCES && errno != EAGAIN) {
      printf("FAIL (%s)\n", "errno != EACCES && errno != EAGAIN");
      return -1;
    }
  } while (0);
  do {
    printf("SUCCEED\n");
    return 0;
  } while (0);
}


x86 assembly (clang, -O0):
<test2>:
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 <test2+0x36>
mov    $0x1,%edi
lea    0xe0f(%rip),%rsi
call   10c0 <err@plt>
movq   $0x0,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
mov    0x2dab(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
mov    0x2da7(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x40(%rbp)
mov    $0x0,%al
call   10e0 <fork@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    1243 <test2+0x83>
mov    $0x1,%edi
lea    0xdc7(%rip),%rsi
call   10c0 <err@plt>
cmpl   $0x0,-0x1c(%rbp)
jne    12b9 <test2+0xf9>
mov    -0x8(%rbp),%edi
mov    0x2d89(%rip),%rax
mov    (%rax),%esi
lea    -0x40(%rbp),%rdx
call   1040 <fcntl@plt>
cmp    $0x0,%eax
jge    127c <test2+0xbc>
mov    $0x1,%edi
lea    0xd93(%rip),%rsi
call   10c0 <err@plt>
mov    -0x20(%rbp),%edi
lea    0xd94(%rip),%rsi
mov    $0x1,%edx
call   1050 <write@plt>
cmp    $0x0,%rax
jge    12ab <test2+0xeb>
mov    $0x1,%edi
lea    0xd76(%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    -0x41(%rbp),%rsi
mov    $0x1,%edx
call   1090 <read@plt>
cmp    $0x1,%eax
je     12e4 <test2+0x124>
mov    $0x1,%edi
lea    0xd55(%rip),%rsi
call   10c0 <err@plt>
lea    0xd63(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x8(%rbp),%edi
mov    0x2ce4(%rip),%rax
mov    (%rax),%esi
lea    -0x40(%rbp),%rdx
call   1040 <fcntl@plt>
mov    %eax,-0x48(%rbp)
mov    -0x1c(%rbp),%edi
mov    0x2cd4(%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>
cmpl   $0x0,-0x48(%rbp)
jne    135e <test2+0x19e>
lea    0xd29(%rip),%rdi
lea    0xd2d(%rip),%rsi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13dc <test2+0x21c>
jmp    1363 <test2+0x1a3>
jmp    1368 <test2+0x1a8>
mov    0x2c59(%rip),%rax
mov    (%rax),%rax
mov    0x2c5f(%rip),%rcx
cmp    (%rcx),%rax
je     13bd <test2+0x1fd>
mov    0x2c3f(%rip),%rax
mov    (%rax),%rax
mov    0x2c15(%rip),%rcx
cmp    (%rcx),%rax
je     13bd <test2+0x1fd>
lea    0xcca(%rip),%rdi
lea    0xcd7(%rip),%rsi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13dc <test2+0x21c>
jmp    13c2 <test2+0x202>
jmp    13c7 <test2+0x207>
lea    0xcd6(%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):
<test2>:
push   %rbp
push   %rbx
sub    $0x28,%rsp
mov    %edi,%ebx
lea    0x8(%rsp),%rdi
call   1090 <pipe@plt>
test   %rax,%rax
jns    11f8 <test2+0x28>
lea    0xe12(%rip),%rsi
mov    $0x1,%edi
call   10d0 <err@plt>
xorps  %xmm0,%xmm0
movups %xmm0,0x18(%rsp)
mov    0x2db1(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x14(%rsp)
mov    0x2dac(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
xor    %eax,%eax
call   10f0 <fork@plt>
mov    %eax,%ebp
test   %eax,%eax
js     128c <test2+0xbc>
jne    129d <test2+0xcd>
mov    0x2db0(%rip),%rax
mov    (%rax),%esi
lea    0x10(%rsp),%rdx
mov    %ebx,%edi
call   1050 <fcntl@plt>
test   %eax,%eax
jns    1253 <test2+0x83>
lea    0xdc1(%rip),%rsi
mov    $0x1,%edi
call   10d0 <err@plt>
mov    0xc(%rsp),%edi
lea    0xdbc(%rip),%rsi
mov    $0x1,%edx
call   1060 <write@plt>
test   %rax,%rax
jns    127e <test2+0xae>
lea    0xda8(%rip),%rsi
mov    $0x1,%edi
call   10d0 <err@plt>
xor    %eax,%eax
call   10c0 <pause@plt>
xor    %edi,%edi
call   10e0 <exit@plt>
lea    0xd72(%rip),%rsi
mov    $0x1,%edi
call   10d0 <err@plt>
mov    0x8(%rsp),%edi
lea    0x7(%rsp),%rsi
mov    $0x1,%edx
call   10a0 <read@plt>
cmp    $0x1,%eax
je     12c6 <test2+0xf6>
lea    0xd78(%rip),%rsi
mov    $0x1,%edi
call   10d0 <err@plt>
lea    0xd81(%rip),%rdi
xor    %eax,%eax
call   1070 <printf@plt>
mov    0x2d05(%rip),%rax
mov    (%rax),%esi
lea    0x10(%rsp),%rdx
mov    %ebx,%edi
call   1050 <fcntl@plt>
mov    %eax,%ebx
mov    0x2cf6(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
call   10b0 <kill@plt>
mov    %ebp,%edi
call   1040 <safe_waitpid@plt>
mov    0x8(%rsp),%edi
call   1080 <close@plt>
mov    0xc(%rsp),%edi
call   1080 <close@plt>
test   %ebx,%ebx
je     134a <test2+0x17a>
mov    0x2ca9(%rip),%rax
mov    (%rax),%rax
mov    0x2caf(%rip),%rcx
cmp    (%rcx),%rax
je     1366 <test2+0x196>
mov    0x2c73(%rip),%rcx
cmp    (%rcx),%rax
je     1366 <test2+0x196>
lea    0xd2c(%rip),%rdi
lea    0xd39(%rip),%rsi
jmp    1358 <test2+0x188>
lea    0xd1c(%rip),%rdi
lea    0xd20(%rip),%rsi
xor    %eax,%eax
call   1070 <printf@plt>
mov    $0xffffffff,%eax
jmp    1374 <test2+0x1a4>
lea    0xd37(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
add    $0x28,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.flock = type { 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 = [31 x i8] c"2 - F_SETLK on locked region: \00"
@SIGTERM = global i32 0
@.str.7 = [11 x i8] c"FAIL (%s)\0A\00"
@.str.8 = [9 x i8] c"res == 0\00"
@errno = global i64 0
@EACCES = global i64 0
@EAGAIN = global i64 0
@.str.9 = [35 x i8] c"errno != EACCES && errno != EAGAIN\00"
@.str.10 = [9 x i8] c"SUCCEED\0A\00"
define i32 @test2(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
%12 = alloca i32
store i32 %0, i32* %5
store i32 %1, i32* %6
store i8** %2, i8*** %7
%13 = getelementptr inbounds [2 x i32], [2 x i32]* %9, i64 0, i64 0
%14 = call i64 @pipe(i32* %13)
%15 = icmp slt i64 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %3
%17 = call i32 @err(i32 1, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %18
18: ; preds = %16, %3
%19 = getelementptr inbounds %struct.flock, %struct.flock* %10, i32 0, i32 3
store i64 0, i64* %19
%20 = getelementptr inbounds %struct.flock, %struct.flock* %10, i32 0, i32 2
store i64 0, i64* %20
%21 = load i32, i32* @F_WRLCK
%22 = getelementptr inbounds %struct.flock, %struct.flock* %10, i32 0, i32 1
store i32 %21, i32* %22
%23 = load i32, i32* @SEEK_SET
%24 = getelementptr inbounds %struct.flock, %struct.flock* %10, i32 0, i32 0
store i32 %23, 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 = %18
%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, %18
%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 i32 @fcntl(i32 %34, i32 %35, %struct.flock* %10)
%37 = icmp slt i32 %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 ([31 x i8], [31 x i8]* @.str.6, i64 0, i64 0))
%59 = load i32, i32* %5
%60 = load i32, i32* @F_SETLK
%61 = call i32 @fcntl(i32 %59, i32 %60, %struct.flock* %10)
store i32 %61, i32* %12
%62 = load i32, i32* %8
%63 = load i32, i32* @SIGTERM
%64 = call i32 @kill(i32 %62, i32 %63)
%65 = load i32, i32* %8
%66 = call i32 @safe_waitpid(i32 %65)
%67 = getelementptr inbounds [2 x i32], [2 x i32]* %9, i64 0, i64 0
%68 = load i32, i32* %67
%69 = call i32 @close(i32 %68)
%70 = getelementptr inbounds [2 x i32], [2 x i32]* %9, i64 0, i64 1
%71 = load i32, i32* %70
%72 = call i32 @close(i32 %71)
br label %73
73: ; preds = %57
%74 = load i32, i32* %12
%75 = icmp eq i32 %74, 0
br i1 %75, label %76, label %78
76: ; preds = %73
%77 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.7, i64 0, i64 0), i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.8, i64 0, i64 0))
store i32 -1, i32* %4
br label %94
78: ; preds = %73
br label %79
79: ; preds = %78
br label %80
80: ; preds = %79
%81 = load i64, i64* @errno
%82 = load i64, i64* @EACCES
%83 = icmp ne i64 %81, %82
br i1 %83, label %84, label %90
84: ; preds = %80
%85 = load i64, i64* @errno
%86 = load i64, i64* @EAGAIN
%87 = icmp ne i64 %85, %86
br i1 %87, label %88, label %90
88: ; preds = %84
%89 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.7, i64 0, i64 0), i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.9, i64 0, i64 0))
store i32 -1, i32* %4
br label %94
90: ; preds = %84, %80
br label %91
91: ; preds = %90
br label %92
92: ; preds = %91
%93 = 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 %94
94: ; preds = %76, %88, %92
%95 = load i32, i32* %4
ret i32 %95
}

LLVM IR (-O2):
%struct.flock = type { 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 = [31 x i8] c"2 - F_SETLK on locked region: \00"
@SIGTERM = global i32 0
@.str.7 = [11 x i8] c"FAIL (%s)\0A\00"
@.str.8 = [9 x i8] c"res == 0\00"
@errno = global i64 0
@EACCES = global i64 0
@EAGAIN = global i64 0
@.str.9 = [35 x i8] c"errno != EACCES && errno != EAGAIN\00"
@str = [8 x i8] c"SUCCEED\00"
define i32 @test2(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 2
%16 = bitcast i64* %15 to i8*
%17 = load i32, i32* @F_WRLCK
%18 = getelementptr inbounds %struct.flock, %struct.flock* %5, i64 0, i32 1
store i32 %17, i32* %18
%19 = load i32, i32* @SEEK_SET
%20 = getelementptr inbounds %struct.flock, %struct.flock* %5, i64 0, i32 0
store i32 %19, i32* %20
%21 = call i32 (...) @fork()
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %14
%24 = call i32 @err(i32 1, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %43
25: ; preds = %14
%26 = icmp eq i32 %21, 0
br i1 %26, label %27, label %43
27: ; preds = %25
%28 = load i32, i32* @F_SETLK
%29 = call i32 @fcntl(i32 %0, i32 %28, %struct.flock* %5)
%30 = icmp slt i32 %29, 0
br i1 %30, label %31, label %33
31: ; preds = %27
%32 = call i32 @err(i32 1, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
br label %33
33: ; preds = %31, %27
%34 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 1
%35 = load i32, i32* %34
%36 = call i64 @write(i32 %35, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0), i32 1)
%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 ([24 x i8], [24 x i8]* @.str.4, i64 0, i64 0))
br label %40
40: ; preds = %38, %33
%41 = call i32 (...) @pause()
%42 = call i32 @exit(i32 0)
unreachable
43: ; preds = %23, %25
%44 = load i32, i32* %9
%45 = call i32 @read(i32 %44, i8* %6, i32 1)
%46 = icmp eq i32 %45, 1
br i1 %46, label %49, label %47
47: ; preds = %43
%48 = call i32 @err(i32 1, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.5, i64 0, i64 0))
br label %49
49: ; preds = %47, %43
%50 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @.str.6, i64 0, i64 0))
%51 = load i32, i32* @F_SETLK
%52 = call i32 @fcntl(i32 %0, i32 %51, %struct.flock* %5)
%53 = load i32, i32* @SIGTERM
%54 = call i32 @kill(i32 %21, i32 %53)
%55 = call i32 @safe_waitpid(i32 %21)
%56 = load i32, i32* %9
%57 = call i32 @close(i32 %56)
%58 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 1
%59 = load i32, i32* %58
%60 = call i32 @close(i32 %59)
%61 = icmp eq i32 %52, 0
br i1 %61, label %62, label %64
62: ; preds = %49
%63 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.7, i64 0, i64 0), i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.8, i64 0, i64 0))
br label %75
64: ; preds = %49
%65 = load i64, i64* @errno
%66 = load i64, i64* @EACCES
%67 = icmp eq i64 %65, %66
%68 = load i64, i64* @EAGAIN
%69 = icmp eq i64 %65, %68
%70 = select i1 %67, i1 true, i1 %69
br i1 %70, label %73, label %71
71: ; preds = %64
%72 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.7, i64 0, i64 0), i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.9, i64 0, i64 0))
br label %75
73: ; preds = %64
%74 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @str, i64 0, i64 0))
br label %75
75: ; preds = %73, %71, %62
%76 = phi i32 [ -1, %62 ], [ -1, %71 ], [ 0, %73 ]
ret i32 %76
}