#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 find_rotation_spot(int *, size_t);
int /*<<< orphan*/ print_array(int *, size_t);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ restore_array(int *, size_t, int);
int scanf(char *, ...);
int main(void) {
  printf("Enter array size, followed by the array elements\n");
  printf("> ");
  size_t arr_sz;
  while (scanf("%zu", &arr_sz) == 1) {
    int array[arr_sz];
    size_t i;
    for (i = 0; i < arr_sz; i++) {
      scanf("%d", &array[i]);
    }
    int pos = find_rotation_spot(array, arr_sz);
    printf("Rotation spot = %d\n", pos);
    restore_array(array, arr_sz, pos);
    printf("Restored array: ");
    print_array(array, arr_sz);
    printf("\n> ");
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe9a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xeeb(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xeb0(%rip),%rdi
lea    -0x10(%rbp),%rsi
mov    $0x0,%al
call   1050 <scanf@plt>
cmp    $0x1,%eax
jne    1268 <main+0x118>
mov    -0x10(%rbp),%rax
mov    %rsp,%rcx
mov    %rcx,-0x18(%rbp)
lea    0xf(,%rax,4),%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x38(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x10(%rbp),%rax
jae    1202 <main+0xb2>
mov    -0x38(%rbp),%rsi
mov    -0x28(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rsi
lea    0xe4c(%rip),%rdi
mov    $0x0,%al
call   1050 <scanf@plt>
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
jmp    11c6 <main+0x76>
mov    -0x38(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1060 <find_rotation_spot@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%esi
lea    0xe1d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x38(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x2c(%rbp),%edx
call   1070 <restore_array@plt>
lea    0xe13(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x38(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1030 <print_array@plt>
lea    0xe09(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x18(%rbp),%rax
mov    %rax,%rsp
jmp    117b <main+0x2b>
xor    %eax,%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
lea    0xebb(%rip),%rdi
call   1030 <puts@plt>
lea    0xeac(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xe71(%rip),%rdi
lea    -0x30(%rbp),%rsi
xor    %eax,%eax
call   1060 <scanf@plt>
cmp    $0x1,%eax
jne    1273 <main+0x113>
lea    0xe5a(%rip),%r14
jmp    121c <main+0xbc>
nopl   0x0(%rax)
xor    %esi,%esi
mov    %r12,%rdi
call   1070 <find_rotation_spot@plt>
mov    %eax,%ebx
lea    0xe44(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    -0x30(%rbp),%rsi
mov    %r12,%rdi
mov    %ebx,%edx
call   1080 <restore_array@plt>
lea    0xe3a(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    -0x30(%rbp),%rsi
mov    %r12,%rdi
call   1040 <print_array@plt>
lea    0xe31(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %r13,%rsp
lea    0xdf4(%rip),%rdi
lea    -0x30(%rbp),%rsi
xor    %eax,%eax
call   1060 <scanf@plt>
cmp    $0x1,%eax
jne    1273 <main+0x113>
mov    %rsp,%r13
mov    -0x30(%rbp),%rax
mov    %rsp,%r12
lea    0xf(,%rax,4),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r12
mov    %r12,%rsp
cmpq   $0x0,-0x30(%rbp)
je     11b0 <main+0x50>
mov    %r12,%r15
xor    %ebx,%ebx
nopl   0x0(%rax,%rax,1)
mov    %r14,%rdi
mov    %r15,%rsi
xor    %eax,%eax
call   1060 <scanf@plt>
add    $0x1,%rbx
mov    -0x30(%rbp),%rsi
add    $0x4,%r15
cmp    %rsi,%rbx
jb     1250 <main+0xf0>
jmp    11b2 <main+0x52>
xor    %eax,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [50 x i8] c"Enter array size, followed by the array elements\0A\00"
@.str.1 = [3 x i8] c"> \00"
@.str.2 = [4 x i8] c"%zu\00"
@.str.3 = [3 x i8] c"%d\00"
@.str.4 = [20 x i8] c"Rotation spot = %d\0A\00"
@.str.5 = [17 x i8] c"Restored array: \00"
@.str.6 = [4 x i8] c"\0A> \00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i64
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i64
%6 = alloca i32
store i32 0, i32* %1
%7 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str, i64 0, i64 0))
%8 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
br label %9
9: ; preds = %27, %0
%10 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i64* %2)
%11 = icmp eq i32 %10, 1
br i1 %11, label %12, label %40
12: ; preds = %9
%13 = load i64, i64* %2
%14 = call i8* @llvm.stacksave()
store i8* %14, i8** %3
%15 = alloca i32, i64 %13
store i64 %13, i64* %4
store i64 0, i64* %5
br label %16
16: ; preds = %24, %12
%17 = load i64, i64* %5
%18 = load i64, i64* %2
%19 = icmp ult i64 %17, %18
br i1 %19, label %20, label %27
20: ; preds = %16
%21 = load i64, i64* %5
%22 = getelementptr inbounds i32, i32* %15, i64 %21
%23 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32* %22)
br label %24
24: ; preds = %20
%25 = load i64, i64* %5
%26 = add i64 %25, 1
store i64 %26, i64* %5
br label %16
27: ; preds = %16
%28 = load i64, i64* %2
%29 = call i32 @find_rotation_spot(i32* %15, i64 %28)
store i32 %29, i32* %6
%30 = load i32, i32* %6
%31 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0), i32 %30)
%32 = load i64, i64* %2
%33 = load i32, i32* %6
%34 = call i32 @restore_array(i32* %15, i64 %32, i32 %33)
%35 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.5, i64 0, i64 0))
%36 = load i64, i64* %2
%37 = call i32 @print_array(i32* %15, i64 %36)
%38 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.6, i64 0, i64 0))
%39 = load i8*, i8** %3
br label %9
40: ; preds = %9
ret i32 0
}

LLVM IR (-O2):
@.str.1 = [3 x i8] c"> \00"
@.str.2 = [4 x i8] c"%zu\00"
@.str.3 = [3 x i8] c"%d\00"
@.str.4 = [20 x i8] c"Rotation spot = %d\0A\00"
@.str.5 = [17 x i8] c"Restored array: \00"
@.str.6 = [4 x i8] c"\0A> \00"
@str = [49 x i8] c"Enter array size, followed by the array elements\00"
define i32 @main() {
%1 = alloca i64
%2 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([49 x i8], [49 x i8]* @str, i64 0, i64 0))
%3 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%4 = bitcast i64* %1 to i8*
%5 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i64* %1)
%6 = icmp eq i32 %5, 1
br i1 %6, label %7, label %32
7: ; preds = %0, %20
%8 = load i64, i64* %1
%9 = call i8* @llvm.stacksave()
%10 = alloca i32, i64 %8
%11 = load i64, i64* %1
%12 = icmp eq i64 %11, 0
br i1 %12, label %20, label %13
13: ; preds = %7, %13
%14 = phi i64 [ %17, %13 ], [ 0, %7 ]
%15 = getelementptr inbounds i32, i32* %10, i64 %14
%16 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32* %15)
%17 = add nuw i64 %14, 1
%18 = load i64, i64* %1
%19 = icmp ult i64 %17, %18
br i1 %19, label %13, label %20
20: ; preds = %13, %7
%21 = phi i64 [ 0, %7 ], [ %18, %13 ]
%22 = call i32 @find_rotation_spot(i32* %10, i64 %21)
%23 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0), i32 %22)
%24 = load i64, i64* %1
%25 = call i32 @restore_array(i32* %10, i64 %24, i32 %22)
%26 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str.5, i64 0, i64 0))
%27 = load i64, i64* %1
%28 = call i32 @print_array(i32* %10, i64 %27)
%29 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.6, i64 0, i64 0))
%30 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i64* %1)
%31 = icmp eq i32 %30, 1
br i1 %31, label %7, label %32
32: ; preds = %20, %0
ret i32 0
}