#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 LOCK_EX;
int LOCK_NB;
int /*<<< orphan*/ LOG_ERR;
int O_CREAT;
int O_RDWR;
char *build_file_name(char *, char const *, char const *);
int /*<<< orphan*/ cleanup_file_name(char *);
int /*<<< orphan*/ errno;
int flock(int, int);
int open(char *, int, int);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
int /*<<< orphan*/ syslog(int /*<<< orphan*/, char *, char *, ...);
int create_lock_file(const char *lock_file_basepath,
                     const char *identify_name) {
  char *lock_file = build_file_name("%s/%s", lock_file_basepath, identify_name);
  int lock_file_descriptor = -1;
  if (lock_file && lock_file[0]) {
    lock_file_descriptor = open(lock_file, O_RDWR | O_CREAT, 0640);
    if (lock_file_descriptor < 0) {
      syslog(LOG_ERR, "failed to create lock file %s, code=%d (%s)", lock_file,
             errno, strerror(errno));
      cleanup_file_name(lock_file);
      return 0;
    }
    if (flock(lock_file_descriptor, LOCK_EX | LOCK_NB) == -1) {
      syslog(LOG_ERR, "failed to lock file %s, code=%d (%s)", lock_file, errno,
             strerror(errno));
      cleanup_file_name(lock_file);
      return 0;
    }
    cleanup_file_name(lock_file);
    return 1;
  }
  syslog(LOG_ERR, "failed to lock file %s, unknown error", lock_file);
  cleanup_file_name(lock_file);
  return 0;
}


x86 assembly (clang, -O0):
<create_lock_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
lea    0xe81(%rip),%rdi
call   1040 <build_file_name@plt>
mov    %rax,-0x20(%rbp)
movl   $0xffffffff,-0x24(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     12cb <create_lock_file+0x16b>
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     12cb <create_lock_file+0x16b>
mov    -0x20(%rbp),%rdi
mov    0x2e2b(%rip),%rax
mov    (%rax),%esi
mov    0x2e3a(%rip),%rax
or     (%rax),%esi
mov    $0x1a0,%edx
call   1060 <open@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    1235 <create_lock_file+0xd5>
mov    0x2dda(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    0x2dd6(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    0x2dca(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x34(%rbp),%edi
mov    -0x30(%rbp),%rdx
mov    -0x28(%rbp),%ecx
mov    %eax,%r8d
lea    0xded(%rip),%rsi
mov    $0x0,%al
call   1050 <syslog@plt>
mov    -0x20(%rbp),%rdi
call   1030 <cleanup_file_name@plt>
movl   $0x0,-0x4(%rbp)
jmp    12f6 <create_lock_file+0x196>
mov    -0x24(%rbp),%edi
mov    0x2d99(%rip),%rax
mov    (%rax),%esi
mov    0x2d68(%rip),%rax
or     (%rax),%esi
call   1070 <flock@plt>
cmp    $0xffffffff,%eax
jne    12b6 <create_lock_file+0x156>
mov    0x2d59(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x44(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    0x2d55(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x38(%rbp)
mov    0x2d49(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x44(%rbp),%edi
mov    -0x40(%rbp),%rdx
mov    -0x38(%rbp),%ecx
mov    %eax,%r8d
lea    0xd98(%rip),%rsi
mov    $0x0,%al
call   1050 <syslog@plt>
mov    -0x20(%rbp),%rdi
call   1030 <cleanup_file_name@plt>
movl   $0x0,-0x4(%rbp)
jmp    12f6 <create_lock_file+0x196>
mov    -0x20(%rbp),%rdi
call   1030 <cleanup_file_name@plt>
movl   $0x1,-0x4(%rbp)
jmp    12f6 <create_lock_file+0x196>
mov    0x2ce6(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%rdx
lea    0xd78(%rip),%rsi
mov    $0x0,%al
call   1050 <syslog@plt>
mov    -0x20(%rbp),%rdi
call   1030 <cleanup_file_name@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<create_lock_file>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rdx
mov    %rdi,%rsi
lea    0xe8c(%rip),%rdi
call   1040 <build_file_name@plt>
mov    %rax,%rbx
test   %rax,%rax
je     11d9 <create_lock_file+0x79>
cmpb   $0x0,(%rbx)
je     11d9 <create_lock_file+0x79>
mov    0x2e53(%rip),%rax
mov    0x2e64(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %rbx,%rdi
mov    $0x1a0,%edx
call   1060 <open@plt>
test   %eax,%eax
js     11f7 <create_lock_file+0x97>
mov    0x2e28(%rip),%rcx
mov    0x2df9(%rip),%rdx
mov    (%rdx),%esi
or     (%rcx),%esi
mov    %eax,%edi
call   1070 <flock@plt>
cmp    $0xffffffff,%eax
je     121c <create_lock_file+0xbc>
mov    %rbx,%rdi
call   1030 <cleanup_file_name@plt>
mov    $0x1,%ebp
jmp    125c <create_lock_file+0xfc>
mov    0x2dd8(%rip),%rax
mov    (%rax),%edi
lea    0xe6e(%rip),%rsi
xor    %ebp,%ebp
mov    %rbx,%rdx
xor    %eax,%eax
call   1050 <syslog@plt>
jmp    1254 <create_lock_file+0xf4>
mov    0x2dba(%rip),%rax
mov    (%rax),%r14d
mov    0x2dc0(%rip),%rax
mov    (%rax),%r15d
mov    %r15d,%edi
call   1080 <strerror@plt>
lea    0xdec(%rip),%rsi
jmp    123f <create_lock_file+0xdf>
mov    0x2d95(%rip),%rax
mov    (%rax),%r14d
mov    0x2d9b(%rip),%rax
mov    (%rax),%r15d
mov    %r15d,%edi
call   1080 <strerror@plt>
lea    0xdf3(%rip),%rsi
xor    %ebp,%ebp
mov    %r14d,%edi
mov    %rbx,%rdx
mov    %r15d,%ecx
mov    %eax,%r8d
xor    %eax,%eax
call   1050 <syslog@plt>
mov    %rbx,%rdi
call   1030 <cleanup_file_name@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [6 x i8] c"%s/%s\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@LOG_ERR = global i32 0
@.str.1 = [44 x i8] c"failed to create lock file %s, code=%d (%s)\00"
@errno = global i32 0
@LOCK_EX = global i32 0
@LOCK_NB = global i32 0
@.str.2 = [37 x i8] c"failed to lock file %s, code=%d (%s)\00"
@.str.3 = [38 x i8] c"failed to lock file %s, unknown error\00"
define i32 @create_lock_file(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
%8 = load i8*, i8** %4
%9 = load i8*, i8** %5
%10 = call i8* @build_file_name(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %8, i8* %9)
store i8* %10, i8** %6
store i32 -1, i32* %7
%11 = load i8*, i8** %6
%12 = icmp ne i8* %11, null
br i1 %12, label %13, label %55
13: ; preds = %2
%14 = load i8*, i8** %6
%15 = getelementptr inbounds i8, i8* %14, i64 0
%16 = load i8, i8* %15
%17 = sext i8 %16 to i32
%18 = icmp ne i32 %17, 0
br i1 %18, label %19, label %55
19: ; preds = %13
%20 = load i8*, i8** %6
%21 = load i32, i32* @O_RDWR
%22 = load i32, i32* @O_CREAT
%23 = or i32 %21, %22
%24 = call i32 @open(i8* %20, i32 %23, i32 416)
store i32 %24, i32* %7
%25 = load i32, i32* %7
%26 = icmp slt i32 %25, 0
br i1 %26, label %27, label %36
27: ; preds = %19
%28 = load i32, i32* @LOG_ERR
%29 = load i8*, i8** %6
%30 = load i32, i32* @errno
%31 = load i32, i32* @errno
%32 = call i32 @strerror(i32 %31)
%33 = call i32 (i32, i8*, i8*, ...) @syslog(i32 %28, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.1, i64 0, i64 0), i8* %29, i32 %30, i32 %32)
%34 = load i8*, i8** %6
%35 = call i32 @cleanup_file_name(i8* %34)
store i32 0, i32* %3
br label %61
36: ; preds = %19
%37 = load i32, i32* %7
%38 = load i32, i32* @LOCK_EX
%39 = load i32, i32* @LOCK_NB
%40 = or i32 %38, %39
%41 = call i32 @flock(i32 %37, i32 %40)
%42 = icmp eq i32 %41, -1
br i1 %42, label %43, label %52
43: ; preds = %36
%44 = load i32, i32* @LOG_ERR
%45 = load i8*, i8** %6
%46 = load i32, i32* @errno
%47 = load i32, i32* @errno
%48 = call i32 @strerror(i32 %47)
%49 = call i32 (i32, i8*, i8*, ...) @syslog(i32 %44, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.2, i64 0, i64 0), i8* %45, i32 %46, i32 %48)
%50 = load i8*, i8** %6
%51 = call i32 @cleanup_file_name(i8* %50)
store i32 0, i32* %3
br label %61
52: ; preds = %36
%53 = load i8*, i8** %6
%54 = call i32 @cleanup_file_name(i8* %53)
store i32 1, i32* %3
br label %61
55: ; preds = %13, %2
%56 = load i32, i32* @LOG_ERR
%57 = load i8*, i8** %6
%58 = call i32 (i32, i8*, i8*, ...) @syslog(i32 %56, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.3, i64 0, i64 0), i8* %57)
%59 = load i8*, i8** %6
%60 = call i32 @cleanup_file_name(i8* %59)
store i32 0, i32* %3
br label %61
61: ; preds = %55, %52, %43, %27
%62 = load i32, i32* %3
ret i32 %62
}

LLVM IR (-O2):
@.str = [6 x i8] c"%s/%s\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@LOG_ERR = global i32 0
@.str.1 = [44 x i8] c"failed to create lock file %s, code=%d (%s)\00"
@errno = global i32 0
@LOCK_EX = global i32 0
@LOCK_NB = global i32 0
@.str.2 = [37 x i8] c"failed to lock file %s, code=%d (%s)\00"
@.str.3 = [38 x i8] c"failed to lock file %s, unknown error\00"
define i32 @create_lock_file(i8* %0, i8* %1) {
%3 = tail call i8* @build_file_name(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %1)
%4 = icmp eq i8* %3, null
br i1 %4, label %34, label %5
5: ; preds = %2
%6 = load i8, i8* %3
%7 = icmp eq i8 %6, 0
br i1 %7, label %34, label %8
8: ; preds = %5
%9 = load i32, i32* @O_RDWR
%10 = load i32, i32* @O_CREAT
%11 = or i32 %10, %9
%12 = tail call i32 @open(i8* %3, i32 %11, i32 416)
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %20
14: ; preds = %8
%15 = load i32, i32* @LOG_ERR
%16 = load i32, i32* @errno
%17 = tail call i32 @strerror(i32 %16)
%18 = tail call i32 (i32, i8*, i8*, ...) @syslog(i32 %15, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.1, i64 0, i64 0), i8* %3, i32 %16, i32 %17)
%19 = tail call i32 @cleanup_file_name(i8* %3)
br label %38
20: ; preds = %8
%21 = load i32, i32* @LOCK_EX
%22 = load i32, i32* @LOCK_NB
%23 = or i32 %22, %21
%24 = tail call i32 @flock(i32 %12, i32 %23)
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %32
26: ; preds = %20
%27 = load i32, i32* @LOG_ERR
%28 = load i32, i32* @errno
%29 = tail call i32 @strerror(i32 %28)
%30 = tail call i32 (i32, i8*, i8*, ...) @syslog(i32 %27, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.2, i64 0, i64 0), i8* %3, i32 %28, i32 %29)
%31 = tail call i32 @cleanup_file_name(i8* %3)
br label %38
32: ; preds = %20
%33 = tail call i32 @cleanup_file_name(i8* %3)
br label %38
34: ; preds = %5, %2
%35 = load i32, i32* @LOG_ERR
%36 = tail call i32 (i32, i8*, i8*, ...) @syslog(i32 %35, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.3, i64 0, i64 0), i8* %3)
%37 = tail call i32 @cleanup_file_name(i8* %3)
br label %38
38: ; preds = %34, %32, %26, %14
%39 = phi i32 [ 0, %14 ], [ 0, %26 ], [ 1, %32 ], [ 0, %34 ]
ret i32 %39
}