#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 */
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
scalar_t__ errno;
int fileno(int /*<<< orphan*/ *);
int flock(int, int);
int /*<<< orphan*/ perror(char *);
bool file_lock(FILE *fp, int operation) {
  errno = 0;
  int fd = fileno(fp);
  if (fd == -1) {
    perror("fileno");
    return false;
  }
  if (flock(fd, operation) == -1) {
    perror("can't lock file; flock");
    return false;
  }
  return true;
}


x86 assembly (clang, -O0):
<file_lock>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    0x2e9a(%rip),%rax
movq   $0x0,(%rax)
mov    -0x10(%rbp),%rdi
call   1030 <fileno@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    117b <file_lock+0x4b>
lea    0xe96(%rip),%rdi
call   1040 <perror@plt>
movl   $0x0,-0x4(%rbp)
jmp    11ae <file_lock+0x7e>
mov    -0x18(%rbp),%edi
mov    -0x14(%rbp),%esi
call   1050 <flock@plt>
cmp    $0xffffffff,%eax
jne    11a7 <file_lock+0x77>
lea    0xe71(%rip),%rdi
call   1040 <perror@plt>
movl   $0x0,-0x4(%rbp)
jmp    11ae <file_lock+0x7e>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<file_lock>:
push   %rbx
mov    %esi,%ebx
mov    0x2ea6(%rip),%rax
movq   $0x0,(%rax)
call   1030 <fileno@plt>
cmp    $0xffffffff,%eax
je     1162 <file_lock+0x32>
mov    %eax,%edi
mov    %ebx,%esi
call   1050 <flock@plt>
mov    %eax,%ecx
mov    $0x1,%eax
cmp    $0xffffffff,%ecx
je     116b <file_lock+0x3b>
pop    %rbx
ret
lea    0xe97(%rip),%rdi
jmp    1172 <file_lock+0x42>
lea    0xe95(%rip),%rdi
call   1040 <perror@plt>
xor    %eax,%eax
pop    %rbx
ret

LLVM IR (-O0):
@errno = global i64 0
@.str = [7 x i8] c"fileno\00"
@.str.1 = [23 x i8] c"can't lock file; flock\00"
define i32 @file_lock(i32* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i32
%6 = alloca i32
store i32* %0, i32** %4
store i32 %1, i32* %5
store i64 0, i64* @errno
%7 = load i32*, i32** %4
%8 = call i32 @fileno(i32* %7)
store i32 %8, i32* %6
%9 = load i32, i32* %6
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %13
11: ; preds = %2
%12 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %3
br label %21
13: ; preds = %2
%14 = load i32, i32* %6
%15 = load i32, i32* %5
%16 = call i32 @flock(i32 %14, i32 %15)
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %20
18: ; preds = %13
%19 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0))
store i32 0, i32* %3
br label %21
20: ; preds = %13
store i32 1, i32* %3
br label %21
21: ; preds = %20, %18, %11
%22 = load i32, i32* %3
ret i32 %22
}

LLVM IR (-O2):
@errno = global i64 0
@.str = [7 x i8] c"fileno\00"
@.str.1 = [23 x i8] c"can't lock file; flock\00"
define i32 @file_lock(i32* nocapture %0, i32 %1) {
store i64 0, i64* @errno
%3 = tail call i32 @fileno(i32* %0)
%4 = icmp eq i32 %3, -1
br i1 %4, label %8, label %5
5: ; preds = %2
%6 = tail call i32 @flock(i32 %3, i32 %1)
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %11
8: ; preds = %5, %2
%9 = phi i8* [ getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), %2 ], [ getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), %5 ]
%10 = tail call i32 @perror(i8* %9)
br label %11
11: ; preds = %8, %5
%12 = phi i32 [ 1, %5 ], [ 0, %8 ]
ret i32 %12
}