#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 */

/* Variables and functions */
int /*<<< orphan*/ free_error(void *, char *, char *);
void *malloc_error(size_t, char *, char *);
int /*<<< orphan*/ memcpy(void *, void *, size_t);
int stub1(void const *, void const *);
void heap_up(int position, void *heap, size_t size,
             int (*compar)(const void *elt1, const void *elt2)) {
  void *elt;
  int parent_idx;
  void *parent;
  void *position_pointer;
  parent_idx = position / 2;
  position_pointer = (char *)heap + position * size;
  parent = (char *)heap + parent_idx * size;
  elt = malloc_error(size, "elt", "heap_up()");
  memcpy(elt, position_pointer, size);
  while ((*compar)(parent, elt) < 0) {
    memcpy(position_pointer, parent, size);
    position_pointer = parent;
    parent_idx = parent_idx / 2;
    parent = (char *)heap + parent_idx * size;
  }
  memcpy(position_pointer, elt, size);
  free_error(elt, "elt", "heap_up()");
}


x86 assembly (clang, -O0):
<heap_up>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    -0x4(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,-0x2c(%rbp)
mov    -0x10(%rbp),%rax
movslq -0x4(%rbp),%rcx
imul   -0x18(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x40(%rbp)
mov    -0x10(%rbp),%rax
movslq -0x2c(%rbp),%rcx
imul   -0x18(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x38(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xe78(%rip),%rsi
lea    0xe75(%rip),%rdx
call   1050 <malloc_error@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x40(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1040 <memcpy@plt>
mov    -0x20(%rbp),%rax
mov    -0x38(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   *%rax
cmp    $0x0,%eax
jge    1200 <heap_up+0xd0>
mov    -0x40(%rbp),%rdi
mov    -0x38(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1040 <memcpy@plt>
mov    -0x38(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x2c(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,-0x2c(%rbp)
mov    -0x10(%rbp),%rax
movslq -0x2c(%rbp),%rcx
imul   -0x18(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x38(%rbp)
jmp    11a9 <heap_up+0x79>
mov    -0x40(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1040 <memcpy@plt>
mov    -0x28(%rbp),%rdi
lea    0xde4(%rip),%rsi
lea    0xde1(%rip),%rdx
call   1030 <free_error@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<heap_up>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rcx,(%rsp)
mov    %rdx,%r13
mov    %rsi,%rbp
movslq %edi,%rbx
mov    %rbx,%r14
imul   %rdx,%r14
add    %rsi,%r14
lea    0xea4(%rip),%rsi
lea    0xea1(%rip),%rdx
mov    %r13,%rdi
call   1050 <malloc_error@plt>
mov    %rax,%r15
mov    %rax,%rdi
mov    %r14,%rsi
mov    %r13,%rdx
call   1040 <memcpy@plt>
mov    %ebx,%r12d
shr    $0x1f,%r12d
add    %ebx,%r12d
mov    %rbp,%rax
sar    %r12d
movslq %r12d,%rbp
imul   %r13,%rbp
mov    %rax,0x10(%rsp)
add    %rax,%rbp
mov    %rbp,%rdi
mov    %r15,0x8(%rsp)
mov    %r15,%rsi
mov    (%rsp),%rbx
call   *%rbx
test   %eax,%eax
js     11b5 <heap_up+0x85>
mov    %r14,%rbx
jmp    11fc <heap_up+0xcc>
mov    %r12d,%r15d
nopl   0x0(%rax,%rax,1)
mov    %rbp,%rbx
mov    %r14,%rdi
mov    %rbp,%rsi
mov    %r13,%rdx
call   1040 <memcpy@plt>
shr    $0x1f,%r15d
add    %r12d,%r15d
sar    %r15d
movslq %r15d,%rbp
imul   %r13,%rbp
add    0x10(%rsp),%rbp
mov    %rbp,%rdi
mov    0x8(%rsp),%rsi
call   *(%rsp)
mov    %r15d,%r12d
mov    %rbx,%r14
test   %eax,%eax
js     11c0 <heap_up+0x90>
mov    %rbx,%rdi
mov    0x8(%rsp),%rbx
mov    %rbx,%rsi
mov    %r13,%rdx
call   1040 <memcpy@plt>
lea    0xdea(%rip),%rsi
lea    0xde7(%rip),%rdx
mov    %rbx,%rdi
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <free_error@plt>

LLVM IR (-O0):
@.str = [4 x i8] c"elt\00"
@.str.1 = [10 x i8] c"heap_up()\00"
define void @heap_up(i32 %0, i8* %1, i64 %2, i32 (i8*, i8*)* %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32 (i8*, i8*)*
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i8*
%12 = alloca i8*
store i32 %0, i32* %5
store i8* %1, i8** %6
store i64 %2, i64* %7
store i32 (i8*, i8*)* %3, i32 (i8*, i8*)** %8
%13 = load i32, i32* %5
%14 = sdiv i32 %13, 2
store i32 %14, i32* %10
%15 = load i8*, i8** %6
%16 = load i32, i32* %5
%17 = sext i32 %16 to i64
%18 = load i64, i64* %7
%19 = mul i64 %17, %18
%20 = getelementptr inbounds i8, i8* %15, i64 %19
store i8* %20, i8** %12
%21 = load i8*, i8** %6
%22 = load i32, i32* %10
%23 = sext i32 %22 to i64
%24 = load i64, i64* %7
%25 = mul i64 %23, %24
%26 = getelementptr inbounds i8, i8* %21, i64 %25
store i8* %26, i8** %11
%27 = load i64, i64* %7
%28 = call i8* @malloc_error(i64 %27, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
store i8* %28, i8** %9
%29 = load i8*, i8** %9
%30 = load i8*, i8** %12
%31 = load i64, i64* %7
%32 = call i32 @memcpy(i8* %29, i8* %30, i64 %31)
br label %33
33: ; preds = %39, %4
%34 = load i32 (i8*, i8*)*, i32 (i8*, i8*)** %8
%35 = load i8*, i8** %11
%36 = load i8*, i8** %9
%37 = call i32 %34(i8* %35, i8* %36)
%38 = icmp slt i32 %37, 0
br i1 %38, label %39, label %53
39: ; preds = %33
%40 = load i8*, i8** %12
%41 = load i8*, i8** %11
%42 = load i64, i64* %7
%43 = call i32 @memcpy(i8* %40, i8* %41, i64 %42)
%44 = load i8*, i8** %11
store i8* %44, i8** %12
%45 = load i32, i32* %10
%46 = sdiv i32 %45, 2
store i32 %46, i32* %10
%47 = load i8*, i8** %6
%48 = load i32, i32* %10
%49 = sext i32 %48 to i64
%50 = load i64, i64* %7
%51 = mul i64 %49, %50
%52 = getelementptr inbounds i8, i8* %47, i64 %51
store i8* %52, i8** %11
br label %33
53: ; preds = %33
%54 = load i8*, i8** %12
%55 = load i8*, i8** %9
%56 = load i64, i64* %7
%57 = call i32 @memcpy(i8* %54, i8* %55, i64 %56)
%58 = load i8*, i8** %9
%59 = call i32 @free_error(i8* %58, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
ret void
}

LLVM IR (-O2):
@.str = [4 x i8] c"elt\00"
@.str.1 = [10 x i8] c"heap_up()\00"
define void @heap_up(i32 %0, i8* %1, i64 %2, i32 (i8*, i8*)* nocapture %3) {
%5 = sext i32 %0 to i64
%6 = mul i64 %5, %2
%7 = getelementptr inbounds i8, i8* %1, i64 %6
%8 = tail call i8* @malloc_error(i64 %2, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
%9 = tail call i32 @memcpy(i8* %8, i8* %7, i64 %2)
%10 = sdiv i32 %0, 2
%11 = sext i32 %10 to i64
%12 = mul i64 %11, %2
%13 = getelementptr inbounds i8, i8* %1, i64 %12
%14 = tail call i32 %3(i8* %13, i8* %8)
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %27
16: ; preds = %4, %16
%17 = phi i8* [ %24, %16 ], [ %13, %4 ]
%18 = phi i32 [ %21, %16 ], [ %10, %4 ]
%19 = phi i8* [ %17, %16 ], [ %7, %4 ]
%20 = tail call i32 @memcpy(i8* %19, i8* %17, i64 %2)
%21 = sdiv i32 %18, 2
%22 = sext i32 %21 to i64
%23 = mul i64 %22, %2
%24 = getelementptr inbounds i8, i8* %1, i64 %23
%25 = tail call i32 %3(i8* %24, i8* %8)
%26 = icmp slt i32 %25, 0
br i1 %26, label %16, label %27
27: ; preds = %16, %4
%28 = phi i8* [ %7, %4 ], [ %17, %16 ]
%29 = tail call i32 @memcpy(i8* %28, i8* %8, i64 %2)
%30 = tail call i32 @free_error(i8* %8, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
ret void
}