#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 */
scalar_t__ calloc(int, int);
int /*<<< orphan*/ countingSort(int *, int *, int, int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ scanf(char *, int *);
int main() {
  printf("Counting Sort \n");
  printf("\n");
  int i, k, n;
  printf("Range of Input ( 0 - k ) \n");
  printf("k = ");
  scanf("%d", &k);
  printf("\n");
  if (k < 0) {
    printf("Incorrect Range \n");
    printf("\n");
    return 1;
  }
  printf("Total Elements : ");
  scanf("%d", &n);
  printf("\n");
  if (n < 1) {
    printf("Invalid Input \n");
    printf("\n");
    return 1;
  }
  int *A = (int *)calloc((n + 1), sizeof(int));
  int *B = (int *)calloc((n + 1), sizeof(int));
  printf("Unsorted Input \n");
  for (i = 1; i <= n; i++) {
    scanf("%d", (A + i));
    if (*(A + i) < 0 || *(A + i) > k) {
      printf("Input Out of Range! \n");
      printf("\n");
      return 1;
    }
  }
  printf("\n");
  countingSort(A, B, k, n);
  printf("Sorted Output \n");
  for (i = 1; i <= n; i++) {
    printf("%d \n", *(B + i));
  }
  printf("\n");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xeaa(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xf28(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xe9e(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xeab(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xea2(%rip),%rdi
lea    -0xc(%rbp),%rsi
call   1050 <scanf@plt>
lea    0xeee(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
cmpl   $0x0,-0xc(%rbp)
jge    11d7 <main+0x97>
lea    0xe7d(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xec8(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1378 <main+0x238>
lea    0xe67(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xe44(%rip),%rdi
lea    -0x10(%rbp),%rsi
call   1050 <scanf@plt>
lea    0xe90(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
cmpl   $0x1,-0x10(%rbp)
jge    1235 <main+0xf5>
lea    0xe43(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xe6a(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1378 <main+0x238>
mov    -0x10(%rbp),%edi
add    $0x1,%edi
mov    $0x4,%esi
call   1040 <calloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%edi
add    $0x1,%edi
mov    $0x4,%esi
call   1040 <calloc@plt>
mov    %rax,-0x20(%rbp)
lea    0xe03(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x1,-0x8(%rbp)
mov    -0x8(%rbp),%eax
cmp    -0x10(%rbp),%eax
jg     12fa <main+0x1ba>
mov    -0x18(%rbp),%rsi
movslq -0x8(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rsi
lea    0xd9c(%rip),%rdi
call   1050 <scanf@plt>
mov    -0x18(%rbp),%rax
movslq -0x8(%rbp),%rcx
cmpl   $0x0,(%rax,%rcx,4)
jl     12bf <main+0x17f>
mov    -0x18(%rbp),%rax
movslq -0x8(%rbp),%rcx
mov    (%rax,%rcx,4),%eax
cmp    -0xc(%rbp),%eax
jle    12e7 <main+0x1a7>
lea    0xdb2(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xdb8(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1378 <main+0x238>
jmp    12ec <main+0x1ac>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    1272 <main+0x132>
lea    0xd8b(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0xc(%rbp),%edx
mov    -0x10(%rbp),%ecx
call   1060 <countingSort@plt>
lea    0xd6c(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x1,-0x8(%rbp)
mov    -0x8(%rbp),%eax
cmp    -0x10(%rbp),%eax
jg     1363 <main+0x223>
mov    -0x20(%rbp),%rax
movslq -0x8(%rbp),%rcx
mov    (%rax,%rcx,4),%esi
lea    0xd50(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    1330 <main+0x1f0>
lea    0xd22(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
lea    0xeaa(%rip),%rdi
call   1040 <puts@plt>
mov    $0xa,%edi
call   1030 <putchar@plt>
lea    0xea3(%rip),%rdi
call   1040 <puts@plt>
lea    0xe69(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xe60(%rip),%rdi
lea    0xc(%rsp),%rsi
call   1070 <scanf@plt>
mov    $0xa,%edi
call   1030 <putchar@plt>
cmpl   $0x0,0xc(%rsp)
js     12ef <main+0x18f>
lea    0xe3d(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xe2c(%rip),%rdi
lea    0x8(%rsp),%rsi
call   1070 <scanf@plt>
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    0x8(%rsp),%edi
test   %edi,%edi
jle    12f8 <main+0x198>
add    $0x1,%edi
mov    $0x4,%esi
call   1060 <calloc@plt>
mov    %rax,0x10(%rsp)
mov    0x8(%rsp),%edi
add    $0x1,%edi
mov    $0x4,%esi
call   1060 <calloc@plt>
mov    %rax,%r13
lea    0xe22(%rip),%rdi
call   1040 <puts@plt>
cmpl   $0x0,0x8(%rsp)
jle    1284 <main+0x124>
mov    0x10(%rsp),%rax
lea    0x4(%rax),%rbx
xor    %ebp,%ebp
lea    0xdc1(%rip),%r12
lea    0xe1c(%rip),%r15
xor    %r14d,%r14d
xchg   %ax,%ax
lea    (%rbx,%rbp,1),%rsi
mov    %r12,%rdi
call   1070 <scanf@plt>
mov    (%rbx,%r14,4),%eax
test   %eax,%eax
js     12ff <main+0x19f>
cmp    0xc(%rsp),%eax
jg     12ff <main+0x19f>
movslq 0x8(%rsp),%rax
add    $0x1,%r14
add    $0x4,%rbp
cmp    %rax,%r14
jl     1250 <main+0xf0>
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    0xc(%rsp),%edx
mov    0x8(%rsp),%ecx
mov    0x10(%rsp),%rdi
mov    %r13,%rsi
call   1080 <countingSort@plt>
lea    0xdae(%rip),%rdi
call   1040 <puts@plt>
cmpl   $0x0,0x8(%rsp)
jle    1329 <main+0x1c9>
xor    %r14d,%r14d
lea    0xd5a(%rip),%rbx
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    0x4(%r13,%rbp,4),%esi
mov    %rbx,%rdi
xor    %eax,%eax
call   1050 <printf@plt>
movslq 0x8(%rsp),%rax
add    $0x1,%rbp
cmp    %rax,%rbp
jl     12d0 <main+0x170>
jmp    130d <main+0x1ad>
lea    0xd95(%rip),%r15
jmp    12ff <main+0x19f>
lea    0xd7d(%rip),%r15
mov    %r15,%rdi
call   1040 <puts@plt>
mov    $0x1,%r14d
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    %r14d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
xor    %r14d,%r14d
jmp    130d <main+0x1ad>

LLVM IR (-O0):
@.str = [16 x i8] c"Counting Sort \0A\00"
@.str.1 = [2 x i8] c"\0A\00"
@.str.2 = [27 x i8] c"Range of Input ( 0 - k ) \0A\00"
@.str.3 = [5 x i8] c"k = \00"
@.str.4 = [3 x i8] c"%d\00"
@.str.5 = [18 x i8] c"Incorrect Range \0A\00"
@.str.6 = [18 x i8] c"Total Elements : \00"
@.str.7 = [16 x i8] c"Invalid Input \0A\00"
@.str.8 = [17 x i8] c"Unsorted Input \0A\00"
@.str.9 = [22 x i8] c"Input Out of Range! \0A\00"
@.str.10 = [16 x i8] c"Sorted Output \0A\00"
@.str.11 = [5 x i8] c"%d \0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i32*
store i32 0, i32* %1
%7 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%8 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%9 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0))
%10 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%11 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i32* %3)
%12 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%13 = load i32, i32* %3
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %18
15: ; preds = %0
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.5, i64 0, i64 0))
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %1
br label %92
18: ; preds = %0
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.6, i64 0, i64 0))
%20 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i32* %4)
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%22 = load i32, i32* %4
%23 = icmp slt i32 %22, 1
br i1 %23, label %24, label %27
24: ; preds = %18
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.7, i64 0, i64 0))
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %1
br label %92
27: ; preds = %18
%28 = load i32, i32* %4
%29 = add nsw i32 %28, 1
%30 = call i64 @calloc(i32 %29, i32 4)
%31 = inttoptr i64 %30 to i32*
store i32* %31, i32** %5
%32 = load i32, i32* %4
%33 = add nsw i32 %32, 1
%34 = call i64 @calloc(i32 %33, i32 4)
%35 = inttoptr i64 %34 to i32*
store i32* %35, i32** %6
%36 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.8, i64 0, i64 0))
store i32 1, i32* %2
br label %37
37: ; preds = %65, %27
%38 = load i32, i32* %2
%39 = load i32, i32* %4
%40 = icmp sle i32 %38, %39
br i1 %40, label %41, label %68
41: ; preds = %37
%42 = load i32*, i32** %5
%43 = load i32, i32* %2
%44 = sext i32 %43 to i64
%45 = getelementptr inbounds i32, i32* %42, i64 %44
%46 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i32* %45)
%47 = load i32*, i32** %5
%48 = load i32, i32* %2
%49 = sext i32 %48 to i64
%50 = getelementptr inbounds i32, i32* %47, i64 %49
%51 = load i32, i32* %50
%52 = icmp slt i32 %51, 0
br i1 %52, label %61, label %53
53: ; preds = %41
%54 = load i32*, i32** %5
%55 = load i32, i32* %2
%56 = sext i32 %55 to i64
%57 = getelementptr inbounds i32, i32* %54, i64 %56
%58 = load i32, i32* %57
%59 = load i32, i32* %3
%60 = icmp sgt i32 %58, %59
br i1 %60, label %61, label %64
61: ; preds = %53, %41
%62 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.9, i64 0, i64 0))
%63 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %1
br label %92
64: ; preds = %53
br label %65
65: ; preds = %64
%66 = load i32, i32* %2
%67 = add nsw i32 %66, 1
store i32 %67, i32* %2
br label %37
68: ; preds = %37
%69 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%70 = load i32*, i32** %5
%71 = load i32*, i32** %6
%72 = load i32, i32* %3
%73 = load i32, i32* %4
%74 = call i32 @countingSort(i32* %70, i32* %71, i32 %72, i32 %73)
%75 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.10, i64 0, i64 0))
store i32 1, i32* %2
br label %76
76: ; preds = %87, %68
%77 = load i32, i32* %2
%78 = load i32, i32* %4
%79 = icmp sle i32 %77, %78
br i1 %79, label %80, label %90
80: ; preds = %76
%81 = load i32*, i32** %6
%82 = load i32, i32* %2
%83 = sext i32 %82 to i64
%84 = getelementptr inbounds i32, i32* %81, i64 %83
%85 = load i32, i32* %84
%86 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.11, i64 0, i64 0), i32 %85)
br label %87
87: ; preds = %80
%88 = load i32, i32* %2
%89 = add nsw i32 %88, 1
store i32 %89, i32* %2
br label %76
90: ; preds = %76
%91 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32 0, i32* %1
br label %92
92: ; preds = %90, %61, %24, %15
%93 = load i32, i32* %1
ret i32 %93
}

LLVM IR (-O2):
@.str.3 = [5 x i8] c"k = \00"
@.str.4 = [3 x i8] c"%d\00"
@.str.6 = [18 x i8] c"Total Elements : \00"
@.str.11 = [5 x i8] c"%d \0A\00"
@str = [15 x i8] c"Counting Sort \00"
@str.12 = [26 x i8] c"Range of Input ( 0 - k ) \00"
@str.13 = [16 x i8] c"Unsorted Input \00"
@str.14 = [15 x i8] c"Sorted Output \00"
@str.15 = [21 x i8] c"Input Out of Range! \00"
@str.16 = [15 x i8] c"Invalid Input \00"
@str.17 = [17 x i8] c"Incorrect Range \00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str, i64 0, i64 0))
%4 = tail call i32 @putchar(i32 10)
%5 = bitcast i32* %1 to i8*
%6 = bitcast i32* %2 to i8*
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @str.12, i64 0, i64 0))
%8 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%9 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i32* %1)
%10 = call i32 @putchar(i32 10)
%11 = load i32, i32* %1
%12 = icmp slt i32 %11, 0
br i1 %12, label %61, label %13
13: ; preds = %0
%14 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.6, i64 0, i64 0))
%15 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i32* %2)
%16 = call i32 @putchar(i32 10)
%17 = load i32, i32* %2
%18 = icmp slt i32 %17, 1
br i1 %18, label %61, label %19
19: ; preds = %13
%20 = add nuw nsw i32 %17, 1
%21 = call i64 @calloc(i32 %20, i32 4)
%22 = inttoptr i64 %21 to i32*
%23 = load i32, i32* %2
%24 = add nsw i32 %23, 1
%25 = call i64 @calloc(i32 %24, i32 4)
%26 = inttoptr i64 %25 to i32*
%27 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str.13, i64 0, i64 0))
%28 = load i32, i32* %2
%29 = icmp slt i32 %28, 1
br i1 %29, label %44, label %35
30: ; preds = %35
%31 = add nuw nsw i64 %36, 1
%32 = load i32, i32* %2
%33 = sext i32 %32 to i64
%34 = icmp slt i64 %36, %33
br i1 %34, label %35, label %44
35: ; preds = %19, %30
%36 = phi i64 [ %31, %30 ], [ 1, %19 ]
%37 = getelementptr inbounds i32, i32* %22, i64 %36
%38 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i32* %37)
%39 = load i32, i32* %37
%40 = icmp slt i32 %39, 0
%41 = load i32, i32* %1
%42 = icmp sgt i32 %39, %41
%43 = select i1 %40, i1 true, i1 %42
br i1 %43, label %61, label %30
44: ; preds = %30, %19
%45 = call i32 @putchar(i32 10)
%46 = load i32, i32* %1
%47 = load i32, i32* %2
%48 = call i32 @countingSort(i32* %22, i32* %26, i32 %46, i32 %47)
%49 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str.14, i64 0, i64 0))
%50 = load i32, i32* %2
%51 = icmp slt i32 %50, 1
br i1 %51, label %64, label %52
52: ; preds = %44, %52
%53 = phi i64 [ %57, %52 ], [ 1, %44 ]
%54 = getelementptr inbounds i32, i32* %26, i64 %53
%55 = load i32, i32* %54
%56 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.11, i64 0, i64 0), i32 %55)
%57 = add nuw nsw i64 %53, 1
%58 = load i32, i32* %2
%59 = sext i32 %58 to i64
%60 = icmp slt i64 %53, %59
br i1 %60, label %52, label %64
61: ; preds = %35, %13, %0
%62 = phi i8* [ getelementptr inbounds ([17 x i8], [17 x i8]* @str.17, i64 0, i64 0), %0 ], [ getelementptr inbounds ([15 x i8], [15 x i8]* @str.16, i64 0, i64 0), %13 ], [ getelementptr inbounds ([21 x i8], [21 x i8]* @str.15, i64 0, i64 0), %35 ]
%63 = call i32 @puts(i8* dereferenceable(1) %62)
br label %64
64: ; preds = %52, %61, %44
%65 = phi i32 [ 0, %44 ], [ 1, %61 ], [ 0, %52 ]
%66 = call i32 @putchar(i32 10)
ret i32 %65
}