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

/* Variables and functions */
int /*<<< orphan*/ F_SETLKW;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ exit(int);
int fcntl(int, int /*<<< orphan*/, struct flock *);
int /*<<< orphan*/ getpid();
int /*<<< orphan*/ perror(char *);
void lock_operation(int fd, int op) {
  struct flock lock;
  lock.l_whence = SEEK_SET;
  lock.l_pid = getpid();
  lock.l_type = op;
  if (fcntl(fd, F_SETLKW, &lock) == -1) {
    perror("lock operation");
    exit(6);
  };
}


x86 assembly (clang, -O0):
<lock_operation>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    0x2e83(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x4(%rbp),%edi
mov    0x2e74(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rdx
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
jne    119e <lock_operation+0x5e>
lea    0xe71(%rip),%rdi
call   1050 <perror@plt>
mov    $0x6,%edi
call   1060 <exit@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<lock_operation>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
mov    %esi,%ebx
mov    %edi,%ebp
mov    0x2e87(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %eax,0xc(%rsp)
mov    %ebx,0x8(%rsp)
mov    0x2e7b(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
mov    %ebp,%edi
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
je     1187 <lock_operation+0x47>
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xe72(%rip),%rdi
call   1050 <perror@plt>
mov    $0x6,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
%struct.flock = type { i32, i32, i32 }
@SEEK_SET = global i32 0
@F_SETLKW = global i32 0
@.str = [15 x i8] c"lock operation\00"
define void @lock_operation(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.flock
store i32 %0, i32* %3
store i32 %1, i32* %4
%6 = load i32, i32* @SEEK_SET
%7 = getelementptr inbounds %struct.flock, %struct.flock* %5, i32 0, i32 2
store i32 %6, i32* %7
%8 = call i32 (...) @getpid()
%9 = getelementptr inbounds %struct.flock, %struct.flock* %5, i32 0, i32 1
store i32 %8, i32* %9
%10 = load i32, i32* %4
%11 = getelementptr inbounds %struct.flock, %struct.flock* %5, i32 0, i32 0
store i32 %10, i32* %11
%12 = load i32, i32* %3
%13 = load i32, i32* @F_SETLKW
%14 = call i32 @fcntl(i32 %12, i32 %13, %struct.flock* %5)
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %19
16: ; preds = %2
%17 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%18 = call i32 @exit(i32 6)
unreachable
19: ; preds = %2
ret void
}

LLVM IR (-O2):
%struct.flock = type { i32, i32, i32 }
@SEEK_SET = global i32 0
@F_SETLKW = global i32 0
@.str = [15 x i8] c"lock operation\00"
define void @lock_operation(i32 %0, i32 %1) {
%3 = alloca %struct.flock
%4 = bitcast %struct.flock* %3 to i8*
%5 = load i32, i32* @SEEK_SET
%6 = getelementptr inbounds %struct.flock, %struct.flock* %3, i64 0, i32 2
store i32 %5, i32* %6
%7 = tail call i32 (...) @getpid()
%8 = getelementptr inbounds %struct.flock, %struct.flock* %3, i64 0, i32 1
store i32 %7, i32* %8
%9 = getelementptr inbounds %struct.flock, %struct.flock* %3, i64 0, i32 0
store i32 %1, i32* %9
%10 = load i32, i32* @F_SETLKW
%11 = call i32 @fcntl(i32 %0, i32 %10, %struct.flock* %3)
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %16
13: ; preds = %2
%14 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%15 = call i32 @exit(i32 6)
unreachable
16: ; preds = %2
ret void
}