#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*/ F_TLOCK;
int O_CREAT;
int O_TRUNC;
int O_WRONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ lockf(int, int /*<<< orphan*/, int /*<<< orphan*/);
int open(char *, int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ stderr;
int get_lock(char *name) {
  int fd;
  if ((fd = open(name, O_WRONLY | O_CREAT | O_TRUNC, 0644)) == -1) {
    perror("open");
    return 0;
  }
  if (!lockf(fd, F_TLOCK, 0))
    return 1;
  fprintf(stderr, "Could not get lock - is another copy already running?\n");
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<get_lock>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e51(%rip),%rax
mov    (%rax),%esi
mov    0x2e88(%rip),%rax
or     (%rax),%esi
mov    0x2e77(%rip),%rax
or     (%rax),%esi
mov    $0x1a4,%edx
call   1060 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    11a9 <get_lock+0x59>
lea    0xe68(%rip),%rdi
call   1070 <perror@plt>
movl   $0x0,-0x4(%rbp)
jmp    11f5 <get_lock+0xa5>
mov    -0x14(%rbp),%edi
mov    0x2e2d(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1030 <lockf@plt>
cmp    $0x0,%eax
jne    11d1 <get_lock+0x81>
movl   $0x1,-0x4(%rbp)
jmp    11f5 <get_lock+0xa5>
mov    0x2df0(%rip),%rax
mov    (%rax),%edi
lea    0xe24(%rip),%rsi
call   1050 <fprintf@plt>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_lock>:
push   %rbp
push   %rbx
push   %rax
mov    0x2e5e(%rip),%rax
mov    0x2e97(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e84(%rip),%rax
or     (%rax),%esi
mov    $0x1a4,%edx
call   1060 <open@plt>
cmp    $0xffffffff,%eax
je     11c3 <get_lock+0x73>
mov    %eax,%ebp
mov    0x2e5a(%rip),%rax
mov    (%rax),%esi
xor    %ebx,%ebx
mov    %ebp,%edi
xor    %edx,%edx
call   1030 <lockf@plt>
test   %eax,%eax
je     11b5 <get_lock+0x65>
mov    0x2e2a(%rip),%rax
mov    (%rax),%edi
lea    0xe5e(%rip),%rsi
call   1050 <fprintf@plt>
mov    %ebp,%edi
call   1040 <close@plt>
jmp    11ba <get_lock+0x6a>
mov    $0x1,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xe36(%rip),%rdi
call   1070 <perror@plt>
xor    %ebx,%ebx
jmp    11ba <get_lock+0x6a>

LLVM IR (-O0):
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@.str = [5 x i8] c"open\00"
@F_TLOCK = global i32 0
@stderr = global i32 0
@.str.1 = [55 x i8] c"Could not get lock - is another copy already running?\0A\00"
define i32 @get_lock(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 = load i32, i32* @O_TRUNC
%10 = or i32 %8, %9
%11 = call i32 @open(i8* %5, i32 %10, i32 420)
store i32 %11, i32* %4
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %15
13: ; preds = %1
%14 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %2
br label %26
15: ; preds = %1
%16 = load i32, i32* %4
%17 = load i32, i32* @F_TLOCK
%18 = call i32 @lockf(i32 %16, i32 %17, i32 0)
%19 = icmp ne i32 %18, 0
br i1 %19, label %21, label %20
20: ; preds = %15
store i32 1, i32* %2
br label %26
21: ; preds = %15
%22 = load i32, i32* @stderr
%23 = call i32 @fprintf(i32 %22, i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str.1, i64 0, i64 0))
%24 = load i32, i32* %4
%25 = call i32 @close(i32 %24)
store i32 0, i32* %2
br label %26
26: ; preds = %21, %20, %13
%27 = load i32, i32* %2
ret i32 %27
}

LLVM IR (-O2):
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@.str = [5 x i8] c"open\00"
@F_TLOCK = global i32 0
@stderr = global i32 0
@.str.1 = [55 x i8] c"Could not get lock - is another copy already running?\0A\00"
define i32 @get_lock(i8* nocapture %0) {
%2 = load i32, i32* @O_WRONLY
%3 = load i32, i32* @O_CREAT
%4 = or i32 %3, %2
%5 = load i32, i32* @O_TRUNC
%6 = or i32 %4, %5
%7 = tail call i32 @open(i8* %0, i32 %6, i32 420)
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %11
9: ; preds = %1
%10 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %19
11: ; preds = %1
%12 = load i32, i32* @F_TLOCK
%13 = tail call i32 @lockf(i32 %7, i32 %12, i32 0)
%14 = icmp eq i32 %13, 0
br i1 %14, label %19, label %15
15: ; preds = %11
%16 = load i32, i32* @stderr
%17 = tail call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str.1, i64 0, i64 0))
%18 = tail call i32 @close(i32 %7)
br label %19
19: ; preds = %11, %15, %9
%20 = phi i32 [ 0, %9 ], [ 0, %15 ], [ 1, %11 ]
ret i32 %20
}