#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 */
scalar_t__ calloc(int, int);
int linearSearch(int *, int, int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ scanf(char *, int *);
int main() {
  printf("Linear Search \n");
  printf("\n");
  int i, n, key, index;
  printf("Total Elements : ");
  scanf("%d", &n);
  printf("\n");
  if (n < 1) {
    printf("Invalid Input \n");
    printf("\n");
    return 1;
  }
  int *A = (int *)calloc(n, sizeof(int));
  printf("Number Input \n");
  for (i = 0; i < n; i++) {
    scanf("%d", (A + i));
  }
  printf("\n");
  printf("Number to be Searched : ");
  scanf("%d", &key);
  printf("\n");
  index = linearSearch(A, n, key);
  if (index == -1) {
    printf("Number (%d) not found \n", key);
  } else {
    printf("Number (%d) found at Index : %d \n", key, index);
  }
  printf("\n");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xeaa(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xf31(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe9e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xea2(%rip),%rdi
lea    -0xc(%rbp),%rsi
call   1060 <scanf@plt>
lea    0xf05(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
cmpl   $0x1,-0xc(%rbp)
jge    11c9 <main+0x89>
lea    0xe7d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xedf(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12b9 <main+0x179>
mov    -0xc(%rbp),%edi
mov    $0x4,%esi
call   1050 <calloc@plt>
mov    %rax,-0x20(%rbp)
lea    0xe54(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    1224 <main+0xe4>
mov    -0x20(%rbp),%rsi
movslq -0x8(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rsi
lea    0xe11(%rip),%rdi
call   1060 <scanf@plt>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    11ef <main+0xaf>
lea    0xe6a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe0b(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xddb(%rip),%rdi
lea    -0x10(%rbp),%rsi
call   1060 <scanf@plt>
lea    0xe3e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x20(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    -0x10(%rbp),%edx
call   1030 <linearSearch@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    1290 <main+0x150>
mov    -0x10(%rbp),%esi
lea    0xdd9(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    12a4 <main+0x164>
mov    -0x10(%rbp),%esi
mov    -0x14(%rbp),%edx
lea    0xdd8(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xdea(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
lea    0xefa(%rip),%rdi
call   1050 <puts@plt>
mov    $0xa,%edi
call   1040 <putchar@plt>
lea    0xe7c(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xe80(%rip),%rdi
mov    %rsp,%rsi
call   1080 <scanf@plt>
mov    $0xa,%edi
call   1040 <putchar@plt>
mov    (%rsp),%edi
test   %edi,%edi
jle    125b <main+0xfb>
mov    $0x4,%esi
call   1070 <calloc@plt>
mov    %rax,%r14
lea    0xeb4(%rip),%rdi
call   1050 <puts@plt>
cmpl   $0x0,(%rsp)
jle    11fc <main+0x9c>
xor    %ebp,%ebp
lea    0xe3b(%rip),%r15
mov    %r14,%rbx
nopw   0x0(%rax,%rax,1)
mov    %r15,%rdi
mov    %rbx,%rsi
call   1080 <scanf@plt>
add    $0x1,%rbp
movslq (%rsp),%rax
add    $0x4,%rbx
cmp    %rax,%rbp
jl     11e0 <main+0x80>
mov    $0xa,%edi
call   1040 <putchar@plt>
lea    0xe08(%rip),%rdi
xor    %ebp,%ebp
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xdf5(%rip),%rdi
lea    0x4(%rsp),%rsi
call   1080 <scanf@plt>
mov    $0xa,%edi
call   1040 <putchar@plt>
mov    (%rsp),%esi
mov    0x4(%rsp),%edx
mov    %r14,%rdi
call   1030 <linearSearch@plt>
mov    0x4(%rsp),%esi
cmp    $0xffffffff,%eax
je     126e <main+0x10e>
lea    0xdf6(%rip),%rdi
mov    %eax,%edx
xor    %eax,%eax
call   1060 <printf@plt>
jmp    127e <main+0x11e>
lea    0xe23(%rip),%rdi
call   1050 <puts@plt>
mov    $0x1,%ebp
jmp    127e <main+0x11e>
lea    0xdb9(%rip),%rdi
xor    %ebp,%ebp
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0xa,%edi
call   1040 <putchar@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [16 x i8] c"Linear Search \0A\00"
@.str.1 = [2 x i8] c"\0A\00"
@.str.2 = [18 x i8] c"Total Elements : \00"
@.str.3 = [3 x i8] c"%d\00"
@.str.4 = [16 x i8] c"Invalid Input \0A\00"
@.str.5 = [15 x i8] c"Number Input \0A\00"
@.str.6 = [25 x i8] c"Number to be Searched : \00"
@.str.7 = [24 x i8] c"Number (%d) not found \0A\00"
@.str.8 = [34 x i8] c"Number (%d) found at Index : %d \0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32*
store i32 0, i32* %1
%7 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%8 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%9 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0))
%10 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32* %3)
%11 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%12 = load i32, i32* %3
%13 = icmp slt i32 %12, 1
br i1 %13, label %14, label %17
14: ; preds = %0
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0))
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %1
br label %55
17: ; preds = %0
%18 = load i32, i32* %3
%19 = call i64 @calloc(i32 %18, i32 4)
%20 = inttoptr i64 %19 to i32*
store i32* %20, i32** %6
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.5, i64 0, i64 0))
store i32 0, i32* %2
br label %22
22: ; preds = %32, %17
%23 = load i32, i32* %2
%24 = load i32, i32* %3
%25 = icmp slt i32 %23, %24
br i1 %25, label %26, label %35
26: ; preds = %22
%27 = load i32*, i32** %6
%28 = load i32, i32* %2
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds i32, i32* %27, i64 %29
%31 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32* %30)
br label %32
32: ; preds = %26
%33 = load i32, i32* %2
%34 = add nsw i32 %33, 1
store i32 %34, i32* %2
br label %22
35: ; preds = %22
%36 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%37 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.6, i64 0, i64 0))
%38 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32* %4)
%39 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%40 = load i32*, i32** %6
%41 = load i32, i32* %3
%42 = load i32, i32* %4
%43 = call i32 @linearSearch(i32* %40, i32 %41, i32 %42)
store i32 %43, i32* %5
%44 = load i32, i32* %5
%45 = icmp eq i32 %44, -1
br i1 %45, label %46, label %49
46: ; preds = %35
%47 = load i32, i32* %4
%48 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.7, i64 0, i64 0), i32 %47)
br label %53
49: ; preds = %35
%50 = load i32, i32* %4
%51 = load i32, i32* %5
%52 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.8, i64 0, i64 0), i32 %50, i32 %51)
br label %53
53: ; preds = %49, %46
%54 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32 0, i32* %1
br label %55
55: ; preds = %53, %14
%56 = load i32, i32* %1
ret i32 %56
}

LLVM IR (-O2):
@.str.2 = [18 x i8] c"Total Elements : \00"
@.str.3 = [3 x i8] c"%d\00"
@.str.6 = [25 x i8] c"Number to be Searched : \00"
@.str.7 = [24 x i8] c"Number (%d) not found \0A\00"
@.str.8 = [34 x i8] c"Number (%d) found at Index : %d \0A\00"
@str = [15 x i8] c"Linear Search \00"
@str.9 = [14 x i8] c"Number Input \00"
@str.10 = [15 x i8] c"Invalid Input \00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str, i64 0, i64 0))
%4 = tail call i32 @putchar(i32 10)
%5 = bitcast i32* %1 to i8*
%6 = bitcast i32* %2 to i8*
%7 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0))
%8 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32* %1)
%9 = call i32 @putchar(i32 10)
%10 = load i32, i32* %1
%11 = icmp slt i32 %10, 1
br i1 %11, label %12, label %14
12: ; preds = %0
%13 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str.10, i64 0, i64 0))
br label %42
14: ; preds = %0
%15 = call i64 @calloc(i32 %10, i32 4)
%16 = inttoptr i64 %15 to i32*
%17 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str.9, i64 0, i64 0))
%18 = load i32, i32* %1
%19 = icmp sgt i32 %18, 0
br i1 %19, label %20, label %28
20: ; preds = %14, %20
%21 = phi i64 [ %24, %20 ], [ 0, %14 ]
%22 = getelementptr inbounds i32, i32* %16, i64 %21
%23 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32* %22)
%24 = add nuw nsw i64 %21, 1
%25 = load i32, i32* %1
%26 = sext i32 %25 to i64
%27 = icmp slt i64 %24, %26
br i1 %27, label %20, label %28
28: ; preds = %20, %14
%29 = call i32 @putchar(i32 10)
%30 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str.6, i64 0, i64 0))
%31 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32* %2)
%32 = call i32 @putchar(i32 10)
%33 = load i32, i32* %1
%34 = load i32, i32* %2
%35 = call i32 @linearSearch(i32* %16, i32 %33, i32 %34)
%36 = icmp eq i32 %35, -1
%37 = load i32, i32* %2
br i1 %36, label %38, label %40
38: ; preds = %28
%39 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.7, i64 0, i64 0), i32 %37)
br label %42
40: ; preds = %28
%41 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @.str.8, i64 0, i64 0), i32 %37, i32 %35)
br label %42
42: ; preds = %38, %40, %12
%43 = phi i32 [ 1, %12 ], [ 0, %40 ], [ 0, %38 ]
%44 = call i32 @putchar(i32 10)
ret i32 %43
}