#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_WRONLY;
int asprintf(char **, char *, char *);
int /*<<< orphan*/ assert(char const *);
scalar_t__ close(int const);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ free(char *);
int open(char *, int, int);
int /*<<< orphan*/ perror(char *);
char *rindex(char const *, char);
int /*<<< orphan*/ stderr;
char *__attribute__((nonnull, warn_unused_result))
acquire_lock(const char *device_path) {
  assert(device_path);
  char *devicename = rindex(device_path, '/');
  if (!devicename || !devicename[0] || !devicename[1]) {
    return NULL;
  }
  char *lock = NULL;
  if (asprintf(&lock, "/tmp/cal.%s.lock", ++devicename) == -1) {
    perror("Could not build lock filename");
    return NULL;
  }
  const int lock_fd = open(lock, O_WRONLY | O_CREAT | O_EXCL, 0666);
  if (lock_fd == -1) {
    fprintf(stderr, "Could not acquire lock file %s: ", lock);
    perror(NULL);
    free(lock);
    return NULL;
  }
  if (close(lock_fd)) {
    fprintf(stderr, "Could not close lock file %s: ", lock);
    perror(NULL);
    free(lock);
    return NULL;
  }
  return lock;
}


x86 assembly (clang, -O0):
<acquire_lock>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <assert@plt>
mov    -0x10(%rbp),%rdi
mov    $0x2f,%esi
call   1090 <rindex@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     11cd <acquire_lock+0x4d>
mov    -0x18(%rbp),%rax
cmpb   $0x0,(%rax)
je     11cd <acquire_lock+0x4d>
mov    -0x18(%rbp),%rax
cmpb   $0x0,0x1(%rax)
jne    11da <acquire_lock+0x5a>
movq   $0x0,-0x8(%rbp)
jmp    12e0 <acquire_lock+0x160>
movq   $0x0,-0x20(%rbp)
mov    -0x18(%rbp),%rdx
add    $0x1,%rdx
mov    %rdx,-0x18(%rbp)
lea    -0x20(%rbp),%rdi
lea    0xe07(%rip),%rsi
call   1070 <asprintf@plt>
cmp    $0xffffffff,%eax
jne    1220 <acquire_lock+0xa0>
lea    0xe03(%rip),%rdi
call   10a0 <perror@plt>
movq   $0x0,-0x8(%rbp)
jmp    12e0 <acquire_lock+0x160>
mov    -0x20(%rbp),%rdi
mov    0x2d95(%rip),%rax
mov    (%rax),%esi
mov    0x2dc4(%rip),%rax
or     (%rax),%esi
mov    0x2d93(%rip),%rax
or     (%rax),%esi
mov    $0x1b6,%edx
call   1080 <open@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    128e <acquire_lock+0x10e>
mov    0x2d7b(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%rdx
lea    0xdc5(%rip),%rsi
call   1060 <fprintf@plt>
xor    %eax,%eax
mov    %eax,%edi
call   10a0 <perror@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    12e0 <acquire_lock+0x160>
mov    -0x24(%rbp),%edi
call   1040 <close@plt>
cmp    $0x0,%rax
je     12d8 <acquire_lock+0x158>
mov    0x2d31(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%rdx
lea    0xd9c(%rip),%rsi
call   1060 <fprintf@plt>
xor    %eax,%eax
mov    %eax,%edi
call   10a0 <perror@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    12e0 <acquire_lock+0x160>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<acquire_lock>:
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%rbx
call   1050 <assert@plt>
mov    %rbx,%rdi
mov    $0x2f,%esi
call   1090 <rindex@plt>
test   %rax,%rax
je     121e <acquire_lock+0x9e>
cmpb   $0x0,(%rax)
je     121e <acquire_lock+0x9e>
cmpb   $0x0,0x1(%rax)
je     121e <acquire_lock+0x9e>
add    $0x1,%rax
movq   $0x0,0x8(%rsp)
lea    0xe42(%rip),%rsi
lea    0x8(%rsp),%rdi
mov    %rax,%rdx
call   1070 <asprintf@plt>
cmp    $0xffffffff,%eax
je     1212 <acquire_lock+0x92>
mov    0x2de9(%rip),%rax
mov    0x2e1a(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x8(%rsp),%rdi
mov    0x2de2(%rip),%rax
or     (%rax),%esi
mov    $0x1b6,%edx
call   1080 <open@plt>
cmp    $0xffffffff,%eax
je     1229 <acquire_lock+0xa9>
mov    %eax,%edi
call   1040 <close@plt>
test   %rax,%rax
jne    1240 <acquire_lock+0xc0>
mov    0x8(%rsp),%rbx
jmp    1220 <acquire_lock+0xa0>
lea    0xdf8(%rip),%rdi
call   10a0 <perror@plt>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x10,%rsp
pop    %rbx
ret
mov    0x2da8(%rip),%rax
mov    (%rax),%edi
mov    0x8(%rsp),%rdx
lea    0xdf1(%rip),%rsi
jmp    1255 <acquire_lock+0xd5>
mov    0x2d91(%rip),%rax
mov    (%rax),%edi
mov    0x8(%rsp),%rdx
lea    0xdfb(%rip),%rsi
call   1060 <fprintf@plt>
xor    %ebx,%ebx
xor    %edi,%edi
call   10a0 <perror@plt>
mov    0x8(%rsp),%rdi
call   1030 <free@plt>
jmp    1220 <acquire_lock+0xa0>

LLVM IR (-O0):
@.str = [17 x i8] c"/tmp/cal.%s.lock\00"
@.str.1 = [30 x i8] c"Could not build lock filename\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_EXCL = global i32 0
@stderr = global i32 0
@.str.2 = [33 x i8] c"Could not acquire lock file %s: \00"
@.str.3 = [31 x i8] c"Could not close lock file %s: \00"
define i8* @acquire_lock(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
store i8* %0, i8** %3
%7 = load i8*, i8** %3
%8 = call i32 @assert(i8* %7)
%9 = load i8*, i8** %3
%10 = call i8* @rindex(i8* %9, i8 signext 47)
store i8* %10, i8** %4
%11 = load i8*, i8** %4
%12 = icmp ne i8* %11, null
br i1 %12, label %13, label %23
13: ; preds = %1
%14 = load i8*, i8** %4
%15 = getelementptr inbounds i8, i8* %14, i64 0
%16 = load i8, i8* %15
%17 = icmp ne i8 %16, 0
br i1 %17, label %18, label %23
18: ; preds = %13
%19 = load i8*, i8** %4
%20 = getelementptr inbounds i8, i8* %19, i64 1
%21 = load i8, i8* %20
%22 = icmp ne i8 %21, 0
br i1 %22, label %24, label %23
23: ; preds = %18, %13, %1
store i8* null, i8** %2
br label %61
24: ; preds = %18
store i8* null, i8** %5
%25 = load i8*, i8** %4
%26 = getelementptr inbounds i8, i8* %25, i32 1
store i8* %26, i8** %4
%27 = call i32 @asprintf(i8** %5, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* %26)
%28 = icmp eq i32 %27, -1
br i1 %28, label %29, label %31
29: ; preds = %24
%30 = call i32 @perror(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0))
store i8* null, i8** %2
br label %61
31: ; preds = %24
%32 = load i8*, i8** %5
%33 = load i32, i32* @O_WRONLY
%34 = load i32, i32* @O_CREAT
%35 = or i32 %33, %34
%36 = load i32, i32* @O_EXCL
%37 = or i32 %35, %36
%38 = call i32 @open(i8* %32, i32 %37, i32 438)
store i32 %38, i32* %6
%39 = load i32, i32* %6
%40 = icmp eq i32 %39, -1
br i1 %40, label %41, label %48
41: ; preds = %31
%42 = load i32, i32* @stderr
%43 = load i8*, i8** %5
%44 = call i32 @fprintf(i32 %42, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0), i8* %43)
%45 = call i32 @perror(i8* null)
%46 = load i8*, i8** %5
%47 = call i32 @free(i8* %46)
store i8* null, i8** %2
br label %61
48: ; preds = %31
%49 = load i32, i32* %6
%50 = call i64 @close(i32 %49)
%51 = icmp ne i64 %50, 0
br i1 %51, label %52, label %59
52: ; preds = %48
%53 = load i32, i32* @stderr
%54 = load i8*, i8** %5
%55 = call i32 @fprintf(i32 %53, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0), i8* %54)
%56 = call i32 @perror(i8* null)
%57 = load i8*, i8** %5
%58 = call i32 @free(i8* %57)
store i8* null, i8** %2
br label %61
59: ; preds = %48
%60 = load i8*, i8** %5
store i8* %60, i8** %2
br label %61
61: ; preds = %59, %52, %41, %29, %23
%62 = load i8*, i8** %2
ret i8* %62
}

LLVM IR (-O2):
@.str = [17 x i8] c"/tmp/cal.%s.lock\00"
@.str.1 = [30 x i8] c"Could not build lock filename\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_EXCL = global i32 0
@stderr = global i32 0
@.str.2 = [33 x i8] c"Could not acquire lock file %s: \00"
@.str.3 = [31 x i8] c"Could not close lock file %s: \00"
define i8* @acquire_lock(i8* %0) {
%2 = alloca i8*
%3 = tail call i32 @assert(i8* %0)
%4 = tail call i8* @rindex(i8* %0, i8 signext 47)
%5 = icmp eq i8* %4, null
br i1 %5, label %49, label %6
6: ; preds = %1
%7 = load i8, i8* %4
%8 = icmp eq i8 %7, 0
br i1 %8, label %49, label %9
9: ; preds = %6
%10 = getelementptr inbounds i8, i8* %4, i64 1
%11 = load i8, i8* %10
%12 = icmp eq i8 %11, 0
br i1 %12, label %49, label %13
13: ; preds = %9
%14 = bitcast i8** %2 to i8*
store i8* null, i8** %2
%15 = call i32 @asprintf(i8** %2, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* %10)
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %19
17: ; preds = %13
%18 = call i32 @perror(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0))
br label %47
19: ; preds = %13
%20 = load i8*, i8** %2
%21 = load i32, i32* @O_WRONLY
%22 = load i32, i32* @O_CREAT
%23 = or i32 %22, %21
%24 = load i32, i32* @O_EXCL
%25 = or i32 %23, %24
%26 = call i32 @open(i8* %20, i32 %25, i32 438)
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %35
28: ; preds = %19
%29 = load i32, i32* @stderr
%30 = load i8*, i8** %2
%31 = call i32 @fprintf(i32 %29, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0), i8* %30)
%32 = call i32 @perror(i8* null)
%33 = load i8*, i8** %2
%34 = call i32 @free(i8* %33)
br label %47
35: ; preds = %19
%36 = call i64 @close(i32 %26)
%37 = icmp eq i64 %36, 0
br i1 %37, label %45, label %38
38: ; preds = %35
%39 = load i32, i32* @stderr
%40 = load i8*, i8** %2
%41 = call i32 @fprintf(i32 %39, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0), i8* %40)
%42 = call i32 @perror(i8* null)
%43 = load i8*, i8** %2
%44 = call i32 @free(i8* %43)
br label %47
45: ; preds = %35
%46 = load i8*, i8** %2
br label %47
47: ; preds = %28, %38, %45, %17
%48 = phi i8* [ null, %17 ], [ null, %28 ], [ null, %38 ], [ %46, %45 ]
br label %49
49: ; preds = %1, %6, %9, %47
%50 = phi i8* [ %48, %47 ], [ null, %9 ], [ null, %6 ], [ null, %1 ]
ret i8* %50
}