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

/* Variables and functions */
int /*<<< orphan*/ F_GETLK;
scalar_t__ F_UNLCK;
scalar_t__ F_WRLCK;
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ errno;
int fcntl(int, int /*<<< orphan*/, struct flock *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int getppid();
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int run_child(char *filename) {
  int fd = -1, ret = -1;
  struct flock lock = {
      0,
  };
  int ppid = 0;
  fd = open(filename, O_RDWR);
  if (fd < 0) {
    fprintf(stderr, "open failed (%s)\n", strerror(errno));
    goto out;
  }
  ppid = getppid();
  lock.l_type = F_WRLCK;
  lock.l_whence = SEEK_SET;
  lock.l_start = 0;
  lock.l_len = 0;
  ret = fcntl(fd, F_GETLK, &lock);
  if (ret < 0) {
    fprintf(stderr, "GETLK failed (%s)\n", strerror(errno));
    goto out;
  }
  if ((lock.l_type == F_UNLCK) || (ppid != lock.l_pid)) {
    fprintf(stderr, "no locks present, though parent has held "
                    "one\n");
    ret = -1;
    goto out;
  }
  ret = 0;
out:
  return ret;
}


x86 assembly (clang, -O0):
<run_child>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0xffffffff,-0xc(%rbp)
movl   $0xffffffff,-0x10(%rbp)
lea    -0x38(%rbp),%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1040 <memset@plt>
movl   $0x0,-0x3c(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2e4c(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    11e3 <run_child+0x83>
mov    0x2e11(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x40(%rbp)
mov    0x2dfd(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x40(%rbp),%edi
mov    %rax,%rdx
lea    0xe29(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
jmp    12b6 <run_child+0x156>
mov    $0x0,%al
call   1070 <getppid@plt>
mov    %eax,-0x3c(%rbp)
mov    0x2dbc(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x38(%rbp)
mov    0x2db6(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0xc(%rbp),%edi
mov    0x2dbf(%rip),%rax
mov    (%rax),%esi
lea    -0x38(%rbp),%rdx
call   1030 <fcntl@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jge    126c <run_child+0x10c>
mov    0x2d88(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x44(%rbp)
mov    0x2d74(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x44(%rbp),%edi
mov    %rax,%rdx
lea    0xdb2(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
jmp    12b6 <run_child+0x156>
mov    -0x38(%rbp),%rax
mov    0x2d61(%rip),%rcx
cmp    (%rcx),%rax
je     128c <run_child+0x12c>
mov    -0x3c(%rbp),%eax
cmp    -0x30(%rbp),%eax
je     12af <run_child+0x14f>
mov    0x2d35(%rip),%rax
mov    (%rax),%edi
lea    0xd89(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0xffffffff,-0x10(%rbp)
jmp    12b6 <run_child+0x156>
movl   $0x0,-0x10(%rbp)
mov    -0x10(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<run_child>:
push   %rbp
push   %r14
push   %rbx
sub    $0x30,%rsp
xorps  %xmm0,%xmm0
movups %xmm0,0x20(%rsp)
movups %xmm0,0x10(%rsp)
mov    0x2e7c(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
test   %eax,%eax
js     11f3 <run_child+0xa3>
mov    %eax,%ebp
xor    %ebx,%ebx
xor    %eax,%eax
call   1060 <getppid@plt>
mov    %eax,%r14d
mov    0x2e24(%rip),%rax
mov    (%rax),%rax
mov    %rax,0x8(%rsp)
mov    0x2e1d(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x28(%rsp)
xorps  %xmm0,%xmm0
movups %xmm0,0x18(%rsp)
mov    0x2e30(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
mov    %ebp,%edi
call   1030 <fcntl@plt>
test   %eax,%eax
js     1224 <run_child+0xd4>
mov    0x8(%rsp),%rax
mov    0x2e0a(%rip),%rcx
cmp    (%rcx),%rax
je     11da <run_child+0x8a>
cmp    0x10(%rsp),%r14d
je     1252 <run_child+0x102>
mov    0x2de7(%rip),%rax
mov    (%rax),%edi
lea    0xe4b(%rip),%rsi
xor    %eax,%eax
call   1040 <fprintf@plt>
jmp    121d <run_child+0xcd>
mov    0x2dce(%rip),%rax
mov    (%rax),%ebx
mov    0x2dbd(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
lea    0xdff(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    $0xffffffff,%ebx
jmp    1252 <run_child+0x102>
mov    %eax,%ebp
mov    0x2d9b(%rip),%rax
mov    (%rax),%ebx
mov    0x2d8a(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
lea    0xdde(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    %ebp,%ebx
mov    %ebx,%eax
add    $0x30,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.flock = type { i64, i32, i64, i64, i32, i32 }
@O_RDWR = global i32 0
@stderr = global i32 0
@.str = [18 x i8] c"open failed (%s)\0A\00"
@errno = global i32 0
@F_WRLCK = global i64 0
@SEEK_SET = global i32 0
@F_GETLK = global i32 0
@.str.1 = [19 x i8] c"GETLK failed (%s)\0A\00"
@F_UNLCK = global i64 0
@.str.2 = [46 x i8] c"no locks present, though parent has held one\0A\00"
define i32 @run_child(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.flock
%6 = alloca i32
store i8* %0, i8** %2
store i32 -1, i32* %3
store i32 -1, i32* %4
%7 = bitcast %struct.flock* %5 to i8*
store i32 0, i32* %6
%8 = load i8*, i8** %2
%9 = load i32, i32* @O_RDWR
%10 = call i32 @open(i8* %8, i32 %9)
store i32 %10, i32* %3
%11 = load i32, i32* %3
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %18
13: ; preds = %1
%14 = load i32, i32* @stderr
%15 = load i32, i32* @errno
%16 = call i8* @strerror(i32 %15)
%17 = call i32 (i32, i8*, ...) @fprintf(i32 %14, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %16)
br label %50
18: ; preds = %1
%19 = call i32 (...) @getppid()
store i32 %19, i32* %6
%20 = load i64, i64* @F_WRLCK
%21 = getelementptr inbounds %struct.flock, %struct.flock* %5, i32 0, i32 0
store i64 %20, i64* %21
%22 = load i32, i32* @SEEK_SET
%23 = getelementptr inbounds %struct.flock, %struct.flock* %5, i32 0, i32 4
store i32 %22, i32* %23
%24 = getelementptr inbounds %struct.flock, %struct.flock* %5, i32 0, i32 3
store i64 0, i64* %24
%25 = getelementptr inbounds %struct.flock, %struct.flock* %5, i32 0, i32 2
store i64 0, i64* %25
%26 = load i32, i32* %3
%27 = load i32, i32* @F_GETLK
%28 = call i32 @fcntl(i32 %26, i32 %27, %struct.flock* %5)
store i32 %28, i32* %4
%29 = load i32, i32* %4
%30 = icmp slt i32 %29, 0
br i1 %30, label %31, label %36
31: ; preds = %18
%32 = load i32, i32* @stderr
%33 = load i32, i32* @errno
%34 = call i8* @strerror(i32 %33)
%35 = call i32 (i32, i8*, ...) @fprintf(i32 %32, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8* %34)
br label %50
36: ; preds = %18
%37 = getelementptr inbounds %struct.flock, %struct.flock* %5, i32 0, i32 0
%38 = load i64, i64* %37
%39 = load i64, i64* @F_UNLCK
%40 = icmp eq i64 %38, %39
br i1 %40, label %46, label %41
41: ; preds = %36
%42 = load i32, i32* %6
%43 = getelementptr inbounds %struct.flock, %struct.flock* %5, i32 0, i32 1
%44 = load i32, i32* %43
%45 = icmp ne i32 %42, %44
br i1 %45, label %46, label %49
46: ; preds = %41, %36
%47 = load i32, i32* @stderr
%48 = call i32 (i32, i8*, ...) @fprintf(i32 %47, i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %4
br label %50
49: ; preds = %41
store i32 0, i32* %4
br label %50
50: ; preds = %49, %46, %31, %13
%51 = load i32, i32* %4
ret i32 %51
}

LLVM IR (-O2):
%struct.flock = type { i64, i32, i64, i64, i32, i32 }
@O_RDWR = global i32 0
@stderr = global i32 0
@.str = [18 x i8] c"open failed (%s)\0A\00"
@errno = global i32 0
@F_WRLCK = global i64 0
@SEEK_SET = global i32 0
@F_GETLK = global i32 0
@.str.1 = [19 x i8] c"GETLK failed (%s)\0A\00"
@F_UNLCK = global i64 0
@.str.2 = [46 x i8] c"no locks present, though parent has held one\0A\00"
define i32 @run_child(i8* nocapture %0) {
%2 = alloca %struct.flock
%3 = bitcast %struct.flock* %2 to i8*
%4 = getelementptr inbounds %struct.flock, %struct.flock* %2, i64 0, i32 1
%5 = bitcast i32* %4 to i8*
%6 = load i32, i32* @O_RDWR
%7 = tail call i32 @open(i8* %0, i32 %6)
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %14
9: ; preds = %1
%10 = load i32, i32* @stderr
%11 = load i32, i32* @errno
%12 = tail call i8* @strerror(i32 %11)
%13 = tail call i32 (i32, i8*, ...) @fprintf(i32 %10, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %12)
br label %41
14: ; preds = %1
%15 = tail call i32 (...) @getppid()
%16 = load i64, i64* @F_WRLCK
%17 = getelementptr inbounds %struct.flock, %struct.flock* %2, i64 0, i32 0
store i64 %16, i64* %17
%18 = load i32, i32* @SEEK_SET
%19 = getelementptr inbounds %struct.flock, %struct.flock* %2, i64 0, i32 4
store i32 %18, i32* %19
%20 = getelementptr inbounds %struct.flock, %struct.flock* %2, i64 0, i32 2
%21 = bitcast i64* %20 to i8*
%22 = load i32, i32* @F_GETLK
%23 = call i32 @fcntl(i32 %7, i32 %22, %struct.flock* %2)
%24 = icmp slt i32 %23, 0
br i1 %24, label %25, label %30
25: ; preds = %14
%26 = load i32, i32* @stderr
%27 = load i32, i32* @errno
%28 = call i8* @strerror(i32 %27)
%29 = call i32 (i32, i8*, ...) @fprintf(i32 %26, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8* %28)
br label %41
30: ; preds = %14
%31 = load i64, i64* %17
%32 = load i64, i64* @F_UNLCK
%33 = icmp ne i64 %31, %32
%34 = getelementptr inbounds %struct.flock, %struct.flock* %2, i64 0, i32 1
%35 = load i32, i32* %34
%36 = icmp eq i32 %15, %35
%37 = select i1 %33, i1 %36, i1 false
br i1 %37, label %41, label %38
38: ; preds = %30
%39 = load i32, i32* @stderr
%40 = call i32 (i32, i8*, ...) @fprintf(i32 %39, i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.2, i64 0, i64 0))
br label %41
41: ; preds = %30, %38, %25, %9
%42 = phi i32 [ -1, %9 ], [ %23, %25 ], [ -1, %38 ], [ 0, %30 ]
ret i32 %42
}