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

/* Variables and functions */
int /*<<< orphan*/ F_SETLK;
int /*<<< orphan*/ F_SETLKW;
int /*<<< orphan*/ F_UNLCK;
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*/);
int fcntl(int, int /*<<< orphan*/, struct flock *);
int fork();
int /*<<< orphan*/ kill(int, int /*<<< orphan*/);
scalar_t__ pipe(int *);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
int /*<<< orphan*/ safe_waitpid(int);
int /*<<< orphan*/ sleep(int);
scalar_t__ write(int, char *, int);
int test12(int fd, __attribute__((__unused__)) int argc,
           const __attribute__((__unused__)) 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)");
    sleep(1);
    exit(0);
  }
  if (read(pfd[0], &ch, 1) != 1)
    err(1, "reading from pipe (child)");
  printf("12 - F_SETLKW on locked region which is then unlocked: ");
  res = fcntl(fd, F_SETLKW, &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);
  fl.l_start = 0;
  fl.l_len = 0;
  fl.l_type = F_UNLCK;
  if (fcntl(fd, F_SETLK, &fl) < 0)
    err(1, "F_UNLCK");
  do {
    printf("SUCCEED\n");
    return 0;
  } while (0);
}


x86 assembly (clang, -O0):
<test12>:
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 <test12+0x36>
mov    $0x1,%edi
lea    0xe0f(%rip),%rsi
call   10b0 <err@plt>
movq   $0x0,-0x38(%rbp)
movq   $0x0,-0x40(%rbp)
mov    0x2dab(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x48(%rbp)
mov    0x2da7(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    $0x0,%al
call   10e0 <fork@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    1243 <test12+0x83>
mov    $0x1,%edi
lea    0xdc7(%rip),%rsi
call   10b0 <err@plt>
cmpl   $0x0,-0x1c(%rbp)
jne    12bc <test12+0xfc>
mov    -0x8(%rbp),%edi
mov    0x2d81(%rip),%rax
mov    (%rax),%esi
lea    -0x48(%rbp),%rdx
call   1040 <fcntl@plt>
cmp    $0x0,%eax
jge    127c <test12+0xbc>
mov    $0x1,%edi
lea    0xd93(%rip),%rsi
call   10b0 <err@plt>
mov    -0x20(%rbp),%edi
lea    0xd94(%rip),%rsi
mov    $0x1,%edx
call   1050 <write@plt>
cmp    $0x0,%rax
jge    12ab <test12+0xeb>
mov    $0x1,%edi
lea    0xd76(%rip),%rsi
call   10b0 <err@plt>
mov    $0x1,%edi
call   10d0 <sleep@plt>
xor    %edi,%edi
call   10c0 <exit@plt>
mov    -0x24(%rbp),%edi
lea    -0x49(%rbp),%rsi
mov    $0x1,%edx
call   1090 <read@plt>
cmp    $0x1,%eax
je     12e7 <test12+0x127>
mov    $0x1,%edi
lea    0xd52(%rip),%rsi
call   10b0 <err@plt>
lea    0xd60(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x8(%rbp),%edi
mov    0x2ce1(%rip),%rax
mov    (%rax),%esi
lea    -0x48(%rbp),%rdx
call   1040 <fcntl@plt>
mov    %eax,-0x50(%rbp)
mov    -0x1c(%rbp),%edi
mov    0x2cd1(%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,-0x50(%rbp)
je     1361 <test12+0x1a1>
lea    0xd3f(%rip),%rdi
lea    0xd43(%rip),%rsi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13cb <test12+0x20b>
jmp    1366 <test12+0x1a6>
movq   $0x0,-0x38(%rbp)
movq   $0x0,-0x40(%rbp)
mov    0x2c53(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x48(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2c4c(%rip),%rax
mov    (%rax),%esi
lea    -0x48(%rbp),%rdx
call   1040 <fcntl@plt>
cmp    $0x0,%eax
jge    13b1 <test12+0x1f1>
mov    $0x1,%edi
lea    0xcee(%rip),%rsi
call   10b0 <err@plt>
jmp    13b6 <test12+0x1f6>
lea    0xce5(%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):
<test12>:
push   %rbp
push   %r14
push   %rbx
sub    $0x30,%rsp
mov    %edi,%r14d
lea    0x8(%rsp),%rdi
call   1090 <pipe@plt>
test   %rax,%rax
jns    11fb <test12+0x2b>
lea    0xe0f(%rip),%rsi
mov    $0x1,%edi
call   10c0 <err@plt>
xorps  %xmm0,%xmm0
movups %xmm0,0x18(%rsp)
mov    0x2dae(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
mov    0x2da9(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x28(%rsp)
xor    %eax,%eax
call   10f0 <fork@plt>
mov    %eax,%ebp
test   %eax,%eax
js     1293 <test12+0xc3>
jne    12a4 <test12+0xd4>
mov    0x2da5(%rip),%rax
mov    (%rax),%esi
lea    0x10(%rsp),%rdx
mov    %r14d,%edi
call   1050 <fcntl@plt>
test   %eax,%eax
jns    1257 <test12+0x87>
lea    0xdbd(%rip),%rsi
mov    $0x1,%edi
call   10c0 <err@plt>
mov    0xc(%rsp),%edi
lea    0xdb8(%rip),%rsi
mov    $0x1,%edx
call   1060 <write@plt>
test   %rax,%rax
jns    1282 <test12+0xb2>
lea    0xda4(%rip),%rsi
mov    $0x1,%edi
call   10c0 <err@plt>
mov    $0x1,%edi
call   10e0 <sleep@plt>
xor    %edi,%edi
call   10d0 <exit@plt>
lea    0xd6b(%rip),%rsi
mov    $0x1,%edi
call   10c0 <err@plt>
mov    0x8(%rsp),%edi
lea    0x7(%rsp),%rsi
mov    $0x1,%edx
call   10a0 <read@plt>
cmp    $0x1,%eax
je     12cd <test12+0xfd>
lea    0xd71(%rip),%rsi
mov    $0x1,%edi
call   10c0 <err@plt>
lea    0xd7a(%rip),%rdi
xor    %eax,%eax
call   1070 <printf@plt>
mov    0x2cfe(%rip),%rax
mov    (%rax),%esi
lea    0x10(%rsp),%rdx
mov    %r14d,%edi
call   1050 <fcntl@plt>
mov    %eax,%ebx
mov    0x2cee(%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     133c <test12+0x16c>
lea    0xd5f(%rip),%rdi
lea    0xd63(%rip),%rsi
xor    %eax,%eax
call   1070 <printf@plt>
mov    $0xffffffff,%eax
jmp    138d <test12+0x1bd>
lea    0x18(%rsp),%rax
xorps  %xmm0,%xmm0
movups %xmm0,(%rax)
mov    0x2c82(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
mov    0x2c7d(%rip),%rax
mov    (%rax),%esi
lea    0x10(%rsp),%rdx
mov    %r14d,%edi
call   1050 <fcntl@plt>
test   %eax,%eax
jns    137f <test12+0x1af>
lea    0xd25(%rip),%rsi
mov    $0x1,%edi
call   10c0 <err@plt>
lea    0xd1c(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.flock = type { i32, i64, i64, i32 }
@.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 = [56 x i8] c"12 - F_SETLKW on locked region which is then unlocked: \00"
@F_SETLKW = global i32 0
@SIGTERM = global i32 0
@.str.7 = [11 x i8] c"FAIL (%s)\0A\00"
@.str.8 = [9 x i8] c"res != 0\00"
@F_UNLCK = global i32 0
@.str.9 = [8 x i8] c"F_UNLCK\00"
@.str.10 = [9 x i8] c"SUCCEED\0A\00"
define i32 @test12(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 2
store i64 0, i64* %19
%20 = getelementptr inbounds %struct.flock, %struct.flock* %10, i32 0, i32 1
store i64 0, i64* %20
%21 = load i32, i32* @F_WRLCK
%22 = getelementptr inbounds %struct.flock, %struct.flock* %10, i32 0, i32 0
store i32 %21, i32* %22
%23 = load i32, i32* @SEEK_SET
%24 = getelementptr inbounds %struct.flock, %struct.flock* %10, i32 0, i32 3
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 @sleep(i32 1)
%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 ([56 x i8], [56 x i8]* @.str.6, i64 0, i64 0))
%59 = load i32, i32* %5
%60 = load i32, i32* @F_SETLKW
%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 ne 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 %93
78: ; preds = %73
br label %79
79: ; preds = %78
%80 = getelementptr inbounds %struct.flock, %struct.flock* %10, i32 0, i32 2
store i64 0, i64* %80
%81 = getelementptr inbounds %struct.flock, %struct.flock* %10, i32 0, i32 1
store i64 0, i64* %81
%82 = load i32, i32* @F_UNLCK
%83 = getelementptr inbounds %struct.flock, %struct.flock* %10, i32 0, i32 0
store i32 %82, i32* %83
%84 = load i32, i32* %5
%85 = load i32, i32* @F_SETLK
%86 = call i32 @fcntl(i32 %84, i32 %85, %struct.flock* %10)
%87 = icmp slt i32 %86, 0
br i1 %87, label %88, label %90
88: ; preds = %79
%89 = call i32 @err(i32 1, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.9, i64 0, i64 0))
br label %90
90: ; preds = %88, %79
br label %91
91: ; preds = %90
%92 = 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 %93
93: ; preds = %76, %91
%94 = load i32, i32* %4
ret i32 %94
}

LLVM IR (-O2):
%struct.flock = type { i32, i64, i64, i32 }
@.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 = [56 x i8] c"12 - F_SETLKW on locked region which is then unlocked: \00"
@F_SETLKW = global i32 0
@SIGTERM = global i32 0
@.str.7 = [11 x i8] c"FAIL (%s)\0A\00"
@.str.8 = [9 x i8] c"res != 0\00"
@F_UNLCK = global i32 0
@.str.9 = [8 x i8] c"F_UNLCK\00"
@str = [8 x i8] c"SUCCEED\00"
define i32 @test12(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 1
%16 = bitcast i64* %15 to i8*
%17 = load i32, i32* @F_WRLCK
%18 = getelementptr inbounds %struct.flock, %struct.flock* %5, i64 0, i32 0
store i32 %17, i32* %18
%19 = load i32, i32* @SEEK_SET
%20 = getelementptr inbounds %struct.flock, %struct.flock* %5, i64 0, i32 3
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 @sleep(i32 1)
%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 ([56 x i8], [56 x i8]* @.str.6, i64 0, i64 0))
%51 = load i32, i32* @F_SETLKW
%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 %64, label %62
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 %74
64: ; preds = %49
%65 = bitcast i64* %15 to i8*
%66 = load i32, i32* @F_UNLCK
store i32 %66, i32* %18
%67 = load i32, i32* @F_SETLK
%68 = call i32 @fcntl(i32 %0, i32 %67, %struct.flock* %5)
%69 = icmp slt i32 %68, 0
br i1 %69, label %70, label %72
70: ; preds = %64
%71 = call i32 @err(i32 1, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.9, i64 0, i64 0))
br label %72
72: ; preds = %64, %70
%73 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @str, i64 0, i64 0))
br label %74
74: ; preds = %72, %62
%75 = phi i32 [ -1, %62 ], [ 0, %72 ]
ret i32 %75
}