#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 */
int /*<<< orphan*/ free(unsigned char *);
unsigned char *malloc(int);
int /*<<< orphan*/ memset(unsigned char *, int /*<<< orphan*/, long);
int rl_append(struct rlite *, unsigned char const *, long, unsigned char *,
              long, long *);
int rl_key_set(struct rlite *, unsigned char const *, long, char, long,
               unsigned long long, long);
int rl_multi_string_setrange(struct rlite *, long, unsigned char *, long, long,
                             long *);
int rl_set(struct rlite *, unsigned char const *, long, unsigned char *, long,
           int /*<<< orphan*/, int /*<<< orphan*/);
int rl_string_get_objects(struct rlite *, unsigned char const *, long, long *,
                          unsigned long long *, long *);
int rl_setrange(struct rlite *db, const unsigned char *key, long keylen,
                long index, unsigned char *value, long valuelen,
                long *newlength) {
  long page_number;
  long version;
  unsigned long long expires;
  int retval;
  if (valuelen + index > 512 * 1024 * 1024) {
    retval = 5;
    goto cleanup;
  }
  retval =
      rl_string_get_objects(db, key, keylen, &page_number, &expires, &version);
  if (0 != retval && 3 != retval)
    goto cleanup;
  ;
  if (retval == 3) {
    unsigned char *padding;
    padding = malloc(sizeof(unsigned char) * index);
    if (!padding) {
      retval = 4;
      goto cleanup;
    };
    memset(padding, 0, index);
    retval = rl_set(db, key, keylen, padding, index, 0, 0);
    if (0 != retval)
      goto cleanup;
    ;
    free(padding);
    retval = rl_append(db, key, keylen, value, valuelen, newlength);
    if (0 != retval)
      goto cleanup;
    ;
  } else if (retval == 0) {
    retval =
        rl_key_set(db, key, keylen, 'T', page_number, expires, version + 1);
    if (0 != retval)
      goto cleanup;
    ;
    retval = rl_multi_string_setrange(db, page_number, value, valuelen, index,
                                      newlength);
    if (0 != retval)
      goto cleanup;
    ;
  }
  retval = 0;
cleanup:
  return retval;
}


x86 assembly (clang, -O0):
<rl_setrange>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    0x10(%rbp),%rax
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    -0x30(%rbp),%rax
add    -0x20(%rbp),%rax
cmp    $0x20000000,%rax
jle    11c4 <rl_setrange+0x44>
movl   $0x5,-0x4c(%rbp)
jmp    1341 <rl_setrange+0x1c1>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
lea    -0x38(%rbp),%rcx
lea    -0x48(%rbp),%r8
lea    -0x40(%rbp),%r9
call   1090 <rl_string_get_objects@plt>
mov    %eax,-0x4c(%rbp)
xor    %eax,%eax
cmp    -0x4c(%rbp),%eax
je     1202 <rl_setrange+0x82>
mov    $0x3,%eax
cmp    -0x4c(%rbp),%eax
je     1202 <rl_setrange+0x82>
jmp    1341 <rl_setrange+0x1c1>
cmpl   $0x3,-0x4c(%rbp)
jne    12b9 <rl_setrange+0x139>
mov    -0x20(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%edi
call   1060 <malloc@plt>
mov    %rax,-0x58(%rbp)
cmpq   $0x0,-0x58(%rbp)
jne    1236 <rl_setrange+0xb6>
movl   $0x4,-0x4c(%rbp)
jmp    1341 <rl_setrange+0x1c1>
mov    -0x58(%rbp),%rdi
mov    -0x20(%rbp),%rdx
xor    %esi,%esi
call   1050 <memset@plt>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
mov    -0x58(%rbp),%rcx
mov    -0x20(%rbp),%r8
xor    %r9d,%r9d
movl   $0x0,(%rsp)
call   10a0 <rl_set@plt>
mov    %eax,-0x4c(%rbp)
xor    %eax,%eax
cmp    -0x4c(%rbp),%eax
je     127b <rl_setrange+0xfb>
jmp    1341 <rl_setrange+0x1c1>
mov    -0x58(%rbp),%rdi
call   1030 <free@plt>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
mov    -0x28(%rbp),%rcx
mov    -0x30(%rbp),%r8
mov    0x10(%rbp),%r9
call   1080 <rl_append@plt>
mov    %eax,-0x4c(%rbp)
xor    %eax,%eax
cmp    -0x4c(%rbp),%eax
je     12b4 <rl_setrange+0x134>
jmp    1341 <rl_setrange+0x1c1>
jmp    133a <rl_setrange+0x1ba>
cmpl   $0x0,-0x4c(%rbp)
jne    1335 <rl_setrange+0x1b5>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
mov    -0x38(%rbp),%r8
mov    -0x48(%rbp),%r9
mov    -0x40(%rbp),%rax
add    $0x1,%rax
mov    $0x54,%ecx
mov    %rax,(%rsp)
call   1070 <rl_key_set@plt>
mov    %eax,-0x4c(%rbp)
xor    %eax,%eax
cmp    -0x4c(%rbp),%eax
je     1300 <rl_setrange+0x180>
jmp    1341 <rl_setrange+0x1c1>
mov    -0x8(%rbp),%rdi
mov    -0x38(%rbp),%rsi
mov    -0x28(%rbp),%rdx
mov    -0x30(%rbp),%rcx
mov    -0x20(%rbp),%r8
mov    0x10(%rbp),%r9
call   1040 <rl_multi_string_setrange@plt>
mov    %eax,-0x4c(%rbp)
xor    %eax,%eax
cmp    -0x4c(%rbp),%eax
je     1330 <rl_setrange+0x1b0>
jmp    1341 <rl_setrange+0x1c1>
jmp    1335 <rl_setrange+0x1b5>
jmp    133a <rl_setrange+0x1ba>
movl   $0x0,-0x4c(%rbp)
mov    -0x4c(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rl_setrange>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %r8,0x8(%rsp)
mov    %rcx,%rbx
add    %r9,%rcx
mov    $0x5,%eax
cmp    $0x20000000,%rcx
jg     129f <rl_setrange+0x11f>
mov    %r9,%r13
mov    %rdx,%rbp
mov    %rsi,%r15
mov    %rdi,%r12
lea    0x10(%rsp),%rcx
lea    0x18(%rsp),%r8
lea    0x20(%rsp),%r9
call   1090 <rl_string_get_objects@plt>
test   %eax,%eax
je     1247 <rl_setrange+0xc7>
cmp    $0x3,%eax
jne    129f <rl_setrange+0x11f>
mov    %ebx,%edi
call   1060 <malloc@plt>
test   %rax,%rax
je     129a <rl_setrange+0x11a>
mov    %rax,%r14
mov    %rax,%rdi
xor    %esi,%esi
mov    %rbx,%rdx
call   1050 <memset@plt>
movl   $0x0,(%rsp)
mov    %r12,%rdi
mov    %r15,%rsi
mov    %rbp,%rdx
mov    %r14,%rcx
mov    %rbx,%r8
xor    %r9d,%r9d
call   10a0 <rl_set@plt>
test   %eax,%eax
jne    129f <rl_setrange+0x11f>
mov    %r14,%rdi
call   1030 <free@plt>
mov    %r12,%rdi
mov    %r15,%rsi
mov    %rbp,%rdx
mov    0x8(%rsp),%rcx
mov    %r13,%r8
mov    0x60(%rsp),%r9
call   1080 <rl_append@plt>
test   %eax,%eax
jne    129f <rl_setrange+0x11f>
jmp    1296 <rl_setrange+0x116>
mov    0x10(%rsp),%r8
mov    0x18(%rsp),%r9
mov    0x20(%rsp),%rax
add    $0x1,%rax
mov    %rax,(%rsp)
mov    %r12,%rdi
mov    %r15,%rsi
mov    %rbp,%rdx
mov    $0x54,%ecx
call   1070 <rl_key_set@plt>
test   %eax,%eax
jne    129f <rl_setrange+0x11f>
mov    0x10(%rsp),%rsi
mov    %r12,%rdi
mov    0x8(%rsp),%rdx
mov    %r13,%rcx
mov    %rbx,%r8
mov    0x60(%rsp),%r9
call   1040 <rl_multi_string_setrange@plt>
test   %eax,%eax
jne    129f <rl_setrange+0x11f>
xor    %eax,%eax
jmp    129f <rl_setrange+0x11f>
mov    $0x4,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.rlite = type { i32 }
define i32 @rl_setrange(%struct.rlite* %0, i8* %1, i64 %2, i64 %3, i8* %4, i64 %5, i64* %6) {
%8 = alloca %struct.rlite*
%9 = alloca i8*
%10 = alloca i64
%11 = alloca i64
%12 = alloca i8*
%13 = alloca i64
%14 = alloca i64*
%15 = alloca i64
%16 = alloca i64
%17 = alloca i64
%18 = alloca i32
%19 = alloca i8*
store %struct.rlite* %0, %struct.rlite** %8
store i8* %1, i8** %9
store i64 %2, i64* %10
store i64 %3, i64* %11
store i8* %4, i8** %12
store i64 %5, i64* %13
store i64* %6, i64** %14
%20 = load i64, i64* %13
%21 = load i64, i64* %11
%22 = add nsw i64 %20, %21
%23 = icmp sgt i64 %22, 536870912
br i1 %23, label %24, label %25
24: ; preds = %7
store i32 5, i32* %18
br label %103
25: ; preds = %7
%26 = load %struct.rlite*, %struct.rlite** %8
%27 = load i8*, i8** %9
%28 = load i64, i64* %10
%29 = call i32 @rl_string_get_objects(%struct.rlite* %26, i8* %27, i64 %28, i64* %15, i64* %17, i64* %16)
store i32 %29, i32* %18
%30 = load i32, i32* %18
%31 = icmp ne i32 0, %30
br i1 %31, label %32, label %36
32: ; preds = %25
%33 = load i32, i32* %18
%34 = icmp ne i32 3, %33
br i1 %34, label %35, label %36
35: ; preds = %32
br label %103
36: ; preds = %32, %25
%37 = load i32, i32* %18
%38 = icmp eq i32 %37, 3
br i1 %38, label %39, label %74
39: ; preds = %36
%40 = load i64, i64* %11
%41 = mul i64 1, %40
%42 = trunc i64 %41 to i32
%43 = call i8* @malloc(i32 %42)
store i8* %43, i8** %19
%44 = load i8*, i8** %19
%45 = icmp ne i8* %44, null
br i1 %45, label %47, label %46
46: ; preds = %39
store i32 4, i32* %18
br label %103
47: ; preds = %39
%48 = load i8*, i8** %19
%49 = load i64, i64* %11
%50 = call i32 @memset(i8* %48, i32 0, i64 %49)
%51 = load %struct.rlite*, %struct.rlite** %8
%52 = load i8*, i8** %9
%53 = load i64, i64* %10
%54 = load i8*, i8** %19
%55 = load i64, i64* %11
%56 = call i32 @rl_set(%struct.rlite* %51, i8* %52, i64 %53, i8* %54, i64 %55, i32 0, i32 0)
store i32 %56, i32* %18
%57 = load i32, i32* %18
%58 = icmp ne i32 0, %57
br i1 %58, label %59, label %60
59: ; preds = %47
br label %103
60: ; preds = %47
%61 = load i8*, i8** %19
%62 = call i32 @free(i8* %61)
%63 = load %struct.rlite*, %struct.rlite** %8
%64 = load i8*, i8** %9
%65 = load i64, i64* %10
%66 = load i8*, i8** %12
%67 = load i64, i64* %13
%68 = load i64*, i64** %14
%69 = call i32 @rl_append(%struct.rlite* %63, i8* %64, i64 %65, i8* %66, i64 %67, i64* %68)
store i32 %69, i32* %18
%70 = load i32, i32* %18
%71 = icmp ne i32 0, %70
br i1 %71, label %72, label %73
72: ; preds = %60
br label %103
73: ; preds = %60
br label %102
74: ; preds = %36
%75 = load i32, i32* %18
%76 = icmp eq i32 %75, 0
br i1 %76, label %77, label %101
77: ; preds = %74
%78 = load %struct.rlite*, %struct.rlite** %8
%79 = load i8*, i8** %9
%80 = load i64, i64* %10
%81 = load i64, i64* %15
%82 = load i64, i64* %17
%83 = load i64, i64* %16
%84 = add nsw i64 %83, 1
%85 = call i32 @rl_key_set(%struct.rlite* %78, i8* %79, i64 %80, i8 signext 84, i64 %81, i64 %82, i64 %84)
store i32 %85, i32* %18
%86 = load i32, i32* %18
%87 = icmp ne i32 0, %86
br i1 %87, label %88, label %89
88: ; preds = %77
br label %103
89: ; preds = %77
%90 = load %struct.rlite*, %struct.rlite** %8
%91 = load i64, i64* %15
%92 = load i8*, i8** %12
%93 = load i64, i64* %13
%94 = load i64, i64* %11
%95 = load i64*, i64** %14
%96 = call i32 @rl_multi_string_setrange(%struct.rlite* %90, i64 %91, i8* %92, i64 %93, i64 %94, i64* %95)
store i32 %96, i32* %18
%97 = load i32, i32* %18
%98 = icmp ne i32 0, %97
br i1 %98, label %99, label %100
99: ; preds = %89
br label %103
100: ; preds = %89
br label %101
101: ; preds = %100, %74
br label %102
102: ; preds = %101, %73
store i32 0, i32* %18
br label %103
103: ; preds = %102, %99, %88, %72, %59, %46, %35, %24
%104 = load i32, i32* %18
ret i32 %104
}

LLVM IR (-O2):
%struct.rlite = type { i32 }
define i32 @rl_setrange(%struct.rlite* %0, i8* %1, i64 %2, i64 %3, i8* %4, i64 %5, i64* %6) {
%8 = alloca i64
%9 = alloca i64
%10 = alloca i64
%11 = bitcast i64* %8 to i8*
%12 = bitcast i64* %9 to i8*
%13 = bitcast i64* %10 to i8*
%14 = add nsw i64 %5, %3
%15 = icmp sgt i64 %14, 536870912
br i1 %15, label %42, label %16
16: ; preds = %7
%17 = call i32 @rl_string_get_objects(%struct.rlite* %0, i8* %1, i64 %2, i64* %8, i64* %10, i64* %9)
switch i32 %17, label %42 [
i32 3, label %18
i32 0, label %30
]
18: ; preds = %16
%19 = trunc i64 %3 to i32
%20 = call i8* @malloc(i32 %19)
%21 = icmp eq i8* %20, null
br i1 %21, label %42, label %22
22: ; preds = %18
%23 = call i32 @memset(i8* %20, i32 0, i64 %3)
%24 = call i32 @rl_set(%struct.rlite* %0, i8* %1, i64 %2, i8* %20, i64 %3, i32 0, i32 0)
%25 = icmp eq i32 %24, 0
br i1 %25, label %26, label %42
26: ; preds = %22
%27 = call i32 @free(i8* %20)
%28 = call i32 @rl_append(%struct.rlite* %0, i8* %1, i64 %2, i8* %4, i64 %5, i64* %6)
%29 = icmp eq i32 %28, 0
br i1 %29, label %41, label %42
30: ; preds = %16
%31 = load i64, i64* %8
%32 = load i64, i64* %10
%33 = load i64, i64* %9
%34 = add nsw i64 %33, 1
%35 = call i32 @rl_key_set(%struct.rlite* %0, i8* %1, i64 %2, i8 signext 84, i64 %31, i64 %32, i64 %34)
%36 = icmp eq i32 %35, 0
br i1 %36, label %37, label %42
37: ; preds = %30
%38 = load i64, i64* %8
%39 = call i32 @rl_multi_string_setrange(%struct.rlite* %0, i64 %38, i8* %4, i64 %5, i64 %3, i64* %6)
%40 = icmp eq i32 %39, 0
br i1 %40, label %41, label %42
41: ; preds = %26, %37
br label %42
42: ; preds = %26, %22, %18, %41, %30, %37, %7, %16
%43 = phi i32 [ %17, %16 ], [ 0, %41 ], [ %35, %30 ], [ %39, %37 ], [ 5, %7 ], [ %28, %26 ], [ %24, %22 ], [ 4, %18 ]
ret i32 %43
}