#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 *malloc(int);
int /*<<< orphan*/ merge(int *, int *, int, int, int);
int min(int, int);
void mergeSortNonRecursive(int A[], int N) {
  int *tmpArray;
  int subListSize, part1Start, part2Start, part2End;
  tmpArray = malloc(sizeof(int) * N);
  for (subListSize = 1; subListSize < N; subListSize *= 2) {
    part1Start = 0;
    while (part1Start + subListSize < N - 1) {
      part2Start = part1Start + subListSize;
      part2End = min(N - 1, part2Start + subListSize - 1);
      merge(A, tmpArray, part1Start, part2Start, part2End);
      part1Start = part2End + 1;
    }
  }
}


x86 assembly (clang, -O0):
<mergeSortNonRecursive>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movslq -0xc(%rbp),%rax
shl    $0x2,%rax
mov    %eax,%edi
call   1050 <malloc@plt>
mov    %rax,-0x18(%rbp)
movl   $0x1,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    11d8 <mergeSortNonRecursive+0xa8>
movl   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%eax
add    -0x1c(%rbp),%eax
mov    -0xc(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jge    11c5 <mergeSortNonRecursive+0x95>
mov    -0x20(%rbp),%eax
add    -0x1c(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0xc(%rbp),%edi
sub    $0x1,%edi
mov    -0x24(%rbp),%esi
add    -0x1c(%rbp),%esi
sub    $0x1,%esi
call   1030 <min@plt>
mov    %eax,-0x28(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%edx
mov    -0x24(%rbp),%ecx
mov    -0x28(%rbp),%r8d
call   1040 <merge@plt>
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    116c <mergeSortNonRecursive+0x3c>
jmp    11ca <mergeSortNonRecursive+0x9a>
mov    -0x1c(%rbp),%eax
shl    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1159 <mergeSortNonRecursive+0x29>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mergeSortNonRecursive>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %esi,%ebx
mov    %rdi,0x10(%rsp)
lea    0x0(,%rbx,4),%edi
call   1050 <malloc@plt>
mov    %rax,0x8(%rsp)
mov    %rbx,(%rsp)
cmp    $0x2,%ebx
jl     11c9 <mergeSortNonRecursive+0x99>
mov    (%rsp),%rax
lea    -0x1(%rax),%r13d
mov    $0x1,%r15d
jmp    1179 <mergeSortNonRecursive+0x49>
nop
add    %r15d,%r15d
cmp    (%rsp),%r15d
jge    11c9 <mergeSortNonRecursive+0x99>
cmp    %r13d,%r15d
jge    1170 <mergeSortNonRecursive+0x40>
lea    -0x1(%r15),%r12d
xor    %ebx,%ebx
mov    %r15d,%ebp
nopw   0x0(%rax,%rax,1)
lea    (%r12,%rbp,1),%esi
mov    %r13d,%edi
call   1030 <min@plt>
mov    %eax,%r14d
mov    0x10(%rsp),%rdi
mov    0x8(%rsp),%rsi
mov    %ebx,%edx
mov    %ebp,%ecx
mov    %eax,%r8d
call   1040 <merge@plt>
mov    %r14d,%ebx
add    $0x1,%ebx
lea    (%r14,%r15,1),%ebp
add    $0x1,%ebp
cmp    %r13d,%ebp
jl     1190 <mergeSortNonRecursive+0x60>
jmp    1170 <mergeSortNonRecursive+0x40>
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define void @mergeSortNonRecursive(i32* %0, i32 %1) {
%3 = alloca i32*
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i32* %0, i32** %3
store i32 %1, i32* %4
%10 = load i32, i32* %4
%11 = sext i32 %10 to i64
%12 = mul i64 4, %11
%13 = trunc i64 %12 to i32
%14 = call i32* @malloc(i32 %13)
store i32* %14, i32** %5
store i32 1, i32* %6
br label %15
15: ; preds = %47, %2
%16 = load i32, i32* %6
%17 = load i32, i32* %4
%18 = icmp slt i32 %16, %17
br i1 %18, label %19, label %50
19: ; preds = %15
store i32 0, i32* %7
br label %20
20: ; preds = %27, %19
%21 = load i32, i32* %7
%22 = load i32, i32* %6
%23 = add nsw i32 %21, %22
%24 = load i32, i32* %4
%25 = sub nsw i32 %24, 1
%26 = icmp slt i32 %23, %25
br i1 %26, label %27, label %46
27: ; preds = %20
%28 = load i32, i32* %7
%29 = load i32, i32* %6
%30 = add nsw i32 %28, %29
store i32 %30, i32* %8
%31 = load i32, i32* %4
%32 = sub nsw i32 %31, 1
%33 = load i32, i32* %8
%34 = load i32, i32* %6
%35 = add nsw i32 %33, %34
%36 = sub nsw i32 %35, 1
%37 = call i32 @min(i32 %32, i32 %36)
store i32 %37, i32* %9
%38 = load i32*, i32** %3
%39 = load i32*, i32** %5
%40 = load i32, i32* %7
%41 = load i32, i32* %8
%42 = load i32, i32* %9
%43 = call i32 @merge(i32* %38, i32* %39, i32 %40, i32 %41, i32 %42)
%44 = load i32, i32* %9
%45 = add nsw i32 %44, 1
store i32 %45, i32* %7
br label %20
46: ; preds = %20
br label %47
47: ; preds = %46
%48 = load i32, i32* %6
%49 = mul nsw i32 %48, 2
store i32 %49, i32* %6
br label %15
50: ; preds = %15
ret void
}

LLVM IR (-O2):
define void @mergeSortNonRecursive(i32* %0, i32 %1) {
%3 = shl i32 %1, 2
%4 = tail call i32* @malloc(i32 %3)
%5 = icmp sgt i32 %1, 1
br i1 %5, label %6, label %25
6: ; preds = %2
%7 = add nsw i32 %1, -1
br label %8
8: ; preds = %6, %22
%9 = phi i32 [ 1, %6 ], [ %23, %22 ]
%10 = icmp slt i32 %9, %7
br i1 %10, label %11, label %22
11: ; preds = %8
%12 = add i32 %9, -1
br label %13
13: ; preds = %11, %13
%14 = phi i32 [ %9, %11 ], [ %20, %13 ]
%15 = phi i32 [ 0, %11 ], [ %19, %13 ]
%16 = add i32 %12, %14
%17 = tail call i32 @min(i32 %7, i32 %16)
%18 = tail call i32 @merge(i32* %0, i32* %4, i32 %15, i32 %14, i32 %17)
%19 = add nsw i32 %17, 1
%20 = add nsw i32 %19, %9
%21 = icmp slt i32 %20, %7
br i1 %21, label %13, label %22
22: ; preds = %13, %8
%23 = shl nsw i32 %9, 1
%24 = icmp slt i32 %23, %1
br i1 %24, label %8, label %25
25: ; preds = %22, %2
ret void
}