#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 */
scalar_t__ EEXIST;
int O_CREAT;
int O_EXCL;
int O_WRONLY;
int _gen_err(int);
int /*<<< orphan*/ close(int);
scalar_t__ errno;
int openat(int, char *, int, int);
int _lock(int dirfd, char *file) {
  int status = 0;
  int fd = openat(dirfd, file, O_CREAT | O_WRONLY | O_EXCL, 0644);
  if (fd < 0) {
    if (errno == EEXIST)
      status = _gen_err(-5);
    else
      status = _gen_err(-2);
  } else {
    close(fd);
  }
  return status;
}


x86 assembly (clang, -O0):
<_lock>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    0x2ea4(%rip),%rax
mov    (%rax),%edx
mov    0x2e5b(%rip),%rax
or     (%rax),%edx
mov    0x2e6a(%rip),%rax
or     (%rax),%edx
mov    $0x1a4,%ecx
call   1030 <openat@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    11bd <_lock+0x8d>
mov    0x2e42(%rip),%rax
mov    (%rax),%rax
mov    0x2e48(%rip),%rcx
cmp    (%rcx),%rax
jne    11ab <_lock+0x7b>
mov    $0xfffffffb,%edi
call   1050 <_gen_err@plt>
mov    %eax,-0x14(%rbp)
jmp    11b8 <_lock+0x88>
mov    $0xfffffffe,%edi
call   1050 <_gen_err@plt>
mov    %eax,-0x14(%rbp)
jmp    11c5 <_lock+0x95>
mov    -0x18(%rbp),%edi
call   1040 <close@plt>
mov    -0x14(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_lock>:
push   %rax
mov    0x2ec0(%rip),%rax
mov    0x2e79(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e86(%rip),%rax
or     (%rax),%edx
mov    $0x1a4,%ecx
call   1030 <openat@plt>
test   %eax,%eax
js     1165 <_lock+0x35>
mov    %eax,%edi
call   1040 <close@plt>
xor    %eax,%eax
pop    %rcx
ret
mov    0x2e5c(%rip),%rax
mov    (%rax),%rax
mov    0x2e62(%rip),%rcx
cmp    (%rcx),%rax
jne    1186 <_lock+0x56>
mov    $0xfffffffb,%edi
pop    %rax
jmp    1050 <_gen_err@plt>
mov    $0xfffffffe,%edi
pop    %rax
jmp    1050 <_gen_err@plt>

LLVM IR (-O0):
@O_CREAT = global i32 0
@O_WRONLY = global i32 0
@O_EXCL = global i32 0
@errno = global i64 0
@EEXIST = global i64 0
define i32 @_lock(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
store i32 %0, i32* %3
store i8* %1, i8** %4
store i32 0, i32* %5
%7 = load i32, i32* %3
%8 = load i8*, i8** %4
%9 = load i32, i32* @O_CREAT
%10 = load i32, i32* @O_WRONLY
%11 = or i32 %9, %10
%12 = load i32, i32* @O_EXCL
%13 = or i32 %11, %12
%14 = call i32 @openat(i32 %7, i8* %8, i32 %13, i32 420)
store i32 %14, i32* %6
%15 = load i32, i32* %6
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %26
17: ; preds = %2
%18 = load i64, i64* @errno
%19 = load i64, i64* @EEXIST
%20 = icmp eq i64 %18, %19
br i1 %20, label %21, label %23
21: ; preds = %17
%22 = call i32 @_gen_err(i32 -5)
store i32 %22, i32* %5
br label %25
23: ; preds = %17
%24 = call i32 @_gen_err(i32 -2)
store i32 %24, i32* %5
br label %25
25: ; preds = %23, %21
br label %29
26: ; preds = %2
%27 = load i32, i32* %6
%28 = call i32 @close(i32 %27)
br label %29
29: ; preds = %26, %25
%30 = load i32, i32* %5
ret i32 %30
}

LLVM IR (-O2):
@O_CREAT = global i32 0
@O_WRONLY = global i32 0
@O_EXCL = global i32 0
@errno = global i64 0
@EEXIST = global i64 0
define i32 @_lock(i32 %0, i8* %1) {
%3 = load i32, i32* @O_CREAT
%4 = load i32, i32* @O_WRONLY
%5 = or i32 %4, %3
%6 = load i32, i32* @O_EXCL
%7 = or i32 %5, %6
%8 = tail call i32 @openat(i32 %0, i8* %1, i32 %7, i32 420)
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %18
10: ; preds = %2
%11 = load i64, i64* @errno
%12 = load i64, i64* @EEXIST
%13 = icmp eq i64 %11, %12
br i1 %13, label %14, label %16
14: ; preds = %10
%15 = tail call i32 @_gen_err(i32 -5)
br label %20
16: ; preds = %10
%17 = tail call i32 @_gen_err(i32 -2)
br label %20
18: ; preds = %2
%19 = tail call i32 @close(i32 %8)
br label %20
20: ; preds = %14, %16, %18
%21 = phi i32 [ %15, %14 ], [ %17, %16 ], [ 0, %18 ]
ret i32 %21
}