#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 relocmap {
  int dummy;
};
struct freemap {
  int dummy;
};
struct delta {
  int dummy;
};
typedef scalar_t__ __u32;

/* Variables and functions */
int range_build_rmap(scalar_t__, scalar_t__, scalar_t__ *, scalar_t__,
                     struct delta *, scalar_t__ *);
int /*<<< orphan*/ range_fix_gaps(struct freemap *, scalar_t__, scalar_t__,
                                  scalar_t__, scalar_t__ *);
int range_split(struct freemap *, struct freemap *, struct relocmap **);
int rmap2freemap(scalar_t__ *, scalar_t__, scalar_t__, struct freemap **,
                 int *);
int range_build(__u32 a_h, __u32 n_free_blocks, __u32 *rmap, __u32 rlen,
                struct delta *delta, struct freemap *freemap,
                struct freemap **rangemap_pp, struct relocmap **relocmap_pp) {
  int ret;
  __u32 s = a_h - n_free_blocks;
  __u32 n;
  int entries_used;
  ret = range_build_rmap(s, a_h, rmap, rlen, delta, &n);
  if (ret)
    return ret;
  if (n != n_free_blocks)
    range_fix_gaps(freemap, s, a_h, n_free_blocks - n, rmap);
  ret = rmap2freemap(rmap, s, a_h, rangemap_pp, &entries_used);
  if (ret)
    return ret;
  ret = range_split(*rangemap_pp, freemap, relocmap_pp);
  if (ret)
    return ret;
  return 0;
}


x86 assembly (clang, -O0):
<range_build>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    0x18(%rbp),%rax
mov    0x10(%rbp),%rax
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    %r8,-0x30(%rbp)
mov    %r9,-0x38(%rbp)
mov    -0x10(%rbp),%rax
sub    -0x18(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x48(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x20(%rbp),%rdx
mov    -0x28(%rbp),%rcx
mov    -0x30(%rbp),%r8
lea    -0x50(%rbp),%r9
call   1040 <range_build_rmap@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0x0,-0x3c(%rbp)
je     11a9 <range_build+0x69>
mov    -0x3c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1238 <range_build+0xf8>
mov    -0x50(%rbp),%rax
cmp    -0x18(%rbp),%rax
je     11d4 <range_build+0x94>
mov    -0x38(%rbp),%rdi
mov    -0x48(%rbp),%rsi
mov    -0x10(%rbp),%rdx
mov    -0x18(%rbp),%rcx
sub    -0x50(%rbp),%rcx
mov    -0x20(%rbp),%r8
call   1060 <range_fix_gaps@plt>
mov    -0x20(%rbp),%rdi
mov    -0x48(%rbp),%rsi
mov    -0x10(%rbp),%rdx
mov    0x10(%rbp),%rcx
lea    -0x54(%rbp),%r8
call   1050 <rmap2freemap@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0x0,-0x3c(%rbp)
je     1205 <range_build+0xc5>
mov    -0x3c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1238 <range_build+0xf8>
mov    0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x38(%rbp),%rsi
mov    0x18(%rbp),%rdx
call   1030 <range_split@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0x0,-0x3c(%rbp)
je     1231 <range_build+0xf1>
mov    -0x3c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1238 <range_build+0xf8>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<range_build>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %r9,%r14
mov    %rdx,%r15
mov    %rsi,%rbp
mov    %rdi,%r12
mov    %rdi,%rbx
sub    %rsi,%rbx
lea    0x8(%rsp),%r9
mov    %rbx,%rdi
mov    %r12,%rsi
call   1040 <range_build_rmap@plt>
test   %eax,%eax
jne    11c5 <range_build+0x85>
mov    0x50(%rsp),%r13
mov    0x8(%rsp),%rax
cmp    %rbp,%rax
je     119a <range_build+0x5a>
sub    %rax,%rbp
mov    %r14,%rdi
mov    %rbx,%rsi
mov    %r12,%rdx
mov    %rbp,%rcx
mov    %r15,%r8
call   1060 <range_fix_gaps@plt>
lea    0x14(%rsp),%r8
mov    %r15,%rdi
mov    %rbx,%rsi
mov    %r12,%rdx
mov    %r13,%rcx
call   1050 <rmap2freemap@plt>
test   %eax,%eax
jne    11c5 <range_build+0x85>
mov    0x58(%rsp),%rdx
mov    0x0(%r13),%rdi
mov    %r14,%rsi
call   1030 <range_split@plt>
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.delta = type { i32 }
%struct.freemap = type { i32 }
%struct.relocmap = type { i32 }
define i32 @range_build(i64 %0, i64 %1, i64* %2, i64 %3, %struct.delta* %4, %struct.freemap* %5, %struct.freemap** %6, %struct.relocmap** %7) {
%9 = alloca i32
%10 = alloca i64
%11 = alloca i64
%12 = alloca i64*
%13 = alloca i64
%14 = alloca %struct.delta*
%15 = alloca %struct.freemap*
%16 = alloca %struct.freemap**
%17 = alloca %struct.relocmap**
%18 = alloca i32
%19 = alloca i64
%20 = alloca i64
%21 = alloca i32
store i64 %0, i64* %10
store i64 %1, i64* %11
store i64* %2, i64** %12
store i64 %3, i64* %13
store %struct.delta* %4, %struct.delta** %14
store %struct.freemap* %5, %struct.freemap** %15
store %struct.freemap** %6, %struct.freemap*** %16
store %struct.relocmap** %7, %struct.relocmap*** %17
%22 = load i64, i64* %10
%23 = load i64, i64* %11
%24 = sub nsw i64 %22, %23
store i64 %24, i64* %19
%25 = load i64, i64* %19
%26 = load i64, i64* %10
%27 = load i64*, i64** %12
%28 = load i64, i64* %13
%29 = load %struct.delta*, %struct.delta** %14
%30 = call i32 @range_build_rmap(i64 %25, i64 %26, i64* %27, i64 %28, %struct.delta* %29, i64* %20)
store i32 %30, i32* %18
%31 = load i32, i32* %18
%32 = icmp ne i32 %31, 0
br i1 %32, label %33, label %35
33: ; preds = %8
%34 = load i32, i32* %18
store i32 %34, i32* %9
br label %69
35: ; preds = %8
%36 = load i64, i64* %20
%37 = load i64, i64* %11
%38 = icmp ne i64 %36, %37
br i1 %38, label %39, label %48
39: ; preds = %35
%40 = load %struct.freemap*, %struct.freemap** %15
%41 = load i64, i64* %19
%42 = load i64, i64* %10
%43 = load i64, i64* %11
%44 = load i64, i64* %20
%45 = sub nsw i64 %43, %44
%46 = load i64*, i64** %12
%47 = call i32 @range_fix_gaps(%struct.freemap* %40, i64 %41, i64 %42, i64 %45, i64* %46)
br label %48
48: ; preds = %39, %35
%49 = load i64*, i64** %12
%50 = load i64, i64* %19
%51 = load i64, i64* %10
%52 = load %struct.freemap**, %struct.freemap*** %16
%53 = call i32 @rmap2freemap(i64* %49, i64 %50, i64 %51, %struct.freemap** %52, i32* %21)
store i32 %53, i32* %18
%54 = load i32, i32* %18
%55 = icmp ne i32 %54, 0
br i1 %55, label %56, label %58
56: ; preds = %48
%57 = load i32, i32* %18
store i32 %57, i32* %9
br label %69
58: ; preds = %48
%59 = load %struct.freemap**, %struct.freemap*** %16
%60 = load %struct.freemap*, %struct.freemap** %59
%61 = load %struct.freemap*, %struct.freemap** %15
%62 = load %struct.relocmap**, %struct.relocmap*** %17
%63 = call i32 @range_split(%struct.freemap* %60, %struct.freemap* %61, %struct.relocmap** %62)
store i32 %63, i32* %18
%64 = load i32, i32* %18
%65 = icmp ne i32 %64, 0
br i1 %65, label %66, label %68
66: ; preds = %58
%67 = load i32, i32* %18
store i32 %67, i32* %9
br label %69
68: ; preds = %58
store i32 0, i32* %9
br label %69
69: ; preds = %68, %66, %56, %33
%70 = load i32, i32* %9
ret i32 %70
}

LLVM IR (-O2):
%struct.delta = type { i32 }
%struct.freemap = type { i32 }
%struct.relocmap = type { i32 }
define i32 @range_build(i64 %0, i64 %1, i64* %2, i64 %3, %struct.delta* %4, %struct.freemap* %5, %struct.freemap** %6, %struct.relocmap** %7) {
%9 = alloca i64
%10 = alloca i32
%11 = sub nsw i64 %0, %1
%12 = bitcast i64* %9 to i8*
%13 = bitcast i32* %10 to i8*
%14 = call i32 @range_build_rmap(i64 %11, i64 %0, i64* %2, i64 %3, %struct.delta* %4, i64* %9)
%15 = icmp eq i32 %14, 0
br i1 %15, label %16, label %28
16: ; preds = %8
%17 = load i64, i64* %9
%18 = icmp eq i64 %17, %1
br i1 %18, label %22, label %19
19: ; preds = %16
%20 = sub nsw i64 %1, %17
%21 = call i32 @range_fix_gaps(%struct.freemap* %5, i64 %11, i64 %0, i64 %20, i64* %2)
br label %22
22: ; preds = %19, %16
%23 = call i32 @rmap2freemap(i64* %2, i64 %11, i64 %0, %struct.freemap** %6, i32* %10)
%24 = icmp eq i32 %23, 0
br i1 %24, label %25, label %28
25: ; preds = %22
%26 = load %struct.freemap*, %struct.freemap** %6
%27 = call i32 @range_split(%struct.freemap* %26, %struct.freemap* %5, %struct.relocmap** %7)
br label %28
28: ; preds = %25, %22, %8
%29 = phi i32 [ %14, %8 ], [ %23, %22 ], [ %27, %25 ]
ret i32 %29
}