#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 int /*<<< orphan*/ uint8_t;

/* Variables and functions */
int /*<<< orphan*/ assert(int);
int /*<<< orphan*/ *find_space(int /*<<< orphan*/ const *,
                               int /*<<< orphan*/ const *);
int round_up(size_t);
scalar_t__ xrealloc(int /*<<< orphan*/ const **, size_t);
size_t split(const uint8_t *buf, size_t size, const uint8_t ***locations) {
  assert(!*locations);
  if (size) {
    size_t locations_capacity = 0xffffull / sizeof(uint8_t *);
    *locations = (const uint8_t **)xrealloc(NULL, 0xffffull);
    **locations = buf;
    size_t locations_size = 1;
    const uint8_t *p = buf + 1;
    while (1) {
      p = find_space(p, buf + size) + 1;
      if (p < buf + size) {
        if (locations_size == locations_capacity) {
          size_t new_capacity =
              round_up((locations_capacity + (locations_capacity >> 2)) *
                       sizeof(uint8_t *)) /
              sizeof(uint8_t *);
          *locations = (const uint8_t **)xrealloc(
              *locations, new_capacity * sizeof(uint8_t *));
          locations_capacity = new_capacity;
        }
        (*locations)[locations_size++] = p;
      } else
        return locations_size;
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<split>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x20(%rbp),%rax
cmpq   $0x0,(%rax)
setne  %al
xor    $0xff,%al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
cmpq   $0x0,-0x18(%rbp)
je     127f <split+0x13f>
movq   $0x1fff,-0x28(%rbp)
xor    %eax,%eax
mov    %eax,%edi
mov    $0xffff,%esi
call   1050 <xrealloc@plt>
mov    %rax,%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x10(%rbp),%rcx
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
mov    %rcx,(%rax)
movq   $0x1,-0x30(%rbp)
mov    -0x10(%rbp),%rax
add    $0x4,%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rsi
call   1030 <find_space@plt>
add    $0x4,%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
mov    -0x10(%rbp),%rcx
mov    -0x18(%rbp),%rdx
shl    $0x2,%rdx
add    %rdx,%rcx
cmp    %rcx,%rax
jae    126d <split+0x12d>
mov    -0x30(%rbp),%rax
cmp    -0x28(%rbp),%rax
jne    124a <split+0x10a>
mov    -0x28(%rbp),%rdi
mov    -0x28(%rbp),%rax
shr    $0x2,%rax
add    %rax,%rdi
shl    $0x3,%rdi
call   1040 <round_up@plt>
cltq
shr    $0x3,%rax
mov    %rax,-0x40(%rbp)
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
mov    -0x40(%rbp),%rsi
shl    $0x3,%rsi
call   1050 <xrealloc@plt>
mov    %rax,%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x40(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x38(%rbp),%rdx
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
mov    -0x30(%rbp),%rcx
mov    %rcx,%rsi
add    $0x1,%rsi
mov    %rsi,-0x30(%rbp)
mov    %rdx,(%rax,%rcx,8)
jmp    127a <split+0x13a>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1287 <split+0x147>
jmp    11b8 <split+0x78>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<split>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%rbx
xor    %edi,%edi
cmpq   $0x0,(%rdx)
sete   %dil
call   1060 <assert@plt>
test   %r15,%r15
je     1205 <split+0xc5>
mov    $0xffff,%esi
xor    %edi,%edi
call   1050 <xrealloc@plt>
mov    %rax,(%r14)
mov    %rbx,(%rax)
lea    (%rbx,%r15,4),%r15
add    $0x4,%rbx
mov    %rbx,%rdi
mov    %r15,%rsi
call   1030 <find_space@plt>
mov    %rax,%rbx
add    $0x4,%rbx
mov    $0x1,%r12d
cmp    %r15,%rbx
jae    1208 <split+0xc8>
mov    $0x1fff,%r13d
mov    $0x1,%r12d
jmp    11fb <split+0xbb>
nop
mov    %r12,%rdi
shr    $0x2,%rdi
add    %r12,%rdi
shl    $0x3,%rdi
call   1040 <round_up@plt>
movslq %eax,%rsi
mov    %rsi,%r13
shr    $0x3,%r13
mov    (%r14),%rdi
and    $0xfffffffffffffff8,%rsi
call   1050 <xrealloc@plt>
mov    %rax,(%r14)
mov    %rbx,(%rax,%r12,8)
add    $0x1,%r12
mov    %rbx,%rdi
mov    %r15,%rsi
call   1030 <find_space@plt>
mov    %rax,%rbx
add    $0x4,%rbx
cmp    %r15,%rbx
jae    1208 <split+0xc8>
cmp    %r13,%r12
je     11b0 <split+0x70>
mov    (%r14),%rax
jmp    11dc <split+0x9c>
xor    %r12d,%r12d
mov    %r12,%rax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i64 @split(i32* %0, i64 %1, i32*** %2) {
%4 = alloca i64
%5 = alloca i32*
%6 = alloca i64
%7 = alloca i32***
%8 = alloca i64
%9 = alloca i64
%10 = alloca i32*
%11 = alloca i64
store i32* %0, i32** %5
store i64 %1, i64* %6
store i32*** %2, i32**** %7
%12 = load i32***, i32**** %7
%13 = load i32**, i32*** %12
%14 = icmp ne i32** %13, null
%15 = xor i1 %14, true
%16 = zext i1 %15 to i32
%17 = call i32 @assert(i32 %16)
%18 = load i64, i64* %6
%19 = icmp ne i64 %18, 0
br i1 %19, label %20, label %72
20: ; preds = %3
store i64 8191, i64* %8
%21 = call i64 @xrealloc(i32** null, i64 65535)
%22 = inttoptr i64 %21 to i32**
%23 = load i32***, i32**** %7
store i32** %22, i32*** %23
%24 = load i32*, i32** %5
%25 = load i32***, i32**** %7
%26 = load i32**, i32*** %25
store i32* %24, i32** %26
store i64 1, i64* %9
%27 = load i32*, i32** %5
%28 = getelementptr inbounds i32, i32* %27, i64 1
store i32* %28, i32** %10
br label %29
29: ; preds = %20, %71
%30 = load i32*, i32** %10
%31 = load i32*, i32** %5
%32 = load i64, i64* %6
%33 = getelementptr inbounds i32, i32* %31, i64 %32
%34 = call i32* @find_space(i32* %30, i32* %33)
%35 = getelementptr inbounds i32, i32* %34, i64 1
store i32* %35, i32** %10
%36 = load i32*, i32** %10
%37 = load i32*, i32** %5
%38 = load i64, i64* %6
%39 = getelementptr inbounds i32, i32* %37, i64 %38
%40 = icmp ult i32* %36, %39
br i1 %40, label %41, label %69
41: ; preds = %29
%42 = load i64, i64* %9
%43 = load i64, i64* %8
%44 = icmp eq i64 %42, %43
br i1 %44, label %45, label %62
45: ; preds = %41
%46 = load i64, i64* %8
%47 = load i64, i64* %8
%48 = lshr i64 %47, 2
%49 = add i64 %46, %48
%50 = mul i64 %49, 8
%51 = call i32 @round_up(i64 %50)
%52 = sext i32 %51 to i64
%53 = udiv i64 %52, 8
store i64 %53, i64* %11
%54 = load i32***, i32**** %7
%55 = load i32**, i32*** %54
%56 = load i64, i64* %11
%57 = mul i64 %56, 8
%58 = call i64 @xrealloc(i32** %55, i64 %57)
%59 = inttoptr i64 %58 to i32**
%60 = load i32***, i32**** %7
store i32** %59, i32*** %60
%61 = load i64, i64* %11
store i64 %61, i64* %8
br label %62
62: ; preds = %45, %41
%63 = load i32*, i32** %10
%64 = load i32***, i32**** %7
%65 = load i32**, i32*** %64
%66 = load i64, i64* %9
%67 = add i64 %66, 1
store i64 %67, i64* %9
%68 = getelementptr inbounds i32*, i32** %65, i64 %66
store i32* %63, i32** %68
br label %71
69: ; preds = %29
%70 = load i64, i64* %9
store i64 %70, i64* %4
br label %73
71: ; preds = %62
br label %29
72: ; preds = %3
store i64 0, i64* %4
br label %73
73: ; preds = %72, %69
%74 = load i64, i64* %4
ret i64 %74
}

LLVM IR (-O2):
define i64 @split(i32* %0, i64 %1, i32*** nocapture %2) {
%4 = load i32**, i32*** %2
%5 = icmp eq i32** %4, null
%6 = zext i1 %5 to i32
%7 = tail call i32 @assert(i32 %6)
%8 = icmp eq i64 %1, 0
br i1 %8, label %43, label %9
9: ; preds = %3
%10 = tail call i64 @xrealloc(i32** null, i64 65535)
%11 = inttoptr i64 %10 to i32**
store i32** %11, i32*** %2
store i32* %0, i32** %11
%12 = getelementptr inbounds i32, i32* %0, i64 1
%13 = getelementptr inbounds i32, i32* %0, i64 %1
%14 = tail call i32* @find_space(i32* %12, i32* %13)
%15 = getelementptr inbounds i32, i32* %14, i64 1
%16 = icmp ult i32* %15, %13
br i1 %16, label %17, label %43
17: ; preds = %9, %35
%18 = phi i32* [ %41, %35 ], [ %15, %9 ]
%19 = phi i64 [ %38, %35 ], [ 1, %9 ]
%20 = phi i64 [ %37, %35 ], [ 8191, %9 ]
%21 = icmp eq i64 %19, %20
br i1 %21, label %24, label %22
22: ; preds = %17
%23 = load i32**, i32*** %2
br label %35
24: ; preds = %17
%25 = lshr i64 %19, 2
%26 = add i64 %25, %19
%27 = shl i64 %26, 3
%28 = tail call i32 @round_up(i64 %27)
%29 = sext i32 %28 to i64
%30 = lshr i64 %29, 3
%31 = load i32**, i32*** %2
%32 = and i64 %29, -8
%33 = tail call i64 @xrealloc(i32** %31, i64 %32)
%34 = inttoptr i64 %33 to i32**
store i32** %34, i32*** %2
br label %35
35: ; preds = %22, %24
%36 = phi i32** [ %34, %24 ], [ %23, %22 ]
%37 = phi i64 [ %30, %24 ], [ %20, %22 ]
%38 = add i64 %19, 1
%39 = getelementptr inbounds i32*, i32** %36, i64 %19
store i32* %18, i32** %39
%40 = tail call i32* @find_space(i32* %18, i32* %13)
%41 = getelementptr inbounds i32, i32* %40, i64 1
%42 = icmp ult i32* %41, %13
br i1 %42, label %17, label %43
43: ; preds = %35, %9, %3
%44 = phi i64 [ 0, %3 ], [ 1, %9 ], [ %38, %35 ]
ret i64 %44
}