#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 */
typedef int /*<<< orphan*/ time_t;

/* Variables and functions */
int LOCK_EX;
int LOCK_NB;
int /*<<< orphan*/ alloca(int /*<<< orphan*/);
double difftime(int /*<<< orphan*/, int /*<<< orphan*/);
int flock(int, int);
int fsync(int);
int /*<<< orphan*/ getpid();
int /*<<< orphan*/ log_message(char *);
char *printf_into(int /*<<< orphan*/, char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf_len(char *, int /*<<< orphan*/);
int /*<<< orphan*/ strlen(char *);
int /*<<< orphan*/ time(int /*<<< orphan*/ *);
int /*<<< orphan*/ usleep(int);
int write(int, char *, int /*<<< orphan*/);
bool atsha_lock(int lockfile) {
  int lock;
  double seconds;
  time_t now;
  time_t start = time(NULL);
  while (1) {
    lock = flock(lockfile, LOCK_EX | LOCK_NB);
    if (lock == -1) {
      now = time(NULL);
      seconds = difftime(now, start);
      if (seconds > 2.2) {
        log_message("api: atsha_lock: operation lock failed");
        return false;
      }
      usleep(10000);
    } else {
      char *pid_str =
          printf_into(alloca(printf_len("%d\n", getpid())), "%d\n", getpid());
      int ret = write(lockfile, pid_str, strlen(pid_str));
      if (ret == -1) {
        log_message("api: atsha_lock: write of PID failed\n");
      }
      ret = fsync(lockfile);
      if (ret == -1) {
        log_message("api: atsha_lock: fsync failed\n");
      }
      return true;
    }
  }
  return true;
}


x86 assembly (clang, -O0):
<atsha_lock>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
xor    %eax,%eax
mov    %eax,%edi
call   1080 <time@plt>
mov    %eax,-0x20(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e07(%rip),%rax
mov    (%rax),%esi
mov    0x2de6(%rip),%rax
or     (%rax),%esi
call   10d0 <flock@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    1258 <atsha_lock+0x98>
xor    %eax,%eax
mov    %eax,%edi
call   1080 <time@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%edi
mov    -0x20(%rbp),%esi
call   1060 <difftime@plt>
movsd  %xmm0,-0x18(%rbp)
movsd  -0x18(%rbp),%xmm0
movsd  0xdd9(%rip),%xmm1
ucomisd %xmm1,%xmm0
jbe    1249 <atsha_lock+0x89>
lea    0xdd0(%rip),%rdi
call   10b0 <log_message@plt>
movl   $0x0,-0x4(%rbp)
jmp    1303 <atsha_lock+0x143>
mov    $0x2710,%edi
call   10e0 <usleep@plt>
jmp    12fe <atsha_lock+0x13e>
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,%esi
lea    0xdc7(%rip),%rdi
call   10a0 <printf_len@plt>
mov    %eax,%edi
call   1070 <alloca@plt>
mov    %eax,-0x40(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    -0x40(%rbp),%edi
mov    %eax,%edx
lea    0xda5(%rip),%rsi
call   1090 <printf_into@plt>
mov    %rax,-0x28(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x28(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x3c(%rbp),%edi
mov    -0x38(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0xffffffff,-0x2c(%rbp)
jne    12d1 <atsha_lock+0x111>
lea    0xd67(%rip),%rdi
call   10b0 <log_message@plt>
mov    -0x8(%rbp),%edi
call   10c0 <fsync@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0xffffffff,-0x2c(%rbp)
jne    12f2 <atsha_lock+0x132>
lea    0xd6c(%rip),%rdi
call   10b0 <log_message@plt>
movl   $0x1,-0x4(%rbp)
jmp    1303 <atsha_lock+0x143>
jmp    11d7 <atsha_lock+0x17>
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<atsha_lock>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edi,%r15d
xor    %edi,%edi
call   1080 <time@plt>
mov    %eax,%ebp
mov    0x2e0a(%rip),%r12
mov    0x2deb(%rip),%r13
mov    0x0(%r13),%esi
or     (%r12),%esi
mov    %r15d,%edi
call   10d0 <flock@plt>
cmp    $0xffffffff,%eax
jne    124f <atsha_lock+0x8f>
lea    0xe07(%rip),%r14
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
xor    %ebx,%ebx
xor    %edi,%edi
call   1080 <time@plt>
mov    %eax,%edi
mov    %ebp,%esi
call   1060 <difftime@plt>
ucomisd 0xdd6(%rip),%xmm0
ja     12c5 <atsha_lock+0x105>
mov    $0x2710,%edi
call   10e0 <usleep@plt>
mov    0x0(%r13),%esi
or     (%r12),%esi
mov    %r15d,%edi
call   10d0 <flock@plt>
cmp    $0xffffffff,%eax
je     1210 <atsha_lock+0x50>
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xdd2(%rip),%rbx
mov    %rbx,%rdi
mov    %eax,%esi
call   10a0 <printf_len@plt>
mov    %eax,%edi
call   1070 <alloca@plt>
mov    %eax,%ebp
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1090 <printf_into@plt>
mov    %rax,%rbx
mov    %rax,%rdi
call   1050 <strlen@plt>
mov    %r15d,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
jne    12ac <atsha_lock+0xec>
lea    0xd8c(%rip),%rdi
call   10b0 <log_message@plt>
mov    %r15d,%edi
call   10c0 <fsync@plt>
mov    $0x1,%ebx
cmp    $0xffffffff,%eax
jne    12cd <atsha_lock+0x10d>
lea    0xd94(%rip),%r14
mov    %r14,%rdi
call   10b0 <log_message@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@LOCK_EX = global i32 0
@LOCK_NB = global i32 0
@.str = [39 x i8] c"api: atsha_lock: operation lock failed\00"
@.str.1 = [4 x i8] c"%d\0A\00"
@.str.2 = [38 x i8] c"api: atsha_lock: write of PID failed\0A\00"
@.str.3 = [31 x i8] c"api: atsha_lock: fsync failed\0A\00"
define i32 @atsha_lock(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca double
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i32
store i32 %0, i32* %3
%10 = call i32 @time(i32* null)
store i32 %10, i32* %7
br label %11
11: ; preds = %1, %53
%12 = load i32, i32* %3
%13 = load i32, i32* @LOCK_EX
%14 = load i32, i32* @LOCK_NB
%15 = or i32 %13, %14
%16 = call i32 @flock(i32 %12, i32 %15)
store i32 %16, i32* %4
%17 = load i32, i32* %4
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %30
19: ; preds = %11
%20 = call i32 @time(i32* null)
store i32 %20, i32* %6
%21 = load i32, i32* %6
%22 = load i32, i32* %7
%23 = call double @difftime(i32 %21, i32 %22)
store double %23, double* %5
%24 = load double, double* %5
%25 = fcmp ogt double %24, 2.200000e+00
br i1 %25, label %26, label %28
26: ; preds = %19
%27 = call i32 @log_message(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %2
br label %54
28: ; preds = %19
%29 = call i32 @usleep(i32 10000)
br label %53
30: ; preds = %11
%31 = call i32 (...) @getpid()
%32 = call i32 @printf_len(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %31)
%33 = call i32 @alloca(i32 %32)
%34 = call i32 (...) @getpid()
%35 = call i8* @printf_into(i32 %33, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %34)
store i8* %35, i8** %8
%36 = load i32, i32* %3
%37 = load i8*, i8** %8
%38 = load i8*, i8** %8
%39 = call i32 @strlen(i8* %38)
%40 = call i32 @write(i32 %36, i8* %37, i32 %39)
store i32 %40, i32* %9
%41 = load i32, i32* %9
%42 = icmp eq i32 %41, -1
br i1 %42, label %43, label %45
43: ; preds = %30
%44 = call i32 @log_message(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.2, i64 0, i64 0))
br label %45
45: ; preds = %43, %30
%46 = load i32, i32* %3
%47 = call i32 @fsync(i32 %46)
store i32 %47, i32* %9
%48 = load i32, i32* %9
%49 = icmp eq i32 %48, -1
br i1 %49, label %50, label %52
50: ; preds = %45
%51 = call i32 @log_message(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0))
br label %52
52: ; preds = %50, %45
store i32 1, i32* %2
br label %54
53: ; preds = %28
br label %11
54: ; preds = %52, %26
%55 = load i32, i32* %2
ret i32 %55
}

LLVM IR (-O2):
@LOCK_EX = global i32 0
@LOCK_NB = global i32 0
@.str = [39 x i8] c"api: atsha_lock: operation lock failed\00"
@.str.1 = [4 x i8] c"%d\0A\00"
@.str.2 = [38 x i8] c"api: atsha_lock: write of PID failed\0A\00"
@.str.3 = [31 x i8] c"api: atsha_lock: fsync failed\0A\00"
define i32 @atsha_lock(i32 %0) {
%2 = tail call i32 @time(i32* null)
%3 = load i32, i32* @LOCK_EX
%4 = load i32, i32* @LOCK_NB
%5 = or i32 %4, %3
%6 = tail call i32 @flock(i32 %0, i32 %5)
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %19
8: ; preds = %1, %12
%9 = tail call i32 @time(i32* null)
%10 = tail call double @difftime(i32 %9, i32 %2)
%11 = fcmp ogt double %10, 2.200000e+00
br i1 %11, label %33, label %12
12: ; preds = %8
%13 = tail call i32 @usleep(i32 10000)
%14 = load i32, i32* @LOCK_EX
%15 = load i32, i32* @LOCK_NB
%16 = or i32 %15, %14
%17 = tail call i32 @flock(i32 %0, i32 %16)
%18 = icmp eq i32 %17, -1
br i1 %18, label %8, label %19
19: ; preds = %12, %1
%20 = tail call i32 (...) @getpid()
%21 = tail call i32 @printf_len(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %20)
%22 = tail call i32 @alloca(i32 %21)
%23 = tail call i32 (...) @getpid()
%24 = tail call i8* @printf_into(i32 %22, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %23)
%25 = tail call i32 @strlen(i8* %24)
%26 = tail call i32 @write(i32 %0, i8* %24, i32 %25)
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %30
28: ; preds = %19
%29 = tail call i32 @log_message(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.2, i64 0, i64 0))
br label %30
30: ; preds = %28, %19
%31 = tail call i32 @fsync(i32 %0)
%32 = icmp eq i32 %31, -1
br i1 %32, label %33, label %37
33: ; preds = %8, %30
%34 = phi i8* [ getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0), %30 ], [ getelementptr inbounds ([39 x i8], [39 x i8]* @.str, i64 0, i64 0), %8 ]
%35 = phi i32 [ 1, %30 ], [ 0, %8 ]
%36 = tail call i32 @log_message(i8* %34)
br label %37
37: ; preds = %33, %30
%38 = phi i32 [ 1, %30 ], [ %35, %33 ]
ret i32 %38
}