#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 l_type;
  int /*<<< orphan*/ l_pid;
};

/* Variables and functions */
scalar_t__ EAGAIN;
scalar_t__ EWOULDBLOCK;
int /*<<< orphan*/ F_SETLK;
int /*<<< orphan*/ F_SETLKW;
int LOCK_NB;
scalar_t__ errno;
int fcntl(int, int /*<<< orphan*/, struct flock *);
int /*<<< orphan*/ getpid();
int /*<<< orphan*/ memset(void *, int /*<<< orphan*/, int);
int flock(int fd, int mode) {
  struct flock lck;
  register int retcode;
  memset((void *)&lck, 0, sizeof(struct flock));
  lck.l_type = mode & ~LOCK_NB;
  lck.l_pid = getpid();
  if ((retcode = fcntl(fd, mode & LOCK_NB ? F_SETLK : F_SETLKW, &lck)) < 0 &&
      errno == EAGAIN)
    errno = EWOULDBLOCK;
  return retcode;
}


x86 assembly (clang, -O0):
<flock>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
lea    -0x10(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1050 <memset@plt>
mov    -0x8(%rbp),%eax
mov    0x2e58(%rip),%rcx
mov    (%rcx),%ecx
xor    $0xffffffff,%ecx
and    %ecx,%eax
mov    %eax,-0x10(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,-0xc(%rbp)
mov    -0x4(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%eax
mov    0x2e34(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     1198 <flock+0x68>
mov    0x2e52(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
jmp    11a4 <flock+0x74>
mov    0x2e49(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%edi
mov    -0x1c(%rbp),%esi
lea    -0x10(%rbp),%rdx
call   1030 <fcntl@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    11ed <flock+0xbd>
mov    0x2e0a(%rip),%rax
mov    (%rax),%rax
mov    0x2de8(%rip),%rcx
cmp    (%rcx),%rax
jne    11ed <flock+0xbd>
mov    0x2de8(%rip),%rax
mov    (%rax),%rcx
mov    0x2de6(%rip),%rax
mov    %rcx,(%rax)
mov    -0x14(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<flock>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %esi,%ebp
mov    %edi,%r15d
mov    %rsp,%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1050 <memset@plt>
mov    0x2e5b(%rip),%rbx
mov    (%rbx),%eax
not    %eax
and    %ebp,%eax
mov    %eax,(%rsp)
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %eax,0x4(%rsp)
and    (%rbx),%ebp
jne    1176 <flock+0x46>
mov    0x2e74(%rip),%rax
jmp    117d <flock+0x4d>
mov    0x2e63(%rip),%rax
mov    (%rax),%esi
mov    %r15d,%edi
mov    %r14,%rdx
call   1030 <fcntl@plt>
test   %eax,%eax
jns    11b1 <flock+0x81>
mov    0x2e3b(%rip),%rcx
mov    (%rcx),%rdx
mov    0x2e19(%rip),%rsi
cmp    (%rsi),%rdx
jne    11b1 <flock+0x81>
mov    0x2e1d(%rip),%rdx
mov    (%rdx),%rdx
mov    %rdx,(%rcx)
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.flock = type { i32, i32 }
@LOCK_NB = global i32 0
@F_SETLK = global i32 0
@F_SETLKW = global i32 0
@errno = global i64 0
@EAGAIN = global i64 0
@EWOULDBLOCK = global i64 0
define i32 @flock(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.flock
%6 = alloca i32
store i32 %0, i32* %3
store i32 %1, i32* %4
%7 = bitcast %struct.flock* %5 to i8*
%8 = call i32 @memset(i8* %7, i32 0, i32 8)
%9 = load i32, i32* %4
%10 = load i32, i32* @LOCK_NB
%11 = xor i32 %10, -1
%12 = and i32 %9, %11
%13 = getelementptr inbounds %struct.flock, %struct.flock* %5, i32 0, i32 0
store i32 %12, i32* %13
%14 = call i32 (...) @getpid()
%15 = getelementptr inbounds %struct.flock, %struct.flock* %5, i32 0, i32 1
store i32 %14, i32* %15
%16 = load i32, i32* %3
%17 = load i32, i32* %4
%18 = load i32, i32* @LOCK_NB
%19 = and i32 %17, %18
%20 = icmp ne i32 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %2
%22 = load i32, i32* @F_SETLK
br label %25
23: ; preds = %2
%24 = load i32, i32* @F_SETLKW
br label %25
25: ; preds = %23, %21
%26 = phi i32 [ %22, %21 ], [ %24, %23 ]
%27 = call i32 @fcntl(i32 %16, i32 %26, %struct.flock* %5)
store i32 %27, i32* %6
%28 = icmp slt i32 %27, 0
br i1 %28, label %29, label %35
29: ; preds = %25
%30 = load i64, i64* @errno
%31 = load i64, i64* @EAGAIN
%32 = icmp eq i64 %30, %31
br i1 %32, label %33, label %35
33: ; preds = %29
%34 = load i64, i64* @EWOULDBLOCK
store i64 %34, i64* @errno
br label %35
35: ; preds = %33, %29, %25
%36 = load i32, i32* %6
ret i32 %36
}

LLVM IR (-O2):
%struct.flock = type { i32, i32 }
@LOCK_NB = global i32 0
@F_SETLK = global i32 0
@F_SETLKW = global i32 0
@errno = global i64 0
@EAGAIN = global i64 0
@EWOULDBLOCK = global i64 0
define i32 @flock(i32 %0, i32 %1) {
%3 = alloca %struct.flock
%4 = bitcast %struct.flock* %3 to i8*
%5 = call i32 @memset(i8* %4, i32 0, i32 8)
%6 = load i32, i32* @LOCK_NB
%7 = xor i32 %6, -1
%8 = and i32 %7, %1
%9 = getelementptr inbounds %struct.flock, %struct.flock* %3, i64 0, i32 0
store i32 %8, i32* %9
%10 = call i32 (...) @getpid()
%11 = getelementptr inbounds %struct.flock, %struct.flock* %3, i64 0, i32 1
store i32 %10, i32* %11
%12 = load i32, i32* @LOCK_NB
%13 = and i32 %12, %1
%14 = icmp eq i32 %13, 0
%15 = load i32, i32* @F_SETLK
%16 = load i32, i32* @F_SETLKW
%17 = select i1 %14, i32 %16, i32 %15
%18 = call i32 @fcntl(i32 %0, i32 %17, %struct.flock* %3)
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %26
20: ; preds = %2
%21 = load i64, i64* @errno
%22 = load i64, i64* @EAGAIN
%23 = icmp eq i64 %21, %22
br i1 %23, label %24, label %26
24: ; preds = %20
%25 = load i64, i64* @EWOULDBLOCK
store i64 %25, i64* @errno
br label %26
26: ; preds = %24, %20, %2
ret i32 %18
}