#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 Ternary_Search(int *, int /*<<< orphan*/, int, int);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ scanf(char *, int *);
int main() {
  int n;
  printf("Enter the number of elements : ");
  scanf("%d", &n);
  int array[n];
  printf("Enter the elements : \n");
  for (int i = 0; i < n; i++)
    scanf("%d", &array[i]);
  printf("Enter the element that you want to search: ");
  int find;
  scanf("%d", &find);
  if (Ternary_Search(array, 0, n, find) != 1)
    printf("Found \n");
  else
    printf("Not Found");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xeba(%rip),%rdi
call   1030 <printf@plt>
lea    0xece(%rip),%rdi
lea    -0x8(%rbp),%rsi
call   1050 <scanf@plt>
mov    -0x8(%rbp),%eax
mov    %rsp,%rcx
mov    %rcx,-0x10(%rbp)
lea    0xf(,%rax,4),%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x28(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x18(%rbp)
lea    0xe9a(%rip),%rdi
call   1030 <printf@plt>
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    11ca <main+0x9a>
mov    -0x28(%rbp),%rsi
movslq -0x1c(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rsi
lea    0xe69(%rip),%rdi
call   1050 <scanf@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1195 <main+0x65>
lea    0xe69(%rip),%rdi
call   1030 <printf@plt>
lea    0xe43(%rip),%rdi
lea    -0x20(%rbp),%rsi
call   1050 <scanf@plt>
mov    -0x28(%rbp),%rdi
mov    -0x8(%rbp),%edx
mov    -0x20(%rbp),%ecx
xor    %esi,%esi
call   1040 <Ternary_Search@plt>
cmp    $0x1,%eax
je     1211 <main+0xe1>
lea    0xe5f(%rip),%rdi
call   1030 <printf@plt>
jmp    121d <main+0xed>
lea    0xe56(%rip),%rdi
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%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    0xeab(%rip),%rdi
call   1040 <printf@plt>
lea    0xebf(%rip),%rdi
lea    -0x2c(%rbp),%rsi
call   1060 <scanf@plt>
mov    %rsp,%r12
mov    -0x2c(%rbp),%eax
mov    %rsp,%r14
lea    0xf(,%rax,4),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r14
mov    %r14,%rsp
lea    0xecd(%rip),%rdi
call   1030 <puts@plt>
cmpl   $0x0,-0x2c(%rbp)
jle    11cc <main+0x8c>
xor    %r13d,%r13d
lea    0xe7f(%rip),%r15
mov    %r14,%rbx
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %r15,%rdi
mov    %rbx,%rsi
call   1060 <scanf@plt>
add    $0x1,%r13
movslq -0x2c(%rbp),%rax
add    $0x4,%rbx
cmp    %rax,%r13
jl     11b0 <main+0x70>
lea    0xe50(%rip),%rdi
call   1040 <printf@plt>
lea    0xe41(%rip),%rdi
lea    -0x30(%rbp),%rsi
call   1060 <scanf@plt>
mov    -0x2c(%rbp),%edx
mov    -0x30(%rbp),%ecx
mov    %r14,%rdi
xor    %esi,%esi
call   1050 <Ternary_Search@plt>
cmp    $0x1,%eax
jne    120b <main+0xcb>
lea    0xe4b(%rip),%rdi
call   1040 <printf@plt>
jmp    1217 <main+0xd7>
lea    0xe5d(%rip),%rdi
call   1030 <puts@plt>
mov    %r12,%rsp
xor    %eax,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [32 x i8] c"Enter the number of elements : \00"
@.str.1 = [3 x i8] c"%d\00"
@.str.2 = [23 x i8] c"Enter the elements : \0A\00"
@.str.3 = [44 x i8] c"Enter the element that you want to search: \00"
@.str.4 = [8 x i8] c"Found \0A\00"
@.str.5 = [10 x i8] c"Not Found\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i32
%6 = alloca i32
store i32 0, i32* %1
%7 = call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0))
%8 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %2)
%9 = load i32, i32* %2
%10 = zext i32 %9 to i64
%11 = call i8* @llvm.stacksave()
store i8* %11, i8** %3
%12 = alloca i32, i64 %10
store i64 %10, i64* %4
%13 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0))
store i32 0, i32* %5
br label %14
14: ; preds = %23, %0
%15 = load i32, i32* %5
%16 = load i32, i32* %2
%17 = icmp slt i32 %15, %16
br i1 %17, label %18, label %26
18: ; preds = %14
%19 = load i32, i32* %5
%20 = sext i32 %19 to i64
%21 = getelementptr inbounds i32, i32* %12, i64 %20
%22 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %21)
br label %23
23: ; preds = %18
%24 = load i32, i32* %5
%25 = add nsw i32 %24, 1
store i32 %25, i32* %5
br label %14
26: ; preds = %14
%27 = call i32 @printf(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.3, i64 0, i64 0))
%28 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %6)
%29 = load i32, i32* %2
%30 = load i32, i32* %6
%31 = call i32 @Ternary_Search(i32* %12, i32 0, i32 %29, i32 %30)
%32 = icmp ne i32 %31, 1
br i1 %32, label %33, label %35
33: ; preds = %26
%34 = call i32 @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0))
br label %37
35: ; preds = %26
%36 = call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.5, i64 0, i64 0))
br label %37
37: ; preds = %35, %33
store i32 0, i32* %1
%38 = load i8*, i8** %3
%39 = load i32, i32* %1
ret i32 %39
}

LLVM IR (-O2):
@.str = [32 x i8] c"Enter the number of elements : \00"
@.str.1 = [3 x i8] c"%d\00"
@.str.3 = [44 x i8] c"Enter the element that you want to search: \00"
@.str.5 = [10 x i8] c"Not Found\00"
@str = [22 x i8] c"Enter the elements : \00"
@str.6 = [7 x i8] c"Found \00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = bitcast i32* %1 to i8*
%4 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0))
%5 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %1)
%6 = load i32, i32* %1
%7 = zext i32 %6 to i64
%8 = call i8* @llvm.stacksave()
%9 = alloca i32, i64 %7
%10 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @str, i64 0, i64 0))
%11 = load i32, i32* %1
%12 = icmp sgt i32 %11, 0
br i1 %12, label %21, label %13
13: ; preds = %21, %0
%14 = call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([44 x i8], [44 x i8]* @.str.3, i64 0, i64 0))
%15 = bitcast i32* %2 to i8*
%16 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %2)
%17 = load i32, i32* %1
%18 = load i32, i32* %2
%19 = call i32 @Ternary_Search(i32* %9, i32 0, i32 %17, i32 %18)
%20 = icmp eq i32 %19, 1
br i1 %20, label %31, label %29
21: ; preds = %0, %21
%22 = phi i64 [ %25, %21 ], [ 0, %0 ]
%23 = getelementptr inbounds i32, i32* %9, i64 %22
%24 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %23)
%25 = add nuw nsw i64 %22, 1
%26 = load i32, i32* %1
%27 = sext i32 %26 to i64
%28 = icmp slt i64 %25, %27
br i1 %28, label %21, label %13
29: ; preds = %13
%30 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @str.6, i64 0, i64 0))
br label %33
31: ; preds = %13
%32 = call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str.5, i64 0, i64 0))
br label %33
33: ; preds = %31, %29
ret i32 0
}