#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*/ assert(int);
scalar_t__ mergei_v2(int *, int, int, int);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ printvi(int *, int);
int /*<<< orphan*/ printvinl(int *, int);
int /*<<< orphan*/ putchar(char);
int simple_top_down_merge_sorti_v2_impl(int arr[], int lb, int ub, int level) {
  int mid;
  assert(arr != NULL && lb >= 0 && lb <= ub);
  {
    int i;
    for (i = 0; i < level * 4; i++)
      putchar(' ');
  };
  printf("Sort: ");
  printvinl(arr + lb, ub - lb);
  if (ub - lb < 2)
    return 0;
  mid = (lb + ub) / 2;
  if (simple_top_down_merge_sorti_v2_impl(arr, lb, mid, level + 1) != 0)
    return 1;
  if (simple_top_down_merge_sorti_v2_impl(arr, mid, ub, level + 1) != 0)
    return 1;
  {
    int i;
    for (i = 0; i < level * 4; i++)
      putchar(' ');
  };
  printf("Merge: ");
  printvi(arr + lb, mid - lb);
  printf(" + ");
  printvi(arr + mid, ub - mid);
  if (mergei_v2(arr, lb, mid, ub) != 0)
    return 1;
  printf(" -> ");
  printvinl(arr + lb, ub - lb);
  return 0;
}


x86 assembly (clang, -O0):
<simple_top_down_merge_sorti_v2_impl>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
xor    %eax,%eax
cmpq   $0x0,-0x10(%rbp)
mov    %al,-0x29(%rbp)
je     11b0 <simple_top_down_merge_sorti_v2_impl+0x40>
xor    %eax,%eax
cmpl   $0x0,-0x14(%rbp)
mov    %al,-0x29(%rbp)
jl     11b0 <simple_top_down_merge_sorti_v2_impl+0x40>
mov    -0x14(%rbp),%eax
cmp    -0x18(%rbp),%eax
setle  %al
mov    %al,-0x29(%rbp)
mov    -0x29(%rbp),%al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
mov    -0x1c(%rbp),%ecx
shl    $0x2,%ecx
cmp    %ecx,%eax
jge    11ed <simple_top_down_merge_sorti_v2_impl+0x7d>
mov    $0x20,%edi
call   1030 <putchar@plt>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    11c4 <simple_top_down_merge_sorti_v2_impl+0x54>
lea    0xe0c(%rip),%rdi
call   1040 <printf@plt>
mov    -0x10(%rbp),%rdi
movslq -0x14(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rdi
mov    -0x18(%rbp),%esi
sub    -0x14(%rbp),%esi
call   1080 <printvinl@plt>
mov    -0x18(%rbp),%eax
sub    -0x14(%rbp),%eax
cmp    $0x2,%eax
jge    122e <simple_top_down_merge_sorti_v2_impl+0xbe>
movl   $0x0,-0x4(%rbp)
jmp    1364 <simple_top_down_merge_sorti_v2_impl+0x1f4>
mov    -0x14(%rbp),%eax
add    -0x18(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    -0x20(%rbp),%edx
mov    -0x1c(%rbp),%ecx
add    $0x1,%ecx
call   1050 <simple_top_down_merge_sorti_v2_impl@plt>
cmp    $0x0,%eax
je     1269 <simple_top_down_merge_sorti_v2_impl+0xf9>
movl   $0x1,-0x4(%rbp)
jmp    1364 <simple_top_down_merge_sorti_v2_impl+0x1f4>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%esi
mov    -0x18(%rbp),%edx
mov    -0x1c(%rbp),%ecx
add    $0x1,%ecx
call   1050 <simple_top_down_merge_sorti_v2_impl@plt>
cmp    $0x0,%eax
je     1293 <simple_top_down_merge_sorti_v2_impl+0x123>
movl   $0x1,-0x4(%rbp)
jmp    1364 <simple_top_down_merge_sorti_v2_impl+0x1f4>
movl   $0x0,-0x28(%rbp)
mov    -0x28(%rbp),%eax
mov    -0x1c(%rbp),%ecx
shl    $0x2,%ecx
cmp    %ecx,%eax
jge    12c3 <simple_top_down_merge_sorti_v2_impl+0x153>
mov    $0x20,%edi
call   1030 <putchar@plt>
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
jmp    129a <simple_top_down_merge_sorti_v2_impl+0x12a>
lea    0xd3d(%rip),%rdi
call   1040 <printf@plt>
mov    -0x10(%rbp),%rdi
movslq -0x14(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rdi
mov    -0x20(%rbp),%esi
sub    -0x14(%rbp),%esi
call   1070 <printvi@plt>
lea    0xd1f(%rip),%rdi
call   1040 <printf@plt>
mov    -0x10(%rbp),%rdi
movslq -0x20(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rdi
mov    -0x18(%rbp),%esi
sub    -0x20(%rbp),%esi
call   1070 <printvi@plt>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    -0x20(%rbp),%edx
mov    -0x18(%rbp),%ecx
call   1090 <mergei_v2@plt>
cmp    $0x0,%rax
je     1337 <simple_top_down_merge_sorti_v2_impl+0x1c7>
movl   $0x1,-0x4(%rbp)
jmp    1364 <simple_top_down_merge_sorti_v2_impl+0x1f4>
lea    0xcd5(%rip),%rdi
call   1040 <printf@plt>
mov    -0x10(%rbp),%rdi
movslq -0x14(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rdi
mov    -0x18(%rbp),%esi
sub    -0x14(%rbp),%esi
call   1080 <printvinl@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<simple_top_down_merge_sorti_v2_impl>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %ecx,%r15d
mov    %edx,%r14d
mov    %esi,%r12d
mov    %rdi,%rbx
test   %rdi,%rdi
setne  %al
test   %esi,%esi
setns  %cl
and    %al,%cl
mov    %edx,%r13d
sub    %esi,%r13d
setge  %al
and    %cl,%al
movzbl %al,%edi
call   1060 <assert@plt>
lea    0x0(,%r15,4),%eax
mov    %eax,0x4(%rsp)
test   %r15d,%r15d
jle    11df <simple_top_down_merge_sorti_v2_impl+0x6f>
mov    0x4(%rsp),%eax
cmp    $0x2,%eax
mov    $0x1,%ebp
cmovge %eax,%ebp
nopw   0x0(%rax,%rax,1)
mov    $0x20,%edi
call   1030 <putchar@plt>
add    $0xffffffff,%ebp
jne    11d0 <simple_top_down_merge_sorti_v2_impl+0x60>
lea    0xe1a(%rip),%rdi
call   1040 <printf@plt>
movslq %r12d,%rax
lea    (%rbx,%rax,4),%rbp
mov    %rbp,%rdi
mov    %r13d,%esi
call   1080 <printvinl@plt>
xor    %ecx,%ecx
cmp    $0x2,%r13d
jl     1306 <simple_top_down_merge_sorti_v2_impl+0x196>
mov    %rbp,0x10(%rsp)
lea    (%r14,%r12,1),%eax
mov    %r14,0x8(%rsp)
mov    %eax,%r14d
shr    $0x1f,%r14d
add    %eax,%r14d
sar    %r14d
lea    0x1(%r15),%ebp
mov    %rbx,%rdi
mov    %r12d,%esi
mov    %r14d,%edx
mov    %ebp,%ecx
call   1050 <simple_top_down_merge_sorti_v2_impl@plt>
mov    $0x1,%ecx
test   %eax,%eax
jne    1306 <simple_top_down_merge_sorti_v2_impl+0x196>
mov    %rbx,%rdi
mov    %r14d,%esi
mov    0x8(%rsp),%rdx
mov    %ebp,%ecx
call   1050 <simple_top_down_merge_sorti_v2_impl@plt>
test   %eax,%eax
je     1265 <simple_top_down_merge_sorti_v2_impl+0xf5>
mov    $0x1,%ecx
jmp    1306 <simple_top_down_merge_sorti_v2_impl+0x196>
test   %r15d,%r15d
jle    128f <simple_top_down_merge_sorti_v2_impl+0x11f>
mov    0x4(%rsp),%eax
cmp    $0x2,%eax
mov    $0x1,%ebp
cmovge %eax,%ebp
nopl   0x0(%rax)
mov    $0x20,%edi
call   1030 <putchar@plt>
add    $0xffffffff,%ebp
jne    1280 <simple_top_down_merge_sorti_v2_impl+0x110>
lea    0xd71(%rip),%rdi
call   1040 <printf@plt>
mov    %r14d,%esi
sub    %r12d,%esi
mov    0x10(%rsp),%r15
mov    %r15,%rdi
call   1070 <printvi@plt>
lea    0xd5a(%rip),%rdi
call   1040 <printf@plt>
movslq %r14d,%rax
mov    %rbx,%rbp
lea    (%rbx,%rax,4),%rdi
mov    0x8(%rsp),%rbx
mov    %ebx,%esi
sub    %r14d,%esi
call   1070 <printvi@plt>
mov    %rbp,%rdi
mov    %r12d,%esi
mov    %r14d,%edx
mov    %ebx,%ecx
call   1090 <mergei_v2@plt>
test   %rax,%rax
mov    $0x1,%ecx
jne    1306 <simple_top_down_merge_sorti_v2_impl+0x196>
lea    0xd1f(%rip),%rdi
call   1040 <printf@plt>
mov    %r15,%rdi
mov    %r13d,%esi
call   1080 <printvinl@plt>
xor    %ecx,%ecx
mov    %ecx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [7 x i8] c"Sort: \00"
@.str.1 = [8 x i8] c"Merge: \00"
@.str.2 = [4 x i8] c" + \00"
@.str.3 = [5 x i8] c" -> \00"
define i32 @simple_top_down_merge_sorti_v2_impl(i32* %0, i32 %1, i32 %2, i32 %3) {
%5 = alloca i32
%6 = alloca i32*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
store i32* %0, i32** %6
store i32 %1, i32* %7
store i32 %2, i32* %8
store i32 %3, i32* %9
%13 = load i32*, i32** %6
%14 = icmp ne i32* %13, null
br i1 %14, label %15, label %22
15: ; preds = %4
%16 = load i32, i32* %7
%17 = icmp sge i32 %16, 0
br i1 %17, label %18, label %22
18: ; preds = %15
%19 = load i32, i32* %7
%20 = load i32, i32* %8
%21 = icmp sle i32 %19, %20
br label %22
22: ; preds = %18, %15, %4
%23 = phi i1 [ false, %15 ], [ false, %4 ], [ %21, %18 ]
%24 = zext i1 %23 to i32
%25 = call i32 @assert(i32 %24)
store i32 0, i32* %11
br label %26
26: ; preds = %33, %22
%27 = load i32, i32* %11
%28 = load i32, i32* %9
%29 = mul nsw i32 %28, 4
%30 = icmp slt i32 %27, %29
br i1 %30, label %31, label %36
31: ; preds = %26
%32 = call i32 @putchar(i8 signext 32)
br label %33
33: ; preds = %31
%34 = load i32, i32* %11
%35 = add nsw i32 %34, 1
store i32 %35, i32* %11
br label %26
36: ; preds = %26
%37 = call i32 @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%38 = load i32*, i32** %6
%39 = load i32, i32* %7
%40 = sext i32 %39 to i64
%41 = getelementptr inbounds i32, i32* %38, i64 %40
%42 = load i32, i32* %8
%43 = load i32, i32* %7
%44 = sub nsw i32 %42, %43
%45 = call i32 @printvinl(i32* %41, i32 %44)
%46 = load i32, i32* %8
%47 = load i32, i32* %7
%48 = sub nsw i32 %46, %47
%49 = icmp slt i32 %48, 2
br i1 %49, label %50, label %51
50: ; preds = %36
store i32 0, i32* %5
br label %120
51: ; preds = %36
%52 = load i32, i32* %7
%53 = load i32, i32* %8
%54 = add nsw i32 %52, %53
%55 = sdiv i32 %54, 2
store i32 %55, i32* %10
%56 = load i32*, i32** %6
%57 = load i32, i32* %7
%58 = load i32, i32* %10
%59 = load i32, i32* %9
%60 = add nsw i32 %59, 1
%61 = call i32 @simple_top_down_merge_sorti_v2_impl(i32* %56, i32 %57, i32 %58, i32 %60)
%62 = icmp ne i32 %61, 0
br i1 %62, label %63, label %64
63: ; preds = %51
store i32 1, i32* %5
br label %120
64: ; preds = %51
%65 = load i32*, i32** %6
%66 = load i32, i32* %10
%67 = load i32, i32* %8
%68 = load i32, i32* %9
%69 = add nsw i32 %68, 1
%70 = call i32 @simple_top_down_merge_sorti_v2_impl(i32* %65, i32 %66, i32 %67, i32 %69)
%71 = icmp ne i32 %70, 0
br i1 %71, label %72, label %73
72: ; preds = %64
store i32 1, i32* %5
br label %120
73: ; preds = %64
store i32 0, i32* %12
br label %74
74: ; preds = %81, %73
%75 = load i32, i32* %12
%76 = load i32, i32* %9
%77 = mul nsw i32 %76, 4
%78 = icmp slt i32 %75, %77
br i1 %78, label %79, label %84
79: ; preds = %74
%80 = call i32 @putchar(i8 signext 32)
br label %81
81: ; preds = %79
%82 = load i32, i32* %12
%83 = add nsw i32 %82, 1
store i32 %83, i32* %12
br label %74
84: ; preds = %74
%85 = call i32 @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
%86 = load i32*, i32** %6
%87 = load i32, i32* %7
%88 = sext i32 %87 to i64
%89 = getelementptr inbounds i32, i32* %86, i64 %88
%90 = load i32, i32* %10
%91 = load i32, i32* %7
%92 = sub nsw i32 %90, %91
%93 = call i32 @printvi(i32* %89, i32 %92)
%94 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0))
%95 = load i32*, i32** %6
%96 = load i32, i32* %10
%97 = sext i32 %96 to i64
%98 = getelementptr inbounds i32, i32* %95, i64 %97
%99 = load i32, i32* %8
%100 = load i32, i32* %10
%101 = sub nsw i32 %99, %100
%102 = call i32 @printvi(i32* %98, i32 %101)
%103 = load i32*, i32** %6
%104 = load i32, i32* %7
%105 = load i32, i32* %10
%106 = load i32, i32* %8
%107 = call i64 @mergei_v2(i32* %103, i32 %104, i32 %105, i32 %106)
%108 = icmp ne i64 %107, 0
br i1 %108, label %109, label %110
109: ; preds = %84
store i32 1, i32* %5
br label %120
110: ; preds = %84
%111 = call i32 @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%112 = load i32*, i32** %6
%113 = load i32, i32* %7
%114 = sext i32 %113 to i64
%115 = getelementptr inbounds i32, i32* %112, i64 %114
%116 = load i32, i32* %8
%117 = load i32, i32* %7
%118 = sub nsw i32 %116, %117
%119 = call i32 @printvinl(i32* %115, i32 %118)
store i32 0, i32* %5
br label %120
120: ; preds = %110, %109, %72, %63, %50
%121 = load i32, i32* %5
ret i32 %121
}

LLVM IR (-O2):
@.str = [7 x i8] c"Sort: \00"
@.str.1 = [8 x i8] c"Merge: \00"
@.str.2 = [4 x i8] c" + \00"
@.str.3 = [5 x i8] c" -> \00"
define i32 @simple_top_down_merge_sorti_v2_impl(i32* %0, i32 %1, i32 %2, i32 %3) {
%5 = icmp ne i32* %0, null
%6 = icmp sgt i32 %1, -1
%7 = and i1 %5, %6
%8 = icmp sge i32 %2, %1
%9 = and i1 %7, %8
%10 = zext i1 %9 to i32
%11 = tail call i32 @assert(i32 %10)
%12 = shl i32 %3, 2
%13 = icmp sgt i32 %3, 0
br i1 %13, label %14, label %21
14: ; preds = %4
%15 = call i32 @llvm.smax.i32(i32 %12, i32 1)
br label %16
16: ; preds = %14, %16
%17 = phi i32 [ %19, %16 ], [ 0, %14 ]
%18 = tail call i32 @putchar(i8 signext 32)
%19 = add nuw nsw i32 %17, 1
%20 = icmp eq i32 %19, %15
br i1 %20, label %21, label %16
21: ; preds = %16, %4
%22 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%23 = sext i32 %1 to i64
%24 = getelementptr inbounds i32, i32* %0, i64 %23
%25 = sub nsw i32 %2, %1
%26 = tail call i32 @printvinl(i32* %24, i32 %25)
%27 = icmp slt i32 %25, 2
br i1 %27, label %59, label %28
28: ; preds = %21
%29 = add nsw i32 %2, %1
%30 = sdiv i32 %29, 2
%31 = add nsw i32 %3, 1
%32 = tail call i32 @simple_top_down_merge_sorti_v2_impl(i32* %0, i32 %1, i32 %30, i32 %31)
%33 = icmp eq i32 %32, 0
br i1 %33, label %34, label %59
34: ; preds = %28
%35 = tail call i32 @simple_top_down_merge_sorti_v2_impl(i32* %0, i32 %30, i32 %2, i32 %31)
%36 = icmp eq i32 %35, 0
br i1 %36, label %37, label %59
37: ; preds = %34
br i1 %13, label %38, label %45
38: ; preds = %37
%39 = call i32 @llvm.smax.i32(i32 %12, i32 1)
br label %40
40: ; preds = %38, %40
%41 = phi i32 [ %43, %40 ], [ 0, %38 ]
%42 = tail call i32 @putchar(i8 signext 32)
%43 = add nuw nsw i32 %41, 1
%44 = icmp eq i32 %43, %39
br i1 %44, label %45, label %40
45: ; preds = %40, %37
%46 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
%47 = sub nsw i32 %30, %1
%48 = tail call i32 @printvi(i32* %24, i32 %47)
%49 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0))
%50 = sext i32 %30 to i64
%51 = getelementptr inbounds i32, i32* %0, i64 %50
%52 = sub nsw i32 %2, %30
%53 = tail call i32 @printvi(i32* %51, i32 %52)
%54 = tail call i64 @mergei_v2(i32* %0, i32 %1, i32 %30, i32 %2)
%55 = icmp eq i64 %54, 0
br i1 %55, label %56, label %59
56: ; preds = %45
%57 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%58 = tail call i32 @printvinl(i32* %24, i32 %25)
br label %59
59: ; preds = %45, %34, %28, %21, %56
%60 = phi i32 [ 0, %56 ], [ 0, %21 ], [ 1, %28 ], [ 1, %34 ], [ 1, %45 ]
ret i32 %60
}