#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*/ exit(int);
int /*<<< orphan*/ free(int *);
int linear_search(int *, int, int);
int *malloc(int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ scanf(char *, int *);
int main() {
  int length;
  for (;;) {
    printf("Enter a positive length: ");
    scanf("%d", &length);
    if (length > 1) {
      break;
    } else {
      printf("You entered length = %d\n\n", length);
    }
  }
  int *arr = malloc(sizeof(int) * length);
  if (arr == NULL) {
    perror("The following error occurred");
    exit(-1);
  }
  for (int i = 0; i < length; i++) {
    scanf("%d", &arr[i]);
  }
  int element;
  printf("\nEnter the element to be searched: ");
  scanf("%d", &element);
  int index = linear_search(arr, length, element);
  if (index == -1) {
    printf("\nElement not in the array");
  } else {
    printf("\nIndex of element is %d", index);
  }
  free(arr);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe7a(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xf10(%rip),%rdi
lea    -0x8(%rbp),%rsi
call   1070 <scanf@plt>
cmpl   $0x1,-0x8(%rbp)
jle    11ac <main+0x3c>
jmp    11c2 <main+0x52>
mov    -0x8(%rbp),%esi
lea    0xe64(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    117f <main+0xf>
movslq -0x8(%rbp),%rax
shl    $0x2,%rax
mov    %eax,%edi
call   1060 <malloc@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11f6 <main+0x86>
lea    0xe4d(%rip),%rdi
call   1080 <perror@plt>
mov    $0xffffffff,%edi
call   1090 <exit@plt>
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    1232 <main+0xc2>
mov    -0x10(%rbp),%rsi
movslq -0x14(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rsi
lea    0xe85(%rip),%rdi
call   1070 <scanf@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    11fd <main+0x8d>
lea    0xe18(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xe5d(%rip),%rdi
lea    -0x18(%rbp),%rsi
call   1070 <scanf@plt>
mov    -0x10(%rbp),%rdi
mov    -0x8(%rbp),%esi
mov    -0x18(%rbp),%edx
call   1040 <linear_search@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    127f <main+0x10f>
lea    0xe02(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    1290 <main+0x120>
mov    -0x1c(%rbp),%esi
lea    0xe06(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
lea    0xe81(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xf17(%rip),%rdi
mov    %rsp,%rsi
call   1070 <scanf@plt>
mov    (%rsp),%esi
cmp    $0x1,%esi
jg     11e7 <main+0x77>
lea    0xe76(%rip),%r14
lea    0xe55(%rip),%r15
lea    0xef2(%rip),%r12
mov    %rsp,%rbx
cs nopw 0x0(%rax,%rax,1)
nop
mov    %r14,%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %r15,%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %r12,%rdi
mov    %rbx,%rsi
call   1070 <scanf@plt>
mov    (%rsp),%esi
cmp    $0x2,%esi
jl     11c0 <main+0x50>
shl    $0x2,%esi
mov    %esi,%edi
call   1060 <malloc@plt>
test   %rax,%rax
je     1295 <main+0x125>
mov    %rax,%r14
xor    %r12d,%r12d
lea    0xe9d(%rip),%r15
mov    %rax,%rbx
nopw   0x0(%rax,%rax,1)
mov    %r15,%rdi
mov    %rbx,%rsi
call   1070 <scanf@plt>
add    $0x1,%r12
movslq (%rsp),%rax
add    $0x4,%rbx
cmp    %rax,%r12
jl     1210 <main+0xa0>
lea    0xe1e(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xe63(%rip),%rdi
lea    0x4(%rsp),%rsi
call   1070 <scanf@plt>
mov    (%rsp),%esi
mov    0x4(%rsp),%edx
mov    %r14,%rdi
call   1040 <linear_search@plt>
cmp    $0xffffffff,%eax
je     1271 <main+0x101>
lea    0xe29(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    127f <main+0x10f>
lea    0xdfd(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %r14,%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
lea    0xd98(%rip),%rdi
call   1080 <perror@plt>
mov    $0xffffffff,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [26 x i8] c"Enter a positive length: \00"
@.str.1 = [3 x i8] c"%d\00"
@.str.2 = [26 x i8] c"You entered length = %d\0A\0A\00"
@.str.3 = [29 x i8] c"The following error occurred\00"
@.str.4 = [36 x i8] c"\0AEnter the element to be searched: \00"
@.str.5 = [26 x i8] c"\0AElement not in the array\00"
@.str.6 = [24 x i8] c"\0AIndex of element is %d\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
br label %7
7: ; preds = %16, %0
%8 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
%9 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %2)
%10 = load i32, i32* %2
%11 = icmp sgt i32 %10, 1
br i1 %11, label %12, label %13
12: ; preds = %7
br label %17
13: ; preds = %7
%14 = load i32, i32* %2
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i32 %14)
br label %16
16: ; preds = %13
br label %7
17: ; preds = %12
%18 = load i32, i32* %2
%19 = sext i32 %18 to i64
%20 = mul i64 4, %19
%21 = trunc i64 %20 to i32
%22 = call i32* @malloc(i32 %21)
store i32* %22, i32** %3
%23 = load i32*, i32** %3
%24 = icmp eq i32* %23, null
br i1 %24, label %25, label %28
25: ; preds = %17
%26 = call i32 @perror(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0))
%27 = call i32 @exit(i32 -1)
unreachable
28: ; preds = %17
store i32 0, i32* %4
br label %29
29: ; preds = %39, %28
%30 = load i32, i32* %4
%31 = load i32, i32* %2
%32 = icmp slt i32 %30, %31
br i1 %32, label %33, label %42
33: ; preds = %29
%34 = load i32*, i32** %3
%35 = load i32, i32* %4
%36 = sext i32 %35 to i64
%37 = getelementptr inbounds i32, i32* %34, i64 %36
%38 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %37)
br label %39
39: ; preds = %33
%40 = load i32, i32* %4
%41 = add nsw i32 %40, 1
store i32 %41, i32* %4
br label %29
42: ; preds = %29
%43 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.4, i64 0, i64 0))
%44 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %5)
%45 = load i32*, i32** %3
%46 = load i32, i32* %2
%47 = load i32, i32* %5
%48 = call i32 @linear_search(i32* %45, i32 %46, i32 %47)
store i32 %48, i32* %6
%49 = load i32, i32* %6
%50 = icmp eq i32 %49, -1
br i1 %50, label %51, label %53
51: ; preds = %42
%52 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.5, i64 0, i64 0))
br label %56
53: ; preds = %42
%54 = load i32, i32* %6
%55 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.6, i64 0, i64 0), i32 %54)
br label %56
56: ; preds = %53, %51
%57 = load i32*, i32** %3
%58 = call i32 @free(i32* %57)
ret i32 0
}

LLVM IR (-O2):
@.str = [26 x i8] c"Enter a positive length: \00"
@.str.1 = [3 x i8] c"%d\00"
@.str.2 = [26 x i8] c"You entered length = %d\0A\0A\00"
@.str.3 = [29 x i8] c"The following error occurred\00"
@.str.4 = [36 x i8] c"\0AEnter the element to be searched: \00"
@.str.5 = [26 x i8] c"\0AElement not in the array\00"
@.str.6 = [24 x i8] c"\0AIndex of element is %d\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = bitcast i32* %1 to i8*
%4 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 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 = icmp sgt i32 %6, 1
br i1 %7, label %15, label %8
8: ; preds = %0, %8
%9 = phi i32 [ %13, %8 ], [ %6, %0 ]
%10 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i32 %9)
%11 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
%12 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %1)
%13 = load i32, i32* %1
%14 = icmp sgt i32 %13, 1
br i1 %14, label %15, label %8
15: ; preds = %8, %0
%16 = phi i32 [ %6, %0 ], [ %13, %8 ]
%17 = shl i32 %16, 2
%18 = call i32* @malloc(i32 %17)
%19 = icmp eq i32* %18, null
br i1 %19, label %20, label %31
20: ; preds = %15
%21 = call i32 @perror(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0))
%22 = call i32 @exit(i32 -1)
unreachable
23: ; preds = %31
%24 = bitcast i32* %2 to i8*
%25 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @.str.4, i64 0, i64 0))
%26 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %2)
%27 = load i32, i32* %1
%28 = load i32, i32* %2
%29 = call i32 @linear_search(i32* %18, i32 %27, i32 %28)
%30 = icmp eq i32 %29, -1
br i1 %30, label %39, label %41
31: ; preds = %15, %31
%32 = phi i64 [ %35, %31 ], [ 0, %15 ]
%33 = getelementptr inbounds i32, i32* %18, i64 %32
%34 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %33)
%35 = add nuw nsw i64 %32, 1
%36 = load i32, i32* %1
%37 = sext i32 %36 to i64
%38 = icmp slt i64 %35, %37
br i1 %38, label %31, label %23
39: ; preds = %23
%40 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.5, i64 0, i64 0))
br label %43
41: ; preds = %23
%42 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.6, i64 0, i64 0), i32 %29)
br label %43
43: ; preds = %41, %39
%44 = call i32 @free(i32* %18)
ret i32 0
}