#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 S_IRUSR;
int S_IWUSR;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ free(char *);
char *genlockfilename(char *);
int open(char *, int, int);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ stderr;
int lockfile(char *file) {
  char *lfile = genlockfilename(file);
  int fd;
  if (!lfile) {
    fprintf(stderr,
            "lockfile::genlockfilename: problem creating lockfile-filename");
    return -1;
  }
  do {
    fd = open(lfile, O_CREAT | O_EXCL, S_IRUSR | S_IWUSR);
    if (fd == -1) {
      fprintf(stderr,
              "lockfile: waiting for file '%s' to become available (delete "
              "file %s if this is an error)",
              file, lfile);
      sleep(1);
    }
  } while (fd == -1);
  free(lfile);
  return fd;
}


x86 assembly (clang, -O0):
<lockfile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <genlockfilename@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1197 <lockfile+0x47>
mov    0x2e55(%rip),%rax
mov    (%rax),%edi
lea    0xe7c(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    121d <lockfile+0xcd>
jmp    119c <lockfile+0x4c>
mov    -0x18(%rbp),%rdi
mov    0x2e51(%rip),%rax
mov    (%rax),%esi
mov    0x2e18(%rip),%rax
or     (%rax),%esi
mov    0x2e07(%rip),%rax
mov    (%rax),%edx
mov    0x2e1e(%rip),%rax
or     (%rax),%edx
call   1050 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    11ff <lockfile+0xaf>
mov    0x2df3(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
mov    -0x18(%rbp),%rcx
lea    0xe50(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1070 <sleep@plt>
jmp    1204 <lockfile+0xb4>
cmpl   $0xffffffff,-0x1c(%rbp)
je     119c <lockfile+0x4c>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<lockfile>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r14
call   1060 <genlockfilename@plt>
test   %rax,%rax
je     1205 <lockfile+0xb5>
mov    %rax,%rbx
mov    0x2e82(%rip),%rax
mov    0x2e4b(%rip),%r12
mov    (%r12),%esi
or     (%rax),%esi
mov    0x2e36(%rip),%r15
mov    0x2e4f(%rip),%rax
mov    (%rax),%edx
or     (%r15),%edx
mov    %rbx,%rdi
call   1050 <open@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
jne    11fb <lockfile+0xab>
lea    0xe92(%rip),%r13
nopl   0x0(%rax)
mov    0x2e19(%rip),%rax
mov    (%rax),%edi
mov    %r13,%rsi
mov    %r14,%rdx
mov    %rbx,%rcx
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1070 <sleep@plt>
mov    (%r12),%esi
mov    0x2e1a(%rip),%rax
or     (%rax),%esi
mov    0x2df9(%rip),%rax
mov    (%rax),%edx
or     (%r15),%edx
mov    %rbx,%rdi
call   1050 <open@plt>
cmp    $0xffffffff,%eax
je     11b0 <lockfile+0x60>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1030 <free@plt>
jmp    1221 <lockfile+0xd1>
mov    0x2dc4(%rip),%rax
mov    (%rax),%edi
lea    0xdeb(%rip),%rsi
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [62 x i8] c"lockfile::genlockfilename: problem creating lockfile-filename\00"
@O_CREAT = global i32 0
@O_EXCL = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@.str.1 = [89 x i8] c"lockfile: waiting for file '%s' to become available (delete file %s if this is an error)\00"
define i32 @lockfile(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
store i8* %0, i8** %3
%6 = load i8*, i8** %3
%7 = call i8* @genlockfilename(i8* %6)
store i8* %7, i8** %4
%8 = load i8*, i8** %4
%9 = icmp ne i8* %8, null
br i1 %9, label %13, label %10
10: ; preds = %1
%11 = load i32, i32* @stderr
%12 = call i32 (i32, i8*, ...) @fprintf(i32 %11, i8* getelementptr inbounds ([62 x i8], [62 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %2
br label %39
13: ; preds = %1
br label %14
14: ; preds = %32, %13
%15 = load i8*, i8** %4
%16 = load i32, i32* @O_CREAT
%17 = load i32, i32* @O_EXCL
%18 = or i32 %16, %17
%19 = load i32, i32* @S_IRUSR
%20 = load i32, i32* @S_IWUSR
%21 = or i32 %19, %20
%22 = call i32 @open(i8* %15, i32 %18, i32 %21)
store i32 %22, i32* %5
%23 = load i32, i32* %5
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %31
25: ; preds = %14
%26 = load i32, i32* @stderr
%27 = load i8*, i8** %3
%28 = load i8*, i8** %4
%29 = call i32 (i32, i8*, ...) @fprintf(i32 %26, i8* getelementptr inbounds ([89 x i8], [89 x i8]* @.str.1, i64 0, i64 0), i8* %27, i8* %28)
%30 = call i32 @sleep(i32 1)
br label %31
31: ; preds = %25, %14
br label %32
32: ; preds = %31
%33 = load i32, i32* %5
%34 = icmp eq i32 %33, -1
br i1 %34, label %14, label %35
35: ; preds = %32
%36 = load i8*, i8** %4
%37 = call i32 @free(i8* %36)
%38 = load i32, i32* %5
store i32 %38, i32* %2
br label %39
39: ; preds = %35, %10
%40 = load i32, i32* %2
ret i32 %40
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [62 x i8] c"lockfile::genlockfilename: problem creating lockfile-filename\00"
@O_CREAT = global i32 0
@O_EXCL = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@.str.1 = [89 x i8] c"lockfile: waiting for file '%s' to become available (delete file %s if this is an error)\00"
define i32 @lockfile(i8* %0) {
%2 = tail call i8* @genlockfilename(i8* %0)
%3 = icmp eq i8* %2, null
br i1 %3, label %13, label %4
4: ; preds = %1
%5 = load i32, i32* @O_CREAT
%6 = load i32, i32* @O_EXCL
%7 = or i32 %6, %5
%8 = load i32, i32* @S_IRUSR
%9 = load i32, i32* @S_IWUSR
%10 = or i32 %9, %8
%11 = tail call i32 @open(i8* %2, i32 %7, i32 %10)
%12 = icmp eq i32 %11, -1
br i1 %12, label %16, label %28
13: ; preds = %1
%14 = load i32, i32* @stderr
%15 = tail call i32 (i32, i8*, ...) @fprintf(i32 %14, i8* getelementptr inbounds ([62 x i8], [62 x i8]* @.str, i64 0, i64 0))
br label %31
16: ; preds = %4, %16
%17 = load i32, i32* @stderr
%18 = tail call i32 (i32, i8*, ...) @fprintf(i32 %17, i8* getelementptr inbounds ([89 x i8], [89 x i8]* @.str.1, i64 0, i64 0), i8* %0, i8* %2)
%19 = tail call i32 @sleep(i32 1)
%20 = load i32, i32* @O_CREAT
%21 = load i32, i32* @O_EXCL
%22 = or i32 %21, %20
%23 = load i32, i32* @S_IRUSR
%24 = load i32, i32* @S_IWUSR
%25 = or i32 %24, %23
%26 = tail call i32 @open(i8* %2, i32 %22, i32 %25)
%27 = icmp eq i32 %26, -1
br i1 %27, label %16, label %28
28: ; preds = %16, %4
%29 = phi i32 [ %11, %4 ], [ %26, %16 ]
%30 = tail call i32 @free(i8* %2)
br label %31
31: ; preds = %28, %13
%32 = phi i32 [ %29, %28 ], [ -1, %13 ]
ret i32 %32
}