#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 *);
int stub2(void const *, void const *);
void heap_to_tieheap(void *tieheap, int count, int *heap_count, size_t size,
                     int (*compar)(const void *elt1, const void *elt2)) {
  void *elt_1;
  void *heap_1;
  void *heap_end;
  void *tieheap_end;
  if ((count <= 1) || (*heap_count == 0))
    *heap_count = 0;
  else {
    heap_1 = (char *)tieheap + size;
    tieheap_end = (char *)tieheap + size * count;
    if ((*heap_count == count) || ((*compar)(heap_1, tieheap_end) == 0)) {
      elt_1 = malloc_error(size, "elt_1", "heap_to_tieheap()");
      do {
        if (*heap_count == 1) {
          *heap_count = 0;
          break;
        } else {
          heap_end = (char *)tieheap + *heap_count * size;
          memcpy(elt_1, heap_1, size);
          memcpy(heap_1, heap_end, size);
          memcpy(heap_end, elt_1, size);
          --(*heap_count);
          heap_down(1, tieheap, *heap_count, size, compar);
        }
      } while ((*compar)(heap_1, tieheap_end) == 0);
      free_error(elt_1, "elt_1", "heap_to_tieheap()");
    }
  }
}


x86 assembly (clang, -O0):
<heap_to_tieheap>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
cmpl   $0x1,-0xc(%rbp)
jle    1172 <heap_to_tieheap+0x32>
mov    -0x18(%rbp),%rax
cmpl   $0x0,(%rax)
jne    1181 <heap_to_tieheap+0x41>
mov    -0x18(%rbp),%rax
movl   $0x0,(%rax)
jmp    12aa <heap_to_tieheap+0x16a>
mov    -0x8(%rbp),%rax
add    -0x20(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x8(%rbp),%rax
mov    -0x20(%rbp),%rcx
movslq -0xc(%rbp),%rdx
imul   %rdx,%rcx
add    %rcx,%rax
mov    %rax,-0x48(%rbp)
mov    -0x18(%rbp),%rax
mov    (%rax),%eax
cmp    -0xc(%rbp),%eax
je     11ca <heap_to_tieheap+0x8a>
mov    -0x28(%rbp),%rax
mov    -0x38(%rbp),%rdi
mov    -0x48(%rbp),%rsi
call   *%rax
cmp    $0x0,%eax
jne    12a5 <heap_to_tieheap+0x165>
mov    -0x20(%rbp),%rdi
lea    0xe2b(%rip),%rsi
lea    0xe2a(%rip),%rdx
call   1060 <malloc_error@plt>
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rax
cmpl   $0x1,(%rax)
jne    1201 <heap_to_tieheap+0xc1>
mov    -0x18(%rbp),%rax
movl   $0x0,(%rax)
jmp    128e <heap_to_tieheap+0x14e>
mov    -0x8(%rbp),%rax
mov    -0x18(%rbp),%rcx
movslq (%rcx),%rcx
imul   -0x20(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x40(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x38(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1040 <memcpy@plt>
mov    -0x38(%rbp),%rdi
mov    -0x40(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1040 <memcpy@plt>
mov    -0x40(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1040 <memcpy@plt>
mov    -0x18(%rbp),%rax
mov    (%rax),%ecx
add    $0xffffffff,%ecx
mov    %ecx,(%rax)
mov    -0x8(%rbp),%rsi
mov    -0x18(%rbp),%rax
mov    (%rax),%edx
mov    -0x20(%rbp),%rcx
mov    -0x28(%rbp),%r8
mov    $0x1,%edi
call   1050 <heap_down@plt>
jmp    1277 <heap_to_tieheap+0x137>
mov    -0x28(%rbp),%rax
mov    -0x38(%rbp),%rdi
mov    -0x48(%rbp),%rsi
call   *%rax
cmp    $0x0,%eax
je     11e5 <heap_to_tieheap+0xa5>
mov    -0x30(%rbp),%rdi
lea    0xd67(%rip),%rsi
lea    0xd66(%rip),%rdx
call   1030 <free_error@plt>
jmp    12aa <heap_to_tieheap+0x16a>
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<heap_to_tieheap>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    %r8,0x18(%rsp)
mov    %rcx,0x8(%rsp)
mov    %rdx,%r13
mov    %rdi,0x20(%rsp)
cmp    $0x2,%esi
jl     125f <heap_to_tieheap+0x11f>
mov    0x0(%r13),%eax
test   %eax,%eax
je     125f <heap_to_tieheap+0x11f>
mov    0x20(%rsp),%rcx
mov    0x8(%rsp),%rdx
lea    (%rcx,%rdx,1),%rbp
mov    %esi,%ebx
imul   %rdx,%rbx
add    %rcx,%rbx
cmp    %esi,%eax
je     11a2 <heap_to_tieheap+0x62>
mov    %rbp,%rdi
mov    %rbx,%rsi
call   *0x18(%rsp)
test   %eax,%eax
jne    1267 <heap_to_tieheap+0x127>
mov    %rbp,0x30(%rsp)
lea    0xe52(%rip),%rsi
lea    0xe51(%rip),%rdx
mov    0x8(%rsp),%rdi
call   1060 <malloc_error@plt>
mov    %rax,0x10(%rsp)
mov    %r13,0x28(%rsp)
mov    0x8(%rsp),%r12
mov    0x30(%rsp),%r15
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
movslq 0x0(%r13),%r14
cmp    $0x1,%r14
je     1276 <heap_to_tieheap+0x136>
imul   %r12,%r14
mov    0x20(%rsp),%rbp
add    %rbp,%r14
mov    0x10(%rsp),%r13
mov    %r13,%rdi
mov    %r15,%rsi
mov    %r12,%rdx
call   1040 <memcpy@plt>
mov    %r15,%rdi
mov    %r14,%rsi
mov    %r12,%rdx
call   1040 <memcpy@plt>
mov    %r14,%rdi
mov    %r13,%rsi
mov    0x28(%rsp),%r13
mov    %r12,%rdx
call   1040 <memcpy@plt>
mov    0x0(%r13),%edx
add    $0xffffffff,%edx
mov    %edx,0x0(%r13)
mov    $0x1,%edi
mov    %rbp,%rsi
mov    %r12,%rcx
mov    0x18(%rsp),%rbp
mov    %rbp,%r8
call   1050 <heap_down@plt>
mov    %r15,%rdi
mov    %rbx,%rsi
call   *%rbp
test   %eax,%eax
je     11e0 <heap_to_tieheap+0xa0>
jmp    127e <heap_to_tieheap+0x13e>
movl   $0x0,0x0(%r13)
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
movl   $0x0,0x0(%r13)
lea    0xd7b(%rip),%rsi
lea    0xd7a(%rip),%rdx
mov    0x10(%rsp),%rdi
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <free_error@plt>

LLVM IR (-O0):
@.str = [6 x i8] c"elt_1\00"
@.str.1 = [18 x i8] c"heap_to_tieheap()\00"
define void @heap_to_tieheap(i8* %0, i32 %1, i32* %2, i64 %3, i32 (i8*, i8*)* %4) {
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32*
%9 = alloca i64
%10 = alloca i32 (i8*, i8*)*
%11 = alloca i8*
%12 = alloca i8*
%13 = alloca i8*
%14 = alloca i8*
store i8* %0, i8** %6
store i32 %1, i32* %7
store i32* %2, i32** %8
store i64 %3, i64* %9
store i32 (i8*, i8*)* %4, i32 (i8*, i8*)** %10
%15 = load i32, i32* %7
%16 = icmp sle i32 %15, 1
br i1 %16, label %21, label %17
17: ; preds = %5
%18 = load i32*, i32** %8
%19 = load i32, i32* %18
%20 = icmp eq i32 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %17, %5
%22 = load i32*, i32** %8
store i32 0, i32* %22
br label %92
23: ; preds = %17
%24 = load i8*, i8** %6
%25 = load i64, i64* %9
%26 = getelementptr inbounds i8, i8* %24, i64 %25
store i8* %26, i8** %12
%27 = load i8*, i8** %6
%28 = load i64, i64* %9
%29 = load i32, i32* %7
%30 = sext i32 %29 to i64
%31 = mul i64 %28, %30
%32 = getelementptr inbounds i8, i8* %27, i64 %31
store i8* %32, i8** %14
%33 = load i32*, i32** %8
%34 = load i32, i32* %33
%35 = load i32, i32* %7
%36 = icmp eq i32 %34, %35
br i1 %36, label %43, label %37
37: ; preds = %23
%38 = load i32 (i8*, i8*)*, i32 (i8*, i8*)** %10
%39 = load i8*, i8** %12
%40 = load i8*, i8** %14
%41 = call i32 %38(i8* %39, i8* %40)
%42 = icmp eq i32 %41, 0
br i1 %42, label %43, label %91
43: ; preds = %37, %23
%44 = load i64, i64* %9
%45 = call i8* @malloc_error(i64 %44, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
store i8* %45, i8** %11
br label %46
46: ; preds = %82, %43
%47 = load i32*, i32** %8
%48 = load i32, i32* %47
%49 = icmp eq i32 %48, 1
br i1 %49, label %50, label %52
50: ; preds = %46
%51 = load i32*, i32** %8
store i32 0, i32* %51
br label %88
52: ; preds = %46
%53 = load i8*, i8** %6
%54 = load i32*, i32** %8
%55 = load i32, i32* %54
%56 = sext i32 %55 to i64
%57 = load i64, i64* %9
%58 = mul i64 %56, %57
%59 = getelementptr inbounds i8, i8* %53, i64 %58
store i8* %59, i8** %13
%60 = load i8*, i8** %11
%61 = load i8*, i8** %12
%62 = load i64, i64* %9
%63 = call i32 @memcpy(i8* %60, i8* %61, i64 %62)
%64 = load i8*, i8** %12
%65 = load i8*, i8** %13
%66 = load i64, i64* %9
%67 = call i32 @memcpy(i8* %64, i8* %65, i64 %66)
%68 = load i8*, i8** %13
%69 = load i8*, i8** %11
%70 = load i64, i64* %9
%71 = call i32 @memcpy(i8* %68, i8* %69, i64 %70)
%72 = load i32*, i32** %8
%73 = load i32, i32* %72
%74 = add nsw i32 %73, -1
store i32 %74, i32* %72
%75 = load i8*, i8** %6
%76 = load i32*, i32** %8
%77 = load i32, i32* %76
%78 = load i64, i64* %9
%79 = load i32 (i8*, i8*)*, i32 (i8*, i8*)** %10
%80 = call i32 @heap_down(i32 1, i8* %75, i32 %77, i64 %78, i32 (i8*, i8*)* %79)
br label %81
81: ; preds = %52
br label %82
82: ; preds = %81
%83 = load i32 (i8*, i8*)*, i32 (i8*, i8*)** %10
%84 = load i8*, i8** %12
%85 = load i8*, i8** %14
%86 = call i32 %83(i8* %84, i8* %85)
%87 = icmp eq i32 %86, 0
br i1 %87, label %46, label %88
88: ; preds = %82, %50
%89 = load i8*, i8** %11
%90 = call i32 @free_error(i8* %89, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
br label %91
91: ; preds = %88, %37
br label %92
92: ; preds = %91, %21
ret void
}

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