#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*/ printArray(int *, int);
int /*<<< orphan*/ printf(char *, int);
void shellSort(int A[], int N) {
  int i, j, increment, tmp;
  for (increment = N / 2; increment > 0; increment /= 2) {
    printf("Increment: %d\n", increment);
    for (i = increment; i < N; i++) {
      printf("i: %d\n", i);
      tmp = A[i];
      printf("tmp: %d\n", tmp);
      for (j = i; j >= increment; j -= increment) {
        printf("j: %d\n", j);
        if (tmp < A[j - increment])
          A[j] = A[j - increment];
        else
          break;
      }
      A[j] = tmp;
      printf("%d-sort:", increment);
      printArray(A, N);
    }
    printf("after %d-sort:", increment);
    printArray(A, N);
  }
}


x86 assembly (clang, -O0):
<shellSort>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0xc(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jle    126d <shellSort+0x14d>
mov    -0x18(%rbp),%esi
lea    0xeaf(%rip),%rdi
call   1030 <printf@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    123f <shellSort+0x11f>
mov    -0x10(%rbp),%esi
lea    0xe9d(%rip),%rdi
call   1030 <printf@plt>
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
mov    (%rax,%rcx,4),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%esi
lea    0xe87(%rip),%rdi
call   1030 <printf@plt>
mov    -0x10(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x18(%rbp),%eax
jl     1208 <shellSort+0xe8>
mov    -0x14(%rbp),%esi
lea    0xe6f(%rip),%rdi
call   1030 <printf@plt>
mov    -0x1c(%rbp),%eax
mov    -0x8(%rbp),%rcx
mov    -0x14(%rbp),%edx
sub    -0x18(%rbp),%edx
movslq %edx,%rdx
cmp    (%rcx,%rdx,4),%eax
jge    11ee <shellSort+0xce>
mov    -0x8(%rbp),%rax
mov    -0x14(%rbp),%ecx
sub    -0x18(%rbp),%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,4),%edx
mov    -0x8(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
jmp    11f3 <shellSort+0xd3>
jmp    1208 <shellSort+0xe8>
jmp    11f8 <shellSort+0xd8>
mov    -0x18(%rbp),%ecx
mov    -0x14(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x14(%rbp)
jmp    119a <shellSort+0x7a>
mov    -0x1c(%rbp),%edx
mov    -0x8(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x18(%rbp),%esi
lea    0xe0c(%rip),%rdi
call   1030 <printf@plt>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
call   1040 <printArray@plt>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    115c <shellSort+0x3c>
mov    -0x18(%rbp),%esi
lea    0xddd(%rip),%rdi
call   1030 <printf@plt>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
call   1040 <printArray@plt>
mov    -0x18(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,-0x18(%rbp)
jmp    113d <shellSort+0x1d>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<shellSort>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
cmp    $0x2,%esi
jl     1257 <shellSort+0x137>
mov    %esi,%r14d
mov    %rdi,%rbx
mov    %esi,%eax
mov    %esi,0x4(%rsp)
jmp    1178 <shellSort+0x58>
cs nopw 0x0(%rax,%rax,1)
nop
lea    0xecf(%rip),%rdi
mov    %r15d,%esi
call   1030 <printf@plt>
mov    %rbx,%rdi
mov    %r14d,%esi
call   1040 <printArray@plt>
cmpl   $0x3,0xc(%rsp)
mov    %r15d,%eax
jle    1257 <shellSort+0x137>
mov    %eax,%r15d
shr    $0x1f,%r15d
mov    %eax,0xc(%rsp)
add    %eax,%r15d
sar    %r15d
lea    0xe70(%rip),%rdi
mov    %r15d,%esi
call   1030 <printf@plt>
cmp    %r14d,%r15d
jge    1150 <shellSort+0x30>
movslq %r15d,%r12
movl   $0x0,0x8(%rsp)
mov    %r12,0x10(%rsp)
jmp    11e8 <shellSort+0xc8>
nop
mov    0x4(%rsp),%r14d
movslq %ebp,%rax
mov    %r13d,(%rbx,%rax,4)
lea    0xe69(%rip),%rdi
mov    %r15d,%esi
call   1030 <printf@plt>
mov    %rbx,%rdi
mov    %r14d,%esi
call   1040 <printArray@plt>
add    $0x1,%r12
addl   $0x1,0x8(%rsp)
cmp    %r14d,%r12d
je     1150 <shellSort+0x30>
lea    0xe20(%rip),%rdi
mov    %r12d,%esi
call   1030 <printf@plt>
mov    (%rbx,%r12,4),%r13d
lea    0xe14(%rip),%rdi
mov    %r13d,%esi
call   1030 <printf@plt>
mov    %r12d,%ebp
cmp    0x10(%rsp),%r12
jl     11b5 <shellSort+0x95>
mov    0x8(%rsp),%eax
mov    %eax,%r14d
nopl   0x0(%rax,%rax,1)
lea    (%r15,%r14,1),%ebp
lea    0xdf4(%rip),%rdi
mov    %ebp,%esi
call   1030 <printf@plt>
movslq %r14d,%rax
mov    (%rbx,%rax,4),%eax
cmp    %eax,%r13d
jge    11b0 <shellSort+0x90>
movslq %ebp,%rcx
mov    %eax,(%rbx,%rcx,4)
sub    %r15d,%r14d
jge    1220 <shellSort+0x100>
add    %r15d,%r14d
mov    %r14d,%ebp
jmp    11b0 <shellSort+0x90>
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [15 x i8] c"Increment: %d\0A\00"
@.str.1 = [7 x i8] c"i: %d\0A\00"
@.str.2 = [9 x i8] c"tmp: %d\0A\00"
@.str.3 = [7 x i8] c"j: %d\0A\00"
@.str.4 = [9 x i8] c"%d-sort:\00"
@.str.5 = [15 x i8] c"after %d-sort:\00"
define void @shellSort(i32* %0, i32 %1) {
%3 = alloca i32*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i32* %0, i32** %3
store i32 %1, i32* %4
%9 = load i32, i32* %4
%10 = sdiv i32 %9, 2
store i32 %10, i32* %7
br label %11
11: ; preds = %87, %2
%12 = load i32, i32* %7
%13 = icmp sgt i32 %12, 0
br i1 %13, label %14, label %90
14: ; preds = %11
%15 = load i32, i32* %7
%16 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %15)
%17 = load i32, i32* %7
store i32 %17, i32* %5
br label %18
18: ; preds = %78, %14
%19 = load i32, i32* %5
%20 = load i32, i32* %4
%21 = icmp slt i32 %19, %20
br i1 %21, label %22, label %81
22: ; preds = %18
%23 = load i32, i32* %5
%24 = call i32 @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i32 %23)
%25 = load i32*, i32** %3
%26 = load i32, i32* %5
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds i32, i32* %25, i64 %27
%29 = load i32, i32* %28
store i32 %29, i32* %8
%30 = load i32, i32* %8
%31 = call i32 @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i32 %30)
%32 = load i32, i32* %5
store i32 %32, i32* %6
br label %33
33: ; preds = %63, %22
%34 = load i32, i32* %6
%35 = load i32, i32* %7
%36 = icmp sge i32 %34, %35
br i1 %36, label %37, label %67
37: ; preds = %33
%38 = load i32, i32* %6
%39 = call i32 @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0), i32 %38)
%40 = load i32, i32* %8
%41 = load i32*, i32** %3
%42 = load i32, i32* %6
%43 = load i32, i32* %7
%44 = sub nsw i32 %42, %43
%45 = sext i32 %44 to i64
%46 = getelementptr inbounds i32, i32* %41, i64 %45
%47 = load i32, i32* %46
%48 = icmp slt i32 %40, %47
br i1 %48, label %49, label %61
49: ; preds = %37
%50 = load i32*, i32** %3
%51 = load i32, i32* %6
%52 = load i32, i32* %7
%53 = sub nsw i32 %51, %52
%54 = sext i32 %53 to i64
%55 = getelementptr inbounds i32, i32* %50, i64 %54
%56 = load i32, i32* %55
%57 = load i32*, i32** %3
%58 = load i32, i32* %6
%59 = sext i32 %58 to i64
%60 = getelementptr inbounds i32, i32* %57, i64 %59
store i32 %56, i32* %60
br label %62
61: ; preds = %37
br label %67
62: ; preds = %49
br label %63
63: ; preds = %62
%64 = load i32, i32* %7
%65 = load i32, i32* %6
%66 = sub nsw i32 %65, %64
store i32 %66, i32* %6
br label %33
67: ; preds = %61, %33
%68 = load i32, i32* %8
%69 = load i32*, i32** %3
%70 = load i32, i32* %6
%71 = sext i32 %70 to i64
%72 = getelementptr inbounds i32, i32* %69, i64 %71
store i32 %68, i32* %72
%73 = load i32, i32* %7
%74 = call i32 @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0), i32 %73)
%75 = load i32*, i32** %3
%76 = load i32, i32* %4
%77 = call i32 @printArray(i32* %75, i32 %76)
br label %78
78: ; preds = %67
%79 = load i32, i32* %5
%80 = add nsw i32 %79, 1
store i32 %80, i32* %5
br label %18
81: ; preds = %18
%82 = load i32, i32* %7
%83 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.5, i64 0, i64 0), i32 %82)
%84 = load i32*, i32** %3
%85 = load i32, i32* %4
%86 = call i32 @printArray(i32* %84, i32 %85)
br label %87
87: ; preds = %81
%88 = load i32, i32* %7
%89 = sdiv i32 %88, 2
store i32 %89, i32* %7
br label %11
90: ; preds = %11
ret void
}

LLVM IR (-O2):
@.str = [15 x i8] c"Increment: %d\0A\00"
@.str.1 = [7 x i8] c"i: %d\0A\00"
@.str.2 = [9 x i8] c"tmp: %d\0A\00"
@.str.3 = [7 x i8] c"j: %d\0A\00"
@.str.4 = [9 x i8] c"%d-sort:\00"
@.str.5 = [15 x i8] c"after %d-sort:\00"
define void @shellSort(i32* %0, i32 %1) {
%3 = icmp sgt i32 %1, 1
br i1 %3, label %4, label %44
4: ; preds = %2, %40
%5 = phi i32 [ %6, %40 ], [ %1, %2 ]
%6 = sdiv i32 %5, 2
%7 = tail call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %6)
%8 = icmp slt i32 %6, %1
br i1 %8, label %9, label %40
9: ; preds = %4
%10 = sext i32 %6 to i64
br label %11
11: ; preds = %9, %31
%12 = phi i64 [ %10, %9 ], [ %37, %31 ]
%13 = trunc i64 %12 to i32
%14 = tail call i32 @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i32 %13)
%15 = getelementptr inbounds i32, i32* %0, i64 %12
%16 = load i32, i32* %15
%17 = tail call i32 @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i32 %16)
%18 = icmp slt i64 %12, %10
br i1 %18, label %31, label %19
19: ; preds = %11, %27
%20 = phi i32 [ %22, %27 ], [ %13, %11 ]
%21 = tail call i32 @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0), i32 %20)
%22 = sub nsw i32 %20, %6
%23 = sext i32 %22 to i64
%24 = getelementptr inbounds i32, i32* %0, i64 %23
%25 = load i32, i32* %24
%26 = icmp slt i32 %16, %25
br i1 %26, label %27, label %31
27: ; preds = %19
%28 = sext i32 %20 to i64
%29 = getelementptr inbounds i32, i32* %0, i64 %28
store i32 %25, i32* %29
%30 = icmp slt i32 %22, %6
br i1 %30, label %31, label %19
31: ; preds = %27, %19, %11
%32 = phi i32 [ %13, %11 ], [ %20, %19 ], [ %22, %27 ]
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds i32, i32* %0, i64 %33
store i32 %16, i32* %34
%35 = tail call i32 @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0), i32 %6)
%36 = tail call i32 @printArray(i32* %0, i32 %1)
%37 = add nsw i64 %12, 1
%38 = trunc i64 %37 to i32
%39 = icmp eq i32 %38, %1
br i1 %39, label %40, label %11
40: ; preds = %31, %4
%41 = tail call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.5, i64 0, i64 0), i32 %6)
%42 = tail call i32 @printArray(i32* %0, i32 %1)
%43 = icmp sgt i32 %5, 3
br i1 %43, label %4, label %44
44: ; preds = %40, %2
ret void
}