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

/* Variables and functions */
int EACCES;
int EAGAIN;
int /*<<< orphan*/ F_GETLK;
int /*<<< orphan*/ F_SETLK;
scalar_t__ F_UNLCK;
scalar_t__ F_WRLCK;
int /*<<< orphan*/ SEEK_SET;
int errno;
int fcntl(int, int /*<<< orphan*/, struct flock *);
int /*<<< orphan*/ printf(char *, ...);
char *strerror(int);
int child(int fd) {
  struct flock fl = {
      .l_type = F_WRLCK, .l_whence = SEEK_SET, .l_start = 1, .l_len = 99};
  if (fcntl(fd, F_GETLK, &fl) == -1) {
    printf("Could not get lock: %s (%d)\n", strerror(errno), errno);
    return 2;
  }
  if (fl.l_type == F_UNLCK) {
    printf("Child found lock is F_UNLCK, should be F_WRLCK\n");
    return 1;
  }
  if (fcntl(fd, F_SETLK, &fl) == -1) {
    if (errno == EACCES || errno == EAGAIN) {
      printf("PASSED: Child locked file already locked by parent\n");
      return 0;
    }
    printf("Unexpected fcntl error: %s (%d)\n", strerror(errno), errno);
    return 2;
  }
  printf("Child did not inherit the lock\n");
  return 1;
}


x86 assembly (clang, -O0):
<child>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    0x2e6e(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x20(%rbp)
movl   $0x1,-0x18(%rbp)
movl   $0x63,-0x14(%rbp)
mov    0x2e5a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e73(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
jne    11b5 <child+0x85>
mov    0x2e38(%rip),%rax
mov    (%rax),%edi
call   1050 <strerror@plt>
mov    %rax,%rsi
mov    0x2e27(%rip),%rax
mov    (%rax),%edx
lea    0xe5e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x2,-0x4(%rbp)
jmp    1294 <child+0x164>
mov    -0x20(%rbp),%rax
mov    0x2e18(%rip),%rcx
cmp    (%rcx),%rax
jne    11e3 <child+0xb3>
lea    0xe4d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1294 <child+0x164>
mov    -0x8(%rbp),%edi
mov    0x2dfb(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
jne    127f <child+0x14f>
mov    0x2db8(%rip),%rax
mov    (%rax),%eax
mov    0x2dbf(%rip),%rcx
cmp    (%rcx),%eax
je     1231 <child+0x101>
mov    0x2da0(%rip),%rax
mov    (%rax),%eax
mov    0x2d77(%rip),%rcx
cmp    (%rcx),%eax
jne    124b <child+0x11b>
lea    0xe15(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1294 <child+0x164>
mov    0x2d6e(%rip),%rax
mov    (%rax),%edi
call   1050 <strerror@plt>
mov    %rax,%rsi
mov    0x2d5d(%rip),%rax
mov    (%rax),%edx
lea    0xe15(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x2,-0x4(%rbp)
jmp    1294 <child+0x164>
lea    0xe1c(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<child>:
push   %rbx
sub    $0x20,%rsp
mov    %edi,%ebx
mov    0x2e62(%rip),%rax
mov    (%rax),%rax
mov    %rax,0x8(%rsp)
movabs $0x6300000001,%rax
mov    %rax,0x10(%rsp)
mov    0x2e4c(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x18(%rsp)
mov    0x2e67(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
call   1040 <fcntl@plt>
cmp    $0xffffffff,%eax
je     11a4 <child+0x64>
mov    0x8(%rsp),%rax
mov    0x2e42(%rip),%rcx
cmp    (%rcx),%rax
jne    11cc <child+0x8c>
lea    0xeee(%rip),%rdi
jmp    11ed <child+0xad>
mov    0x2e15(%rip),%rbx
mov    (%rbx),%edi
call   1060 <strerror@plt>
mov    (%rbx),%edx
lea    0xe45(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x2,%eax
jmp    11f7 <child+0xb7>
mov    0x2e15(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
mov    %ebx,%edi
call   1040 <fcntl@plt>
cmp    $0xffffffff,%eax
je     11fd <child+0xbd>
lea    0xe51(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%eax
add    $0x20,%rsp
pop    %rbx
ret
mov    0x2dbc(%rip),%rbx
mov    (%rbx),%edi
mov    0x2dc3(%rip),%rax
cmp    (%rax),%edi
je     122c <child+0xec>
mov    0x2d88(%rip),%rax
cmp    (%rax),%edi
je     122c <child+0xec>
call   1060 <strerror@plt>
mov    (%rbx),%edx
lea    0xdf3(%rip),%rdi
jmp    11bb <child+0x7b>
lea    0xe2a(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
jmp    11f7 <child+0xb7>

LLVM IR (-O0):
%struct.flock = type { i64, i32, i32, i32 }
@F_WRLCK = global i64 0
@SEEK_SET = global i32 0
@F_GETLK = global i32 0
@.str = [29 x i8] c"Could not get lock: %s (%d)\0A\00"
@errno = global i32 0
@F_UNLCK = global i64 0
@.str.1 = [48 x i8] c"Child found lock is F_UNLCK, should be F_WRLCK\0A\00"
@F_SETLK = global i32 0
@EACCES = global i32 0
@EAGAIN = global i32 0
@.str.2 = [52 x i8] c"PASSED: Child locked file already locked by parent\0A\00"
@.str.3 = [33 x i8] c"Unexpected fcntl error: %s (%d)\0A\00"
@.str.4 = [32 x i8] c"Child did not inherit the lock\0A\00"
define i32 @child(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.flock
store i32 %0, i32* %3
%5 = getelementptr inbounds %struct.flock, %struct.flock* %4, i32 0, i32 0
%6 = load i64, i64* @F_WRLCK
store i64 %6, i64* %5
%7 = getelementptr inbounds %struct.flock, %struct.flock* %4, i32 0, i32 1
store i32 1, i32* %7
%8 = getelementptr inbounds %struct.flock, %struct.flock* %4, i32 0, i32 2
store i32 99, i32* %8
%9 = getelementptr inbounds %struct.flock, %struct.flock* %4, i32 0, i32 3
%10 = load i32, i32* @SEEK_SET
store i32 %10, i32* %9
%11 = load i32, i32* %3
%12 = load i32, i32* @F_GETLK
%13 = call i32 @fcntl(i32 %11, i32 %12, %struct.flock* %4)
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %20
15: ; preds = %1
%16 = load i32, i32* @errno
%17 = call i8* @strerror(i32 %16)
%18 = load i32, i32* @errno
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i8* %17, i32 %18)
store i32 2, i32* %2
br label %49
20: ; preds = %1
%21 = getelementptr inbounds %struct.flock, %struct.flock* %4, i32 0, i32 0
%22 = load i64, i64* %21
%23 = load i64, i64* @F_UNLCK
%24 = icmp eq i64 %22, %23
br i1 %24, label %25, label %27
25: ; preds = %20
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %2
br label %49
27: ; preds = %20
%28 = load i32, i32* %3
%29 = load i32, i32* @F_SETLK
%30 = call i32 @fcntl(i32 %28, i32 %29, %struct.flock* %4)
%31 = icmp eq i32 %30, -1
br i1 %31, label %32, label %47
32: ; preds = %27
%33 = load i32, i32* @errno
%34 = load i32, i32* @EACCES
%35 = icmp eq i32 %33, %34
br i1 %35, label %40, label %36
36: ; preds = %32
%37 = load i32, i32* @errno
%38 = load i32, i32* @EAGAIN
%39 = icmp eq i32 %37, %38
br i1 %39, label %40, label %42
40: ; preds = %36, %32
%41 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str.2, i64 0, i64 0))
store i32 0, i32* %2
br label %49
42: ; preds = %36
%43 = load i32, i32* @errno
%44 = call i8* @strerror(i32 %43)
%45 = load i32, i32* @errno
%46 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.3, i64 0, i64 0), i8* %44, i32 %45)
store i32 2, i32* %2
br label %49
47: ; preds = %27
%48 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.4, i64 0, i64 0))
store i32 1, i32* %2
br label %49
49: ; preds = %47, %42, %40, %25, %15
%50 = load i32, i32* %2
ret i32 %50
}

LLVM IR (-O2):
%struct.flock = type { i64, i32, i32, i32 }
@F_WRLCK = global i64 0
@SEEK_SET = global i32 0
@F_GETLK = global i32 0
@.str = [29 x i8] c"Could not get lock: %s (%d)\0A\00"
@errno = global i32 0
@F_UNLCK = global i64 0
@F_SETLK = global i32 0
@EACCES = global i32 0
@EAGAIN = global i32 0
@.str.3 = [33 x i8] c"Unexpected fcntl error: %s (%d)\0A\00"
@str = [31 x i8] c"Child did not inherit the lock\00"
@str.5 = [51 x i8] c"PASSED: Child locked file already locked by parent\00"
@str.6 = [47 x i8] c"Child found lock is F_UNLCK, should be F_WRLCK\00"
define i32 @child(i32 %0) {
%2 = alloca %struct.flock
%3 = bitcast %struct.flock* %2 to i8*
%4 = getelementptr inbounds %struct.flock, %struct.flock* %2, i64 0, i32 0
%5 = load i64, i64* @F_WRLCK
store i64 %5, i64* %4
%6 = getelementptr inbounds %struct.flock, %struct.flock* %2, i64 0, i32 1
store i32 1, i32* %6
%7 = getelementptr inbounds %struct.flock, %struct.flock* %2, i64 0, i32 2
store i32 99, i32* %7
%8 = getelementptr inbounds %struct.flock, %struct.flock* %2, i64 0, i32 3
%9 = load i32, i32* @SEEK_SET
store i32 %9, i32* %8
%10 = load i32, i32* @F_GETLK
%11 = call i32 @fcntl(i32 %0, i32 %10, %struct.flock* %2)
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %18
13: ; preds = %1
%14 = load i32, i32* @errno
%15 = call i8* @strerror(i32 %14)
%16 = load i32, i32* @errno
%17 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i8* %15, i32 %16)
br label %43
18: ; preds = %1
%19 = load i64, i64* %4
%20 = load i64, i64* @F_UNLCK
%21 = icmp eq i64 %19, %20
br i1 %21, label %22, label %24
22: ; preds = %18
%23 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([47 x i8], [47 x i8]* @str.6, i64 0, i64 0))
br label %43
24: ; preds = %18
%25 = load i32, i32* @F_SETLK
%26 = call i32 @fcntl(i32 %0, i32 %25, %struct.flock* %2)
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %41
28: ; preds = %24
%29 = load i32, i32* @errno
%30 = load i32, i32* @EACCES
%31 = icmp eq i32 %29, %30
%32 = load i32, i32* @EAGAIN
%33 = icmp eq i32 %29, %32
%34 = select i1 %31, i1 true, i1 %33
br i1 %34, label %35, label %37
35: ; preds = %28
%36 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([51 x i8], [51 x i8]* @str.5, i64 0, i64 0))
br label %43
37: ; preds = %28
%38 = call i8* @strerror(i32 %29)
%39 = load i32, i32* @errno
%40 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @.str.3, i64 0, i64 0), i8* %38, i32 %39)
br label %43
41: ; preds = %24
%42 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @str, i64 0, i64 0))
br label %43
43: ; preds = %41, %37, %35, %22, %13
%44 = phi i32 [ 2, %13 ], [ 1, %22 ], [ 0, %35 ], [ 2, %37 ], [ 1, %41 ]
ret i32 %44
}