#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__ EINTR;
int /*<<< orphan*/ F_SETLK;
int /*<<< orphan*/ F_SETLKW;
int /*<<< orphan*/ F_WRLCK;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ SIGTERM;
int /*<<< orphan*/ alarm(int);
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 test3(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)");
    pause();
    exit(0);
  }
  if (read(pfd[0], &ch, 1) != 1)
    err(1, "reading from pipe (child)");
  printf("3 - F_SETLKW on locked region: ");
  alarm(1);
  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);
  do {
    if (errno != EINTR) {
      printf("FAIL (%s)\n", "errno != EINTR");
      return -1;
    }
  } while (0);
  do {
    printf("SUCCEED\n");
    return 0;
  } while (0);
}


x86 assembly (clang, -O0):
<test3>:
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   1090 <pipe@plt>
cmp    $0x0,%rax
jge    1206 <test3+0x36>
mov    $0x1,%edi
lea    0xdff(%rip),%rsi
call   10d0 <err@plt>
movq   $0x0,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
mov    0x2d93(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
mov    0x2d97(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x40(%rbp)
mov    $0x0,%al
call   10f0 <fork@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    1253 <test3+0x83>
mov    $0x1,%edi
lea    0xdb7(%rip),%rsi
call   10d0 <err@plt>
cmpl   $0x0,-0x1c(%rbp)
jne    12c9 <test3+0xf9>
mov    -0x8(%rbp),%edi
mov    0x2d71(%rip),%rax
mov    (%rax),%esi
lea    -0x40(%rbp),%rdx
call   1040 <fcntl@plt>
cmp    $0x0,%eax
jge    128c <test3+0xbc>
mov    $0x1,%edi
lea    0xd83(%rip),%rsi
call   10d0 <err@plt>
mov    -0x20(%rbp),%edi
lea    0xd84(%rip),%rsi
mov    $0x1,%edx
call   1050 <write@plt>
cmp    $0x0,%rax
jge    12bb <test3+0xeb>
mov    $0x1,%edi
lea    0xd66(%rip),%rsi
call   10d0 <err@plt>
mov    $0x0,%al
call   10c0 <pause@plt>
xor    %edi,%edi
call   10e0 <exit@plt>
mov    -0x24(%rbp),%edi
lea    -0x41(%rbp),%rsi
mov    $0x1,%edx
call   10a0 <read@plt>
cmp    $0x1,%eax
je     12f4 <test3+0x124>
mov    $0x1,%edi
lea    0xd45(%rip),%rsi
call   10d0 <err@plt>
lea    0xd53(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0x1,%edi
call   1070 <alarm@plt>
mov    -0x8(%rbp),%edi
mov    0x2cca(%rip),%rax
mov    (%rax),%esi
lea    -0x40(%rbp),%rdx
call   1040 <fcntl@plt>
mov    %eax,-0x48(%rbp)
mov    -0x1c(%rbp),%edi
mov    0x2cba(%rip),%rax
mov    (%rax),%esi
call   10b0 <kill@plt>
mov    -0x1c(%rbp),%edi
call   1030 <safe_waitpid@plt>
mov    -0x24(%rbp),%edi
call   1080 <close@plt>
mov    -0x20(%rbp),%edi
call   1080 <close@plt>
cmpl   $0x0,-0x48(%rbp)
jne    1378 <test3+0x1a8>
lea    0xd10(%rip),%rdi
lea    0xd14(%rip),%rsi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13dc <test3+0x20c>
jmp    137d <test3+0x1ad>
jmp    1382 <test3+0x1b2>
mov    0x2c3f(%rip),%rax
mov    (%rax),%rax
mov    0x2c25(%rip),%rcx
cmp    (%rcx),%rax
je     13bd <test3+0x1ed>
lea    0xccb(%rip),%rdi
lea    0xcd8(%rip),%rsi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13dc <test3+0x20c>
jmp    13c2 <test3+0x1f2>
jmp    13c7 <test3+0x1f7>
lea    0xcc3(%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):
<test3>:
push   %rbp
push   %rbx
sub    $0x28,%rsp
mov    %edi,%ebx
lea    0x8(%rsp),%rdi
call   10a0 <pipe@plt>
test   %rax,%rax
jns    1208 <test3+0x28>
lea    0xe02(%rip),%rsi
mov    $0x1,%edi
call   10e0 <err@plt>
xorps  %xmm0,%xmm0
movups %xmm0,0x18(%rsp)
mov    0x2d99(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x14(%rsp)
mov    0x2d9c(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
xor    %eax,%eax
call   1100 <fork@plt>
mov    %eax,%ebp
test   %eax,%eax
js     129c <test3+0xbc>
jne    12ad <test3+0xcd>
mov    0x2d98(%rip),%rax
mov    (%rax),%esi
lea    0x10(%rsp),%rdx
mov    %ebx,%edi
call   1050 <fcntl@plt>
test   %eax,%eax
jns    1263 <test3+0x83>
lea    0xdb1(%rip),%rsi
mov    $0x1,%edi
call   10e0 <err@plt>
mov    0xc(%rsp),%edi
lea    0xdac(%rip),%rsi
mov    $0x1,%edx
call   1060 <write@plt>
test   %rax,%rax
jns    128e <test3+0xae>
lea    0xd98(%rip),%rsi
mov    $0x1,%edi
call   10e0 <err@plt>
xor    %eax,%eax
call   10d0 <pause@plt>
xor    %edi,%edi
call   10f0 <exit@plt>
lea    0xd62(%rip),%rsi
mov    $0x1,%edi
call   10e0 <err@plt>
mov    0x8(%rsp),%edi
lea    0x7(%rsp),%rsi
mov    $0x1,%edx
call   10b0 <read@plt>
cmp    $0x1,%eax
je     12d6 <test3+0xf6>
lea    0xd68(%rip),%rsi
mov    $0x1,%edi
call   10e0 <err@plt>
lea    0xd71(%rip),%rdi
xor    %eax,%eax
call   1070 <printf@plt>
mov    $0x1,%edi
call   1080 <alarm@plt>
mov    0x2ceb(%rip),%rax
mov    (%rax),%esi
lea    0x10(%rsp),%rdx
mov    %ebx,%edi
call   1050 <fcntl@plt>
mov    %eax,%ebx
mov    0x2cdc(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
call   10c0 <kill@plt>
mov    %ebp,%edi
call   1040 <safe_waitpid@plt>
mov    0x8(%rsp),%edi
call   1090 <close@plt>
mov    0xc(%rsp),%edi
call   1090 <close@plt>
test   %ebx,%ebx
je     1358 <test3+0x178>
mov    0x2c8f(%rip),%rax
mov    (%rax),%rax
mov    0x2c75(%rip),%rcx
cmp    (%rcx),%rax
jne    1368 <test3+0x188>
lea    0xd42(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
jmp    1382 <test3+0x1a2>
lea    0xd0f(%rip),%rdi
lea    0xd13(%rip),%rsi
jmp    1376 <test3+0x196>
lea    0xcff(%rip),%rdi
lea    0xd0c(%rip),%rsi
xor    %eax,%eax
call   1070 <printf@plt>
mov    $0xffffffff,%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 = [32 x i8] c"3 - F_SETLKW on locked region: \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"
@errno = global i64 0
@EINTR = global i64 0
@.str.9 = [15 x i8] c"errno != EINTR\00"
@.str.10 = [9 x i8] c"SUCCEED\0A\00"
define i32 @test3(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 ([32 x i8], [32 x i8]* @.str.6, i64 0, i64 0))
%59 = call i32 @alarm(i32 1)
%60 = load i32, i32* %5
%61 = load i32, i32* @F_SETLKW
%62 = call i32 @fcntl(i32 %60, i32 %61, %struct.flock* %10)
store i32 %62, i32* %12
%63 = load i32, i32* %8
%64 = load i32, i32* @SIGTERM
%65 = call i32 @kill(i32 %63, i32 %64)
%66 = load i32, i32* %8
%67 = call i32 @safe_waitpid(i32 %66)
%68 = getelementptr inbounds [2 x i32], [2 x i32]* %9, i64 0, i64 0
%69 = load i32, i32* %68
%70 = call i32 @close(i32 %69)
%71 = getelementptr inbounds [2 x i32], [2 x i32]* %9, i64 0, i64 1
%72 = load i32, i32* %71
%73 = call i32 @close(i32 %72)
br label %74
74: ; preds = %57
%75 = load i32, i32* %12
%76 = icmp eq i32 %75, 0
br i1 %76, label %77, label %79
77: ; preds = %74
%78 = 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 %91
79: ; preds = %74
br label %80
80: ; preds = %79
br label %81
81: ; preds = %80
%82 = load i64, i64* @errno
%83 = load i64, i64* @EINTR
%84 = icmp ne i64 %82, %83
br i1 %84, label %85, label %87
85: ; preds = %81
%86 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.7, i64 0, i64 0), i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.9, i64 0, i64 0))
store i32 -1, i32* %4
br label %91
87: ; preds = %81
br label %88
88: ; preds = %87
br label %89
89: ; preds = %88
%90 = 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 %91
91: ; preds = %77, %85, %89
%92 = load i32, i32* %4
ret i32 %92
}

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 = [32 x i8] c"3 - F_SETLKW on locked region: \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"
@errno = global i64 0
@EINTR = global i64 0
@.str.9 = [15 x i8] c"errno != EINTR\00"
@str = [8 x i8] c"SUCCEED\00"
define i32 @test3(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 ([32 x i8], [32 x i8]* @.str.6, i64 0, i64 0))
%51 = call i32 @alarm(i32 1)
%52 = load i32, i32* @F_SETLKW
%53 = call i32 @fcntl(i32 %0, i32 %52, %struct.flock* %5)
%54 = load i32, i32* @SIGTERM
%55 = call i32 @kill(i32 %21, i32 %54)
%56 = call i32 @safe_waitpid(i32 %21)
%57 = load i32, i32* %9
%58 = call i32 @close(i32 %57)
%59 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 1
%60 = load i32, i32* %59
%61 = call i32 @close(i32 %60)
%62 = icmp eq i32 %53, 0
br i1 %62, label %63, label %65
63: ; preds = %49
%64 = 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 %73
65: ; preds = %49
%66 = load i64, i64* @errno
%67 = load i64, i64* @EINTR
%68 = icmp eq i64 %66, %67
br i1 %68, label %71, label %69
69: ; preds = %65
%70 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.7, i64 0, i64 0), i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.9, i64 0, i64 0))
br label %73
71: ; preds = %65
%72 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @str, i64 0, i64 0))
br label %73
73: ; preds = %71, %69, %63
%74 = phi i32 [ -1, %63 ], [ -1, %69 ], [ 0, %71 ]
ret i32 %74
}