#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 rlite {
  int dummy;
};

/* Variables and functions */
long rand();
int /*<<< orphan*/ rl_alloc_page_number(struct rlite *, long *);
int rl_key_get(struct rlite *, unsigned char const *, long, unsigned char *,
               int /*<<< orphan*/ *, long *, int /*<<< orphan*/ *, long *);
int rl_key_set(struct rlite *, unsigned char const *, long, unsigned char, long,
               int /*<<< orphan*/, long);
int rl_key_get_or_create(struct rlite *db, const unsigned char *key,
                         long keylen, unsigned char type, long *page,
                         long *version) {
  unsigned char existing_type;
  int retval =
      rl_key_get(db, key, keylen, &existing_type, NULL, page, NULL, version);
  long _version;
  if (retval == 2) {
    if (existing_type != type) {
      return 8;
    }
  } else if (retval == 3) {
    _version = rand();
    if (version) {
      *version = _version;
    }
    rl_alloc_page_number(db, page);
    retval = rl_key_set(db, key, keylen, type, *page, 0, _version);
    if (0 != retval)
      goto cleanup;
    ;
    retval = 3;
  }
cleanup:
  return retval;
}


x86 assembly (clang, -O0):
<rl_key_get_or_create>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %cl,%al
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %al,-0x21(%rbp)
mov    %r8,-0x30(%rbp)
mov    %r9,-0x38(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
mov    -0x30(%rbp),%r9
mov    -0x38(%rbp),%rax
lea    -0x39(%rbp),%rcx
xor    %r8d,%r8d
movq   $0x0,(%rsp)
mov    %rax,0x8(%rsp)
call   1030 <rl_key_get@plt>
mov    %eax,-0x40(%rbp)
cmpl   $0x2,-0x40(%rbp)
jne    11bc <rl_key_get_or_create+0x7c>
movzbl -0x39(%rbp),%eax
movzbl -0x21(%rbp),%ecx
cmp    %ecx,%eax
je     11b7 <rl_key_get_or_create+0x77>
movl   $0x8,-0x4(%rbp)
jmp    1247 <rl_key_get_or_create+0x107>
jmp    123c <rl_key_get_or_create+0xfc>
cmpl   $0x3,-0x40(%rbp)
jne    1237 <rl_key_get_or_create+0xf7>
mov    $0x0,%al
call   1060 <rand@plt>
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x38(%rbp)
je     11e7 <rl_key_get_or_create+0xa7>
mov    -0x48(%rbp),%rcx
mov    -0x38(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x10(%rbp),%rdi
mov    -0x30(%rbp),%rsi
call   1040 <rl_alloc_page_number@plt>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
mov    -0x21(%rbp),%cl
mov    -0x30(%rbp),%rax
mov    (%rax),%r8
mov    -0x48(%rbp),%rax
xor    %r9d,%r9d
movzbl %cl,%ecx
mov    %rax,(%rsp)
call   1050 <rl_key_set@plt>
mov    %eax,-0x40(%rbp)
xor    %eax,%eax
cmp    -0x40(%rbp),%eax
je     1230 <rl_key_get_or_create+0xf0>
jmp    1241 <rl_key_get_or_create+0x101>
movl   $0x3,-0x40(%rbp)
jmp    123c <rl_key_get_or_create+0xfc>
jmp    1241 <rl_key_get_or_create+0x101>
mov    -0x40(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rl_key_get_or_create>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %r9,%rbx
mov    %r8,%rbp
mov    %ecx,%r14d
mov    %rdx,%r15
mov    %rsi,%r12
mov    %rdi,%r13
lea    0x7(%rsp),%rcx
xor    %r8d,%r8d
mov    %rbp,%r9
push   %rbx
push   $0x0
call   1030 <rl_key_get@plt>
add    $0x10,%rsp
cmp    $0x3,%eax
je     1191 <rl_key_get_or_create+0x51>
cmp    $0x2,%eax
jne    11dc <rl_key_get_or_create+0x9c>
mov    $0x8,%ecx
mov    $0x2,%eax
cmp    %r14b,0x7(%rsp)
je     11dc <rl_key_get_or_create+0x9c>
jmp    11de <rl_key_get_or_create+0x9e>
xor    %eax,%eax
call   1060 <rand@plt>
test   %rbx,%rbx
je     11a0 <rl_key_get_or_create+0x60>
mov    %rax,(%rbx)
mov    %r13,%rdi
mov    %rbp,%rsi
mov    %rax,%rbx
call   1040 <rl_alloc_page_number@plt>
mov    0x0(%rbp),%r8
sub    $0x8,%rsp
movzbl %r14b,%ecx
mov    %r13,%rdi
mov    %r12,%rsi
mov    %r15,%rdx
xor    %r9d,%r9d
push   %rbx
call   1050 <rl_key_set@plt>
add    $0x10,%rsp
mov    %eax,%ecx
test   %eax,%eax
mov    $0x3,%eax
cmovne %ecx,%eax
mov    %eax,%ecx
mov    %ecx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.rlite = type { i32 }
define i32 @rl_key_get_or_create(%struct.rlite* %0, i8* %1, i64 %2, i8 zeroext %3, i64* %4, i64* %5) {
%7 = alloca i32
%8 = alloca %struct.rlite*
%9 = alloca i8*
%10 = alloca i64
%11 = alloca i8
%12 = alloca i64*
%13 = alloca i64*
%14 = alloca i8
%15 = alloca i32
%16 = alloca i64
store %struct.rlite* %0, %struct.rlite** %8
store i8* %1, i8** %9
store i64 %2, i64* %10
store i8 %3, i8* %11
store i64* %4, i64** %12
store i64* %5, i64** %13
%17 = load %struct.rlite*, %struct.rlite** %8
%18 = load i8*, i8** %9
%19 = load i64, i64* %10
%20 = load i64*, i64** %12
%21 = load i64*, i64** %13
%22 = call i32 @rl_key_get(%struct.rlite* %17, i8* %18, i64 %19, i8* %14, i32* null, i64* %20, i32* null, i64* %21)
store i32 %22, i32* %15
%23 = load i32, i32* %15
%24 = icmp eq i32 %23, 2
br i1 %24, label %25, label %33
25: ; preds = %6
%26 = load i8, i8* %14
%27 = zext i8 %26 to i32
%28 = load i8, i8* %11
%29 = zext i8 %28 to i32
%30 = icmp ne i32 %27, %29
br i1 %30, label %31, label %32
31: ; preds = %25
store i32 8, i32* %7
br label %63
32: ; preds = %25
br label %60
33: ; preds = %6
%34 = load i32, i32* %15
%35 = icmp eq i32 %34, 3
br i1 %35, label %36, label %59
36: ; preds = %33
%37 = call i64 (...) @rand()
store i64 %37, i64* %16
%38 = load i64*, i64** %13
%39 = icmp ne i64* %38, null
br i1 %39, label %40, label %43
40: ; preds = %36
%41 = load i64, i64* %16
%42 = load i64*, i64** %13
store i64 %41, i64* %42
br label %43
43: ; preds = %40, %36
%44 = load %struct.rlite*, %struct.rlite** %8
%45 = load i64*, i64** %12
%46 = call i32 @rl_alloc_page_number(%struct.rlite* %44, i64* %45)
%47 = load %struct.rlite*, %struct.rlite** %8
%48 = load i8*, i8** %9
%49 = load i64, i64* %10
%50 = load i8, i8* %11
%51 = load i64*, i64** %12
%52 = load i64, i64* %51
%53 = load i64, i64* %16
%54 = call i32 @rl_key_set(%struct.rlite* %47, i8* %48, i64 %49, i8 zeroext %50, i64 %52, i32 0, i64 %53)
store i32 %54, i32* %15
%55 = load i32, i32* %15
%56 = icmp ne i32 0, %55
br i1 %56, label %57, label %58
57: ; preds = %43
br label %61
58: ; preds = %43
store i32 3, i32* %15
br label %59
59: ; preds = %58, %33
br label %60
60: ; preds = %59, %32
br label %61
61: ; preds = %60, %57
%62 = load i32, i32* %15
store i32 %62, i32* %7
br label %63
63: ; preds = %61, %31
%64 = load i32, i32* %7
ret i32 %64
}

LLVM IR (-O2):
%struct.rlite = type { i32 }
define i32 @rl_key_get_or_create(%struct.rlite* %0, i8* %1, i64 %2, i8 zeroext %3, i64* %4, i64* %5) {
%7 = alloca i8
%8 = call i32 @rl_key_get(%struct.rlite* %0, i8* %1, i64 %2, i8* %7, i32* null, i64* %4, i32* null, i64* %5)
switch i32 %8, label %22 [
i32 2, label %9
i32 3, label %12
]
9: ; preds = %6
%10 = load i8, i8* %7
%11 = icmp eq i8 %10, %3
br i1 %11, label %22, label %24
12: ; preds = %6
%13 = call i64 (...) @rand()
%14 = icmp eq i64* %5, null
br i1 %14, label %16, label %15
15: ; preds = %12
store i64 %13, i64* %5
br label %16
16: ; preds = %15, %12
%17 = call i32 @rl_alloc_page_number(%struct.rlite* %0, i64* %4)
%18 = load i64, i64* %4
%19 = call i32 @rl_key_set(%struct.rlite* %0, i8* %1, i64 %2, i8 zeroext %3, i64 %18, i32 0, i64 %13)
%20 = icmp eq i32 %19, 0
%21 = select i1 %20, i32 3, i32 %19
br label %22
22: ; preds = %6, %16, %9
%23 = phi i32 [ 2, %9 ], [ %21, %16 ], [ %8, %6 ]
br label %24
24: ; preds = %9, %22
%25 = phi i32 [ %23, %22 ], [ 8, %9 ]
ret i32 %25
}