#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*/ merge(int, int, int, int *);
int /*<<< orphan*/ print_vector(int *, int);
int /*<<< orphan*/ printf(char *);
void merge_sort(int init, int end, int *vec) {
  if (end > init) {
    int middle;
    middle = (init + end) / 2;
    merge_sort(init, middle, vec);
    merge_sort(middle + 1, end, vec);
    printf("\nMERGE\n");
    printf("VECTOR A - ");
    print_vector(vec + init, middle - init + 1);
    printf("VECTOR B - ");
    print_vector(vec + middle + 1, end - (middle + 1) + 1);
    merge(init, end, middle, vec);
    printf("MERGE VECTOR - ");
    print_vector(vec + init, end - init + 1);
  }
}


x86 assembly (clang, -O0):
<merge_sort>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %rdx,-0x10(%rbp)
mov    -0x8(%rbp),%eax
cmp    -0x4(%rbp),%eax
jle    1232 <merge_sort+0xf2>
mov    -0x4(%rbp),%eax
add    -0x8(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,-0x14(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x14(%rbp),%esi
mov    -0x10(%rbp),%rdx
call   1060 <merge_sort@plt>
mov    -0x14(%rbp),%edi
add    $0x1,%edi
mov    -0x8(%rbp),%esi
mov    -0x10(%rbp),%rdx
call   1060 <merge_sort@plt>
lea    0xe69(%rip),%rdi
call   1030 <printf@plt>
lea    0xe65(%rip),%rdi
call   1030 <printf@plt>
mov    -0x10(%rbp),%rdi
movslq -0x4(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rdi
mov    -0x14(%rbp),%esi
sub    -0x4(%rbp),%esi
add    $0x1,%esi
call   1050 <print_vector@plt>
lea    0xe48(%rip),%rdi
call   1030 <printf@plt>
mov    -0x10(%rbp),%rdi
movslq -0x14(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rdi
add    $0x4,%rdi
mov    -0x8(%rbp),%esi
mov    -0x14(%rbp),%eax
add    $0x1,%eax
sub    %eax,%esi
add    $0x1,%esi
call   1050 <print_vector@plt>
mov    -0x4(%rbp),%edi
mov    -0x8(%rbp),%esi
mov    -0x14(%rbp),%edx
mov    -0x10(%rbp),%rcx
call   1040 <merge@plt>
lea    0xe10(%rip),%rdi
call   1030 <printf@plt>
mov    -0x10(%rbp),%rdi
movslq -0x4(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rdi
mov    -0x8(%rbp),%esi
sub    -0x4(%rbp),%esi
add    $0x1,%esi
call   1050 <print_vector@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<merge_sort>:
cmp    %edi,%esi
jle    1218 <merge_sort+0xc8>
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %esi,%r15d
mov    %edi,%r12d
lea    (%r15,%r12,1),%eax
mov    %eax,%ebx
shr    $0x1f,%ebx
add    %eax,%ebx
sar    %ebx
mov    %ebx,%esi
call   1070 <merge_sort@plt>
lea    0x1(%rbx),%edi
mov    %r15d,%esi
mov    %r14,%rdx
call   1070 <merge_sort@plt>
lea    0xe93(%rip),%rdi
call   1030 <puts@plt>
lea    0xe5f(%rip),%rdi
call   1040 <printf@plt>
movslq %r12d,%rax
lea    (%r14,%rax,4),%r13
mov    $0x1,%ebp
sub    %r12d,%ebp
lea    (%rbx,%rbp,1),%esi
mov    %r13,%rdi
call   1060 <print_vector@plt>
lea    0xe45(%rip),%rdi
call   1040 <printf@plt>
movslq %ebx,%rbx
lea    (%r14,%rbx,4),%rdi
add    $0x4,%rdi
mov    %r15d,%esi
sub    %ebx,%esi
call   1060 <print_vector@plt>
mov    %r12d,%edi
mov    %r15d,%esi
mov    %ebx,%edx
mov    %r14,%rcx
call   1050 <merge@plt>
lea    0xe20(%rip),%rdi
call   1040 <printf@plt>
add    %r15d,%ebp
mov    %r13,%rdi
mov    %ebp,%esi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1060 <print_vector@plt>
ret

LLVM IR (-O0):
@.str = [8 x i8] c"\0AMERGE\0A\00"
@.str.1 = [12 x i8] c"VECTOR A - \00"
@.str.2 = [12 x i8] c"VECTOR B - \00"
@.str.3 = [16 x i8] c"MERGE VECTOR - \00"
define void @merge_sort(i32 %0, i32 %1, i32* %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32*
%7 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
store i32* %2, i32** %6
%8 = load i32, i32* %5
%9 = load i32, i32* %4
%10 = icmp sgt i32 %8, %9
br i1 %10, label %11, label %61
11: ; preds = %3
%12 = load i32, i32* %4
%13 = load i32, i32* %5
%14 = add nsw i32 %12, %13
%15 = sdiv i32 %14, 2
store i32 %15, i32* %7
%16 = load i32, i32* %4
%17 = load i32, i32* %7
%18 = load i32*, i32** %6
%19 = load i32, i32* %7
%20 = add nsw i32 %19, 1
%21 = load i32, i32* %5
%22 = load i32*, i32** %6
%23 = call i32 @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%24 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
%25 = load i32*, i32** %6
%26 = load i32, i32* %4
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds i32, i32* %25, i64 %27
%29 = load i32, i32* %7
%30 = load i32, i32* %4
%31 = sub nsw i32 %29, %30
%32 = add nsw i32 %31, 1
%33 = call i32 @print_vector(i32* %28, i32 %32)
%34 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
%35 = load i32*, i32** %6
%36 = load i32, i32* %7
%37 = sext i32 %36 to i64
%38 = getelementptr inbounds i32, i32* %35, i64 %37
%39 = getelementptr inbounds i32, i32* %38, i64 1
%40 = load i32, i32* %5
%41 = load i32, i32* %7
%42 = add nsw i32 %41, 1
%43 = sub nsw i32 %40, %42
%44 = add nsw i32 %43, 1
%45 = call i32 @print_vector(i32* %39, i32 %44)
%46 = load i32, i32* %4
%47 = load i32, i32* %5
%48 = load i32, i32* %7
%49 = load i32*, i32** %6
%50 = call i32 @merge(i32 %46, i32 %47, i32 %48, i32* %49)
%51 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0))
%52 = load i32*, i32** %6
%53 = load i32, i32* %4
%54 = sext i32 %53 to i64
%55 = getelementptr inbounds i32, i32* %52, i64 %54
%56 = load i32, i32* %5
%57 = load i32, i32* %4
%58 = sub nsw i32 %56, %57
%59 = add nsw i32 %58, 1
%60 = call i32 @print_vector(i32* %55, i32 %59)
br label %61
61: ; preds = %11, %3
ret void
}

LLVM IR (-O2):
@.str.1 = [12 x i8] c"VECTOR A - \00"
@.str.2 = [12 x i8] c"VECTOR B - \00"
@.str.3 = [16 x i8] c"MERGE VECTOR - \00"
@str = [7 x i8] c"\0AMERGE\00"
define void @merge_sort(i32 %0, i32 %1, i32* %2) {
%4 = icmp sgt i32 %1, %0
br i1 %4, label %6, label %5
5: ; preds = %3, %6
ret void
6: ; preds = %3
%7 = add nsw i32 %1, %0
%8 = sdiv i32 %7, 2
tail call void @merge_sort(i32 %0, i32 %8, i32* %2)
%9 = add nsw i32 %8, 1
tail call void @merge_sort(i32 %9, i32 %1, i32* %2)
%10 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @str, i64 0, i64 0))
%11 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
%12 = sext i32 %0 to i64
%13 = getelementptr inbounds i32, i32* %2, i64 %12
%14 = sub i32 1, %0
%15 = add i32 %14, %8
%16 = tail call i32 @print_vector(i32* %13, i32 %15)
%17 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
%18 = sext i32 %8 to i64
%19 = getelementptr inbounds i32, i32* %2, i64 %18
%20 = getelementptr inbounds i32, i32* %19, i64 1
%21 = sub i32 %1, %8
%22 = tail call i32 @print_vector(i32* %20, i32 %21)
%23 = tail call i32 @merge(i32 %0, i32 %1, i32 %8, i32* %2)
%24 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0))
%25 = add i32 %14, %1
%26 = tail call i32 @print_vector(i32* %13, i32 %25)
br label %5
}