#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 atoi(char *);
int /*<<< orphan*/ free(void *);
void *lookup_function(char *);
void *malloc(int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ selection_sort(int, int, void *, void *);
int main(int argc, char **argv) {
  if (!argv[1] || !argv[2]) {
    printf("Correct use: ./universal_sort <function> <size> [element 1] "
           "[element 2] [element 3]...\n");
    return 1;
  }
  void *function = lookup_function(argv[1]);
  if (!function) {
    printf("<function> must be a valid function name.\n");
    return 1;
  }
  int size = atoi(argv[2]);
  if (size == 0) {
    printf("<size> must be a valid integer.\n");
    return 1;
  }
  int num_elements = argc - 3;
  void *array = malloc(num_elements * size);
  int i;
  for (i = 0; i < num_elements; i++) {
    *((int *)(array + (i * size))) = atoi(argv[i + 3]);
  }
  selection_sort(num_elements, size, function, array);
  printf("Sorted array of %d elements using %s:\n[", num_elements, argv[1]);
  for (i = 0; i < num_elements; i++) {
    if (i < num_elements - 1) {
      printf("%d, ", *(int *)(array + (i * size)));
    } else {
      printf("%d].\n", *(int *)(array + (i * size)));
    }
  }
  free(array);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
cmpq   $0x0,0x8(%rax)
je     1194 <main+0x34>
mov    -0x10(%rbp),%rax
cmpq   $0x0,0x10(%rax)
jne    11ae <main+0x4e>
lea    0xe65(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1331 <main+0x1d1>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1080 <lookup_function@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11e4 <main+0x84>
lea    0xe87(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1331 <main+0x1d1>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   1070 <atoi@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jne    1218 <main+0xb8>
lea    0xe7e(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1331 <main+0x1d1>
mov    -0x8(%rbp),%eax
sub    $0x3,%eax
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%edi
imul   -0x1c(%rbp),%edi
call   1060 <malloc@plt>
mov    %rax,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x20(%rbp),%eax
jge    127b <main+0x11b>
mov    -0x10(%rbp),%rax
mov    -0x2c(%rbp),%ecx
add    $0x3,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,8),%rdi
call   1070 <atoi@plt>
mov    %eax,%edx
mov    -0x28(%rbp),%rax
mov    -0x2c(%rbp),%ecx
imul   -0x1c(%rbp),%ecx
movslq %ecx,%rcx
mov    %edx,(%rax,%rcx,1)
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    1238 <main+0xd8>
mov    -0x20(%rbp),%edi
mov    -0x1c(%rbp),%esi
mov    -0x18(%rbp),%rdx
mov    -0x28(%rbp),%rcx
call   1030 <selection_sort@plt>
mov    -0x20(%rbp),%esi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdx
lea    0xe04(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x0,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x20(%rbp),%eax
jge    1321 <main+0x1c1>
mov    -0x2c(%rbp),%eax
mov    -0x20(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jge    12ef <main+0x18f>
mov    -0x28(%rbp),%rax
mov    -0x2c(%rbp),%ecx
imul   -0x1c(%rbp),%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,1),%esi
lea    0xde9(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    130e <main+0x1ae>
mov    -0x28(%rbp),%rax
mov    -0x2c(%rbp),%ecx
imul   -0x1c(%rbp),%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,1),%esi
lea    0xdca(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    1313 <main+0x1b3>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    12ae <main+0x14e>
mov    -0x28(%rbp),%rdi
call   1040 <free@plt>
movl   $0x0,-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
mov    %edi,%r15d
mov    0x8(%rsi),%rdi
test   %rdi,%rdi
je     1298 <main+0x128>
mov    %rsi,%rbx
cmpq   $0x0,0x10(%rsi)
je     1298 <main+0x128>
call   1090 <lookup_function@plt>
test   %rax,%rax
je     12b8 <main+0x148>
mov    %rax,%r14
mov    0x10(%rbx),%rdi
call   1080 <atoi@plt>
test   %eax,%eax
je     12c1 <main+0x151>
mov    %eax,%ebp
lea    -0x3(%r15),%r12d
mov    %eax,%edi
imul   %r12d,%edi
call   1070 <malloc@plt>
cmp    $0x4,%r15d
mov    %rax,(%rsp)
jl     12ca <main+0x15a>
mov    %r14,0x10(%rsp)
mov    %ebp,0x8(%rsp)
movslq %ebp,%r14
mov    %r12d,0xc(%rsp)
mov    %r12d,%r12d
xor    %r13d,%r13d
mov    %rax,%rbp
nopw   0x0(%rax,%rax,1)
mov    0x18(%rbx,%r13,8),%rdi
call   1080 <atoi@plt>
mov    %eax,0x0(%rbp)
add    $0x1,%r13
add    %r14,%rbp
cmp    %r13,%r12
jne    1200 <main+0x90>
mov    0xc(%rsp),%ebp
mov    %ebp,%edi
mov    0x8(%rsp),%esi
mov    0x10(%rsp),%rdx
mov    (%rsp),%rcx
call   1030 <selection_sort@plt>
mov    0x8(%rbx),%rdx
lea    0xdc4(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1060 <printf@plt>
cmp    $0x4,%r15d
jl     12ef <main+0x17f>
add    $0xfffffffc,%r15d
lea    0xdd3(%rip),%rbp
mov    (%rsp),%rbx
xor    %r13d,%r13d
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
cmp    %r15,%r13
mov    (%rbx),%esi
mov    %rbp,%rdi
lea    0xda9(%rip),%rax
cmovb  %rax,%rdi
xor    %eax,%eax
call   1060 <printf@plt>
add    $0x1,%r13
add    %r14,%rbx
cmp    %r13,%r12
jne    1270 <main+0x100>
jmp    12ef <main+0x17f>
lea    0xd94(%rip),%rdi
call   1050 <puts@plt>
mov    $0x1,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdcb(%rip),%rdi
jmp    129f <main+0x12f>
lea    0xdec(%rip),%rdi
jmp    129f <main+0x12f>
mov    %r12d,%edi
mov    %ebp,%esi
mov    %r14,%rdx
mov    %rax,%rcx
call   1030 <selection_sort@plt>
mov    0x8(%rbx),%rdx
lea    0xd1b(%rip),%rdi
mov    %r12d,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    (%rsp),%rdi
call   1040 <free@plt>
xor    %eax,%eax
jmp    12a9 <main+0x139>

LLVM IR (-O0):
@.str = [88 x i8] c"Correct use: ./universal_sort <function> <size> [element 1] [element 2] [element 3]...\0A\00"
@.str.1 = [43 x i8] c"<function> must be a valid function name.\0A\00"
@.str.2 = [33 x i8] c"<size> must be a valid integer.\0A\00"
@.str.3 = [40 x i8] c"Sorted array of %d elements using %s:\0A[\00"
@.str.4 = [5 x i8] c"%d, \00"
@.str.5 = [6 x i8] c"%d].\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = load i8**, i8*** %5
%12 = getelementptr inbounds i8*, i8** %11, i64 1
%13 = load i8*, i8** %12
%14 = icmp ne i8* %13, null
br i1 %14, label %15, label %20
15: ; preds = %2
%16 = load i8**, i8*** %5
%17 = getelementptr inbounds i8*, i8** %16, i64 2
%18 = load i8*, i8** %17
%19 = icmp ne i8* %18, null
br i1 %19, label %22, label %20
20: ; preds = %15, %2
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([88 x i8], [88 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %116
22: ; preds = %15
%23 = load i8**, i8*** %5
%24 = getelementptr inbounds i8*, i8** %23, i64 1
%25 = load i8*, i8** %24
%26 = call i8* @lookup_function(i8* %25)
store i8* %26, i8** %6
%27 = load i8*, i8** %6
%28 = icmp ne i8* %27, null
br i1 %28, label %31, label %29
29: ; preds = %22
%30 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %3
br label %116
31: ; preds = %22
%32 = load i8**, i8*** %5
%33 = getelementptr inbounds i8*, i8** %32, i64 2
%34 = load i8*, i8** %33
%35 = call i32 @atoi(i8* %34)
store i32 %35, i32* %7
%36 = load i32, i32* %7
%37 = icmp eq i32 %36, 0
br i1 %37, label %38, label %40
38: ; preds = %31
%39 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %3
br label %116
40: ; preds = %31
%41 = load i32, i32* %4
%42 = sub nsw i32 %41, 3
store i32 %42, i32* %8
%43 = load i32, i32* %8
%44 = load i32, i32* %7
%45 = mul nsw i32 %43, %44
%46 = call i8* @malloc(i32 %45)
store i8* %46, i8** %9
store i32 0, i32* %10
br label %47
47: ; preds = %66, %40
%48 = load i32, i32* %10
%49 = load i32, i32* %8
%50 = icmp slt i32 %48, %49
br i1 %50, label %51, label %69
51: ; preds = %47
%52 = load i8**, i8*** %5
%53 = load i32, i32* %10
%54 = add nsw i32 %53, 3
%55 = sext i32 %54 to i64
%56 = getelementptr inbounds i8*, i8** %52, i64 %55
%57 = load i8*, i8** %56
%58 = call i32 @atoi(i8* %57)
%59 = load i8*, i8** %9
%60 = load i32, i32* %10
%61 = load i32, i32* %7
%62 = mul nsw i32 %60, %61
%63 = sext i32 %62 to i64
%64 = getelementptr i8, i8* %59, i64 %63
%65 = bitcast i8* %64 to i32*
store i32 %58, i32* %65
br label %66
66: ; preds = %51
%67 = load i32, i32* %10
%68 = add nsw i32 %67, 1
store i32 %68, i32* %10
br label %47
69: ; preds = %47
%70 = load i32, i32* %8
%71 = load i32, i32* %7
%72 = load i8*, i8** %6
%73 = load i8*, i8** %9
%74 = call i32 @selection_sort(i32 %70, i32 %71, i8* %72, i8* %73)
%75 = load i32, i32* %8
%76 = load i8**, i8*** %5
%77 = getelementptr inbounds i8*, i8** %76, i64 1
%78 = load i8*, i8** %77
%79 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.3, i64 0, i64 0), i32 %75, i8* %78)
store i32 0, i32* %10
br label %80
80: ; preds = %110, %69
%81 = load i32, i32* %10
%82 = load i32, i32* %8
%83 = icmp slt i32 %81, %82
br i1 %83, label %84, label %113
84: ; preds = %80
%85 = load i32, i32* %10
%86 = load i32, i32* %8
%87 = sub nsw i32 %86, 1
%88 = icmp slt i32 %85, %87
br i1 %88, label %89, label %99
89: ; preds = %84
%90 = load i8*, i8** %9
%91 = load i32, i32* %10
%92 = load i32, i32* %7
%93 = mul nsw i32 %91, %92
%94 = sext i32 %93 to i64
%95 = getelementptr i8, i8* %90, i64 %94
%96 = bitcast i8* %95 to i32*
%97 = load i32, i32* %96
%98 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0), i32 %97)
br label %109
99: ; preds = %84
%100 = load i8*, i8** %9
%101 = load i32, i32* %10
%102 = load i32, i32* %7
%103 = mul nsw i32 %101, %102
%104 = sext i32 %103 to i64
%105 = getelementptr i8, i8* %100, i64 %104
%106 = bitcast i8* %105 to i32*
%107 = load i32, i32* %106
%108 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0), i32 %107)
br label %109
109: ; preds = %99, %89
br label %110
110: ; preds = %109
%111 = load i32, i32* %10
%112 = add nsw i32 %111, 1
store i32 %112, i32* %10
br label %80
113: ; preds = %80
%114 = load i8*, i8** %9
%115 = call i32 @free(i8* %114)
store i32 0, i32* %3
br label %116
116: ; preds = %113, %38, %29, %20
%117 = load i32, i32* %3
ret i32 %117
}

LLVM IR (-O2):
@.str.3 = [40 x i8] c"Sorted array of %d elements using %s:\0A[\00"
@.str.4 = [5 x i8] c"%d, \00"
@.str.5 = [6 x i8] c"%d].\0A\00"
@str = [87 x i8] c"Correct use: ./universal_sort <function> <size> [element 1] [element 2] [element 3]...\00"
@str.6 = [42 x i8] c"<function> must be a valid function name.\00"
@str.7 = [32 x i8] c"<size> must be a valid integer.\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = getelementptr inbounds i8*, i8** %1, i64 1
%4 = load i8*, i8** %3
%5 = icmp eq i8* %4, null
br i1 %5, label %10, label %6
6: ; preds = %2
%7 = getelementptr inbounds i8*, i8** %1, i64 2
%8 = load i8*, i8** %7
%9 = icmp eq i8* %8, null
br i1 %9, label %10, label %12
10: ; preds = %6, %2
%11 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([87 x i8], [87 x i8]* @str, i64 0, i64 0))
br label %68
12: ; preds = %6
%13 = tail call i8* @lookup_function(i8* %4)
%14 = icmp eq i8* %13, null
br i1 %14, label %15, label %17
15: ; preds = %12
%16 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([42 x i8], [42 x i8]* @str.6, i64 0, i64 0))
br label %68
17: ; preds = %12
%18 = load i8*, i8** %7
%19 = tail call i32 @atoi(i8* %18)
%20 = icmp eq i32 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %17
%22 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @str.7, i64 0, i64 0))
br label %68
23: ; preds = %17
%24 = add nsw i32 %0, -3
%25 = mul nsw i32 %19, %24
%26 = tail call i8* @malloc(i32 %25)
%27 = icmp sgt i32 %0, 3
br i1 %27, label %28, label %62
28: ; preds = %23
%29 = sext i32 %19 to i64
%30 = zext i32 %24 to i64
br label %31
31: ; preds = %28, %31
%32 = phi i64 [ 0, %28 ], [ %40, %31 ]
%33 = add nuw nsw i64 %32, 3
%34 = getelementptr inbounds i8*, i8** %1, i64 %33
%35 = load i8*, i8** %34
%36 = tail call i32 @atoi(i8* %35)
%37 = mul nsw i64 %32, %29
%38 = getelementptr i8, i8* %26, i64 %37
%39 = bitcast i8* %38 to i32*
store i32 %36, i32* %39
%40 = add nuw nsw i64 %32, 1
%41 = icmp eq i64 %40, %30
br i1 %41, label %42, label %31
42: ; preds = %31
%43 = tail call i32 @selection_sort(i32 %24, i32 %19, i8* %13, i8* %26)
%44 = load i8*, i8** %3
%45 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([40 x i8], [40 x i8]* @.str.3, i64 0, i64 0), i32 %24, i8* %44)
br i1 %27, label %46, label %66
46: ; preds = %42
%47 = add nsw i32 %0, -4
%48 = sext i32 %19 to i64
%49 = zext i32 %47 to i64
%50 = zext i32 %24 to i64
br label %51
51: ; preds = %46, %51
%52 = phi i64 [ 0, %46 ], [ %60, %51 ]
%53 = icmp ult i64 %52, %49
%54 = mul nsw i64 %52, %48
%55 = getelementptr i8, i8* %26, i64 %54
%56 = bitcast i8* %55 to i32*
%57 = load i32, i32* %56
%58 = select i1 %53, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0)
%59 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) %58, i32 %57)
%60 = add nuw nsw i64 %52, 1
%61 = icmp eq i64 %60, %50
br i1 %61, label %66, label %51
62: ; preds = %23
%63 = tail call i32 @selection_sort(i32 %24, i32 %19, i8* %13, i8* %26)
%64 = load i8*, i8** %3
%65 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([40 x i8], [40 x i8]* @.str.3, i64 0, i64 0), i32 %24, i8* %64)
br label %66
66: ; preds = %51, %62, %42
%67 = tail call i32 @free(i8* %26)
br label %68
68: ; preds = %15, %66, %21, %10
%69 = phi i32 [ 1, %10 ], [ 1, %15 ], [ 1, %21 ], [ 0, %66 ]
ret i32 %69
}