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

/* Variables and functions */
int /*<<< orphan*/ F_GETLK;
int /*<<< orphan*/ F_SETLK;
scalar_t__ F_UNLCK;
void *F_WRLCK;
int O_CREAT;
int /*<<< orphan*/ O_RDONLY;
int O_RDWR;
int O_TRUNC;
int /*<<< orphan*/ exit(int);
int fcntl(int, int /*<<< orphan*/, struct flock *);
int /*<<< orphan*/ fork();
void *getpid();
int open(char *, int, ...);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ sys_waitpid(int /*<<< orphan*/, int *, int /*<<< orphan*/);
int /*<<< orphan*/ unlink(char *);
int main(int argc, char *argv[]) {
  struct flock lock;
  int fd, ret, status = 1;
  pid_t pid;
  if (!(pid = fork())) {
    sleep(2);
    fd = open("conftest.fcntl", O_RDONLY);
    if (fd == -1)
      exit(1);
    lock.l_type = F_WRLCK;
    lock.l_whence = 0;
    lock.l_start = 0;
    lock.l_len = 4;
    lock.l_pid = getpid();
    lock.l_type = F_WRLCK;
    ret = fcntl(fd, F_GETLK, &lock);
    if ((ret == -1) || (lock.l_type == F_UNLCK)) {
      exit(1);
    } else {
      exit(0);
    }
  }
  fd = open("conftest.fcntl", O_RDWR | O_CREAT | O_TRUNC, 0600);
  lock.l_type = F_WRLCK;
  lock.l_whence = 0;
  lock.l_start = 0;
  lock.l_len = 4;
  lock.l_pid = getpid();
  fcntl(fd, F_SETLK, &lock);
  sys_waitpid(pid, &status, 0);
  unlink("conftest.fcntl");
  status = (status == 0) ? 0 : 1;
  exit(status);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x1,-0x44(%rbp)
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %eax,-0x48(%rbp)
cmp    $0x0,%eax
jne    126d <main+0xed>
mov    $0x2,%edi
call   1090 <sleep@plt>
mov    0x2e0f(%rip),%rax
mov    (%rax),%esi
lea    0xe36(%rip),%rdi
mov    $0x0,%al
call   1060 <open@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0xffffffff,-0x3c(%rbp)
jne    11e8 <main+0x68>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    0x2db9(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x38(%rbp)
movq   $0x0,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
movl   $0x4,-0x30(%rbp)
mov    $0x0,%al
call   1050 <getpid@plt>
mov    %rax,-0x28(%rbp)
mov    0x2d89(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x38(%rbp)
mov    -0x3c(%rbp),%edi
mov    0x2d90(%rip),%rax
mov    (%rax),%esi
lea    -0x38(%rbp),%rdx
call   1040 <fcntl@plt>
mov    %eax,-0x40(%rbp)
cmpl   $0xffffffff,-0x40(%rbp)
je     125c <main+0xdc>
mov    -0x38(%rbp),%rax
mov    0x2d65(%rip),%rcx
cmp    (%rcx),%rax
jne    1266 <main+0xe6>
mov    $0x1,%edi
call   1080 <exit@plt>
xor    %edi,%edi
call   1080 <exit@plt>
mov    0x2d64(%rip),%rax
mov    (%rax),%esi
mov    0x2d7b(%rip),%rax
or     (%rax),%esi
mov    0x2d6a(%rip),%rax
or     (%rax),%esi
lea    0xd71(%rip),%rdi
mov    $0x180,%edx
mov    $0x0,%al
call   1060 <open@plt>
mov    %eax,-0x3c(%rbp)
mov    0x2d03(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x38(%rbp)
movq   $0x0,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
movl   $0x4,-0x30(%rbp)
mov    $0x0,%al
call   1050 <getpid@plt>
mov    %rax,-0x28(%rbp)
mov    -0x3c(%rbp),%edi
mov    0x2cf0(%rip),%rax
mov    (%rax),%esi
lea    -0x38(%rbp),%rdx
call   1040 <fcntl@plt>
mov    -0x48(%rbp),%edi
lea    -0x44(%rbp),%rsi
xor    %edx,%edx
call   1070 <sys_waitpid@plt>
lea    0xd08(%rip),%rdi
call   1030 <unlink@plt>
mov    -0x44(%rbp),%edx
mov    $0x1,%eax
xor    %ecx,%ecx
cmp    $0x0,%edx
cmove  %ecx,%eax
mov    %eax,-0x44(%rbp)
mov    -0x44(%rbp),%edi
call   1080 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x30,%rsp
movl   $0x1,0x4(%rsp)
xor    %eax,%eax
call   10a0 <fork@plt>
test   %eax,%eax
jne    122b <main+0xab>
mov    $0x2,%edi
call   1090 <sleep@plt>
mov    0x2e20(%rip),%rax
mov    (%rax),%esi
lea    0xe47(%rip),%rdi
xor    %eax,%eax
call   1060 <open@plt>
cmp    $0xffffffff,%eax
je     1221 <main+0xa1>
mov    %eax,%ebx
xorps  %xmm0,%xmm0
movups %xmm0,0x20(%rsp)
movl   $0x4,0x10(%rsp)
xor    %eax,%eax
call   1050 <getpid@plt>
mov    %rax,0x18(%rsp)
mov    0x2dbe(%rip),%rax
mov    (%rax),%rax
mov    %rax,0x8(%rsp)
mov    0x2dc7(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
mov    %ebx,%edi
call   1040 <fcntl@plt>
cmp    $0xffffffff,%eax
je     1221 <main+0xa1>
mov    0x8(%rsp),%rax
mov    0x2da0(%rip),%rcx
cmp    (%rcx),%rax
jne    12ca <main+0x14a>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    %eax,%ebx
mov    0x2da4(%rip),%rax
mov    0x2dbd(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2daa(%rip),%rax
or     (%rax),%esi
lea    0xdb1(%rip),%r14
mov    %r14,%rdi
mov    $0x180,%edx
xor    %eax,%eax
call   1060 <open@plt>
mov    %eax,%ebp
mov    0x2d41(%rip),%rax
mov    (%rax),%rax
mov    %rax,0x8(%rsp)
xorps  %xmm0,%xmm0
movups %xmm0,0x20(%rsp)
movl   $0x4,0x10(%rsp)
xor    %eax,%eax
call   1050 <getpid@plt>
mov    %rax,0x18(%rsp)
mov    0x2d36(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
mov    %ebp,%edi
call   1040 <fcntl@plt>
lea    0x4(%rsp),%rsi
mov    %ebx,%edi
xor    %edx,%edx
call   1070 <sys_waitpid@plt>
mov    %r14,%rdi
call   1030 <unlink@plt>
xor    %edi,%edi
cmpl   $0x0,0x4(%rsp)
setne  %dil
mov    %edi,0x4(%rsp)
call   1080 <exit@plt>
xor    %edi,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
%struct.flock = type { i64, i32, i8*, i64, i64 }
@.str = [15 x i8] c"conftest.fcntl\00"
@O_RDONLY = global i32 0
@F_WRLCK = global i8* null
@F_GETLK = global i32 0
@F_UNLCK = global i64 0
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@F_SETLK = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca %struct.flock
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 1, i32* %9
%11 = call i32 (...) @fork()
store i32 %11, i32* %10
%12 = icmp ne i32 %11, 0
br i1 %12, label %47, label %13
13: ; preds = %2
%14 = call i32 @sleep(i32 2)
%15 = load i32, i32* @O_RDONLY
%16 = call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %15)
store i32 %16, i32* %7
%17 = load i32, i32* %7
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %21
19: ; preds = %13
%20 = call i32 @exit(i32 1)
unreachable
21: ; preds = %13
%22 = load i8*, i8** @F_WRLCK
%23 = ptrtoint i8* %22 to i64
%24 = getelementptr inbounds %struct.flock, %struct.flock* %6, i32 0, i32 0
store i64 %23, i64* %24
%25 = getelementptr inbounds %struct.flock, %struct.flock* %6, i32 0, i32 4
store i64 0, i64* %25
%26 = getelementptr inbounds %struct.flock, %struct.flock* %6, i32 0, i32 3
store i64 0, i64* %26
%27 = getelementptr inbounds %struct.flock, %struct.flock* %6, i32 0, i32 1
store i32 4, i32* %27
%28 = call i8* (...) @getpid()
%29 = getelementptr inbounds %struct.flock, %struct.flock* %6, i32 0, i32 2
store i8* %28, i8** %29
%30 = load i8*, i8** @F_WRLCK
%31 = ptrtoint i8* %30 to i64
%32 = getelementptr inbounds %struct.flock, %struct.flock* %6, i32 0, i32 0
store i64 %31, i64* %32
%33 = load i32, i32* %7
%34 = load i32, i32* @F_GETLK
%35 = call i32 @fcntl(i32 %33, i32 %34, %struct.flock* %6)
store i32 %35, i32* %8
%36 = load i32, i32* %8
%37 = icmp eq i32 %36, -1
br i1 %37, label %43, label %38
38: ; preds = %21
%39 = getelementptr inbounds %struct.flock, %struct.flock* %6, i32 0, i32 0
%40 = load i64, i64* %39
%41 = load i64, i64* @F_UNLCK
%42 = icmp eq i64 %40, %41
br i1 %42, label %43, label %45
43: ; preds = %38, %21
%44 = call i32 @exit(i32 1)
unreachable
45: ; preds = %38
%46 = call i32 @exit(i32 0)
unreachable
47: ; preds = %2
%48 = load i32, i32* @O_RDWR
%49 = load i32, i32* @O_CREAT
%50 = or i32 %48, %49
%51 = load i32, i32* @O_TRUNC
%52 = or i32 %50, %51
%53 = call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %52, i32 384)
store i32 %53, i32* %7
%54 = load i8*, i8** @F_WRLCK
%55 = ptrtoint i8* %54 to i64
%56 = getelementptr inbounds %struct.flock, %struct.flock* %6, i32 0, i32 0
store i64 %55, i64* %56
%57 = getelementptr inbounds %struct.flock, %struct.flock* %6, i32 0, i32 4
store i64 0, i64* %57
%58 = getelementptr inbounds %struct.flock, %struct.flock* %6, i32 0, i32 3
store i64 0, i64* %58
%59 = getelementptr inbounds %struct.flock, %struct.flock* %6, i32 0, i32 1
store i32 4, i32* %59
%60 = call i8* (...) @getpid()
%61 = getelementptr inbounds %struct.flock, %struct.flock* %6, i32 0, i32 2
store i8* %60, i8** %61
%62 = load i32, i32* %7
%63 = load i32, i32* @F_SETLK
%64 = call i32 @fcntl(i32 %62, i32 %63, %struct.flock* %6)
%65 = load i32, i32* %10
%66 = call i32 @sys_waitpid(i32 %65, i32* %9, i32 0)
%67 = call i32 @unlink(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%68 = load i32, i32* %9
%69 = icmp eq i32 %68, 0
%70 = zext i1 %69 to i64
%71 = select i1 %69, i32 0, i32 1
store i32 %71, i32* %9
%72 = load i32, i32* %9
%73 = call i32 @exit(i32 %72)
unreachable
}

LLVM IR (-O2):
%struct.flock = type { i64, i32, i8*, i64, i64 }
@.str = [15 x i8] c"conftest.fcntl\00"
@O_RDONLY = global i32 0
@F_WRLCK = global i8* null
@F_GETLK = global i32 0
@F_UNLCK = global i64 0
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@F_SETLK = global i32 0
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca %struct.flock
%4 = alloca i32
%5 = bitcast %struct.flock* %3 to i8*
%6 = bitcast i32* %4 to i8*
store i32 1, i32* %4
%7 = tail call i32 (...) @fork()
%8 = icmp eq i32 %7, 0
br i1 %8, label %9, label %36
9: ; preds = %2
%10 = tail call i32 @sleep(i32 2)
%11 = load i32, i32* @O_RDONLY
%12 = tail call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %11)
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %16
14: ; preds = %9
%15 = tail call i32 @exit(i32 1)
unreachable
16: ; preds = %9
%17 = getelementptr inbounds %struct.flock, %struct.flock* %3, i64 0, i32 0
%18 = getelementptr inbounds %struct.flock, %struct.flock* %3, i64 0, i32 3
%19 = getelementptr inbounds %struct.flock, %struct.flock* %3, i64 0, i32 1
%20 = bitcast i64* %18 to i8*
store i32 4, i32* %19
%21 = tail call i8* (...) @getpid()
%22 = getelementptr inbounds %struct.flock, %struct.flock* %3, i64 0, i32 2
store i8* %21, i8** %22
%23 = load i8*, i8** @F_WRLCK
%24 = ptrtoint i8* %23 to i64
store i64 %24, i64* %17
%25 = load i32, i32* @F_GETLK
%26 = call i32 @fcntl(i32 %12, i32 %25, %struct.flock* %3)
%27 = icmp eq i32 %26, -1
br i1 %27, label %32, label %28
28: ; preds = %16
%29 = load i64, i64* %17
%30 = load i64, i64* @F_UNLCK
%31 = icmp eq i64 %29, %30
br i1 %31, label %32, label %34
32: ; preds = %28, %16
%33 = call i32 @exit(i32 1)
unreachable
34: ; preds = %28
%35 = call i32 @exit(i32 0)
unreachable
36: ; preds = %2
%37 = load i32, i32* @O_RDWR
%38 = load i32, i32* @O_CREAT
%39 = or i32 %38, %37
%40 = load i32, i32* @O_TRUNC
%41 = or i32 %39, %40
%42 = tail call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %41, i32 384)
%43 = load i8*, i8** @F_WRLCK
%44 = ptrtoint i8* %43 to i64
%45 = getelementptr inbounds %struct.flock, %struct.flock* %3, i64 0, i32 0
store i64 %44, i64* %45
%46 = getelementptr inbounds %struct.flock, %struct.flock* %3, i64 0, i32 3
%47 = getelementptr inbounds %struct.flock, %struct.flock* %3, i64 0, i32 1
%48 = bitcast i64* %46 to i8*
store i32 4, i32* %47
%49 = tail call i8* (...) @getpid()
%50 = getelementptr inbounds %struct.flock, %struct.flock* %3, i64 0, i32 2
store i8* %49, i8** %50
%51 = load i32, i32* @F_SETLK
%52 = call i32 @fcntl(i32 %42, i32 %51, %struct.flock* %3)
%53 = call i32 @sys_waitpid(i32 %7, i32* %4, i32 0)
%54 = call i32 @unlink(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%55 = load i32, i32* %4
%56 = icmp ne i32 %55, 0
%57 = zext i1 %56 to i32
store i32 %57, i32* %4
%58 = call i32 @exit(i32 %57)
unreachable
}