#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 rl_key_set(struct rlite *, unsigned char const *, long, char, long,
               int /*<<< orphan*/, long);
int rl_multi_string_append(struct rlite *, long, unsigned char *, long, long *);
int rl_multi_string_set(struct rlite *, long *, unsigned char *, long);
int rl_string_get_objects(struct rlite *, unsigned char const *, long, long *,
                          int /*<<< orphan*/ *, long *);
int rl_append(struct rlite *db, const unsigned char *key, long keylen,
              unsigned char *value, long valuelen, long *newlength) {
  int retval;
  long page_number;
  long version;
  retval = rl_string_get_objects(db, key, keylen, &page_number, NULL, &version);
  if (0 != retval && 3 != retval)
    goto cleanup;
  ;
  if (retval == 3) {
    retval = rl_multi_string_set(db, &page_number, value, valuelen);
    if (0 != retval)
      goto cleanup;
    ;
    version = rand();
    retval = rl_key_set(db, key, keylen, 'T', page_number, 0, version);
    if (0 != retval)
      goto cleanup;
    ;
    if (newlength) {
      *newlength = valuelen;
    }
  } else {
    retval = rl_key_set(db, key, keylen, 'T', page_number, 0, version + 1);
    if (0 != retval)
      goto cleanup;
    ;
    retval =
        rl_multi_string_append(db, page_number, value, valuelen, newlength);
    if (0 != retval)
      goto cleanup;
    ;
  }
  retval = 0;
cleanup:
  return retval;
}


x86 assembly (clang, -O0):
<rl_append>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
mov    %r9,-0x30(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
lea    -0x40(%rbp),%rcx
xor    %eax,%eax
mov    %eax,%r8d
lea    -0x48(%rbp),%r9
call   1050 <rl_string_get_objects@plt>
mov    %eax,-0x34(%rbp)
xor    %eax,%eax
cmp    -0x34(%rbp),%eax
je     11af <rl_append+0x5f>
mov    $0x3,%eax
cmp    -0x34(%rbp),%eax
je     11af <rl_append+0x5f>
jmp    12b3 <rl_append+0x163>
cmpl   $0x3,-0x34(%rbp)
jne    123f <rl_append+0xef>
mov    -0x8(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    -0x28(%rbp),%rcx
lea    -0x40(%rbp),%rsi
call   1030 <rl_multi_string_set@plt>
mov    %eax,-0x34(%rbp)
xor    %eax,%eax
cmp    -0x34(%rbp),%eax
je     11e1 <rl_append+0x91>
jmp    12b3 <rl_append+0x163>
mov    $0x0,%al
call   1070 <rand@plt>
mov    %rax,-0x48(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
mov    -0x40(%rbp),%r8
mov    -0x48(%rbp),%rax
mov    $0x54,%ecx
xor    %r9d,%r9d
mov    %rax,(%rsp)
call   1040 <rl_key_set@plt>
mov    %eax,-0x34(%rbp)
xor    %eax,%eax
cmp    -0x34(%rbp),%eax
je     1224 <rl_append+0xd4>
jmp    12b3 <rl_append+0x163>
cmpq   $0x0,-0x30(%rbp)
je     123a <rl_append+0xea>
mov    -0x28(%rbp),%rcx
mov    -0x30(%rbp),%rax
mov    %rcx,(%rax)
jmp    12ac <rl_append+0x15c>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
mov    -0x40(%rbp),%r8
mov    -0x48(%rbp),%rax
add    $0x1,%rax
mov    $0x54,%ecx
xor    %r9d,%r9d
mov    %rax,(%rsp)
call   1040 <rl_key_set@plt>
mov    %eax,-0x34(%rbp)
xor    %eax,%eax
cmp    -0x34(%rbp),%eax
je     127b <rl_append+0x12b>
jmp    12b3 <rl_append+0x163>
mov    -0x8(%rbp),%rdi
mov    -0x40(%rbp),%rsi
mov    -0x20(%rbp),%rdx
mov    -0x28(%rbp),%rcx
mov    -0x30(%rbp),%r8
call   1060 <rl_multi_string_append@plt>
mov    %eax,-0x34(%rbp)
xor    %eax,%eax
cmp    -0x34(%rbp),%eax
je     12a7 <rl_append+0x157>
jmp    12b3 <rl_append+0x163>
jmp    12ac <rl_append+0x15c>
movl   $0x0,-0x34(%rbp)
mov    -0x34(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rl_append>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %r9,%r14
mov    %r8,%r15
mov    %rcx,%rbp
mov    %rdx,%r12
mov    %rsi,%r13
mov    %rdi,%rbx
lea    0x8(%rsp),%rcx
lea    0x10(%rsp),%r9
xor    %r8d,%r8d
call   1050 <rl_string_get_objects@plt>
test   %eax,%eax
je     11e3 <rl_append+0x93>
cmp    $0x3,%eax
jne    122b <rl_append+0xdb>
lea    0x8(%rsp),%rsi
mov    %rbx,%rdi
mov    %rbp,%rdx
mov    %r15,%rcx
call   1030 <rl_multi_string_set@plt>
test   %eax,%eax
jne    122b <rl_append+0xdb>
xor    %eax,%eax
call   1070 <rand@plt>
mov    %rax,0x10(%rsp)
mov    0x8(%rsp),%r8
mov    %rax,(%rsp)
mov    %rbx,%rdi
mov    %r13,%rsi
mov    %r12,%rdx
mov    $0x54,%ecx
xor    %r9d,%r9d
call   1040 <rl_key_set@plt>
test   %eax,%eax
jne    122b <rl_append+0xdb>
test   %r14,%r14
je     1229 <rl_append+0xd9>
mov    %r15,(%r14)
jmp    1229 <rl_append+0xd9>
mov    0x8(%rsp),%r8
mov    0x10(%rsp),%rax
add    $0x1,%rax
mov    %rax,(%rsp)
mov    %rbx,%rdi
mov    %r13,%rsi
mov    %r12,%rdx
mov    $0x54,%ecx
xor    %r9d,%r9d
call   1040 <rl_key_set@plt>
test   %eax,%eax
jne    122b <rl_append+0xdb>
mov    0x8(%rsp),%rsi
mov    %rbx,%rdi
mov    %rbp,%rdx
mov    %r15,%rcx
mov    %r14,%r8
call   1060 <rl_multi_string_append@plt>
test   %eax,%eax
jne    122b <rl_append+0xdb>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.rlite = type { i32 }
define i32 @rl_append(%struct.rlite* %0, i8* %1, i64 %2, i8* %3, i64 %4, i64* %5) {
%7 = alloca %struct.rlite*
%8 = alloca i8*
%9 = alloca i64
%10 = alloca i8*
%11 = alloca i64
%12 = alloca i64*
%13 = alloca i32
%14 = alloca i64
%15 = alloca i64
store %struct.rlite* %0, %struct.rlite** %7
store i8* %1, i8** %8
store i64 %2, i64* %9
store i8* %3, i8** %10
store i64 %4, i64* %11
store i64* %5, i64** %12
%16 = load %struct.rlite*, %struct.rlite** %7
%17 = load i8*, i8** %8
%18 = load i64, i64* %9
%19 = call i32 @rl_string_get_objects(%struct.rlite* %16, i8* %17, i64 %18, i64* %14, i32* null, i64* %15)
store i32 %19, i32* %13
%20 = load i32, i32* %13
%21 = icmp ne i32 0, %20
br i1 %21, label %22, label %26
22: ; preds = %6
%23 = load i32, i32* %13
%24 = icmp ne i32 3, %23
br i1 %24, label %25, label %26
25: ; preds = %22
br label %78
26: ; preds = %22, %6
%27 = load i32, i32* %13
%28 = icmp eq i32 %27, 3
br i1 %28, label %29, label %55
29: ; preds = %26
%30 = load %struct.rlite*, %struct.rlite** %7
%31 = load i8*, i8** %10
%32 = load i64, i64* %11
%33 = call i32 @rl_multi_string_set(%struct.rlite* %30, i64* %14, i8* %31, i64 %32)
store i32 %33, i32* %13
%34 = load i32, i32* %13
%35 = icmp ne i32 0, %34
br i1 %35, label %36, label %37
36: ; preds = %29
br label %78
37: ; preds = %29
%38 = call i64 (...) @rand()
store i64 %38, i64* %15
%39 = load %struct.rlite*, %struct.rlite** %7
%40 = load i8*, i8** %8
%41 = load i64, i64* %9
%42 = load i64, i64* %14
%43 = load i64, i64* %15
%44 = call i32 @rl_key_set(%struct.rlite* %39, i8* %40, i64 %41, i8 signext 84, i64 %42, i32 0, i64 %43)
store i32 %44, i32* %13
%45 = load i32, i32* %13
%46 = icmp ne i32 0, %45
br i1 %46, label %47, label %48
47: ; preds = %37
br label %78
48: ; preds = %37
%49 = load i64*, i64** %12
%50 = icmp ne i64* %49, null
br i1 %50, label %51, label %54
51: ; preds = %48
%52 = load i64, i64* %11
%53 = load i64*, i64** %12
store i64 %52, i64* %53
br label %54
54: ; preds = %51, %48
br label %77
55: ; preds = %26
%56 = load %struct.rlite*, %struct.rlite** %7
%57 = load i8*, i8** %8
%58 = load i64, i64* %9
%59 = load i64, i64* %14
%60 = load i64, i64* %15
%61 = add nsw i64 %60, 1
%62 = call i32 @rl_key_set(%struct.rlite* %56, i8* %57, i64 %58, i8 signext 84, i64 %59, i32 0, i64 %61)
store i32 %62, i32* %13
%63 = load i32, i32* %13
%64 = icmp ne i32 0, %63
br i1 %64, label %65, label %66
65: ; preds = %55
br label %78
66: ; preds = %55
%67 = load %struct.rlite*, %struct.rlite** %7
%68 = load i64, i64* %14
%69 = load i8*, i8** %10
%70 = load i64, i64* %11
%71 = load i64*, i64** %12
%72 = call i32 @rl_multi_string_append(%struct.rlite* %67, i64 %68, i8* %69, i64 %70, i64* %71)
store i32 %72, i32* %13
%73 = load i32, i32* %13
%74 = icmp ne i32 0, %73
br i1 %74, label %75, label %76
75: ; preds = %66
br label %78
76: ; preds = %66
br label %77
77: ; preds = %76, %54
store i32 0, i32* %13
br label %78
78: ; preds = %77, %75, %65, %47, %36, %25
%79 = load i32, i32* %13
ret i32 %79
}

LLVM IR (-O2):
%struct.rlite = type { i32 }
define i32 @rl_append(%struct.rlite* %0, i8* %1, i64 %2, i8* %3, i64 %4, i64* %5) {
%7 = alloca i64
%8 = alloca i64
%9 = bitcast i64* %7 to i8*
%10 = bitcast i64* %8 to i8*
%11 = call i32 @rl_string_get_objects(%struct.rlite* %0, i8* %1, i64 %2, i64* %7, i32* null, i64* %8)
switch i32 %11, label %34 [
i32 3, label %12
i32 0, label %23
]
12: ; preds = %6
%13 = call i32 @rl_multi_string_set(%struct.rlite* %0, i64* %7, i8* %3, i64 %4)
%14 = icmp eq i32 %13, 0
br i1 %14, label %15, label %34
15: ; preds = %12
%16 = call i64 (...) @rand()
store i64 %16, i64* %8
%17 = load i64, i64* %7
%18 = call i32 @rl_key_set(%struct.rlite* %0, i8* %1, i64 %2, i8 signext 84, i64 %17, i32 0, i64 %16)
%19 = icmp eq i32 %18, 0
br i1 %19, label %20, label %34
20: ; preds = %15
%21 = icmp eq i64* %5, null
br i1 %21, label %33, label %22
22: ; preds = %20
store i64 %4, i64* %5
br label %33
23: ; preds = %6
%24 = load i64, i64* %7
%25 = load i64, i64* %8
%26 = add nsw i64 %25, 1
%27 = call i32 @rl_key_set(%struct.rlite* %0, i8* %1, i64 %2, i8 signext 84, i64 %24, i32 0, i64 %26)
%28 = icmp eq i32 %27, 0
br i1 %28, label %29, label %34
29: ; preds = %23
%30 = load i64, i64* %7
%31 = call i32 @rl_multi_string_append(%struct.rlite* %0, i64 %30, i8* %3, i64 %4, i64* %5)
%32 = icmp eq i32 %31, 0
br i1 %32, label %33, label %34
33: ; preds = %29, %20, %22
br label %34
34: ; preds = %6, %29, %23, %15, %12, %33
%35 = phi i32 [ %11, %6 ], [ %13, %12 ], [ %18, %15 ], [ 0, %33 ], [ %27, %23 ], [ %31, %29 ]
ret i32 %35
}