#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;
  scalar_t__ l_len;
  scalar_t__ l_start;
  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_start = lock.l_len = 0;
  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    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    0x2e83(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x18(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,-0x24(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0x4(%rbp),%edi
mov    0x2e64(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdx
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
jne    11ae <lock_operation+0x6e>
lea    0xe61(%rip),%rdi
call   1050 <perror@plt>
mov    $0x6,%edi
call   1060 <exit@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<lock_operation>:
push   %rbp
push   %rbx
sub    $0x28,%rsp
mov    %esi,%ebx
mov    %edi,%ebp
mov    0x2e87(%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)
mov    %ebx,0x8(%rsp)
mov    0x2e73(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
mov    %ebp,%edi
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
je     118f <lock_operation+0x4f>
add    $0x28,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xe6a(%rip),%rdi
call   1050 <perror@plt>
mov    $0x6,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
%struct.flock = type { i32, i32, i64, i64, 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 4
store i32 %6, i32* %7
%8 = getelementptr inbounds %struct.flock, %struct.flock* %5, i32 0, i32 2
store i64 0, i64* %8
%9 = getelementptr inbounds %struct.flock, %struct.flock* %5, i32 0, i32 3
store i64 0, i64* %9
%10 = call i32 (...) @getpid()
%11 = getelementptr inbounds %struct.flock, %struct.flock* %5, i32 0, i32 1
store i32 %10, i32* %11
%12 = load i32, i32* %4
%13 = getelementptr inbounds %struct.flock, %struct.flock* %5, i32 0, i32 0
store i32 %12, i32* %13
%14 = load i32, i32* %3
%15 = load i32, i32* @F_SETLKW
%16 = call i32 @fcntl(i32 %14, i32 %15, %struct.flock* %5)
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %21
18: ; preds = %2
%19 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%20 = call i32 @exit(i32 6)
unreachable
21: ; preds = %2
ret void
}

LLVM IR (-O2):
%struct.flock = type { i32, i32, i64, i64, 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 4
store i32 %5, i32* %6
%7 = getelementptr inbounds %struct.flock, %struct.flock* %3, i64 0, i32 2
%8 = bitcast i64* %7 to i8*
%9 = tail call i32 (...) @getpid()
%10 = getelementptr inbounds %struct.flock, %struct.flock* %3, i64 0, i32 1
store i32 %9, i32* %10
%11 = getelementptr inbounds %struct.flock, %struct.flock* %3, i64 0, i32 0
store i32 %1, i32* %11
%12 = load i32, i32* @F_SETLKW
%13 = call i32 @fcntl(i32 %0, i32 %12, %struct.flock* %3)
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %18
15: ; preds = %2
%16 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%17 = call i32 @exit(i32 6)
unreachable
18: ; preds = %2
ret void
}