#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 */
char EOF;
int EXIT_FAILURE;
int EXIT_SUCCESS;
int /*<<< orphan*/ SelectionSortRecursive(int *, int);
scalar_t__ calloc(int, int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ free(int *);
char getchar();
int /*<<< orphan*/ isspace(char);
int /*<<< orphan*/ memcpy(int *, int *, int);
int /*<<< orphan*/ memset(int *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ putchar(char);
int scanf(char *, int *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ ungetc(char, int /*<<< orphan*/);
int main(void) {
  printf("Please enter a series of integers you would like sorted\n"
         "using one of the slowest sorting algorithm in the world\n"
         " - selection sort:\n");
  int numsCapacity = 4;
  int *nums = (int *)calloc(numsCapacity, sizeof(int));
  if (!nums) {
    return EXIT_FAILURE;
  }
  int numsCount = 0;
  char c = '\0';
  while (1) {
    while (scanf("%d", &nums[numsCount]) != 1) {
      memset(nums, 0, numsCount * sizeof(int));
      numsCount = 0;
      fprintf(stderr, "Input invalid. Please try re-entering "
                      "the number of integers again.\n");
      while ((c = getchar()) != '\n' && c != EOF)
        continue;
    }
    ++numsCount;
    if (numsCount >= numsCapacity) {
      int newCapacity = numsCapacity * 2;
      int *newNums = (int *)calloc(newCapacity, sizeof(int));
      if (!newNums) {
        return EXIT_FAILURE;
      }
      memcpy(newNums, nums, numsCount * sizeof(int));
      free(nums);
      nums = NULL;
      nums = newNums;
      numsCapacity = newCapacity;
    }
    while ((c = getchar()) != '\n') {
      if (!isspace(c)) {
        ungetc(c, stdin);
        break;
      }
    }
    if (c == '\n')
      break;
  }
  (void)SelectionSortRecursive(nums, numsCount);
  for (int i = 0; i < numsCount; ++i)
    printf("%d ", nums[i]);
  putchar('\n');
  free(nums);
  getchar();
  return EXIT_SUCCESS;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe2a(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x4,-0x8(%rbp)
mov    -0x8(%rbp),%edi
mov    $0x4,%esi
call   1090 <calloc@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1211 <main+0x51>
mov    0x2dc1(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1410 <main+0x250>
movl   $0x0,-0x14(%rbp)
movb   $0x0,-0x15(%rbp)
jmp    1221 <main+0x61>
mov    -0x10(%rbp),%rsi
movslq -0x14(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rsi
lea    0xe4d(%rip),%rdi
call   10e0 <scanf@plt>
cmp    $0x1,%eax
je     12c1 <main+0x101>
mov    -0x10(%rbp),%rdi
movslq -0x14(%rbp),%rax
shl    $0x2,%rax
mov    %eax,%edx
xor    %esi,%esi
call   1070 <memset@plt>
movl   $0x0,-0x14(%rbp)
mov    0x2d68(%rip),%rax
mov    (%rax),%edi
lea    0xe16(%rip),%rsi
call   10b0 <fprintf@plt>
mov    $0x0,%al
call   10a0 <getchar@plt>
mov    %al,-0x15(%rbp)
movsbl %al,%ecx
xor    %eax,%eax
cmp    $0xa,%ecx
mov    %al,-0x2d(%rbp)
je     12a7 <main+0xe7>
movsbl -0x15(%rbp),%eax
mov    0x2d44(%rip),%rcx
movsbl (%rcx),%ecx
cmp    %ecx,%eax
setne  %al
mov    %al,-0x2d(%rbp)
mov    -0x2d(%rbp),%al
test   $0x1,%al
jne    12b7 <main+0xf7>
jmp    12bc <main+0xfc>
jmp    1276 <main+0xb6>
jmp    1221 <main+0x61>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x8(%rbp),%eax
jl     1342 <main+0x182>
mov    -0x8(%rbp),%eax
shl    $0x1,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%edi
mov    $0x4,%esi
call   1090 <calloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    130c <main+0x14c>
mov    0x2cc6(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1410 <main+0x250>
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rsi
movslq -0x14(%rbp),%rax
shl    $0x2,%rax
mov    %eax,%edx
call   10c0 <memcpy@plt>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x10(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x10(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x8(%rbp)
jmp    1347 <main+0x187>
mov    $0x0,%al
call   10a0 <getchar@plt>
mov    %al,-0x15(%rbp)
movsbl %al,%eax
cmp    $0xa,%eax
je     138d <main+0x1cd>
movsbl -0x15(%rbp),%edi
call   1080 <isspace@plt>
cmp    $0x0,%eax
jne    1388 <main+0x1c8>
mov    -0x15(%rbp),%al
mov    0x2c6f(%rip),%rcx
mov    (%rcx),%esi
movsbl %al,%edi
call   10d0 <ungetc@plt>
jmp    138d <main+0x1cd>
jmp    1347 <main+0x187>
movsbl -0x15(%rbp),%eax
cmp    $0xa,%eax
jne    139f <main+0x1df>
jmp    13a4 <main+0x1e4>
jmp    121c <main+0x5c>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1050 <SelectionSortRecursive@plt>
movl   $0x0,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    13ea <main+0x22a>
mov    -0x10(%rbp),%rax
movslq -0x2c(%rbp),%rcx
mov    (%rax,%rcx,4),%esi
lea    0xcf7(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    13b7 <main+0x1f7>
mov    $0xa,%edi
call   1040 <putchar@plt>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
mov    $0x0,%al
call   10a0 <getchar@plt>
mov    0x2bb5(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%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    0xe67(%rip),%rdi
call   1050 <puts@plt>
mov    $0x4,%ebx
mov    $0x4,%edi
mov    $0x4,%esi
call   10a0 <calloc@plt>
mov    %rax,%r14
mov    0x2dc0(%rip),%rax
test   %r14,%r14
je     1397 <main+0x1c7>
lea    0xde8(%rip),%r13
lea    0xde4(%rip),%r15
mov    0x2dba(%rip),%r12
xor    %ebp,%ebp
movslq %ebp,%rax
lea    (%r14,%rax,4),%rsi
mov    %r13,%rdi
call   10f0 <scanf@plt>
jmp    124d <main+0x7d>
nopl   0x0(%rax)
mov    %r13,%rdi
mov    %r14,%rsi
call   10f0 <scanf@plt>
xor    %ebp,%ebp
cmp    $0x1,%eax
je     12a0 <main+0xd0>
shl    $0x2,%ebp
mov    %r14,%rdi
xor    %esi,%esi
mov    %ebp,%edx
call   1080 <memset@plt>
mov    0x2d68(%rip),%rax
mov    (%rax),%edi
mov    %r15,%rsi
call   10c0 <fprintf@plt>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
xor    %eax,%eax
call   10b0 <getchar@plt>
cmp    $0xa,%al
je     1240 <main+0x70>
cmp    (%r12),%al
jne    1280 <main+0xb0>
jmp    1240 <main+0x70>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
lea    0x1(%rbp),%ecx
mov    %rcx,%rax
mov    %rcx,0x10(%rsp)
cmp    %ebx,%ecx
jge    12c0 <main+0xf0>
mov    %ebx,0xc(%rsp)
jmp    1300 <main+0x130>
cs nopw 0x0(%rax,%rax,1)
nop
add    %ebx,%ebx
mov    %ebx,0xc(%rsp)
mov    %ebx,%edi
mov    $0x4,%esi
call   10a0 <calloc@plt>
test   %rax,%rax
je     13a8 <main+0x1d8>
mov    %rax,%rbx
mov    0x10(%rsp),%rax
lea    0x0(,%rax,4),%edx
mov    %rbx,%rdi
mov    %r14,%rsi
call   10d0 <memcpy@plt>
mov    %r14,%rdi
call   1030 <free@plt>
mov    %rbx,%r14
xor    %eax,%eax
call   10b0 <getchar@plt>
cmp    $0xa,%al
je     1337 <main+0x167>
movsbl %al,%ebx
mov    %ebx,%edi
call   1090 <isspace@plt>
test   %eax,%eax
jne    1300 <main+0x130>
mov    0x2cc8(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   10e0 <ungetc@plt>
mov    0xc(%rsp),%ebx
mov    0x10(%rsp),%rbp
jmp    1228 <main+0x58>
mov    %r14,%rdi
mov    0x10(%rsp),%rbx
mov    %ebx,%esi
call   1060 <SelectionSortRecursive@plt>
test   %ebp,%ebp
js     1377 <main+0x1a7>
mov    %ebx,%ebx
lea    0xcf5(%rip),%r15
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
nop
mov    (%r14,%rbp,4),%esi
mov    %r15,%rdi
xor    %eax,%eax
call   1070 <printf@plt>
add    $0x1,%rbp
cmp    %rbp,%rbx
jne    1360 <main+0x190>
mov    $0xa,%edi
call   1040 <putchar@plt>
mov    %r14,%rdi
call   1030 <free@plt>
xor    %eax,%eax
call   10b0 <getchar@plt>
mov    0x2c29(%rip),%rax
mov    (%rax),%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2c19(%rip),%rax
jmp    1397 <main+0x1c7>

LLVM IR (-O0):
@.str = [132 x i8] c"Please enter a series of integers you would like sorted\0Ausing one of the slowest sorting algorithm in the world\0A - selection sort:\0A\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [3 x i8] c"%d\00"
@stderr = global i32 0
@.str.2 = [69 x i8] c"Input invalid. Please try re-entering the number of integers again.\0A\00"
@EOF = global i8 0
@stdin = global i32 0
@.str.3 = [4 x i8] c"%d \00"
@EXIT_SUCCESS = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32*
%4 = alloca i32
%5 = alloca i8
%6 = alloca i32
%7 = alloca i32*
%8 = alloca i32
store i32 0, i32* %1
%9 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([132 x i8], [132 x i8]* @.str, i64 0, i64 0))
store i32 4, i32* %2
%10 = load i32, i32* %2
%11 = call i64 @calloc(i32 %10, i32 4)
%12 = inttoptr i64 %11 to i32*
store i32* %12, i32** %3
%13 = load i32*, i32** %3
%14 = icmp ne i32* %13, null
br i1 %14, label %17, label %15
15: ; preds = %0
%16 = load i32, i32* @EXIT_FAILURE
store i32 %16, i32* %1
br label %121
17: ; preds = %0
store i32 0, i32* %4
store i8 0, i8* %5
br label %18
18: ; preds = %17, %96
br label %19
19: ; preds = %48, %18
%20 = load i32*, i32** %3
%21 = load i32, i32* %4
%22 = sext i32 %21 to i64
%23 = getelementptr inbounds i32, i32* %20, i64 %22
%24 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %23)
%25 = icmp ne i32 %24, 1
br i1 %25, label %26, label %49
26: ; preds = %19
%27 = load i32*, i32** %3
%28 = load i32, i32* %4
%29 = sext i32 %28 to i64
%30 = mul i64 %29, 4
%31 = trunc i64 %30 to i32
%32 = call i32 @memset(i32* %27, i32 0, i32 %31)
store i32 0, i32* %4
%33 = load i32, i32* @stderr
%34 = call i32 @fprintf(i32 %33, i8* getelementptr inbounds ([69 x i8], [69 x i8]* @.str.2, i64 0, i64 0))
br label %35
35: ; preds = %47, %26
%36 = call signext i8 (...) @getchar()
store i8 %36, i8* %5
%37 = sext i8 %36 to i32
%38 = icmp ne i32 %37, 10
br i1 %38, label %39, label %45
39: ; preds = %35
%40 = load i8, i8* %5
%41 = sext i8 %40 to i32
%42 = load i8, i8* @EOF
%43 = sext i8 %42 to i32
%44 = icmp ne i32 %41, %43
br label %45
45: ; preds = %39, %35
%46 = phi i1 [ false, %35 ], [ %44, %39 ]
br i1 %46, label %47, label %48
47: ; preds = %45
br label %35
48: ; preds = %45
br label %19
49: ; preds = %19
%50 = load i32, i32* %4
%51 = add nsw i32 %50, 1
store i32 %51, i32* %4
%52 = load i32, i32* %4
%53 = load i32, i32* %2
%54 = icmp sge i32 %52, %53
br i1 %54, label %55, label %77
55: ; preds = %49
%56 = load i32, i32* %2
%57 = mul nsw i32 %56, 2
store i32 %57, i32* %6
%58 = load i32, i32* %6
%59 = call i64 @calloc(i32 %58, i32 4)
%60 = inttoptr i64 %59 to i32*
store i32* %60, i32** %7
%61 = load i32*, i32** %7
%62 = icmp ne i32* %61, null
br i1 %62, label %65, label %63
63: ; preds = %55
%64 = load i32, i32* @EXIT_FAILURE
store i32 %64, i32* %1
br label %121
65: ; preds = %55
%66 = load i32*, i32** %7
%67 = load i32*, i32** %3
%68 = load i32, i32* %4
%69 = sext i32 %68 to i64
%70 = mul i64 %69, 4
%71 = trunc i64 %70 to i32
%72 = call i32 @memcpy(i32* %66, i32* %67, i32 %71)
%73 = load i32*, i32** %3
%74 = call i32 @free(i32* %73)
store i32* null, i32** %3
%75 = load i32*, i32** %7
store i32* %75, i32** %3
%76 = load i32, i32* %6
store i32 %76, i32* %2
br label %77
77: ; preds = %65, %49
br label %78
78: ; preds = %90, %77
%79 = call signext i8 (...) @getchar()
store i8 %79, i8* %5
%80 = sext i8 %79 to i32
%81 = icmp ne i32 %80, 10
br i1 %81, label %82, label %91
82: ; preds = %78
%83 = load i8, i8* %5
%84 = call i32 @isspace(i8 signext %83)
%85 = icmp ne i32 %84, 0
br i1 %85, label %90, label %86
86: ; preds = %82
%87 = load i8, i8* %5
%88 = load i32, i32* @stdin
%89 = call i32 @ungetc(i8 signext %87, i32 %88)
br label %91
90: ; preds = %82
br label %78
91: ; preds = %86, %78
%92 = load i8, i8* %5
%93 = sext i8 %92 to i32
%94 = icmp eq i32 %93, 10
br i1 %94, label %95, label %96
95: ; preds = %91
br label %97
96: ; preds = %91
br label %18
97: ; preds = %95
%98 = load i32*, i32** %3
%99 = load i32, i32* %4
%100 = call i32 @SelectionSortRecursive(i32* %98, i32 %99)
store i32 0, i32* %8
br label %101
101: ; preds = %112, %97
%102 = load i32, i32* %8
%103 = load i32, i32* %4
%104 = icmp slt i32 %102, %103
br i1 %104, label %105, label %115
105: ; preds = %101
%106 = load i32*, i32** %3
%107 = load i32, i32* %8
%108 = sext i32 %107 to i64
%109 = getelementptr inbounds i32, i32* %106, i64 %108
%110 = load i32, i32* %109
%111 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i32 %110)
br label %112
112: ; preds = %105
%113 = load i32, i32* %8
%114 = add nsw i32 %113, 1
store i32 %114, i32* %8
br label %101
115: ; preds = %101
%116 = call i32 @putchar(i8 signext 10)
%117 = load i32*, i32** %3
%118 = call i32 @free(i32* %117)
%119 = call signext i8 (...) @getchar()
%120 = load i32, i32* @EXIT_SUCCESS
store i32 %120, i32* %1
br label %121
121: ; preds = %115, %63, %15
%122 = load i32, i32* %1
ret i32 %122
}

LLVM IR (-O2):
@EXIT_FAILURE = global i32 0
@.str.1 = [3 x i8] c"%d\00"
@stderr = global i32 0
@.str.2 = [69 x i8] c"Input invalid. Please try re-entering the number of integers again.\0A\00"
@EOF = global i8 0
@stdin = global i32 0
@.str.3 = [4 x i8] c"%d \00"
@EXIT_SUCCESS = global i32 0
@str = [131 x i8] c"Please enter a series of integers you would like sorted\0Ausing one of the slowest sorting algorithm in the world\0A - selection sort:\00"
define i32 @main() {
%1 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([131 x i8], [131 x i8]* @str, i64 0, i64 0))
%2 = tail call i64 @calloc(i32 4, i32 4)
%3 = icmp eq i64 %2, 0
br i1 %3, label %70, label %4
4: ; preds = %0
%5 = inttoptr i64 %2 to i32*
br label %6
6: ; preds = %51, %4
%7 = phi i32* [ %5, %4 ], [ %43, %51 ]
%8 = phi i32 [ 0, %4 ], [ %31, %51 ]
%9 = phi i32 [ 4, %4 ], [ %44, %51 ]
%10 = sext i32 %8 to i64
%11 = getelementptr inbounds i32, i32* %7, i64 %10
%12 = tail call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %11)
%13 = icmp eq i32 %12, 1
br i1 %13, label %29, label %17
14: ; preds = %23
%15 = tail call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %7)
%16 = icmp eq i32 %15, 1
br i1 %16, label %29, label %17
17: ; preds = %6, %14
%18 = phi i32 [ 0, %14 ], [ %8, %6 ]
%19 = shl i32 %18, 2
%20 = tail call i32 @memset(i32* %7, i32 0, i32 %19)
%21 = load i32, i32* @stderr
%22 = tail call i32 @fprintf(i32 %21, i8* getelementptr inbounds ([69 x i8], [69 x i8]* @.str.2, i64 0, i64 0))
br label %23
23: ; preds = %23, %17
%24 = tail call signext i8 (...) @getchar()
%25 = icmp ne i8 %24, 10
%26 = load i8, i8* @EOF
%27 = icmp ne i8 %24, %26
%28 = select i1 %25, i1 %27, i1 false
br i1 %28, label %23, label %14
29: ; preds = %14, %6
%30 = phi i32 [ %8, %6 ], [ 0, %14 ]
%31 = add i32 %30, 1
%32 = icmp slt i32 %31, %9
br i1 %32, label %42, label %33
33: ; preds = %29
%34 = shl nsw i32 %9, 1
%35 = tail call i64 @calloc(i32 %34, i32 4)
%36 = inttoptr i64 %35 to i32*
%37 = icmp eq i64 %35, 0
br i1 %37, label %70, label %38
38: ; preds = %33
%39 = shl i32 %31, 2
%40 = tail call i32 @memcpy(i32* %36, i32* %7, i32 %39)
%41 = tail call i32 @free(i32* %7)
br label %42
42: ; preds = %38, %29
%43 = phi i32* [ %36, %38 ], [ %7, %29 ]
%44 = phi i32 [ %34, %38 ], [ %9, %29 ]
br label %45
45: ; preds = %48, %42
%46 = tail call signext i8 (...) @getchar()
%47 = icmp eq i8 %46, 10
br i1 %47, label %54, label %48
48: ; preds = %45
%49 = tail call i32 @isspace(i8 signext %46)
%50 = icmp eq i32 %49, 0
br i1 %50, label %51, label %45
51: ; preds = %48
%52 = load i32, i32* @stdin
%53 = tail call i32 @ungetc(i8 signext %46, i32 %52)
br label %6
54: ; preds = %45
%55 = tail call i32 @SelectionSortRecursive(i32* %43, i32 %31)
%56 = icmp slt i32 %30, 0
br i1 %56, label %59, label %57
57: ; preds = %54
%58 = zext i32 %31 to i64
br label %63
59: ; preds = %63, %54
%60 = tail call i32 @putchar(i8 signext 10)
%61 = tail call i32 @free(i32* %43)
%62 = tail call signext i8 (...) @getchar()
br label %70
63: ; preds = %57, %63
%64 = phi i64 [ 0, %57 ], [ %68, %63 ]
%65 = getelementptr inbounds i32, i32* %43, i64 %64
%66 = load i32, i32* %65
%67 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i32 %66)
%68 = add nuw nsw i64 %64, 1
%69 = icmp eq i64 %68, %58
br i1 %69, label %59, label %63
70: ; preds = %33, %0, %59
%71 = phi i32* [ @EXIT_SUCCESS, %59 ], [ @EXIT_FAILURE, %0 ], [ @EXIT_FAILURE, %33 ]
%72 = load i32, i32* %71
ret i32 %72
}