#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*/ puts(char *);
int /*<<< orphan*/ swap(int *, int, int);
int selection_sort(int *array, int n) {
  if (!array) {
    puts("Null value passed for array");
    return -1;
  }
  if (n <= 0) {
    puts("Size of the array is bad value");
    return -2;
  }
  int minimum;
  int i, j;
  for (i = 0; i <= n - 2; i++) {
    minimum = i;
    for (j = i + 1; j <= n - 1; j++) {
      if (array[minimum] > array[j])
        minimum = j;
    }
    swap(array, i, minimum);
  }
  return 0;
}


x86 assembly (clang, -O0):
<selection_sort>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1152 <selection_sort+0x32>
lea    0xebf(%rip),%rdi
call   1030 <puts@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1205 <selection_sort+0xe5>
cmpl   $0x0,-0x14(%rbp)
jg     1174 <selection_sort+0x54>
lea    0xeb9(%rip),%rdi
call   1030 <puts@plt>
movl   $0xfffffffe,-0x4(%rbp)
jmp    1205 <selection_sort+0xe5>
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    -0x14(%rbp),%ecx
sub    $0x2,%ecx
cmp    %ecx,%eax
jg     11fe <selection_sort+0xde>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%eax
mov    -0x14(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jg     11e1 <selection_sort+0xc1>
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
mov    (%rax,%rcx,4),%eax
mov    -0x10(%rbp),%rcx
movslq -0x20(%rbp),%rdx
cmp    (%rcx,%rdx,4),%eax
jle    11ce <selection_sort+0xae>
mov    -0x20(%rbp),%eax
mov    %eax,-0x18(%rbp)
jmp    11d3 <selection_sort+0xb3>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    119b <selection_sort+0x7b>
mov    -0x10(%rbp),%rdi
mov    -0x1c(%rbp),%esi
mov    -0x18(%rbp),%edx
call   1040 <swap@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    117b <selection_sort+0x5b>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<selection_sort>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
test   %rdi,%rdi
je     11ac <selection_sort+0x8c>
mov    %esi,%r14d
test   %esi,%esi
jle    11bf <selection_sort+0x9f>
xor    %ebp,%ebp
cmp    $0x1,%r14d
je     11d4 <selection_sort+0xb4>
mov    %rdi,%rbx
lea    -0x1(%r14),%r15d
mov    %r14d,%ebp
xor    %esi,%esi
mov    $0x1,%r12d
jmp    1174 <selection_sort+0x54>
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
call   1040 <swap@plt>
add    $0x1,%r12
mov    %r13d,%esi
cmp    %r15d,%r13d
je     11d2 <selection_sort+0xb2>
lea    0x1(%rsi),%r13d
mov    %esi,%edx
cmp    %r14d,%r13d
jge    1160 <selection_sort+0x40>
mov    %r12,%rax
mov    %esi,%ecx
jmp    119b <selection_sort+0x7b>
cs nopw 0x0(%rax,%rax,1)
add    $0x1,%rax
mov    %edx,%ecx
cmp    %rax,%rbp
je     1160 <selection_sort+0x40>
movslq %ecx,%rdx
mov    (%rbx,%rdx,4),%edi
mov    %eax,%edx
cmp    (%rbx,%rax,4),%edi
jg     1190 <selection_sort+0x70>
mov    %ecx,%edx
jmp    1190 <selection_sort+0x70>
mov    $0xffffffff,%ebp
lea    0xe48(%rip),%rdi
call   1030 <puts@plt>
jmp    11d4 <selection_sort+0xb4>
mov    $0xfffffffe,%ebp
lea    0xe51(%rip),%rdi
call   1030 <puts@plt>
jmp    11d4 <selection_sort+0xb4>
xor    %ebp,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [28 x i8] c"Null value passed for array\00"
@.str.1 = [31 x i8] c"Size of the array is bad value\00"
define i32 @selection_sort(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** %4
store i32 %1, i32* %5
%9 = load i32*, i32** %4
%10 = icmp ne i32* %9, null
br i1 %10, label %13, label %11
11: ; preds = %2
%12 = call i32 @puts(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %60
13: ; preds = %2
%14 = load i32, i32* %5
%15 = icmp sle i32 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %13
%17 = call i32 @puts(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0))
store i32 -2, i32* %3
br label %60
18: ; preds = %13
store i32 0, i32* %7
br label %19
19: ; preds = %56, %18
%20 = load i32, i32* %7
%21 = load i32, i32* %5
%22 = sub nsw i32 %21, 2
%23 = icmp sle i32 %20, %22
br i1 %23, label %24, label %59
24: ; preds = %19
%25 = load i32, i32* %7
store i32 %25, i32* %6
%26 = load i32, i32* %7
%27 = add nsw i32 %26, 1
store i32 %27, i32* %8
br label %28
28: ; preds = %48, %24
%29 = load i32, i32* %8
%30 = load i32, i32* %5
%31 = sub nsw i32 %30, 1
%32 = icmp sle i32 %29, %31
br i1 %32, label %33, label %51
33: ; preds = %28
%34 = load i32*, i32** %4
%35 = load i32, i32* %6
%36 = sext i32 %35 to i64
%37 = getelementptr inbounds i32, i32* %34, i64 %36
%38 = load i32, i32* %37
%39 = load i32*, i32** %4
%40 = load i32, i32* %8
%41 = sext i32 %40 to i64
%42 = getelementptr inbounds i32, i32* %39, i64 %41
%43 = load i32, i32* %42
%44 = icmp sgt i32 %38, %43
br i1 %44, label %45, label %47
45: ; preds = %33
%46 = load i32, i32* %8
store i32 %46, i32* %6
br label %47
47: ; preds = %45, %33
br label %48
48: ; preds = %47
%49 = load i32, i32* %8
%50 = add nsw i32 %49, 1
store i32 %50, i32* %8
br label %28
51: ; preds = %28
%52 = load i32*, i32** %4
%53 = load i32, i32* %7
%54 = load i32, i32* %6
%55 = call i32 @swap(i32* %52, i32 %53, i32 %54)
br label %56
56: ; preds = %51
%57 = load i32, i32* %7
%58 = add nsw i32 %57, 1
store i32 %58, i32* %7
br label %19
59: ; preds = %19
store i32 0, i32* %3
br label %60
60: ; preds = %59, %16, %11
%61 = load i32, i32* %3
ret i32 %61
}

LLVM IR (-O2):
@.str = [28 x i8] c"Null value passed for array\00"
@.str.1 = [31 x i8] c"Size of the array is bad value\00"
define i32 @selection_sort(i32* %0, i32 %1) {
%3 = icmp eq i32* %0, null
br i1 %3, label %34, label %4
4: ; preds = %2
%5 = icmp slt i32 %1, 1
br i1 %5, label %34, label %6
6: ; preds = %4
%7 = icmp eq i32 %1, 1
br i1 %7, label %38, label %8
8: ; preds = %6
%9 = add nsw i32 %1, -1
%10 = zext i32 %1 to i64
br label %11
11: ; preds = %8, %29
%12 = phi i64 [ 1, %8 ], [ %32, %29 ]
%13 = phi i32 [ 0, %8 ], [ %14, %29 ]
%14 = add nuw i32 %13, 1
%15 = icmp slt i32 %14, %1
br i1 %15, label %16, label %29
16: ; preds = %11, %16
%17 = phi i64 [ %27, %16 ], [ %12, %11 ]
%18 = phi i32 [ %26, %16 ], [ %13, %11 ]
%19 = sext i32 %18 to i64
%20 = getelementptr inbounds i32, i32* %0, i64 %19
%21 = load i32, i32* %20
%22 = getelementptr inbounds i32, i32* %0, i64 %17
%23 = load i32, i32* %22
%24 = icmp sgt i32 %21, %23
%25 = trunc i64 %17 to i32
%26 = select i1 %24, i32 %25, i32 %18
%27 = add nuw nsw i64 %17, 1
%28 = icmp eq i64 %27, %10
br i1 %28, label %29, label %16
29: ; preds = %16, %11
%30 = phi i32 [ %13, %11 ], [ %26, %16 ]
%31 = tail call i32 @swap(i32* %0, i32 %13, i32 %30)
%32 = add nuw nsw i64 %12, 1
%33 = icmp eq i32 %14, %9
br i1 %33, label %38, label %11
34: ; preds = %4, %2
%35 = phi i8* [ getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), %2 ], [ getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0), %4 ]
%36 = phi i32 [ -1, %2 ], [ -2, %4 ]
%37 = tail call i32 @puts(i8* dereferenceable(1) %35)
br label %38
38: ; preds = %29, %34, %6
%39 = phi i32 [ 0, %6 ], [ %36, %34 ], [ 0, %29 ]
ret i32 %39
}