#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 *);
int /*<<< orphan*/ heap_down(int, void *, int, size_t,
                             int (*)(void const *, void const *));
void *malloc_error(size_t, char *, char *);
int /*<<< orphan*/ memcpy(void *, void *, size_t);
int stub1(void const *, void const *);
void heap_replace(void *new_elt, void *heap, int count, size_t size,
                  int (*compar)(const void *elt1, const void *elt2)) {
  void *elt_1;
  void *heap_1 = (char *)heap + size;
  if ((*compar)(heap_1, new_elt) > 0) {
    elt_1 = malloc_error(size, "elt_1", "heap_replace()");
    memcpy(elt_1, heap_1, size);
    memcpy(heap_1, new_elt, size);
    memcpy(new_elt, elt_1, size);
    heap_down(1, heap, count, size, compar);
    free_error(elt_1, "elt_1", "heap_replace()");
  }
}


x86 assembly (clang, -O0):
<heap_replace>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
mov    -0x10(%rbp),%rax
add    -0x20(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x28(%rbp),%rax
mov    -0x38(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   *%rax
cmp    $0x0,%eax
jle    11fc <heap_replace+0xbc>
mov    -0x20(%rbp),%rdi
lea    0xe77(%rip),%rsi
lea    0xe76(%rip),%rdx
call   1060 <malloc_error@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x38(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1040 <memcpy@plt>
mov    -0x38(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1040 <memcpy@plt>
mov    -0x8(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1040 <memcpy@plt>
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
mov    -0x20(%rbp),%rcx
mov    -0x28(%rbp),%r8
mov    $0x1,%edi
call   1050 <heap_down@plt>
mov    -0x30(%rbp),%rdi
lea    0xe10(%rip),%rsi
lea    0xe0f(%rip),%rdx
call   1030 <free_error@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<heap_replace>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %r8,%r14
mov    %rcx,%rbx
mov    %edx,%r15d
mov    %rsi,%rbp
mov    %rdi,%r12
lea    (%rsi,%rcx,1),%r13
mov    %r13,%rdi
mov    %r12,%rsi
call   *%r8
test   %eax,%eax
jle    11f2 <heap_replace+0xb2>
lea    0xe8a(%rip),%rsi
lea    0xe89(%rip),%rdx
mov    %rbx,%rdi
call   1060 <malloc_error@plt>
mov    %r15d,0x4(%rsp)
mov    %rax,%r15
mov    %rax,%rdi
mov    %r13,%rsi
mov    %rbx,%rdx
call   1040 <memcpy@plt>
mov    %r13,%rdi
mov    %r12,%rsi
mov    %rbx,%rdx
call   1040 <memcpy@plt>
mov    %r12,%rdi
mov    %r15,%rsi
mov    %rbx,%rdx
call   1040 <memcpy@plt>
mov    $0x1,%edi
mov    %rbp,%rsi
mov    0x4(%rsp),%edx
mov    %rbx,%rcx
mov    %r14,%r8
call   1050 <heap_down@plt>
mov    %r15,%rdi
lea    0xe28(%rip),%rsi
lea    0xe27(%rip),%rdx
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <free_error@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [6 x i8] c"elt_1\00"
@.str.1 = [15 x i8] c"heap_replace()\00"
define void @heap_replace(i8* %0, i8* %1, i32 %2, i64 %3, i32 (i8*, i8*)* %4) {
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i64
%10 = alloca i32 (i8*, i8*)*
%11 = alloca i8*
%12 = alloca i8*
store i8* %0, i8** %6
store i8* %1, i8** %7
store i32 %2, i32* %8
store i64 %3, i64* %9
store i32 (i8*, i8*)* %4, i32 (i8*, i8*)** %10
%13 = load i8*, i8** %7
%14 = load i64, i64* %9
%15 = getelementptr inbounds i8, i8* %13, i64 %14
store i8* %15, i8** %12
%16 = load i32 (i8*, i8*)*, i32 (i8*, i8*)** %10
%17 = load i8*, i8** %12
%18 = load i8*, i8** %6
%19 = call i32 %16(i8* %17, i8* %18)
%20 = icmp sgt i32 %19, 0
br i1 %20, label %21, label %43
21: ; preds = %5
%22 = load i64, i64* %9
%23 = call i8* @malloc_error(i64 %22, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
store i8* %23, i8** %11
%24 = load i8*, i8** %11
%25 = load i8*, i8** %12
%26 = load i64, i64* %9
%27 = call i32 @memcpy(i8* %24, i8* %25, i64 %26)
%28 = load i8*, i8** %12
%29 = load i8*, i8** %6
%30 = load i64, i64* %9
%31 = call i32 @memcpy(i8* %28, i8* %29, i64 %30)
%32 = load i8*, i8** %6
%33 = load i8*, i8** %11
%34 = load i64, i64* %9
%35 = call i32 @memcpy(i8* %32, i8* %33, i64 %34)
%36 = load i8*, i8** %7
%37 = load i32, i32* %8
%38 = load i64, i64* %9
%39 = load i32 (i8*, i8*)*, i32 (i8*, i8*)** %10
%40 = call i32 @heap_down(i32 1, i8* %36, i32 %37, i64 %38, i32 (i8*, i8*)* %39)
%41 = load i8*, i8** %11
%42 = call i32 @free_error(i8* %41, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
br label %43
43: ; preds = %21, %5
ret void
}

LLVM IR (-O2):
@.str = [6 x i8] c"elt_1\00"
@.str.1 = [15 x i8] c"heap_replace()\00"
define void @heap_replace(i8* %0, i8* %1, i32 %2, i64 %3, i32 (i8*, i8*)* %4) {
%6 = getelementptr inbounds i8, i8* %1, i64 %3
%7 = tail call i32 %4(i8* %6, i8* %0)
%8 = icmp sgt i32 %7, 0
br i1 %8, label %9, label %16
9: ; preds = %5
%10 = tail call i8* @malloc_error(i64 %3, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
%11 = tail call i32 @memcpy(i8* %10, i8* %6, i64 %3)
%12 = tail call i32 @memcpy(i8* %6, i8* %0, i64 %3)
%13 = tail call i32 @memcpy(i8* %0, i8* %10, i64 %3)
%14 = tail call i32 @heap_down(i32 1, i8* %1, i32 %2, i64 %3, i32 (i8*, i8*)* %4)
%15 = tail call i32 @free_error(i8* %10, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
br label %16
16: ; preds = %9, %5
ret void
}