#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 */
typedef size_t uint32_t;

/* Variables and functions */
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
scalar_t__ read32(int, int /*<<< orphan*/, size_t *);
scalar_t__ readcons(int, size_t, size_t *, size_t *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ writecons(int, size_t, size_t, size_t);
uint32_t db_alloc(int dbfd, size_t sz) {
  uint32_t freeent, freecar, freecdr;
  if (read32(dbfd, 0, &freeent)) {
    fprintf(stderr, "persist: db_alloc: failed to find freeent\n");
    return (0);
  }
  while (freeent) {
    if (readcons(dbfd, freeent, &freecar, &freecdr)) {
      fprintf(stderr, "persist: db_alloc: failed to read freeent\n");
      return (0);
    }
    if (!freecar) {
      fprintf(stderr, "persist: db_alloc: freecar==nil\n");
      return (0);
    }
    uint32_t freeaddr, freelen;
    if (readcons(dbfd, freecar, &freeaddr, &freelen)) {
      fprintf(stderr, "persist: db_alloc: failed to read freecar\n");
      return (0);
    }
    if (sz <= freelen) {
      writecons(dbfd, freecar, freeaddr + sz, freelen - sz);
      return (freeaddr);
    }
    freeent = freecdr;
  }
  fprintf(stderr, "persist: db_alloc: freeent==nil\n");
  return (0);
}


x86 assembly (clang, -O0):
<db_alloc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0xc(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0xc(%rbp),%edi
xor    %esi,%esi
lea    -0x20(%rbp),%rdx
call   1050 <read32@plt>
cmp    $0x0,%rax
je     1189 <db_alloc+0x49>
mov    0x2e72(%rip),%rax
mov    (%rax),%edi
lea    0xe89(%rip),%rsi
call   1030 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    12a7 <db_alloc+0x167>
jmp    118e <db_alloc+0x4e>
cmpq   $0x0,-0x20(%rbp)
je     128a <db_alloc+0x14a>
mov    -0xc(%rbp),%edi
mov    -0x20(%rbp),%rsi
lea    -0x28(%rbp),%rdx
lea    -0x30(%rbp),%rcx
call   1040 <readcons@plt>
cmp    $0x0,%rax
je     11d9 <db_alloc+0x99>
mov    0x2e22(%rip),%rax
mov    (%rax),%edi
lea    0xe64(%rip),%rsi
call   1030 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    12a7 <db_alloc+0x167>
cmpq   $0x0,-0x28(%rbp)
jne    1206 <db_alloc+0xc6>
mov    0x2df5(%rip),%rax
mov    (%rax),%edi
lea    0xe62(%rip),%rsi
call   1030 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    12a7 <db_alloc+0x167>
mov    -0xc(%rbp),%edi
mov    -0x28(%rbp),%rsi
lea    -0x38(%rbp),%rdx
lea    -0x40(%rbp),%rcx
call   1040 <readcons@plt>
cmp    $0x0,%rax
je     1246 <db_alloc+0x106>
mov    0x2db5(%rip),%rax
mov    (%rax),%edi
lea    0xe43(%rip),%rsi
call   1030 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    12a7 <db_alloc+0x167>
mov    -0x18(%rbp),%rax
cmp    -0x40(%rbp),%rax
ja     127d <db_alloc+0x13d>
mov    -0xc(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    -0x38(%rbp),%rdx
add    -0x18(%rbp),%rdx
mov    -0x40(%rbp),%rcx
sub    -0x18(%rbp),%rcx
call   1060 <writecons@plt>
mov    -0x38(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    12a7 <db_alloc+0x167>
mov    -0x30(%rbp),%rax
mov    %rax,-0x20(%rbp)
jmp    118e <db_alloc+0x4e>
mov    0x2d4f(%rip),%rax
mov    (%rax),%edi
lea    0xe08(%rip),%rsi
call   1030 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<db_alloc>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rsi,%rbx
mov    %edi,%ebp
lea    0x10(%rsp),%rdx
xor    %esi,%esi
call   1050 <read32@plt>
test   %rax,%rax
je     1179 <db_alloc+0x39>
mov    0x2e75(%rip),%rax
mov    (%rax),%edi
lea    0xe8c(%rip),%rsi
jmp    122d <db_alloc+0xed>
mov    0x10(%rsp),%rsi
test   %rsi,%rsi
je     11e7 <db_alloc+0xa7>
lea    0x8(%rsp),%r14
lea    0x20(%rsp),%r15
mov    %rsp,%r12
lea    0x18(%rsp),%r13
cs nopw 0x0(%rax,%rax,1)
nop
mov    %ebp,%edi
mov    %r14,%rdx
mov    %r15,%rcx
call   1040 <readcons@plt>
test   %rax,%rax
jne    11f9 <db_alloc+0xb9>
mov    0x8(%rsp),%rsi
test   %rsi,%rsi
je     120b <db_alloc+0xcb>
mov    %ebp,%edi
mov    %r12,%rdx
mov    %r13,%rcx
call   1040 <readcons@plt>
test   %rax,%rax
jne    121d <db_alloc+0xdd>
mov    0x18(%rsp),%rcx
sub    %rbx,%rcx
jae    1243 <db_alloc+0x103>
mov    0x20(%rsp),%rsi
mov    %rsi,0x10(%rsp)
test   %rsi,%rsi
jne    11a0 <db_alloc+0x60>
mov    0x2df2(%rip),%rax
mov    (%rax),%edi
lea    0xeab(%rip),%rsi
jmp    122d <db_alloc+0xed>
mov    0x2de0(%rip),%rax
mov    (%rax),%edi
lea    0xe22(%rip),%rsi
jmp    122d <db_alloc+0xed>
mov    0x2dce(%rip),%rax
mov    (%rax),%edi
lea    0xe3b(%rip),%rsi
jmp    122d <db_alloc+0xed>
mov    0x2dbc(%rip),%rax
mov    (%rax),%edi
lea    0xe4a(%rip),%rsi
call   1030 <fprintf@plt>
xor    %eax,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x8(%rsp),%rsi
add    (%rsp),%rbx
mov    %ebp,%edi
mov    %rbx,%rdx
call   1060 <writecons@plt>
mov    (%rsp),%rax
jmp    1234 <db_alloc+0xf4>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [43 x i8] c"persist: db_alloc: failed to find freeent\0A\00"
@.str.1 = [43 x i8] c"persist: db_alloc: failed to read freeent\0A\00"
@.str.2 = [33 x i8] c"persist: db_alloc: freecar==nil\0A\00"
@.str.3 = [43 x i8] c"persist: db_alloc: failed to read freecar\0A\00"
@.str.4 = [33 x i8] c"persist: db_alloc: freeent==nil\0A\00"
define i64 @db_alloc(i32 %0, i64 %1) {
%3 = alloca i64
%4 = alloca i32
%5 = alloca i64
%6 = alloca i64
%7 = alloca i64
%8 = alloca i64
%9 = alloca i64
%10 = alloca i64
store i32 %0, i32* %4
store i64 %1, i64* %5
%11 = load i32, i32* %4
%12 = call i64 @read32(i32 %11, i32 0, i64* %6)
%13 = icmp ne i64 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %2
%15 = load i32, i32* @stderr
%16 = call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str, i64 0, i64 0))
store i64 0, i64* %3
br label %63
17: ; preds = %2
br label %18
18: ; preds = %58, %17
%19 = load i64, i64* %6
%20 = icmp ne i64 %19, 0
br i1 %20, label %21, label %60
21: ; preds = %18
%22 = load i32, i32* %4
%23 = load i64, i64* %6
%24 = call i64 @readcons(i32 %22, i64 %23, i64* %7, i64* %8)
%25 = icmp ne i64 %24, 0
br i1 %25, label %26, label %29
26: ; preds = %21
%27 = load i32, i32* @stderr
%28 = call i32 @fprintf(i32 %27, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.1, i64 0, i64 0))
store i64 0, i64* %3
br label %63
29: ; preds = %21
%30 = load i64, i64* %7
%31 = icmp ne i64 %30, 0
br i1 %31, label %35, label %32
32: ; preds = %29
%33 = load i32, i32* @stderr
%34 = call i32 @fprintf(i32 %33, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0))
store i64 0, i64* %3
br label %63
35: ; preds = %29
%36 = load i32, i32* %4
%37 = load i64, i64* %7
%38 = call i64 @readcons(i32 %36, i64 %37, i64* %9, i64* %10)
%39 = icmp ne i64 %38, 0
br i1 %39, label %40, label %43
40: ; preds = %35
%41 = load i32, i32* @stderr
%42 = call i32 @fprintf(i32 %41, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.3, i64 0, i64 0))
store i64 0, i64* %3
br label %63
43: ; preds = %35
%44 = load i64, i64* %5
%45 = load i64, i64* %10
%46 = icmp ule i64 %44, %45
br i1 %46, label %47, label %58
47: ; preds = %43
%48 = load i32, i32* %4
%49 = load i64, i64* %7
%50 = load i64, i64* %9
%51 = load i64, i64* %5
%52 = add i64 %50, %51
%53 = load i64, i64* %10
%54 = load i64, i64* %5
%55 = sub i64 %53, %54
%56 = call i32 @writecons(i32 %48, i64 %49, i64 %52, i64 %55)
%57 = load i64, i64* %9
store i64 %57, i64* %3
br label %63
58: ; preds = %43
%59 = load i64, i64* %8
store i64 %59, i64* %6
br label %18
60: ; preds = %18
%61 = load i32, i32* @stderr
%62 = call i32 @fprintf(i32 %61, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.4, i64 0, i64 0))
store i64 0, i64* %3
br label %63
63: ; preds = %60, %47, %40, %32, %26, %14
%64 = load i64, i64* %3
ret i64 %64
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [43 x i8] c"persist: db_alloc: failed to find freeent\0A\00"
@.str.1 = [43 x i8] c"persist: db_alloc: failed to read freeent\0A\00"
@.str.2 = [33 x i8] c"persist: db_alloc: freecar==nil\0A\00"
@.str.3 = [43 x i8] c"persist: db_alloc: failed to read freecar\0A\00"
@.str.4 = [33 x i8] c"persist: db_alloc: freeent==nil\0A\00"
define i64 @db_alloc(i32 %0, i64 %1) {
%3 = alloca i64
%4 = alloca i64
%5 = alloca i64
%6 = alloca i64
%7 = alloca i64
%8 = bitcast i64* %3 to i8*
%9 = bitcast i64* %4 to i8*
%10 = bitcast i64* %5 to i8*
%11 = call i64 @read32(i32 %0, i32 0, i64* %3)
%12 = icmp eq i64 %11, 0
br i1 %12, label %16, label %13
13: ; preds = %2
%14 = load i32, i32* @stderr
%15 = call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str, i64 0, i64 0))
br label %59
16: ; preds = %2
%17 = load i64, i64* %3
%18 = icmp eq i64 %17, 0
br i1 %18, label %56, label %19
19: ; preds = %16
%20 = bitcast i64* %6 to i8*
%21 = bitcast i64* %7 to i8*
br label %22
22: ; preds = %19, %53
%23 = phi i64 [ %17, %19 ], [ %54, %53 ]
%24 = call i64 @readcons(i32 %0, i64 %23, i64* %4, i64* %5)
%25 = icmp eq i64 %24, 0
br i1 %25, label %29, label %26
26: ; preds = %22
%27 = load i32, i32* @stderr
%28 = call i32 @fprintf(i32 %27, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.1, i64 0, i64 0))
br label %59
29: ; preds = %22
%30 = load i64, i64* %4
%31 = icmp eq i64 %30, 0
br i1 %31, label %32, label %35
32: ; preds = %29
%33 = load i32, i32* @stderr
%34 = call i32 @fprintf(i32 %33, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0))
br label %59
35: ; preds = %29
%36 = call i64 @readcons(i32 %0, i64 %30, i64* %6, i64* %7)
%37 = icmp eq i64 %36, 0
br i1 %37, label %41, label %38
38: ; preds = %35
%39 = load i32, i32* @stderr
%40 = call i32 @fprintf(i32 %39, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.3, i64 0, i64 0))
br label %51
41: ; preds = %35
%42 = load i64, i64* %7
%43 = icmp ult i64 %42, %1
br i1 %43, label %53, label %44
44: ; preds = %41
%45 = load i64, i64* %4
%46 = load i64, i64* %6
%47 = add i64 %46, %1
%48 = sub i64 %42, %1
%49 = call i32 @writecons(i32 %0, i64 %45, i64 %47, i64 %48)
%50 = load i64, i64* %6
br label %51
51: ; preds = %38, %44
%52 = phi i64 [ %50, %44 ], [ 0, %38 ]
br label %59
53: ; preds = %41
%54 = load i64, i64* %5
store i64 %54, i64* %3
%55 = icmp eq i64 %54, 0
br i1 %55, label %56, label %22
56: ; preds = %53, %16
%57 = load i32, i32* @stderr
%58 = call i32 @fprintf(i32 %57, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.4, i64 0, i64 0))
br label %59
59: ; preds = %51, %56, %32, %26, %13
%60 = phi i64 [ 0, %13 ], [ 0, %26 ], [ 0, %32 ], [ 0, %56 ], [ %52, %51 ]
ret i64 %60
}