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

/* Variables and functions */
int /*<<< orphan*/ F_SETLKW;
int /*<<< orphan*/ F_WRLCK;
int /*<<< orphan*/ SEEK_SET;
scalar_t__ fcntl(int, int /*<<< orphan*/, struct flock *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int lock_file(int fd) {
  struct flock lock;
  lock.l_type = F_WRLCK;
  lock.l_start = 0;
  lock.l_whence = SEEK_SET;
  lock.l_len = 0;
  printf("Try to lock file...\n");
  if (fcntl(fd, F_SETLKW, &lock) < 0) {
    perror("fcntl error");
    return -1;
  }
  printf("File lock succeeded\n");
  return 0;
}


x86 assembly (clang, -O0):
<lock_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    0x2e8e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
mov    0x2e82(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
lea    0xe96(%rip),%rdi
call   1040 <printf@plt>
mov    -0x8(%rbp),%edi
mov    0x2e6f(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdx
call   1030 <fcntl@plt>
cmp    $0x0,%rax
jge    11a6 <lock_file+0x76>
lea    0xe80(%rip),%rdi
call   1050 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11b9 <lock_file+0x89>
lea    0xe74(%rip),%rdi
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<lock_file>:
push   %rbx
sub    $0x20,%rsp
mov    %edi,%ebx
mov    0x2e92(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x18(%rsp)
movq   $0x0,0x10(%rsp)
mov    0x2e84(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
movq   $0x0,(%rsp)
lea    0xea3(%rip),%rdi
call   1030 <puts@plt>
mov    0x2e73(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
mov    %ebx,%edi
call   1040 <fcntl@plt>
test   %rax,%rax
js     119a <lock_file+0x6a>
lea    0xe93(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbx
ret
lea    0xe5f(%rip),%rdi
call   1050 <perror@plt>
mov    $0xffffffff,%eax
jmp    1194 <lock_file+0x64>

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

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