#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 */

/* Variables and functions */
int /*<<< orphan*/ LOCK_EX;
int O_CREAT;
int O_WRONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ err(char *, char *);
scalar_t__ flock(int, int /*<<< orphan*/);
int open(char *, int, int);
int lock_file(char *name) {
  int lfd;
  if ((lfd = open(name, O_WRONLY | O_CREAT, 0600)) < 0) {
    err("*open: '%s'", name);
    return -1;
  }
  if (flock(lfd, LOCK_EX) < 0) {
    close(lfd);
    err("*flock: '%s'", name);
    return -1;
  }
  return lfd;
}


x86 assembly (clang, -O0):
<lock_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e71(%rip),%rax
mov    (%rax),%esi
mov    0x2e98(%rip),%rax
or     (%rax),%esi
mov    $0x180,%edx
call   1050 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    1194 <lock_file+0x54>
mov    -0x10(%rbp),%rsi
lea    0xe7d(%rip),%rdi
call   1040 <err@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d9 <lock_file+0x99>
mov    -0x14(%rbp),%edi
mov    0x2e42(%rip),%rax
mov    (%rax),%esi
call   1060 <flock@plt>
cmp    $0x0,%rax
jge    11d3 <lock_file+0x93>
mov    -0x14(%rbp),%edi
call   1030 <close@plt>
mov    -0x10(%rbp),%rsi
lea    0xe4a(%rip),%rdi
call   1040 <err@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d9 <lock_file+0x99>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<lock_file>:
push   %rbp
push   %rbx
push   %rax
mov    %rdi,%rbx
mov    0x2e7b(%rip),%rax
mov    0x2ea4(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    $0x180,%edx
call   1050 <open@plt>
test   %eax,%eax
js     118d <lock_file+0x4d>
mov    %eax,%ebp
mov    0x2e71(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
call   1060 <flock@plt>
test   %rax,%rax
jns    11a1 <lock_file+0x61>
mov    %ebp,%edi
call   1030 <close@plt>
lea    0xe81(%rip),%rdi
jmp    1194 <lock_file+0x54>
lea    0xe6c(%rip),%rdi
mov    %rbx,%rsi
call   1040 <err@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@.str = [12 x i8] c"*open: '%s'\00"
@LOCK_EX = global i32 0
@.str.1 = [13 x i8] c"*flock: '%s'\00"
define i32 @lock_file(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
store i8* %0, i8** %3
%5 = load i8*, i8** %3
%6 = load i32, i32* @O_WRONLY
%7 = load i32, i32* @O_CREAT
%8 = or i32 %6, %7
%9 = call i32 @open(i8* %5, i32 %8, i32 384)
store i32 %9, i32* %4
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %14
11: ; preds = %1
%12 = load i8*, i8** %3
%13 = call i32 @err(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8* %12)
store i32 -1, i32* %2
br label %26
14: ; preds = %1
%15 = load i32, i32* %4
%16 = load i32, i32* @LOCK_EX
%17 = call i64 @flock(i32 %15, i32 %16)
%18 = icmp slt i64 %17, 0
br i1 %18, label %19, label %24
19: ; preds = %14
%20 = load i32, i32* %4
%21 = call i32 @close(i32 %20)
%22 = load i8*, i8** %3
%23 = call i32 @err(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i8* %22)
store i32 -1, i32* %2
br label %26
24: ; preds = %14
%25 = load i32, i32* %4
store i32 %25, i32* %2
br label %26
26: ; preds = %24, %19, %11
%27 = load i32, i32* %2
ret i32 %27
}

LLVM IR (-O2):
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@.str = [12 x i8] c"*open: '%s'\00"
@LOCK_EX = global i32 0
@.str.1 = [13 x i8] c"*flock: '%s'\00"
define i32 @lock_file(i8* %0) {
%2 = load i32, i32* @O_WRONLY
%3 = load i32, i32* @O_CREAT
%4 = or i32 %3, %2
%5 = tail call i32 @open(i8* %0, i32 %4, i32 384)
%6 = icmp slt i32 %5, 0
br i1 %6, label %13, label %7
7: ; preds = %1
%8 = load i32, i32* @LOCK_EX
%9 = tail call i64 @flock(i32 %5, i32 %8)
%10 = icmp slt i64 %9, 0
br i1 %10, label %11, label %16
11: ; preds = %7
%12 = tail call i32 @close(i32 %5)
br label %13
13: ; preds = %1, %11
%14 = phi i8* [ getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), %11 ], [ getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), %1 ]
%15 = tail call i32 @err(i8* %14, i8* %0)
br label %16
16: ; preds = %13, %7
%17 = phi i32 [ %5, %7 ], [ -1, %13 ]
ret i32 %17
}