#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 O_CREAT;
int O_EXCL;
int O_RDWR;
int /*<<< orphan*/ exit(int);
int open(char *, int, ...);
int /*<<< orphan*/ perror(char *);
int create_lock_file(char *lock_name) {
  int fd;
  if ((fd = open(lock_name, O_RDWR)) < 0) {
    if ((fd = open(lock_name, O_RDWR | O_CREAT | O_EXCL, 0666)) < 0) {
      perror("cannot create lock file");
      exit(-1);
    }
  }
  return (fd);
}


x86 assembly (clang, -O0):
<create_lock_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2e99(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1030 <open@plt>
mov    %eax,-0xc(%rbp)
cmp    $0x0,%eax
jge    11ae <create_lock_file+0x7e>
mov    -0x8(%rbp),%rdi
mov    0x2e79(%rip),%rax
mov    (%rax),%esi
mov    0x2e88(%rip),%rax
or     (%rax),%esi
mov    0x2e57(%rip),%rax
or     (%rax),%esi
mov    $0x1b6,%edx
mov    $0x0,%al
call   1030 <open@plt>
mov    %eax,-0xc(%rbp)
cmp    $0x0,%eax
jge    11a9 <create_lock_file+0x79>
lea    0xe66(%rip),%rdi
call   1040 <perror@plt>
mov    $0xffffffff,%edi
call   1050 <exit@plt>
jmp    11ae <create_lock_file+0x7e>
mov    -0xc(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<create_lock_file>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbx
mov    0x2ea2(%rip),%r14
mov    (%r14),%esi
xor    %eax,%eax
call   1030 <open@plt>
test   %eax,%eax
jns    1174 <create_lock_file+0x44>
mov    0x2ea5(%rip),%rax
mov    (%rax),%esi
or     (%r14),%esi
mov    0x2e71(%rip),%rax
or     (%rax),%esi
mov    %rbx,%rdi
mov    $0x1b6,%edx
xor    %eax,%eax
call   1030 <open@plt>
test   %eax,%eax
js     117c <create_lock_file+0x4c>
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xe7d(%rip),%rdi
call   1040 <perror@plt>
mov    $0xffffffff,%edi
call   1050 <exit@plt>

LLVM IR (-O0):
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@O_EXCL = global i32 0
@.str = [24 x i8] c"cannot create lock file\00"
define i32 @create_lock_file(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
store i8* %0, i8** %2
%4 = load i8*, i8** %2
%5 = load i32, i32* @O_RDWR
%6 = call i32 (i8*, i32, ...) @open(i8* %4, i32 %5)
store i32 %6, i32* %3
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %21
8: ; preds = %1
%9 = load i8*, i8** %2
%10 = load i32, i32* @O_RDWR
%11 = load i32, i32* @O_CREAT
%12 = or i32 %10, %11
%13 = load i32, i32* @O_EXCL
%14 = or i32 %12, %13
%15 = call i32 (i8*, i32, ...) @open(i8* %9, i32 %14, i32 438)
store i32 %15, i32* %3
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %8
%18 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
%19 = call i32 @exit(i32 -1)
unreachable
20: ; preds = %8
br label %21
21: ; preds = %20, %1
%22 = load i32, i32* %3
ret i32 %22
}

LLVM IR (-O2):
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@O_EXCL = global i32 0
@.str = [24 x i8] c"cannot create lock file\00"
define i32 @create_lock_file(i8* nocapture %0) {
%2 = load i32, i32* @O_RDWR
%3 = tail call i32 (i8*, i32, ...) @open(i8* %0, i32 %2)
%4 = icmp slt i32 %3, 0
br i1 %4, label %5, label %16
5: ; preds = %1
%6 = load i32, i32* @O_RDWR
%7 = load i32, i32* @O_CREAT
%8 = or i32 %7, %6
%9 = load i32, i32* @O_EXCL
%10 = or i32 %8, %9
%11 = tail call i32 (i8*, i32, ...) @open(i8* %0, i32 %10, i32 438)
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %16
13: ; preds = %5
%14 = tail call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
%15 = tail call i32 @exit(i32 -1)
unreachable
16: ; preds = %5, %1
%17 = phi i32 [ %11, %5 ], [ %3, %1 ]
ret i32 %17
}