#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 */
typedef int /*<<< orphan*/ array;

/* Variables and functions */
scalar_t__ malloc(int);
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ strcasecmp(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
void insertionsort(char **array, int strMax) {
  char *current;
  int position;
  for (int i = 1; i < strMax; i++) {
    current = (char *)malloc(sizeof(array[i]));
    strcpy(current, array[i]);
    position = i;
    while ((position > 0) && (strcasecmp(array[position - 1], current) > 0)) {
      strcpy(array[position], array[position - 1]);
      --position;
      printf(" item moved : %s\n", array[position]);
    }
    printf(" item inserted : %s, at position : %d\n", current, position);
    strcpy(array[position], current);
    printf("Iteration: %d\n", i);
    for (int i = 0; i < strMax; i++) {
      printf("%s\n", array[i]);
    }
  }
}


x86 assembly (clang, -O0):
<insertionsort>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0x1,-0x20(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    12a1 <insertionsort+0x161>
mov    $0x8,%edi
call   1060 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x8(%rbp),%rax
movslq -0x20(%rbp),%rcx
mov    (%rax,%rcx,8),%rsi
call   1040 <strcpy@plt>
mov    -0x20(%rbp),%eax
mov    %eax,-0x1c(%rbp)
xor    %eax,%eax
cmpl   $0x0,-0x1c(%rbp)
mov    %al,-0x25(%rbp)
jle    11be <insertionsort+0x7e>
mov    -0x8(%rbp),%rax
mov    -0x1c(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,8),%rdi
mov    -0x18(%rbp),%rsi
call   1030 <strcasecmp@plt>
cmp    $0x0,%rax
setg   %al
mov    %al,-0x25(%rbp)
mov    -0x25(%rbp),%al
test   $0x1,%al
jne    11ce <insertionsort+0x8e>
jmp    1218 <insertionsort+0xd8>
mov    -0x8(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
mov    -0x8(%rbp),%rax
mov    -0x1c(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,8),%rsi
call   1040 <strcpy@plt>
mov    -0x1c(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x8(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    (%rax,%rcx,8),%rsi
lea    0xdf4(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    118b <insertionsort+0x4b>
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
lea    0xdec(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x8(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
mov    -0x18(%rbp),%rsi
call   1040 <strcpy@plt>
mov    -0x20(%rbp),%esi
lea    0xded(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    128e <insertionsort+0x14e>
mov    -0x8(%rbp),%rax
movslq -0x24(%rbp),%rcx
mov    (%rax,%rcx,8),%rsi
lea    0xd95(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    125a <insertionsort+0x11a>
jmp    1293 <insertionsort+0x153>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    1156 <insertionsort+0x16>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<insertionsort>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
cmp    $0x2,%esi
jl     125f <insertionsort+0x10f>
mov    %rdi,%r15
mov    %esi,%ebp
mov    $0x1,%r13d
mov    %rbp,0x8(%rsp)
nopw   0x0(%rax,%rax,1)
mov    $0x8,%edi
call   1070 <malloc@plt>
mov    (%r15,%r13,8),%rsi
mov    %rax,(%rsp)
mov    %rax,%rdi
call   1040 <strcpy@plt>
mov    %r13,0x10(%rsp)
nop
lea    -0x1(%r13),%r14
mov    %r14d,%ebx
mov    (%r15,%rbx,8),%rbp
mov    %rbp,%rdi
mov    (%rsp),%rsi
call   1030 <strcasecmp@plt>
test   %rax,%rax
jle    11f0 <insertionsort+0xa0>
mov    (%r15,%r13,8),%rdi
mov    %rbp,%rsi
call   1040 <strcpy@plt>
mov    (%r15,%rbx,8),%rsi
xor    %r12d,%r12d
lea    0xe2a(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
lea    0x1(%r14),%rax
mov    %r14,%r13
cmp    $0x1,%rax
jg     11a0 <insertionsort+0x50>
jmp    11f3 <insertionsort+0xa3>
nopl   0x0(%rax)
mov    %r13d,%r12d
lea    0xe18(%rip),%rdi
mov    (%rsp),%rbx
mov    %rbx,%rsi
mov    %r12d,%edx
xor    %eax,%eax
call   1060 <printf@plt>
movslq %r12d,%rax
mov    (%r15,%rax,8),%rdi
mov    %rbx,%rsi
call   1040 <strcpy@plt>
lea    0xe18(%rip),%rdi
mov    0x10(%rsp),%r13
mov    %r13d,%esi
xor    %eax,%eax
call   1060 <printf@plt>
xor    %ebx,%ebx
mov    0x8(%rsp),%rbp
nopw   0x0(%rax,%rax,1)
mov    (%r15,%rbx,8),%rdi
call   1050 <puts@plt>
add    $0x1,%rbx
cmp    %rbx,%rbp
jne    1240 <insertionsort+0xf0>
add    $0x1,%r13
cmp    %rbp,%r13
jne    1180 <insertionsort+0x30>
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [18 x i8] c" item moved : %s\0A\00"
@.str.1 = [39 x i8] c" item inserted : %s, at position : %d\0A\00"
@.str.2 = [15 x i8] c"Iteration: %d\0A\00"
define void @insertionsort(i8** nocapture %0, i32 %1) {
%3 = icmp sgt i32 %1, 1
br i1 %3, label %4, label %6
4: ; preds = %2
%5 = zext i32 %1 to i64
br label %7
6: ; preds = %40, %2
ret void
7: ; preds = %4, %40
%8 = phi i64 [ 1, %4 ], [ %41, %40 ]
%9 = tail call i64 @malloc(i32 8)
%10 = inttoptr i64 %9 to i8*
%11 = getelementptr inbounds i8*, i8** %0, i64 %8
%12 = load i8*, i8** %11
%13 = tail call i32 @strcpy(i8* %10, i8* %12)
br label %14
14: ; preds = %7, %22
%15 = phi i64 [ %8, %7 ], [ %16, %22 ]
%16 = add nsw i64 %15, -1
%17 = and i64 %16, 4294967295
%18 = getelementptr inbounds i8*, i8** %0, i64 %17
%19 = load i8*, i8** %18
%20 = tail call i64 @strcasecmp(i8* %19, i8* %10)
%21 = icmp sgt i64 %20, 0
br i1 %21, label %22, label %29
22: ; preds = %14
%23 = getelementptr inbounds i8*, i8** %0, i64 %15
%24 = load i8*, i8** %23
%25 = tail call i32 @strcpy(i8* %24, i8* %19)
%26 = load i8*, i8** %18
%27 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %26)
%28 = icmp sgt i64 %15, 1
br i1 %28, label %14, label %31
29: ; preds = %14
%30 = trunc i64 %15 to i32
br label %31
31: ; preds = %22, %29
%32 = phi i32 [ %30, %29 ], [ 0, %22 ]
%33 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0), i8* %10, i32 %32)
%34 = sext i32 %32 to i64
%35 = getelementptr inbounds i8*, i8** %0, i64 %34
%36 = load i8*, i8** %35
%37 = tail call i32 @strcpy(i8* %36, i8* %10)
%38 = trunc i64 %8 to i32
%39 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i32 %38)
br label %43
40: ; preds = %43
%41 = add nuw nsw i64 %8, 1
%42 = icmp eq i64 %41, %5
br i1 %42, label %6, label %7
43: ; preds = %31, %43
%44 = phi i64 [ 0, %31 ], [ %48, %43 ]
%45 = getelementptr inbounds i8*, i8** %0, i64 %44
%46 = load i8*, i8** %45
%47 = tail call i32 @puts(i8* dereferenceable(1) %46)
%48 = add nuw nsw i64 %44, 1
%49 = icmp eq i64 %48, %5
br i1 %49, label %40, label %43
}