#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 */

/* Variables and functions */
scalar_t__ EWOULDBLOCK;
int LOCK_EX;
int LOCK_NB;
int /*<<< orphan*/ O_RDWR;
scalar_t__ errno;
int flock(int, int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
char *strerror(scalar_t__);
int run_child(char *filename) {
  int fd = -1, ret = -1;
  fd = open(filename, O_RDWR);
  if (fd < 0) {
    fprintf(stderr, "open failed (%s)\n", strerror(errno));
    goto out;
  }
  ret = flock(fd, LOCK_EX | LOCK_NB);
  if ((ret == 0) || (errno != EWOULDBLOCK)) {
    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    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0xffffffff,-0xc(%rbp)
movl   $0xffffffff,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2e83(%rip),%rax
mov    (%rax),%esi
call   1040 <open@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    11ad <run_child+0x6d>
mov    0x2e50(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    0x2e3c(%rip),%rax
mov    (%rax),%rdi
call   1060 <strerror@plt>
mov    -0x14(%rbp),%edi
mov    %rax,%rdx
lea    0xe5f(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
jmp    1218 <run_child+0xd8>
mov    -0xc(%rbp),%edi
mov    0x2e29(%rip),%rax
mov    (%rax),%esi
mov    0x2df0(%rip),%rax
or     (%rax),%esi
call   1050 <flock@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
je     11ee <run_child+0xae>
mov    0x2ded(%rip),%rax
mov    (%rax),%rax
mov    0x2ddb(%rip),%rcx
cmp    (%rcx),%rax
je     1211 <run_child+0xd1>
mov    0x2ddb(%rip),%rax
mov    (%rax),%edi
lea    0xe14(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
movl   $0xffffffff,-0x10(%rbp)
jmp    1218 <run_child+0xd8>
movl   $0x0,-0x10(%rbp)
mov    -0x10(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<run_child>:
push   %rbx
mov    0x2ea0(%rip),%rax
mov    (%rax),%esi
call   1040 <open@plt>
test   %eax,%eax
js     11a1 <run_child+0x61>
mov    0x2e86(%rip),%rcx
mov    0x2e4f(%rip),%rdx
mov    (%rdx),%esi
or     (%rcx),%esi
mov    %eax,%edi
call   1050 <flock@plt>
test   %eax,%eax
je     1188 <run_child+0x48>
mov    0x2e51(%rip),%rax
mov    (%rax),%rcx
mov    0x2e3f(%rip),%rdx
xor    %eax,%eax
cmp    (%rdx),%rcx
je     11d1 <run_child+0x91>
mov    0x2e41(%rip),%rax
mov    (%rax),%edi
lea    0xe7a(%rip),%rsi
xor    %eax,%eax
call   1030 <fprintf@plt>
jmp    11cc <run_child+0x8c>
mov    0x2e28(%rip),%rax
mov    (%rax),%ebx
mov    0x2e17(%rip),%rax
mov    (%rax),%rdi
call   1060 <strerror@plt>
lea    0xe40(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   1030 <fprintf@plt>
mov    $0xffffffff,%eax
pop    %rbx
ret

LLVM IR (-O0):
@O_RDWR = global i32 0
@stderr = global i32 0
@.str = [18 x i8] c"open failed (%s)\0A\00"
@errno = global i64 0
@LOCK_EX = global i32 0
@LOCK_NB = global i32 0
@EWOULDBLOCK = global i64 0
@.str.1 = [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
store i8* %0, i8** %2
store i32 -1, i32* %3
store i32 -1, i32* %4
%5 = load i8*, i8** %2
%6 = load i32, i32* @O_RDWR
%7 = call i32 @open(i8* %5, i32 %6)
store i32 %7, i32* %3
%8 = load i32, i32* %3
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %15
10: ; preds = %1
%11 = load i32, i32* @stderr
%12 = load i64, i64* @errno
%13 = call i8* @strerror(i64 %12)
%14 = call i32 (i32, i8*, ...) @fprintf(i32 %11, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %13)
br label %31
15: ; preds = %1
%16 = load i32, i32* %3
%17 = load i32, i32* @LOCK_EX
%18 = load i32, i32* @LOCK_NB
%19 = or i32 %17, %18
%20 = call i32 @flock(i32 %16, i32 %19)
store i32 %20, i32* %4
%21 = load i32, i32* %4
%22 = icmp eq i32 %21, 0
br i1 %22, label %27, label %23
23: ; preds = %15
%24 = load i64, i64* @errno
%25 = load i64, i64* @EWOULDBLOCK
%26 = icmp ne i64 %24, %25
br i1 %26, label %27, label %30
27: ; preds = %23, %15
%28 = load i32, i32* @stderr
%29 = call i32 (i32, i8*, ...) @fprintf(i32 %28, i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %4
br label %31
30: ; preds = %23
store i32 0, i32* %4
br label %31
31: ; preds = %30, %27, %10
%32 = load i32, i32* %4
ret i32 %32
}

LLVM IR (-O2):
@O_RDWR = global i32 0
@stderr = global i32 0
@.str = [18 x i8] c"open failed (%s)\0A\00"
@errno = global i64 0
@LOCK_EX = global i32 0
@LOCK_NB = global i32 0
@EWOULDBLOCK = global i64 0
@.str.1 = [46 x i8] c"no locks present, though parent has held one\0A\00"
define i32 @run_child(i8* nocapture %0) {
%2 = load i32, i32* @O_RDWR
%3 = tail call i32 @open(i8* %0, i32 %2)
%4 = icmp slt i32 %3, 0
br i1 %4, label %5, label %10
5: ; preds = %1
%6 = load i32, i32* @stderr
%7 = load i64, i64* @errno
%8 = tail call i8* @strerror(i64 %7)
%9 = tail call i32 (i32, i8*, ...) @fprintf(i32 %6, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %8)
br label %23
10: ; preds = %1
%11 = load i32, i32* @LOCK_EX
%12 = load i32, i32* @LOCK_NB
%13 = or i32 %12, %11
%14 = tail call i32 @flock(i32 %3, i32 %13)
%15 = icmp eq i32 %14, 0
br i1 %15, label %20, label %16
16: ; preds = %10
%17 = load i64, i64* @errno
%18 = load i64, i64* @EWOULDBLOCK
%19 = icmp eq i64 %17, %18
br i1 %19, label %23, label %20
20: ; preds = %16, %10
%21 = load i32, i32* @stderr
%22 = tail call i32 (i32, i8*, ...) @fprintf(i32 %21, i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.1, i64 0, i64 0))
br label %23
23: ; preds = %16, %20, %5
%24 = phi i32 [ -1, %5 ], [ -1, %20 ], [ 0, %16 ]
ret i32 %24
}