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

/* Variables and functions */
int /*<<< orphan*/ F_RDLCK;
int F_SETLK;
int F_SETLKW;
int /*<<< orphan*/ F_UNLCK;
int /*<<< orphan*/ F_WRLCK;
int /*<<< orphan*/ SEEK_SET;
int fcntl(int, int, struct flock *);
int /*<<< orphan*/ getpid();
int /*<<< orphan*/ memset(struct flock *, int /*<<< orphan*/, int);
int pgut_flock(int fd, int operation) {
  struct flock lck;
  int cmd;
  memset(&lck, 0, sizeof(lck));
  lck.l_whence = SEEK_SET;
  lck.l_start = 0;
  lck.l_len = 0;
  lck.l_pid = getpid();
  if (operation & 8)
    lck.l_type = F_UNLCK;
  else if (operation & 2)
    lck.l_type = F_WRLCK;
  else
    lck.l_type = F_RDLCK;
  if (operation & 4)
    cmd = F_SETLK;
  else
    cmd = F_SETLKW;
  return fcntl(fd, cmd, &lck);
}


x86 assembly (clang, -O0):
<pgut_flock>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
lea    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1050 <memset@plt>
mov    0x2e73(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,-0x24(%rbp)
mov    -0x8(%rbp),%eax
and    $0x8,%eax
cmp    $0x0,%eax
je     1194 <pgut_flock+0x64>
mov    0x2e4e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
jmp    11c5 <pgut_flock+0x95>
mov    -0x8(%rbp),%eax
and    $0x2,%eax
cmp    $0x0,%eax
je     11b4 <pgut_flock+0x84>
mov    0x2e16(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
jmp    11c0 <pgut_flock+0x90>
mov    0x2df5(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
jmp    11c5 <pgut_flock+0x95>
mov    -0x8(%rbp),%eax
and    $0x4,%eax
cmp    $0x0,%eax
je     11e5 <pgut_flock+0xb5>
mov    0x2e05(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
jmp    11f1 <pgut_flock+0xc1>
mov    0x2dfc(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x2c(%rbp),%esi
lea    -0x28(%rbp),%rdx
call   1030 <fcntl@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pgut_flock>:
push   %rbp
push   %rbx
sub    $0x28,%rsp
mov    %esi,%ebp
mov    %edi,%ebx
lea    0x8(%rsp),%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1050 <memset@plt>
mov    0x2e76(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x20(%rsp)
xorps  %xmm0,%xmm0
movups %xmm0,0x10(%rsp)
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %eax,0xc(%rsp)
test   $0x8,%bpl
jne    1180 <pgut_flock+0x50>
test   $0x2,%bpl
jne    1189 <pgut_flock+0x59>
mov    0x2e32(%rip),%rax
jmp    1190 <pgut_flock+0x60>
mov    0x2e51(%rip),%rax
jmp    1190 <pgut_flock+0x60>
mov    0x2e30(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
test   $0x4,%bpl
jne    11a5 <pgut_flock+0x75>
mov    0x2e45(%rip),%rax
jmp    11ac <pgut_flock+0x7c>
mov    0x2e34(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
mov    %ebx,%edi
call   1030 <fcntl@plt>
add    $0x28,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.flock = type { i32, i32, i64, i64, i32 }
@SEEK_SET = global i32 0
@F_UNLCK = global i32 0
@F_WRLCK = global i32 0
@F_RDLCK = global i32 0
@F_SETLK = global i32 0
@F_SETLKW = global i32 0
define i32 @pgut_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 = call i32 @memset(%struct.flock* %5, i32 0, i32 32)
%8 = load i32, i32* @SEEK_SET
%9 = getelementptr inbounds %struct.flock, %struct.flock* %5, i32 0, i32 4
store i32 %8, i32* %9
%10 = getelementptr inbounds %struct.flock, %struct.flock* %5, i32 0, i32 3
store i64 0, i64* %10
%11 = getelementptr inbounds %struct.flock, %struct.flock* %5, i32 0, i32 2
store i64 0, i64* %11
%12 = call i32 (...) @getpid()
%13 = getelementptr inbounds %struct.flock, %struct.flock* %5, i32 0, i32 1
store i32 %12, i32* %13
%14 = load i32, i32* %4
%15 = and i32 %14, 8
%16 = icmp ne i32 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %2
%18 = load i32, i32* @F_UNLCK
%19 = getelementptr inbounds %struct.flock, %struct.flock* %5, i32 0, i32 0
store i32 %18, i32* %19
br label %31
20: ; preds = %2
%21 = load i32, i32* %4
%22 = and i32 %21, 2
%23 = icmp ne i32 %22, 0
br i1 %23, label %24, label %27
24: ; preds = %20
%25 = load i32, i32* @F_WRLCK
%26 = getelementptr inbounds %struct.flock, %struct.flock* %5, i32 0, i32 0
store i32 %25, i32* %26
br label %30
27: ; preds = %20
%28 = load i32, i32* @F_RDLCK
%29 = getelementptr inbounds %struct.flock, %struct.flock* %5, i32 0, i32 0
store i32 %28, i32* %29
br label %30
30: ; preds = %27, %24
br label %31
31: ; preds = %30, %17
%32 = load i32, i32* %4
%33 = and i32 %32, 4
%34 = icmp ne i32 %33, 0
br i1 %34, label %35, label %37
35: ; preds = %31
%36 = load i32, i32* @F_SETLK
store i32 %36, i32* %6
br label %39
37: ; preds = %31
%38 = load i32, i32* @F_SETLKW
store i32 %38, i32* %6
br label %39
39: ; preds = %37, %35
%40 = load i32, i32* %3
%41 = load i32, i32* %6
%42 = call i32 @fcntl(i32 %40, i32 %41, %struct.flock* %5)
ret i32 %42
}

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