#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*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ fscanf(int /*<<< orphan*/, char *, int *);
int getMagicIndex(int *, int /*<<< orphan*/, int);
scalar_t__ malloc(int);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ stdout;
int main(void) {
  int arraySize = 0;
  while (arraySize <= 0) {
    printf("Plese enter the array size: ");
    fscanf(stdin, "%i", &arraySize);
  }
  fprintf(stdout, "Please enter unique array elements in increasing order one "
                  "element at a time: \n");
  int *array = (int *)malloc(arraySize * sizeof(int));
  for (int i = 0; i < arraySize; i++) {
    fprintf(stdout, "A[%i]: ", i);
    fscanf(stdin, "%i", &array[i]);
  }
  fprintf(stdout, "Magic element index is: %i\n",
          getMagicIndex(array, 0, arraySize - 1));
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jg     119a <main+0x4a>
lea    0xe89(%rip),%rdi
call   1030 <printf@plt>
mov    0x2e65(%rip),%rax
mov    (%rax),%edi
lea    0xe91(%rip),%rsi
lea    -0x8(%rbp),%rdx
call   1040 <fscanf@plt>
jmp    1166 <main+0x16>
mov    0x2e37(%rip),%rax
mov    (%rax),%edi
lea    0xe76(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movslq -0x8(%rbp),%rax
shl    $0x2,%rax
mov    %eax,%edi
call   1060 <malloc@plt>
mov    %rax,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    1223 <main+0xd3>
mov    0x2dfa(%rip),%rax
mov    (%rax),%edi
mov    -0x14(%rbp),%edx
lea    0xe86(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    0x2df0(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
movslq -0x14(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rdx
lea    0xe0d(%rip),%rsi
call   1040 <fscanf@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    11cb <main+0x7b>
mov    0x2dae(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x8(%rbp),%edx
sub    $0x1,%edx
xor    %esi,%esi
call   1070 <getMagicIndex@plt>
mov    -0x18(%rbp),%edi
mov    %eax,%edx
lea    0xe2c(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
xor    %eax,%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
movl   $0x0,0xc(%rsp)
lea    0xe93(%rip),%r14
mov    0x2e74(%rip),%rbp
lea    0xea2(%rip),%r15
lea    0xc(%rsp),%rbx
mov    %r14,%rdi
call   1030 <printf@plt>
mov    0x0(%rbp),%edi
mov    %r15,%rsi
mov    %rbx,%rdx
call   1040 <fscanf@plt>
cmpl   $0x0,0xc(%rsp)
jle    1180 <main+0x30>
mov    0x2e34(%rip),%r14
mov    (%r14),%edi
lea    0xe72(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    0xc(%rsp),%edi
shl    $0x2,%edi
call   1060 <malloc@plt>
mov    0xc(%rsp),%edx
test   %edx,%edx
jle    1224 <main+0xd4>
lea    0xea0(%rip),%r15
lea    0xe46(%rip),%r12
mov    %rax,0x10(%rsp)
mov    %rax,%r13
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    (%r14),%edi
mov    %r15,%rsi
mov    %ebx,%edx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    0x0(%rbp),%edi
mov    %r12,%rsi
mov    %r13,%rdx
call   1040 <fscanf@plt>
add    $0x1,%rbx
movslq 0xc(%rsp),%rdx
add    $0x4,%r13
cmp    %rdx,%rbx
jl     11f0 <main+0xa0>
mov    0x10(%rsp),%rax
mov    (%r14),%ebx
add    $0xffffffff,%edx
mov    %rax,%rdi
xor    %esi,%esi
call   1070 <getMagicIndex@plt>
lea    0xe3d(%rip),%rsi
mov    %ebx,%edi
mov    %eax,%edx
xor    %eax,%eax
call   1050 <fprintf@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [29 x i8] c"Plese enter the array size: \00"
@stdin = global i32 0
@.str.1 = [3 x i8] c"%i\00"
@stdout = global i32 0
@.str.2 = [80 x i8] c"Please enter unique array elements in increasing order one element at a time: \0A\00"
@.str.3 = [8 x i8] c"A[%i]: \00"
@.str.4 = [28 x i8] c"Magic element index is: %i\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32*
%4 = alloca i32
store i32 0, i32* %1
store i32 0, i32* %2
br label %5
5: ; preds = %8, %0
%6 = load i32, i32* %2
%7 = icmp sle i32 %6, 0
br i1 %7, label %8, label %12
8: ; preds = %5
%9 = call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0))
%10 = load i32, i32* @stdin
%11 = call i32 @fscanf(i32 %10, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %2)
br label %5
12: ; preds = %5
%13 = load i32, i32* @stdout
%14 = call i32 (i32, i8*, ...) @fprintf(i32 %13, i8* getelementptr inbounds ([80 x i8], [80 x i8]* @.str.2, i64 0, i64 0))
%15 = load i32, i32* %2
%16 = sext i32 %15 to i64
%17 = mul i64 %16, 4
%18 = trunc i64 %17 to i32
%19 = call i64 @malloc(i32 %18)
%20 = inttoptr i64 %19 to i32*
store i32* %20, i32** %3
store i32 0, i32* %4
br label %21
21: ; preds = %35, %12
%22 = load i32, i32* %4
%23 = load i32, i32* %2
%24 = icmp slt i32 %22, %23
br i1 %24, label %25, label %38
25: ; preds = %21
%26 = load i32, i32* @stdout
%27 = load i32, i32* %4
%28 = call i32 (i32, i8*, ...) @fprintf(i32 %26, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i32 %27)
%29 = load i32, i32* @stdin
%30 = load i32*, i32** %3
%31 = load i32, i32* %4
%32 = sext i32 %31 to i64
%33 = getelementptr inbounds i32, i32* %30, i64 %32
%34 = call i32 @fscanf(i32 %29, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %33)
br label %35
35: ; preds = %25
%36 = load i32, i32* %4
%37 = add nsw i32 %36, 1
store i32 %37, i32* %4
br label %21
38: ; preds = %21
%39 = load i32, i32* @stdout
%40 = load i32*, i32** %3
%41 = load i32, i32* %2
%42 = sub nsw i32 %41, 1
%43 = call i32 @getMagicIndex(i32* %40, i32 0, i32 %42)
%44 = call i32 (i32, i8*, ...) @fprintf(i32 %39, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.4, i64 0, i64 0), i32 %43)
ret i32 0
}

LLVM IR (-O2):
@.str = [29 x i8] c"Plese enter the array size: \00"
@stdin = global i32 0
@.str.1 = [3 x i8] c"%i\00"
@stdout = global i32 0
@.str.2 = [80 x i8] c"Please enter unique array elements in increasing order one element at a time: \0A\00"
@.str.3 = [8 x i8] c"A[%i]: \00"
@.str.4 = [28 x i8] c"Magic element index is: %i\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = bitcast i32* %1 to i8*
store i32 0, i32* %1
br label %3
3: ; preds = %0, %3
%4 = call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0))
%5 = load i32, i32* @stdin
%6 = call i32 @fscanf(i32 %5, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %1)
%7 = load i32, i32* %1
%8 = icmp slt i32 %7, 1
br i1 %8, label %3, label %9
9: ; preds = %3
%10 = load i32, i32* @stdout
%11 = call i32 (i32, i8*, ...) @fprintf(i32 %10, i8* getelementptr inbounds ([80 x i8], [80 x i8]* @.str.2, i64 0, i64 0))
%12 = load i32, i32* %1
%13 = shl i32 %12, 2
%14 = call i64 @malloc(i32 %13)
%15 = inttoptr i64 %14 to i32*
%16 = load i32, i32* %1
%17 = icmp sgt i32 %16, 0
br i1 %17, label %24, label %18
18: ; preds = %24, %9
%19 = phi i32 [ %16, %9 ], [ %33, %24 ]
%20 = load i32, i32* @stdout
%21 = add nsw i32 %19, -1
%22 = call i32 @getMagicIndex(i32* %15, i32 0, i32 %21)
%23 = call i32 (i32, i8*, ...) @fprintf(i32 %20, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.4, i64 0, i64 0), i32 %22)
ret i32 0
24: ; preds = %9, %24
%25 = phi i64 [ %32, %24 ], [ 0, %9 ]
%26 = load i32, i32* @stdout
%27 = trunc i64 %25 to i32
%28 = call i32 (i32, i8*, ...) @fprintf(i32 %26, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i32 %27)
%29 = load i32, i32* @stdin
%30 = getelementptr inbounds i32, i32* %15, i64 %25
%31 = call i32 @fscanf(i32 %29, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %30)
%32 = add nuw nsw i64 %25, 1
%33 = load i32, i32* %1
%34 = sext i32 %33 to i64
%35 = icmp slt i64 %32, %34
br i1 %35, label %24, label %18
}