#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*/ printf(char *);
int root_element(int *p_array, int size, int index) {
  if (!p_array) {
    printf("p_array is NULL!\nNeedless to initialize\n");
    return -1;
  }
  if (index > (size - 1)) {
    printf("'index' bigger than size of array which 'p_array' point to.\n");
    return -1;
  }
  for (; p_array[index] != index;) {
    index = p_array[index];
  }
  return index;
}


x86 assembly (clang, -O0):
<root_element>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1145 <root_element+0x35>
lea    0xecc(%rip),%rdi
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11a0 <root_element+0x90>
mov    -0x18(%rbp),%eax
mov    -0x14(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jle    116e <root_element+0x5e>
lea    0xecc(%rip),%rdi
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11a0 <root_element+0x90>
jmp    1173 <root_element+0x63>
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
mov    (%rax,%rcx,4),%eax
cmp    -0x18(%rbp),%eax
je     119a <root_element+0x8a>
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
mov    (%rax,%rcx,4),%eax
mov    %eax,-0x18(%rbp)
jmp    1173 <root_element+0x63>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<root_element>:
push   %rax
test   %rdi,%rdi
je     112e <root_element+0x1e>
cmp    %edx,%esi
jle    1137 <root_element+0x27>
nopw   0x0(%rax,%rax,1)
mov    %edx,%eax
movslq %edx,%rcx
mov    (%rdi,%rcx,4),%edx
cmp    %ecx,%edx
jne    1120 <root_element+0x10>
pop    %rcx
ret
lea    0xecb(%rip),%rdi
jmp    113e <root_element+0x2e>
lea    0xeea(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%eax
pop    %rcx
ret

LLVM IR (-O0):
@.str = [41 x i8] c"p_array is NULL!\0ANeedless to initialize\0A\00"
@.str.1 = [61 x i8] c"'index' bigger than size of array which 'p_array' point to.\0A\00"
define i32 @root_element(i32* %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i32
store i32* %0, i32** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
%8 = load i32*, i32** %5
%9 = icmp ne i32* %8, null
br i1 %9, label %12, label %10
10: ; preds = %3
%11 = call i32 @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %4
br label %36
12: ; preds = %3
%13 = load i32, i32* %7
%14 = load i32, i32* %6
%15 = sub nsw i32 %14, 1
%16 = icmp sgt i32 %13, %15
br i1 %16, label %17, label %19
17: ; preds = %12
%18 = call i32 @printf(i8* getelementptr inbounds ([61 x i8], [61 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %4
br label %36
19: ; preds = %12
br label %20
20: ; preds = %28, %19
%21 = load i32*, i32** %5
%22 = load i32, i32* %7
%23 = sext i32 %22 to i64
%24 = getelementptr inbounds i32, i32* %21, i64 %23
%25 = load i32, i32* %24
%26 = load i32, i32* %7
%27 = icmp ne i32 %25, %26
br i1 %27, label %28, label %34
28: ; preds = %20
%29 = load i32*, i32** %5
%30 = load i32, i32* %7
%31 = sext i32 %30 to i64
%32 = getelementptr inbounds i32, i32* %29, i64 %31
%33 = load i32, i32* %32
store i32 %33, i32* %7
br label %20
34: ; preds = %20
%35 = load i32, i32* %7
store i32 %35, i32* %4
br label %36
36: ; preds = %34, %17, %10
%37 = load i32, i32* %4
ret i32 %37
}

LLVM IR (-O2):
@str = [40 x i8] c"p_array is NULL!\0ANeedless to initialize\00"
@str.2 = [60 x i8] c"'index' bigger than size of array which 'p_array' point to.\00"
define i32 @root_element(i32* %0, i32 %1, i32 %2) {
%4 = icmp eq i32* %0, null
br i1 %4, label %13, label %5
5: ; preds = %3
%6 = icmp sgt i32 %1, %2
br i1 %6, label %7, label %13
7: ; preds = %5, %7
%8 = phi i32 [ %11, %7 ], [ %2, %5 ]
%9 = sext i32 %8 to i64
%10 = getelementptr inbounds i32, i32* %0, i64 %9
%11 = load i32, i32* %10
%12 = icmp eq i32 %11, %8
br i1 %12, label %16, label %7
13: ; preds = %5, %3
%14 = phi i8* [ getelementptr inbounds ([40 x i8], [40 x i8]* @str, i64 0, i64 0), %3 ], [ getelementptr inbounds ([60 x i8], [60 x i8]* @str.2, i64 0, i64 0), %5 ]
%15 = tail call i32 @puts(i8* dereferenceable(1) %14)
br label %16
16: ; preds = %7, %13
%17 = phi i32 [ -1, %13 ], [ %8, %7 ]
ret i32 %17
}