#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*/ LC_ALL;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ free(char *);
scalar_t__ malloc(int);
char *realloc(char *, int);
int /*<<< orphan*/ setlocale(int /*<<< orphan*/, char *);
int /*<<< orphan*/ sift(char **, unsigned long, long);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strcat(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
int strlen(char *);
char *heapsort(char **array, unsigned long string_sum) {
  char *tmp = NULL;
  setlocale(LC_ALL, "cs_CZ");
  for (long i = (string_sum - 1) / 2; i >= 0; i--)
    sift(array, string_sum - 1, i);
  for (unsigned long i = string_sum - 1; i > 0; i--) {
    tmp = array[0];
    array[0] = array[i];
    array[i] = tmp;
    sift(array, i - 1, 0);
  }
  tmp = NULL;
  if ((tmp = (char *)malloc((1 + (sizeof(char) * strlen(array[0]))))) == NULL) {
    fprintf(stderr, "error: not enought memory\n");
    exit(1);
  }
  strcpy(tmp, array[0]);
  free(array[0]);
  for (unsigned long i = 1; i < string_sum; i++) {
    if ((tmp = realloc(tmp, (1 + (sizeof(char) * (strlen(array[i]) +
                                                  strlen(tmp)))))) == NULL) {
      fprintf(stderr, "error: not enought memory\n");
      exit(1);
    }
    strcat(tmp, array[i]);
    free(array[i]);
  }
  return tmp;
}


x86 assembly (clang, -O0):
<heapsort>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
mov    0x2e19(%rip),%rax
mov    (%rax),%edi
lea    0xe38(%rip),%rsi
call   10a0 <setlocale@plt>
mov    -0x10(%rbp),%rax
sub    $0x1,%rax
shr    $0x1,%rax
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jl     120e <heapsort+0x6e>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
sub    $0x1,%rsi
mov    -0x20(%rbp),%rdx
call   1040 <sift@plt>
mov    -0x20(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x20(%rbp)
jmp    11dd <heapsort+0x3d>
mov    -0x10(%rbp),%rax
sub    $0x1,%rax
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jbe    1279 <heapsort+0xd9>
mov    -0x8(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rax
mov    -0x28(%rbp),%rcx
mov    (%rax,%rcx,8),%rcx
mov    -0x8(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x18(%rbp),%rdx
mov    -0x8(%rbp),%rax
mov    -0x28(%rbp),%rcx
mov    %rdx,(%rax,%rcx,8)
mov    -0x8(%rbp),%rdi
mov    -0x28(%rbp),%rsi
sub    $0x1,%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1040 <sift@plt>
mov    -0x28(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x28(%rbp)
jmp    121a <heapsort+0x7a>
movq   $0x0,-0x18(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
call   1060 <strlen@plt>
cltq
shl    $0x0,%rax
add    $0x1,%rax
mov    %eax,%edi
call   1080 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    12cb <heapsort+0x12b>
mov    0x2d2d(%rip),%rax
mov    (%rax),%edi
lea    0xd4a(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    -0x18(%rbp),%rdi
mov    -0x8(%rbp),%rax
mov    (%rax),%rsi
call   1050 <strcpy@plt>
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
call   1030 <free@plt>
movq   $0x1,-0x30(%rbp)
mov    -0x30(%rbp),%rax
cmp    -0x10(%rbp),%rax
jae    13a2 <heapsort+0x202>
mov    -0x18(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x8(%rbp),%rax
mov    -0x30(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
call   1060 <strlen@plt>
mov    %eax,-0x34(%rbp)
mov    -0x18(%rbp),%rdi
call   1060 <strlen@plt>
mov    -0x40(%rbp),%rdi
mov    %eax,%ecx
mov    -0x34(%rbp),%eax
add    %ecx,%eax
cltq
shl    $0x0,%rax
add    $0x1,%rax
mov    %eax,%esi
call   1090 <realloc@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    136b <heapsort+0x1cb>
mov    0x2c8d(%rip),%rax
mov    (%rax),%edi
lea    0xcaa(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    -0x18(%rbp),%rdi
mov    -0x8(%rbp),%rax
mov    -0x30(%rbp),%rcx
mov    (%rax,%rcx,8),%rsi
call   10b0 <strcat@plt>
mov    -0x8(%rbp),%rax
mov    -0x30(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
call   1030 <free@plt>
mov    -0x30(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x30(%rbp)
jmp    12ef <heapsort+0x14f>
mov    -0x18(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<heapsort>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rsi,%r14
mov    %rdi,%r15
mov    0x2e22(%rip),%rax
mov    (%rax),%edi
lea    0xe41(%rip),%rsi
call   10a0 <setlocale@plt>
lea    -0x1(%r14),%r12
mov    %r12,%rbx
shr    %rbx
xchg   %ax,%ax
mov    %r15,%rdi
mov    %r12,%rsi
mov    %rbx,%rdx
call   1040 <sift@plt>
add    $0xffffffffffffffff,%rbx
jb     11d0 <heapsort+0x30>
test   %r12,%r12
je     1213 <heapsort+0x73>
lea    -0x2(%r14),%rbx
nopl   (%rax)
mov    (%r15),%rax
mov    0x8(%r15,%rbx,8),%rcx
mov    %rcx,(%r15)
mov    %rax,0x8(%r15,%rbx,8)
mov    %r15,%rdi
mov    %rbx,%rsi
xor    %edx,%edx
call   1040 <sift@plt>
add    $0xffffffffffffffff,%rbx
jb     11f0 <heapsort+0x50>
mov    (%r15),%rdi
call   1060 <strlen@plt>
lea    0x1(%rax),%edi
call   1080 <malloc@plt>
test   %rax,%rax
je     12a6 <heapsort+0x106>
mov    %rax,%r13
mov    (%r15),%rsi
mov    %rax,%rdi
call   1050 <strcpy@plt>
mov    (%r15),%rdi
call   1030 <free@plt>
cmp    $0x2,%r14
jb     1299 <heapsort+0xf9>
mov    $0x1,%ebx
nopl   0x0(%rax)
mov    (%r15,%rbx,8),%rdi
call   1060 <strlen@plt>
mov    %eax,%r12d
mov    %r13,%rdi
call   1060 <strlen@plt>
lea    (%r12,%rax,1),%esi
add    $0x1,%esi
mov    %r13,%rdi
call   1090 <realloc@plt>
test   %rax,%rax
je     12a6 <heapsort+0x106>
mov    %rax,%r13
mov    (%r15,%rbx,8),%rsi
mov    %rax,%rdi
call   10b0 <strcat@plt>
mov    (%r15,%rbx,8),%rdi
call   1030 <free@plt>
add    $0x1,%rbx
cmp    %rbx,%r14
jne    1250 <heapsort+0xb0>
mov    %r13,%rax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret
mov    0x2d33(%rip),%rax
mov    (%rax),%edi
lea    0xd50(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
@LC_ALL = global i32 0
@.str = [6 x i8] c"cs_CZ\00"
@stderr = global i32 0
@.str.1 = [27 x i8] c"error: not enought memory\0A\00"
define i8* @heapsort(i8** %0, i64 %1) {
%3 = alloca i8**
%4 = alloca i64
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i64
%8 = alloca i64
store i8** %0, i8*** %3
store i64 %1, i64* %4
store i8* null, i8** %5
%9 = load i32, i32* @LC_ALL
%10 = call i32 @setlocale(i32 %9, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%11 = load i64, i64* %4
%12 = sub i64 %11, 1
%13 = udiv i64 %12, 2
store i64 %13, i64* %6
br label %14
14: ; preds = %23, %2
%15 = load i64, i64* %6
%16 = icmp sge i64 %15, 0
br i1 %16, label %17, label %26
17: ; preds = %14
%18 = load i8**, i8*** %3
%19 = load i64, i64* %4
%20 = sub i64 %19, 1
%21 = load i64, i64* %6
%22 = call i32 @sift(i8** %18, i64 %20, i64 %21)
br label %23
23: ; preds = %17
%24 = load i64, i64* %6
%25 = add nsw i64 %24, -1
store i64 %25, i64* %6
br label %14
26: ; preds = %14
%27 = load i64, i64* %4
%28 = sub i64 %27, 1
store i64 %28, i64* %7
br label %29
29: ; preds = %50, %26
%30 = load i64, i64* %7
%31 = icmp ugt i64 %30, 0
br i1 %31, label %32, label %53
32: ; preds = %29
%33 = load i8**, i8*** %3
%34 = getelementptr inbounds i8*, i8** %33, i64 0
%35 = load i8*, i8** %34
store i8* %35, i8** %5
%36 = load i8**, i8*** %3
%37 = load i64, i64* %7
%38 = getelementptr inbounds i8*, i8** %36, i64 %37
%39 = load i8*, i8** %38
%40 = load i8**, i8*** %3
%41 = getelementptr inbounds i8*, i8** %40, i64 0
store i8* %39, i8** %41
%42 = load i8*, i8** %5
%43 = load i8**, i8*** %3
%44 = load i64, i64* %7
%45 = getelementptr inbounds i8*, i8** %43, i64 %44
store i8* %42, i8** %45
%46 = load i8**, i8*** %3
%47 = load i64, i64* %7
%48 = sub i64 %47, 1
%49 = call i32 @sift(i8** %46, i64 %48, i64 0)
br label %50
50: ; preds = %32
%51 = load i64, i64* %7
%52 = add i64 %51, -1
store i64 %52, i64* %7
br label %29
53: ; preds = %29
store i8* null, i8** %5
%54 = load i8**, i8*** %3
%55 = getelementptr inbounds i8*, i8** %54, i64 0
%56 = load i8*, i8** %55
%57 = call i32 @strlen(i8* %56)
%58 = sext i32 %57 to i64
%59 = mul i64 1, %58
%60 = add i64 1, %59
%61 = trunc i64 %60 to i32
%62 = call i64 @malloc(i32 %61)
%63 = inttoptr i64 %62 to i8*
store i8* %63, i8** %5
%64 = icmp eq i8* %63, null
br i1 %64, label %65, label %69
65: ; preds = %53
%66 = load i32, i32* @stderr
%67 = call i32 @fprintf(i32 %66, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0))
%68 = call i32 @exit(i32 1)
unreachable
69: ; preds = %53
%70 = load i8*, i8** %5
%71 = load i8**, i8*** %3
%72 = getelementptr inbounds i8*, i8** %71, i64 0
%73 = load i8*, i8** %72
%74 = call i32 @strcpy(i8* %70, i8* %73)
%75 = load i8**, i8*** %3
%76 = getelementptr inbounds i8*, i8** %75, i64 0
%77 = load i8*, i8** %76
%78 = call i32 @free(i8* %77)
store i64 1, i64* %8
br label %79
79: ; preds = %115, %69
%80 = load i64, i64* %8
%81 = load i64, i64* %4
%82 = icmp ult i64 %80, %81
br i1 %82, label %83, label %118
83: ; preds = %79
%84 = load i8*, i8** %5
%85 = load i8**, i8*** %3
%86 = load i64, i64* %8
%87 = getelementptr inbounds i8*, i8** %85, i64 %86
%88 = load i8*, i8** %87
%89 = call i32 @strlen(i8* %88)
%90 = load i8*, i8** %5
%91 = call i32 @strlen(i8* %90)
%92 = add nsw i32 %89, %91
%93 = sext i32 %92 to i64
%94 = mul i64 1, %93
%95 = add i64 1, %94
%96 = trunc i64 %95 to i32
%97 = call i8* @realloc(i8* %84, i32 %96)
store i8* %97, i8** %5
%98 = icmp eq i8* %97, null
br i1 %98, label %99, label %103
99: ; preds = %83
%100 = load i32, i32* @stderr
%101 = call i32 @fprintf(i32 %100, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0))
%102 = call i32 @exit(i32 1)
unreachable
103: ; preds = %83
%104 = load i8*, i8** %5
%105 = load i8**, i8*** %3
%106 = load i64, i64* %8
%107 = getelementptr inbounds i8*, i8** %105, i64 %106
%108 = load i8*, i8** %107
%109 = call i32 @strcat(i8* %104, i8* %108)
%110 = load i8**, i8*** %3
%111 = load i64, i64* %8
%112 = getelementptr inbounds i8*, i8** %110, i64 %111
%113 = load i8*, i8** %112
%114 = call i32 @free(i8* %113)
br label %115
115: ; preds = %103
%116 = load i64, i64* %8
%117 = add i64 %116, 1
store i64 %117, i64* %8
br label %79
118: ; preds = %79
%119 = load i8*, i8** %5
ret i8* %119
}

LLVM IR (-O2):
@LC_ALL = global i32 0
@.str = [6 x i8] c"cs_CZ\00"
@stderr = global i32 0
@.str.1 = [27 x i8] c"error: not enought memory\0A\00"
define i8* @heapsort(i8** %0, i64 %1) {
%3 = load i32, i32* @LC_ALL
%4 = tail call i32 @setlocale(i32 %3, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%5 = add i64 %1, -1
%6 = lshr i64 %5, 1
br label %9
7: ; preds = %9
%8 = icmp eq i64 %5, 0
br i1 %8, label %14, label %21
9: ; preds = %2, %9
%10 = phi i64 [ %6, %2 ], [ %12, %9 ]
%11 = tail call i32 @sift(i8** %0, i64 %5, i64 %10)
%12 = add nsw i64 %10, -1
%13 = icmp eq i64 %10, 0
br i1 %13, label %7, label %9
14: ; preds = %21, %7
%15 = load i8*, i8** %0
%16 = tail call i32 @strlen(i8* %15)
%17 = add i32 %16, 1
%18 = tail call i64 @malloc(i32 %17)
%19 = inttoptr i64 %18 to i8*
%20 = icmp eq i64 %18, 0
br i1 %20, label %29, label %33
21: ; preds = %7, %21
%22 = phi i64 [ %26, %21 ], [ %5, %7 ]
%23 = load i8*, i8** %0
%24 = getelementptr inbounds i8*, i8** %0, i64 %22
%25 = load i8*, i8** %24
store i8* %25, i8** %0
store i8* %23, i8** %24
%26 = add i64 %22, -1
%27 = tail call i32 @sift(i8** %0, i64 %26, i64 0)
%28 = icmp eq i64 %26, 0
br i1 %28, label %14, label %21
29: ; preds = %14
%30 = load i32, i32* @stderr
%31 = tail call i32 @fprintf(i32 %30, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0))
%32 = tail call i32 @exit(i32 1)
unreachable
33: ; preds = %14
%34 = load i8*, i8** %0
%35 = tail call i32 @strcpy(i8* %19, i8* %34)
%36 = load i8*, i8** %0
%37 = tail call i32 @free(i8* %36)
%38 = icmp ugt i64 %1, 1
br i1 %38, label %41, label %39
39: ; preds = %56, %33
%40 = phi i8* [ %19, %33 ], [ %50, %56 ]
ret i8* %40
41: ; preds = %33, %56
%42 = phi i64 [ %61, %56 ], [ 1, %33 ]
%43 = phi i8* [ %50, %56 ], [ %19, %33 ]
%44 = getelementptr inbounds i8*, i8** %0, i64 %42
%45 = load i8*, i8** %44
%46 = tail call i32 @strlen(i8* %45)
%47 = tail call i32 @strlen(i8* %43)
%48 = add i32 %46, 1
%49 = add i32 %48, %47
%50 = tail call i8* @realloc(i8* %43, i32 %49)
%51 = icmp eq i8* %50, null
br i1 %51, label %52, label %56
52: ; preds = %41
%53 = load i32, i32* @stderr
%54 = tail call i32 @fprintf(i32 %53, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0))
%55 = tail call i32 @exit(i32 1)
unreachable
56: ; preds = %41
%57 = load i8*, i8** %44
%58 = tail call i32 @strcat(i8* %50, i8* %57)
%59 = load i8*, i8** %44
%60 = tail call i32 @free(i8* %59)
%61 = add nuw i64 %42, 1
%62 = icmp eq i64 %61, %1
br i1 %62, label %39, label %41
}