#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 */
void *get_node(void *, size_t, size_t);
int /*<<< orphan*/ memcpy(void *, void *, size_t);
size_t parent(size_t);
size_t bubble(size_t i, void const *key, void *heap, size_t size,
              int (*compar)(void const *, void const *)) {
  size_t j, k;
  void *p, *dest;
  dest = get_node(heap, i, size);
  for (j = i; j > 0; j = k) {
    k = parent(j);
    p = get_node(heap, k, size);
    if (compar(p, key) < 0) {
      break;
    }
    (void)memcpy(dest, p, size);
    dest = p;
  }
  return j;
}


x86 assembly (clang, -O0):
<bubble>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1050 <get_node@plt>
mov    %rax,-0x48(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jbe    11d8 <bubble+0xa8>
mov    -0x30(%rbp),%rdi
call   1030 <parent@plt>
mov    %rax,-0x38(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x38(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1050 <get_node@plt>
mov    %rax,-0x40(%rbp)
mov    -0x28(%rbp),%rax
mov    -0x40(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   *%rax
cmp    $0x0,%eax
jge    11b2 <bubble+0x82>
jmp    11d8 <bubble+0xa8>
mov    -0x48(%rbp),%rdi
mov    -0x40(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1040 <memcpy@plt>
mov    -0x40(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x38(%rbp),%rax
mov    %rax,-0x30(%rbp)
jmp    1169 <bubble+0x39>
mov    -0x30(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bubble>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %r8,0x10(%rsp)
mov    %rcx,%r13
mov    %rdx,%r15
mov    %rsi,0x8(%rsp)
mov    %rdi,%r14
mov    %rdx,%rdi
mov    %r14,%rsi
mov    %rcx,%rdx
call   1050 <get_node@plt>
test   %r14,%r14
je     11b5 <bubble+0x85>
mov    %rax,%r12
nopw   0x0(%rax,%rax,1)
mov    %r14,%rdi
call   1030 <parent@plt>
mov    %rax,%rbx
mov    %r15,%rdi
mov    %rax,%rsi
mov    %r13,%rdx
call   1050 <get_node@plt>
mov    %rax,%rbp
mov    %rax,%rdi
mov    0x8(%rsp),%rsi
call   *0x10(%rsp)
test   %eax,%eax
js     11b8 <bubble+0x88>
mov    %r12,%rdi
mov    %rbp,%rsi
mov    %r13,%rdx
call   1040 <memcpy@plt>
mov    %rbp,%r12
mov    %rbx,%r14
test   %rbx,%rbx
jne    1170 <bubble+0x40>
xor    %r14d,%r14d
mov    %r14,%rax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i64 @bubble(i64 %0, i8* %1, i8* %2, i64 %3, i32 (i8*, i8*)* %4) {
%6 = alloca i64
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i64
%10 = alloca i32 (i8*, i8*)*
%11 = alloca i64
%12 = alloca i64
%13 = alloca i8*
%14 = alloca i8*
store i64 %0, i64* %6
store i8* %1, i8** %7
store i8* %2, i8** %8
store i64 %3, i64* %9
store i32 (i8*, i8*)* %4, i32 (i8*, i8*)** %10
%15 = load i8*, i8** %8
%16 = load i64, i64* %6
%17 = load i64, i64* %9
%18 = call i8* @get_node(i8* %15, i64 %16, i64 %17)
store i8* %18, i8** %14
%19 = load i64, i64* %6
store i64 %19, i64* %11
br label %20
20: ; preds = %42, %5
%21 = load i64, i64* %11
%22 = icmp ugt i64 %21, 0
br i1 %22, label %23, label %44
23: ; preds = %20
%24 = load i64, i64* %11
%25 = call i64 @parent(i64 %24)
store i64 %25, i64* %12
%26 = load i8*, i8** %8
%27 = load i64, i64* %12
%28 = load i64, i64* %9
%29 = call i8* @get_node(i8* %26, i64 %27, i64 %28)
store i8* %29, i8** %13
%30 = load i32 (i8*, i8*)*, i32 (i8*, i8*)** %10
%31 = load i8*, i8** %13
%32 = load i8*, i8** %7
%33 = call i32 %30(i8* %31, i8* %32)
%34 = icmp slt i32 %33, 0
br i1 %34, label %35, label %36
35: ; preds = %23
br label %44
36: ; preds = %23
%37 = load i8*, i8** %14
%38 = load i8*, i8** %13
%39 = load i64, i64* %9
%40 = call i32 @memcpy(i8* %37, i8* %38, i64 %39)
%41 = load i8*, i8** %13
store i8* %41, i8** %14
br label %42
42: ; preds = %36
%43 = load i64, i64* %12
store i64 %43, i64* %11
br label %20
44: ; preds = %35, %20
%45 = load i64, i64* %11
ret i64 %45
}

LLVM IR (-O2):
define i64 @bubble(i64 %0, i8* %1, i8* %2, i64 %3, i32 (i8*, i8*)* nocapture %4) {
%6 = tail call i8* @get_node(i8* %2, i64 %0, i64 %3)
%7 = icmp eq i64 %0, 0
br i1 %7, label %18, label %8
8: ; preds = %5, %15
%9 = phi i8* [ %12, %15 ], [ %6, %5 ]
%10 = phi i64 [ %11, %15 ], [ %0, %5 ]
%11 = tail call i64 @parent(i64 %10)
%12 = tail call i8* @get_node(i8* %2, i64 %11, i64 %3)
%13 = tail call i32 %4(i8* %12, i8* %1)
%14 = icmp slt i32 %13, 0
br i1 %14, label %18, label %15
15: ; preds = %8
%16 = tail call i32 @memcpy(i8* %9, i8* %12, i64 %3)
%17 = icmp eq i64 %11, 0
br i1 %17, label %18, label %8
18: ; preds = %15, %8, %5
%19 = phi i64 [ 0, %5 ], [ %10, %8 ], [ 0, %15 ]
ret i64 %19
}