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

/* Variables and functions */
int /*<<< orphan*/ F_SETLKW;
int /*<<< orphan*/ SEEK_SET;
scalar_t__ fcntl(int, int /*<<< orphan*/, struct flock *);
int /*<<< orphan*/ perror(char *);
int lock_file(int fd, int type) {
  struct flock st_flock = {0};
  st_flock.l_type = type;
  st_flock.l_whence = SEEK_SET;
  if (fcntl(fd, F_SETLKW, &st_flock) < 0) {
    perror("fcntl");
    goto err;
  }
  return 0;
err:
  return -1;
}


x86 assembly (clang, -O0):
<lock_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1040 <memset@plt>
mov    -0xc(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    0x2e7d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e7e(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rdx
call   1030 <fcntl@plt>
cmp    $0x0,%rax
jge    1190 <lock_file+0x60>
lea    0xe7a(%rip),%rdi
call   1050 <perror@plt>
jmp    119c <lock_file+0x6c>
movl   $0x0,-0x4(%rbp)
jmp    11a3 <lock_file+0x73>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<lock_file>:
sub    $0x18,%rsp
movl   $0x0,0x10(%rsp)
mov    %esi,0x8(%rsp)
mov    0x2ea1(%rip),%rax
mov    (%rax),%eax
mov    %eax,0xc(%rsp)
mov    0x2ea4(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
call   1030 <fcntl@plt>
mov    %rax,%rcx
xor    %eax,%eax
test   %rcx,%rcx
js     115f <lock_file+0x3f>
add    $0x18,%rsp
ret
lea    0xe9a(%rip),%rdi
call   1040 <perror@plt>
mov    $0xffffffff,%eax
add    $0x18,%rsp
ret

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

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