#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*/ lock();
scalar_t__ locked();
int rand();
int /*<<< orphan*/ unlock();
int f() {
  if (!locked()) {
    lock();
  }
  if (rand() % 2 == 0) {
    if (locked()) {
      unlock();
    }
  } else {
    f();
    if (!locked()) {
      lock();
    }
  }
}


x86 assembly (clang, -O0):
<f>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    $0x0,%al
call   1060 <locked@plt>
cmp    $0x0,%rax
jne    1170 <f+0x20>
mov    $0x0,%al
call   1040 <lock@plt>
mov    $0x0,%al
call   1070 <rand@plt>
mov    $0x2,%ecx
cltd
idiv   %ecx
cmp    $0x0,%edx
jne    11a5 <f+0x55>
mov    $0x0,%al
call   1060 <locked@plt>
cmp    $0x0,%rax
je     11a0 <f+0x50>
mov    $0x0,%al
call   1030 <unlock@plt>
jmp    11c7 <f+0x77>
call   1050 <f@plt>
mov    $0x0,%al
call   1060 <locked@plt>
cmp    $0x0,%rax
jne    11c2 <f+0x72>
mov    $0x0,%al
call   1040 <lock@plt>
jmp    11c7 <f+0x77>
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<f>:
push   %rax
xor    %eax,%eax
call   1060 <locked@plt>
test   %rax,%rax
jne    1164 <f+0x14>
xor    %eax,%eax
call   1040 <lock@plt>
xor    %eax,%eax
call   1070 <rand@plt>
test   $0x1,%al
jne    1184 <f+0x34>
xor    %eax,%eax
call   1060 <locked@plt>
test   %rax,%rax
je     1195 <f+0x45>
xor    %eax,%eax
call   1030 <unlock@plt>
pop    %rcx
ret
call   1050 <f@plt>
xor    %eax,%eax
call   1060 <locked@plt>
test   %rax,%rax
je     1197 <f+0x47>
pop    %rcx
ret
xor    %eax,%eax
call   1040 <lock@plt>
pop    %rcx
ret

LLVM IR (-O0):
define i32 @f() {
%1 = alloca i32
%2 = call i64 (...) @locked()
%3 = icmp ne i64 %2, 0
br i1 %3, label %6, label %4
4: ; preds = %0
%5 = call i32 (...) @lock()
br label %6
6: ; preds = %4, %0
%7 = call i32 (...) @rand()
%8 = srem i32 %7, 2
%9 = icmp eq i32 %8, 0
br i1 %9, label %10, label %16
10: ; preds = %6
%11 = call i64 (...) @locked()
%12 = icmp ne i64 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %10
%14 = call i32 (...) @unlock()
br label %15
15: ; preds = %13, %10
br label %23
16: ; preds = %6
%17 = call i32 @f()
%18 = call i64 (...) @locked()
%19 = icmp ne i64 %18, 0
br i1 %19, label %22, label %20
20: ; preds = %16
%21 = call i32 (...) @lock()
br label %22
22: ; preds = %20, %16
br label %23
23: ; preds = %22, %15
%24 = load i32, i32* %1
ret i32 %24
}

LLVM IR (-O2):
define i32 @f() {
%1 = tail call i64 (...) @locked()
%2 = icmp eq i64 %1, 0
br i1 %2, label %3, label %5
3: ; preds = %0
%4 = tail call i32 (...) @lock()
br label %5
5: ; preds = %3, %0
%6 = tail call i32 (...) @rand()
%7 = and i32 %6, 1
%8 = icmp eq i32 %7, 0
br i1 %8, label %9, label %14
9: ; preds = %5
%10 = tail call i64 (...) @locked()
%11 = icmp eq i64 %10, 0
br i1 %11, label %20, label %12
12: ; preds = %9
%13 = tail call i32 (...) @unlock()
br label %20
14: ; preds = %5
%15 = tail call i32 @f()
%16 = tail call i64 (...) @locked()
%17 = icmp eq i64 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %14
%19 = tail call i32 (...) @lock()
br label %20
20: ; preds = %14, %18, %9, %12
ret i32 undef
}