#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 merge(char **, int, int (*)(void *, void *), int, int);
int mergesort(char **auxfilename, int elementsize,
              int (*elementorder)(void *, void *), int *passes) {
  int a = 0, b = 2, c, sortcomplete;
  *passes = 0;
  do {
    sortcomplete = merge(auxfilename, elementsize, elementorder, a, b);
    if (!sortcomplete) {
      (*passes)++;
      c = a;
      a = b;
      b = c;
    }
  } while (!sortcomplete);
  return a;
}


x86 assembly (clang, -O0):
<mergesort>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
movl   $0x2,-0x28(%rbp)
mov    -0x20(%rbp),%rax
movl   $0x0,(%rax)
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    -0x18(%rbp),%rdx
mov    -0x24(%rbp),%ecx
mov    -0x28(%rbp),%r8d
call   1030 <merge@plt>
mov    %eax,-0x30(%rbp)
cmpl   $0x0,-0x30(%rbp)
jne    1180 <mergesort+0x70>
mov    -0x20(%rbp),%rax
mov    (%rax),%ecx
add    $0x1,%ecx
mov    %ecx,(%rax)
mov    -0x24(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x28(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x2c(%rbp),%eax
mov    %eax,-0x28(%rbp)
jmp    1185 <mergesort+0x75>
cmpl   $0x0,-0x30(%rbp)
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    113f <mergesort+0x2f>
mov    -0x24(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mergesort>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rcx,%r14
mov    %rdx,%r15
mov    %esi,%r12d
mov    %rdi,%r13
movl   $0x0,(%rcx)
xor    %ebp,%ebp
mov    $0x2,%ebx
xor    %ecx,%ecx
mov    $0x2,%r8d
call   1030 <merge@plt>
test   %eax,%eax
jne    1171 <mergesort+0x61>
xor    %ecx,%ecx
nopw   0x0(%rax,%rax,1)
mov    %ebx,%ebp
mov    %ecx,%ebx
addl   $0x1,(%r14)
mov    %r13,%rdi
mov    %r12d,%esi
mov    %r15,%rdx
mov    %ebp,%ecx
mov    %ebx,%r8d
call   1030 <merge@plt>
mov    %ebp,%ecx
test   %eax,%eax
je     1150 <mergesort+0x40>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @mergesort(i8** %0, i32 %1, i32 (i8*, i8*)* %2, i32* %3) {
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32 (i8*, i8*)*
%8 = alloca i32*
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
store i8** %0, i8*** %5
store i32 %1, i32* %6
store i32 (i8*, i8*)* %2, i32 (i8*, i8*)** %7
store i32* %3, i32** %8
store i32 0, i32* %9
store i32 2, i32* %10
%13 = load i32*, i32** %8
store i32 0, i32* %13
br label %14
14: ; preds = %31, %4
%15 = load i8**, i8*** %5
%16 = load i32, i32* %6
%17 = load i32 (i8*, i8*)*, i32 (i8*, i8*)** %7
%18 = load i32, i32* %9
%19 = load i32, i32* %10
%20 = call i32 @merge(i8** %15, i32 %16, i32 (i8*, i8*)* %17, i32 %18, i32 %19)
store i32 %20, i32* %12
%21 = load i32, i32* %12
%22 = icmp ne i32 %21, 0
br i1 %22, label %30, label %23
23: ; preds = %14
%24 = load i32*, i32** %8
%25 = load i32, i32* %24
%26 = add nsw i32 %25, 1
store i32 %26, i32* %24
%27 = load i32, i32* %9
store i32 %27, i32* %11
%28 = load i32, i32* %10
store i32 %28, i32* %9
%29 = load i32, i32* %11
store i32 %29, i32* %10
br label %30
30: ; preds = %23, %14
br label %31
31: ; preds = %30
%32 = load i32, i32* %12
%33 = icmp ne i32 %32, 0
%34 = xor i1 %33, true
br i1 %34, label %14, label %35
35: ; preds = %31
%36 = load i32, i32* %9
ret i32 %36
}

LLVM IR (-O2):
define i32 @mergesort(i8** %0, i32 %1, i32 (i8*, i8*)* %2, i32* nocapture %3) {
store i32 0, i32* %3
%5 = tail call i32 @merge(i8** %0, i32 %1, i32 (i8*, i8*)* %2, i32 0, i32 2)
%6 = icmp eq i32 %5, 0
br i1 %6, label %7, label %14
7: ; preds = %4, %7
%8 = phi i32 [ %9, %7 ], [ 0, %4 ]
%9 = phi i32 [ %8, %7 ], [ 2, %4 ]
%10 = load i32, i32* %3
%11 = add nsw i32 %10, 1
store i32 %11, i32* %3
%12 = tail call i32 @merge(i8** %0, i32 %1, i32 (i8*, i8*)* %2, i32 %9, i32 %8)
%13 = icmp eq i32 %12, 0
br i1 %13, label %7, label %14
14: ; preds = %7, %4
%15 = phi i32 [ 0, %4 ], [ %9, %7 ]
ret i32 %15
}