#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 */
struct commonio_db {
  int dummy;
};

/* Variables and functions */
scalar_t__ commonio_lock_nowait(struct commonio_db *);
scalar_t__ geteuid();
int /*<<< orphan*/ sleep(int);
int commonio_lock(struct commonio_db *db) {
  int i;
  for (i = 0; i < 15; i++) {
    if (i > 0)
      sleep(1);
    if (commonio_lock_nowait(db))
      return 1;
    if (geteuid() != 0)
      return 0;
  }
  return 0;
}


x86 assembly (clang, -O0):
<commonio_lock>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
cmpl   $0xf,-0x14(%rbp)
jge    11b0 <commonio_lock+0x80>
cmpl   $0x0,-0x14(%rbp)
jle    1161 <commonio_lock+0x31>
mov    $0x1,%edi
call   1050 <sleep@plt>
mov    -0x10(%rbp),%rdi
call   1040 <commonio_lock_nowait@plt>
cmp    $0x0,%rax
je     1180 <commonio_lock+0x50>
movl   $0x1,-0x4(%rbp)
jmp    11b7 <commonio_lock+0x87>
mov    $0x0,%al
call   1030 <geteuid@plt>
cmp    $0x0,%rax
je     119d <commonio_lock+0x6d>
movl   $0x0,-0x4(%rbp)
jmp    11b7 <commonio_lock+0x87>
jmp    11a2 <commonio_lock+0x72>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1143 <commonio_lock+0x13>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<commonio_lock>:
push   %rbp
push   %r14
push   %rbx
mov    %rdi,%r14
mov    $0xe,%ebx
xor    %ebp,%ebp
xchg   %ax,%ax
cmp    $0xe,%ebx
je     114f <commonio_lock+0x1f>
mov    $0x1,%edi
call   1050 <sleep@plt>
mov    %r14,%rdi
call   1040 <commonio_lock_nowait@plt>
test   %rax,%rax
jne    1174 <commonio_lock+0x44>
xor    %eax,%eax
call   1030 <geteuid@plt>
sub    $0x1,%ebx
setb   %cl
test   %rax,%rax
jne    1179 <commonio_lock+0x49>
test   %cl,%cl
je     1140 <commonio_lock+0x10>
jmp    1179 <commonio_lock+0x49>
mov    $0x1,%ebp
mov    %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.commonio_db = type { i32 }
define i32 @commonio_lock(%struct.commonio_db* %0) {
%2 = alloca i32
%3 = alloca %struct.commonio_db*
%4 = alloca i32
store %struct.commonio_db* %0, %struct.commonio_db** %3
store i32 0, i32* %4
br label %5
5: ; preds = %23, %1
%6 = load i32, i32* %4
%7 = icmp slt i32 %6, 15
br i1 %7, label %8, label %26
8: ; preds = %5
%9 = load i32, i32* %4
%10 = icmp sgt i32 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %8
%12 = call i32 @sleep(i32 1)
br label %13
13: ; preds = %11, %8
%14 = load %struct.commonio_db*, %struct.commonio_db** %3
%15 = call i64 @commonio_lock_nowait(%struct.commonio_db* %14)
%16 = icmp ne i64 %15, 0
br i1 %16, label %17, label %18
17: ; preds = %13
store i32 1, i32* %2
br label %27
18: ; preds = %13
%19 = call i64 (...) @geteuid()
%20 = icmp ne i64 %19, 0
br i1 %20, label %21, label %22
21: ; preds = %18
store i32 0, i32* %2
br label %27
22: ; preds = %18
br label %23
23: ; preds = %22
%24 = load i32, i32* %4
%25 = add nsw i32 %24, 1
store i32 %25, i32* %4
br label %5
26: ; preds = %5
store i32 0, i32* %2
br label %27
27: ; preds = %26, %21, %17
%28 = load i32, i32* %2
ret i32 %28
}

LLVM IR (-O2):
%struct.commonio_db = type { i32 }
define i32 @commonio_lock(%struct.commonio_db* %0) {
br label %2
2: ; preds = %10, %1
%3 = phi i32 [ 0, %1 ], [ %13, %10 ]
%4 = icmp eq i32 %3, 0
br i1 %4, label %7, label %5
5: ; preds = %2
%6 = tail call i32 @sleep(i32 1)
br label %7
7: ; preds = %5, %2
%8 = tail call i64 @commonio_lock_nowait(%struct.commonio_db* %0)
%9 = icmp eq i64 %8, 0
br i1 %9, label %10, label %16
10: ; preds = %7
%11 = tail call i64 (...) @geteuid()
%12 = icmp ne i64 %11, 0
%13 = add nuw nsw i32 %3, 1
%14 = icmp eq i32 %13, 15
%15 = select i1 %12, i1 true, i1 %14
br i1 %15, label %16, label %2
16: ; preds = %10, %7
%17 = phi i32 [ 1, %7 ], [ 0, %10 ]
ret i32 %17
}