#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 */
typedef int uint32_t;
typedef int /*<<< orphan*/ ba;

/* Variables and functions */
int backtrace(void **, int);
char **backtrace_symbols(void **, int);
int /*<<< orphan*/ free(char **);
int /*<<< orphan*/ printf(char *, char *);
int compare(const void *p1, const void *p2) {
  void *ba[20];
  int n = backtrace(ba, sizeof(ba) / sizeof(ba[0]));
  if (n != 0) {
    char **names = backtrace_symbols(ba, n);
    if (names != NULL) {
      int i;
      printf("called from %s\n", names[0]);
      for (i = 1; i < n; ++i)
        printf("            %s\n", names[i]);
      free(names);
    }
  }
  return *(const uint32_t *)p1 - *(const uint32_t *)p2;
}


x86 assembly (clang, -O0):
<compare>:
push   %rbp
mov    %rsp,%rbp
sub    $0xd0,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0xb0(%rbp),%rdi
mov    $0x14,%esi
call   1050 <backtrace@plt>
mov    %eax,-0xb4(%rbp)
cmpl   $0x0,-0xb4(%rbp)
je     1213 <compare+0xd3>
lea    -0xb0(%rbp),%rdi
mov    -0xb4(%rbp),%esi
call   1040 <backtrace_symbols@plt>
mov    %rax,-0xc0(%rbp)
cmpq   $0x0,-0xc0(%rbp)
je     120e <compare+0xce>
mov    -0xc0(%rbp),%rax
mov    (%rax),%rsi
lea    0xe51(%rip),%rdi
call   1060 <printf@plt>
movl   $0x1,-0xc4(%rbp)
mov    -0xc4(%rbp),%eax
cmp    -0xb4(%rbp),%eax
jge    1202 <compare+0xc2>
mov    -0xc0(%rbp),%rax
movslq -0xc4(%rbp),%rcx
mov    (%rax,%rcx,8),%rsi
lea    0xe27(%rip),%rdi
call   1060 <printf@plt>
mov    -0xc4(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc4(%rbp)
jmp    11be <compare+0x7e>
mov    -0xc0(%rbp),%rdi
call   1030 <free@plt>
jmp    1213 <compare+0xd3>
mov    -0x8(%rbp),%rax
mov    (%rax),%eax
mov    -0x10(%rbp),%rcx
sub    (%rcx),%eax
add    $0xd0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<compare>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0xa8,%rsp
mov    %rsi,%r14
mov    %rdi,%r15
mov    %rsp,%rdi
mov    $0x14,%esi
call   1050 <backtrace@plt>
test   %eax,%eax
je     11be <compare+0x7e>
mov    %eax,%ebp
mov    %rsp,%rdi
mov    %eax,%esi
call   1040 <backtrace_symbols@plt>
test   %rax,%rax
je     11be <compare+0x7e>
mov    %rax,%r13
mov    (%rax),%rsi
lea    0xe7a(%rip),%rdi
call   1060 <printf@plt>
cmp    $0x2,%ebp
jl     11b6 <compare+0x76>
mov    %ebp,%ebp
mov    $0x1,%ebx
lea    0xe72(%rip),%r12
xchg   %ax,%ax
mov    0x0(%r13,%rbx,8),%rsi
mov    %r12,%rdi
call   1060 <printf@plt>
add    $0x1,%rbx
cmp    %rbx,%rbp
jne    11a0 <compare+0x60>
mov    %r13,%rdi
call   1030 <free@plt>
mov    (%r15),%eax
sub    (%r14),%eax
add    $0xa8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [16 x i8] c"called from %s\0A\00"
@.str.1 = [16 x i8] c" %s\0A\00"
define i32 @compare(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca [20 x i8*]
%6 = alloca i32
%7 = alloca i8**
%8 = alloca i32
store i8* %0, i8** %3
store i8* %1, i8** %4
%9 = getelementptr inbounds [20 x i8*], [20 x i8*]* %5, i64 0, i64 0
%10 = call i32 @backtrace(i8** %9, i32 20)
store i32 %10, i32* %6
%11 = load i32, i32* %6
%12 = icmp ne i32 %11, 0
br i1 %12, label %13, label %42
13: ; preds = %2
%14 = getelementptr inbounds [20 x i8*], [20 x i8*]* %5, i64 0, i64 0
%15 = load i32, i32* %6
%16 = call i8** @backtrace_symbols(i8** %14, i32 %15)
store i8** %16, i8*** %7
%17 = load i8**, i8*** %7
%18 = icmp ne i8** %17, null
br i1 %18, label %19, label %41
19: ; preds = %13
%20 = load i8**, i8*** %7
%21 = getelementptr inbounds i8*, i8** %20, i64 0
%22 = load i8*, i8** %21
%23 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8* %22)
store i32 1, i32* %8
br label %24
24: ; preds = %35, %19
%25 = load i32, i32* %8
%26 = load i32, i32* %6
%27 = icmp slt i32 %25, %26
br i1 %27, label %28, label %38
28: ; preds = %24
%29 = load i8**, i8*** %7
%30 = load i32, i32* %8
%31 = sext i32 %30 to i64
%32 = getelementptr inbounds i8*, i8** %29, i64 %31
%33 = load i8*, i8** %32
%34 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8* %33)
br label %35
35: ; preds = %28
%36 = load i32, i32* %8
%37 = add nsw i32 %36, 1
store i32 %37, i32* %8
br label %24
38: ; preds = %24
%39 = load i8**, i8*** %7
%40 = call i32 @free(i8** %39)
br label %41
41: ; preds = %38, %13
br label %42
42: ; preds = %41, %2
%43 = load i8*, i8** %3
%44 = bitcast i8* %43 to i32*
%45 = load i32, i32* %44
%46 = load i8*, i8** %4
%47 = bitcast i8* %46 to i32*
%48 = load i32, i32* %47
%49 = sub nsw i32 %45, %48
ret i32 %49
}

LLVM IR (-O2):
@.str = [16 x i8] c"called from %s\0A\00"
@.str.1 = [16 x i8] c" %s\0A\00"
define i32 @compare(i8* nocapture %0, i8* nocapture %1) {
%3 = alloca [20 x i8*]
%4 = bitcast [20 x i8*]* %3 to i8*
%5 = getelementptr inbounds [20 x i8*], [20 x i8*]* %3, i64 0, i64 0
%6 = call i32 @backtrace(i8** %5, i32 20)
%7 = icmp eq i32 %6, 0
br i1 %7, label %26, label %8
8: ; preds = %2
%9 = call i8** @backtrace_symbols(i8** %5, i32 %6)
%10 = icmp eq i8** %9, null
br i1 %10, label %26, label %11
11: ; preds = %8
%12 = load i8*, i8** %9
%13 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8* %12)
%14 = icmp sgt i32 %6, 1
br i1 %14, label %15, label %24
15: ; preds = %11
%16 = zext i32 %6 to i64
br label %17
17: ; preds = %15, %17
%18 = phi i64 [ 1, %15 ], [ %22, %17 ]
%19 = getelementptr inbounds i8*, i8** %9, i64 %18
%20 = load i8*, i8** %19
%21 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8* %20)
%22 = add nuw nsw i64 %18, 1
%23 = icmp eq i64 %22, %16
br i1 %23, label %24, label %17
24: ; preds = %17, %11
%25 = call i32 @free(i8** %9)
br label %26
26: ; preds = %8, %24, %2
%27 = bitcast i8* %0 to i32*
%28 = load i32, i32* %27
%29 = bitcast i8* %1 to i32*
%30 = load i32, i32* %29
%31 = sub nsw i32 %28, %30
ret i32 %31
}