#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 linearSearch(int *, int, int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ scanf(char *, int *);
int main(void) {
  int i = 0, len = 0, key = 0, pos = 0;
  printf("How many elements would you like on your list?\n");
  scanf("%d", &len);
  len += 1;
  int input[len];
  printf("Please enter the elements.\n");
  for (i = 0; i < len - 1; ++i)
    scanf("%d", &input[i]);
  printf("The elements in the list are ");
  for (i = 0; i < len - 1; i++)
    printf("%d ", input[i]);
  printf("\n");
  printf("Enter the key value to search for:\n");
  scanf("%d", &key);
  input[len] = key;
  pos = linearSearch(input, len, key);
  if (pos == -1)
    printf("Search for the key %d failed!\n", key);
  else
    printf("Key %d was found in position %d.\n", key, pos);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
lea    0xe9e(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xec0(%rip),%rdi
lea    -0xc(%rbp),%rsi
call   1050 <scanf@plt>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%eax
mov    %rsp,%rcx
mov    %rcx,-0x20(%rbp)
lea    0xf(,%rax,4),%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x30(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x28(%rbp)
lea    0xe83(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%eax
mov    -0xc(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jge    11f8 <main+0xc8>
mov    -0x30(%rbp),%rsi
movslq -0x8(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rsi
lea    0xe4b(%rip),%rdi
call   1050 <scanf@plt>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    11be <main+0x8e>
lea    0xe50(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%eax
mov    -0xc(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jge    1245 <main+0x115>
mov    -0x30(%rbp),%rax
movslq -0x8(%rbp),%rcx
mov    (%rax,%rcx,4),%esi
lea    0xe3d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    120d <main+0xdd>
lea    0xe66(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe17(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xdc8(%rip),%rdi
lea    -0x10(%rbp),%rsi
call   1050 <scanf@plt>
mov    -0x30(%rbp),%rdi
mov    -0x10(%rbp),%ecx
movslq -0xc(%rbp),%rax
mov    %ecx,(%rdi,%rax,4)
mov    -0xc(%rbp),%esi
mov    -0x10(%rbp),%edx
call   1030 <linearSearch@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    12ad <main+0x17d>
mov    -0x10(%rbp),%esi
lea    0xdf4(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    12c1 <main+0x191>
mov    -0x10(%rbp),%esi
mov    -0x14(%rbp),%edx
lea    0xdfa(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x20(%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
movl   $0x0,-0x2c(%rbp)
movl   $0x0,-0x30(%rbp)
lea    0xef3(%rip),%rdi
call   1050 <puts@plt>
lea    0xe81(%rip),%rdi
lea    -0x2c(%rbp),%rsi
call   1070 <scanf@plt>
mov    %rsp,%r12
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
mov    %rsp,%r15
lea    0xf(,%rax,4),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r15
mov    %r15,%rsp
lea    0xee5(%rip),%rdi
call   1050 <puts@plt>
cmpl   $0x2,-0x2c(%rbp)
jl     11f0 <main+0xa0>
xor    %r13d,%r13d
lea    0xe3b(%rip),%r14
mov    %r15,%rbx
nopl   0x0(%rax,%rax,1)
mov    %r14,%rdi
mov    %rbx,%rsi
call   1070 <scanf@plt>
add    $0x1,%r13
movslq -0x2c(%rbp),%rax
add    $0xffffffffffffffff,%rax
add    $0x4,%rbx
cmp    %rax,%r13
jl     11d0 <main+0x80>
lea    0xe0c(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
cmpl   $0x2,-0x2c(%rbp)
jl     122f <main+0xdf>
lea    0xe16(%rip),%r14
xor    %ebx,%ebx
nopl   (%rax)
mov    (%r15,%rbx,4),%esi
mov    %r14,%rdi
xor    %eax,%eax
call   1060 <printf@plt>
add    $0x1,%rbx
movslq -0x2c(%rbp),%rax
add    $0xffffffffffffffff,%rax
cmp    %rax,%rbx
jl     1210 <main+0xc0>
mov    $0xa,%edi
call   1040 <putchar@plt>
lea    0xe70(%rip),%rdi
call   1050 <puts@plt>
lea    0xdb4(%rip),%rdi
lea    -0x30(%rbp),%rsi
call   1070 <scanf@plt>
mov    -0x30(%rbp),%edx
movslq -0x2c(%rbp),%rsi
mov    %edx,(%r15,%rsi,4)
mov    %r15,%rdi
call   1030 <linearSearch@plt>
mov    -0x30(%rbp),%esi
cmp    $0xffffffff,%eax
je     1282 <main+0x132>
lea    0xdcd(%rip),%rdi
mov    %eax,%edx
xor    %eax,%eax
call   1060 <printf@plt>
jmp    1290 <main+0x140>
lea    0xd9c(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@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 = [48 x i8] c"How many elements would you like on your list?\0A\00"
@.str.1 = [3 x i8] c"%d\00"
@.str.2 = [28 x i8] c"Please enter the elements.\0A\00"
@.str.3 = [30 x i8] c"The elements in the list are \00"
@.str.4 = [4 x i8] c"%d \00"
@.str.5 = [2 x i8] c"\0A\00"
@.str.6 = [36 x i8] c"Enter the key value to search for:\0A\00"
@.str.7 = [31 x i8] c"Search for the key %d failed!\0A\00"
@.str.8 = [34 x i8] c"Key %d was found in position %d.\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
store i32 0, i32* %1
store i32 0, i32* %2
store i32 0, i32* %3
store i32 0, i32* %4
store i32 0, i32* %5
%8 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([48 x i8], [48 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* %3)
%10 = load i32, i32* %3
%11 = add nsw i32 %10, 1
store i32 %11, i32* %3
%12 = load i32, i32* %3
%13 = zext i32 %12 to i64
%14 = call i8* @llvm.stacksave()
store i8* %14, i8** %6
%15 = alloca i32, i64 %13
store i64 %13, i64* %7
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0))
store i32 0, i32* %2
br label %17
17: ; preds = %27, %0
%18 = load i32, i32* %2
%19 = load i32, i32* %3
%20 = sub nsw i32 %19, 1
%21 = icmp slt i32 %18, %20
br i1 %21, label %22, label %30
22: ; preds = %17
%23 = load i32, i32* %2
%24 = sext i32 %23 to i64
%25 = getelementptr inbounds i32, i32* %15, i64 %24
%26 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %25)
br label %27
27: ; preds = %22
%28 = load i32, i32* %2
%29 = add nsw i32 %28, 1
store i32 %29, i32* %2
br label %17
30: ; preds = %17
%31 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.3, i64 0, i64 0))
store i32 0, i32* %2
br label %32
32: ; preds = %43, %30
%33 = load i32, i32* %2
%34 = load i32, i32* %3
%35 = sub nsw i32 %34, 1
%36 = icmp slt i32 %33, %35
br i1 %36, label %37, label %46
37: ; preds = %32
%38 = load i32, i32* %2
%39 = sext i32 %38 to i64
%40 = getelementptr inbounds i32, i32* %15, i64 %39
%41 = load i32, i32* %40
%42 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i32 %41)
br label %43
43: ; preds = %37
%44 = load i32, i32* %2
%45 = add nsw i32 %44, 1
store i32 %45, i32* %2
br label %32
46: ; preds = %32
%47 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
%48 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.6, i64 0, i64 0))
%49 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %4)
%50 = load i32, i32* %4
%51 = load i32, i32* %3
%52 = sext i32 %51 to i64
%53 = getelementptr inbounds i32, i32* %15, i64 %52
store i32 %50, i32* %53
%54 = load i32, i32* %3
%55 = load i32, i32* %4
%56 = call i32 @linearSearch(i32* %15, i32 %54, i32 %55)
store i32 %56, i32* %5
%57 = load i32, i32* %5
%58 = icmp eq i32 %57, -1
br i1 %58, label %59, label %62
59: ; preds = %46
%60 = load i32, i32* %4
%61 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.7, i64 0, i64 0), i32 %60)
br label %66
62: ; preds = %46
%63 = load i32, i32* %4
%64 = load i32, i32* %5
%65 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.8, i64 0, i64 0), i32 %63, i32 %64)
br label %66
66: ; preds = %62, %59
store i32 0, i32* %1
%67 = load i8*, i8** %6
%68 = load i32, i32* %1
ret i32 %68
}

LLVM IR (-O2):
@.str.1 = [3 x i8] c"%d\00"
@.str.3 = [30 x i8] c"The elements in the list are \00"
@.str.4 = [4 x i8] c"%d \00"
@.str.7 = [31 x i8] c"Search for the key %d failed!\0A\00"
@.str.8 = [34 x i8] c"Key %d was found in position %d.\0A\00"
@str = [47 x i8] c"How many elements would you like on your list?\00"
@str.9 = [27 x i8] c"Please enter the elements.\00"
@str.10 = [35 x i8] c"Enter the key value to search for:\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = bitcast i32* %1 to i8*
store i32 0, i32* %1
%4 = bitcast i32* %2 to i8*
store i32 0, i32* %2
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([47 x i8], [47 x i8]* @str, i64 0, i64 0))
%6 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %1)
%7 = load i32, i32* %1
%8 = add nsw i32 %7, 1
store i32 %8, i32* %1
%9 = zext i32 %8 to i64
%10 = call i8* @llvm.stacksave()
%11 = alloca i32, i64 %9
%12 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @str.9, i64 0, i64 0))
%13 = load i32, i32* %1
%14 = icmp sgt i32 %13, 1
br i1 %14, label %15, label %24
15: ; preds = %0, %15
%16 = phi i64 [ %19, %15 ], [ 0, %0 ]
%17 = getelementptr inbounds i32, i32* %11, i64 %16
%18 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %17)
%19 = add nuw nsw i64 %16, 1
%20 = load i32, i32* %1
%21 = add nsw i32 %20, -1
%22 = sext i32 %21 to i64
%23 = icmp slt i64 %19, %22
br i1 %23, label %15, label %24
24: ; preds = %15, %0
%25 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @.str.3, i64 0, i64 0))
%26 = load i32, i32* %1
%27 = icmp sgt i32 %26, 1
br i1 %27, label %28, label %38
28: ; preds = %24, %28
%29 = phi i64 [ %33, %28 ], [ 0, %24 ]
%30 = getelementptr inbounds i32, i32* %11, i64 %29
%31 = load i32, i32* %30
%32 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i32 %31)
%33 = add nuw nsw i64 %29, 1
%34 = load i32, i32* %1
%35 = add nsw i32 %34, -1
%36 = sext i32 %35 to i64
%37 = icmp slt i64 %33, %36
br i1 %37, label %28, label %38
38: ; preds = %28, %24
%39 = call i32 @putchar(i32 10)
%40 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @str.10, i64 0, i64 0))
%41 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %2)
%42 = load i32, i32* %2
%43 = load i32, i32* %1
%44 = sext i32 %43 to i64
%45 = getelementptr inbounds i32, i32* %11, i64 %44
store i32 %42, i32* %45
%46 = call i32 @linearSearch(i32* %11, i32 %43, i32 %42)
%47 = icmp eq i32 %46, -1
%48 = load i32, i32* %2
br i1 %47, label %49, label %51
49: ; preds = %38
%50 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @.str.7, i64 0, i64 0), i32 %48)
br label %53
51: ; preds = %38
%52 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @.str.8, i64 0, i64 0), i32 %48, i32 %46)
br label %53
53: ; preds = %51, %49
ret i32 0
}