#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 mpatch_flist {
  int dummy;
};
typedef scalar_t__ ssize_t;

/* Variables and functions */
struct mpatch_flist *combine(struct mpatch_flist *, struct mpatch_flist *);
struct mpatch_flist *
mpatch_fold(void *bins, struct mpatch_flist *(*get_next_item)(void *, ssize_t),
            ssize_t start, ssize_t end) {
  ssize_t len;
  if (start + 1 == end) {
    return get_next_item(bins, start);
  }
  len = (end - start) / 2;
  return combine(mpatch_fold(bins, get_next_item, start, start + len),
                 mpatch_fold(bins, get_next_item, start + len, end));
}


x86 assembly (clang, -O0):
<mpatch_fold>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    -0x20(%rbp),%rax
add    $0x1,%rax
cmp    -0x28(%rbp),%rax
jne    1161 <mpatch_fold+0x41>
mov    -0x18(%rbp),%rax
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   *%rax
mov    %rax,-0x8(%rbp)
jmp    11bd <mpatch_fold+0x9d>
mov    -0x28(%rbp),%rax
sub    -0x20(%rbp),%rax
mov    $0x2,%ecx
cqto
idiv   %rcx
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
mov    -0x20(%rbp),%rcx
add    -0x30(%rbp),%rcx
call   1030 <mpatch_fold@plt>
mov    %rax,-0x38(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
add    -0x30(%rbp),%rdx
mov    -0x28(%rbp),%rcx
call   1030 <mpatch_fold@plt>
mov    -0x38(%rbp),%rdi
mov    %rax,%rsi
call   1040 <combine@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mpatch_fold>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rcx,%r15
mov    %rsi,%rcx
mov    %rdi,%r14
lea    0x1(%rdx),%rax
cmp    %r15,%rax
jne    114c <mpatch_fold+0x2c>
mov    %r14,%rdi
mov    %rdx,%rsi
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
jmp    *%rcx
mov    %r15,%rax
sub    %rdx,%rax
mov    %rax,%rbx
shr    $0x3f,%rbx
add    %rax,%rbx
sar    %rbx
add    %rdx,%rbx
mov    %r14,%rdi
mov    %rcx,%rsi
mov    %rcx,%r13
mov    %rbx,%rcx
call   1030 <mpatch_fold@plt>
mov    %rax,%r12
mov    %r14,%rdi
mov    %r13,%rsi
mov    %rbx,%rdx
mov    %r15,%rcx
call   1030 <mpatch_fold@plt>
mov    %r12,%rdi
mov    %rax,%rsi
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
jmp    1040 <combine@plt>

LLVM IR (-O0):
%struct.mpatch_flist = type { i32 }
define %struct.mpatch_flist* @mpatch_fold(i8* %0, %struct.mpatch_flist* (i8*, i64)* %1, i64 %2, i64 %3) {
%5 = alloca %struct.mpatch_flist*
%6 = alloca i8*
%7 = alloca %struct.mpatch_flist* (i8*, i64)*
%8 = alloca i64
%9 = alloca i64
%10 = alloca i64
store i8* %0, i8** %6
store %struct.mpatch_flist* (i8*, i64)* %1, %struct.mpatch_flist* (i8*, i64)** %7
store i64 %2, i64* %8
store i64 %3, i64* %9
%11 = load i64, i64* %8
%12 = add nsw i64 %11, 1
%13 = load i64, i64* %9
%14 = icmp eq i64 %12, %13
br i1 %14, label %15, label %20
15: ; preds = %4
%16 = load %struct.mpatch_flist* (i8*, i64)*, %struct.mpatch_flist* (i8*, i64)** %7
%17 = load i8*, i8** %6
%18 = load i64, i64* %8
%19 = call %struct.mpatch_flist* %16(i8* %17, i64 %18)
store %struct.mpatch_flist* %19, %struct.mpatch_flist** %5
br label %40
20: ; preds = %4
%21 = load i64, i64* %9
%22 = load i64, i64* %8
%23 = sub nsw i64 %21, %22
%24 = sdiv i64 %23, 2
store i64 %24, i64* %10
%25 = load i8*, i8** %6
%26 = load %struct.mpatch_flist* (i8*, i64)*, %struct.mpatch_flist* (i8*, i64)** %7
%27 = load i64, i64* %8
%28 = load i64, i64* %8
%29 = load i64, i64* %10
%30 = add nsw i64 %28, %29
%31 = call %struct.mpatch_flist* @mpatch_fold(i8* %25, %struct.mpatch_flist* (i8*, i64)* %26, i64 %27, i64 %30)
%32 = load i8*, i8** %6
%33 = load %struct.mpatch_flist* (i8*, i64)*, %struct.mpatch_flist* (i8*, i64)** %7
%34 = load i64, i64* %8
%35 = load i64, i64* %10
%36 = add nsw i64 %34, %35
%37 = load i64, i64* %9
%38 = call %struct.mpatch_flist* @mpatch_fold(i8* %32, %struct.mpatch_flist* (i8*, i64)* %33, i64 %36, i64 %37)
%39 = call %struct.mpatch_flist* @combine(%struct.mpatch_flist* %31, %struct.mpatch_flist* %38)
store %struct.mpatch_flist* %39, %struct.mpatch_flist** %5
br label %40
40: ; preds = %20, %15
%41 = load %struct.mpatch_flist*, %struct.mpatch_flist** %5
ret %struct.mpatch_flist* %41
}

LLVM IR (-O2):
%struct.mpatch_flist = type { i32 }
define %struct.mpatch_flist* @mpatch_fold(i8* %0, %struct.mpatch_flist* (i8*, i64)* %1, i64 %2, i64 %3) {
%5 = add nsw i64 %2, 1
%6 = icmp eq i64 %5, %3
br i1 %6, label %7, label %11
7: ; preds = %4
%8 = tail call %struct.mpatch_flist* %1(i8* %0, i64 %2)
br label %9
9: ; preds = %7, %11
%10 = phi %struct.mpatch_flist* [ %17, %11 ], [ %8, %7 ]
ret %struct.mpatch_flist* %10
11: ; preds = %4
%12 = sub nsw i64 %3, %2
%13 = sdiv i64 %12, 2
%14 = add nsw i64 %13, %2
%15 = tail call %struct.mpatch_flist* @mpatch_fold(i8* %0, %struct.mpatch_flist* (i8*, i64)* %1, i64 %2, i64 %14)
%16 = tail call %struct.mpatch_flist* @mpatch_fold(i8* %0, %struct.mpatch_flist* (i8*, i64)* %1, i64 %14, i64 %3)
%17 = tail call %struct.mpatch_flist* @combine(%struct.mpatch_flist* %15, %struct.mpatch_flist* %16)
br label %9
}