#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 */
char getchar();
int /*<<< orphan*/ pause(int *);
int /*<<< orphan*/ printArray(int, int *);
int /*<<< orphan*/ printf(char *, ...);
int linearSearch(int len, int array[], int find) {
  printf("LINEAR SEARCH for %d in ", find);
  printArray(len, array);
  printf("Press ENTER to step through, enter s to skip: ");
  int skip = getchar() == 's' ? 1 : 0;
  if (skip) {
    getchar();
  }
  char newline = skip ? '\n' : ' ';
  int i = 0;
  while (i < len) {
    printf("ind: %d, val: %d%c", i, array[i], newline);
    if (array[i] == find) {
      if (!skip) {
        printf("\n");
      }
      printf("\n%d was found at index %d!\n\n", find, i);
      return i;
    }
    pause(&skip);
    i++;
  }
  printf("\n%d was not found in the array\n\n", find);
  return -1;
}


x86 assembly (clang, -O0):
<linearSearch>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x14(%rbp),%esi
lea    0xea4(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
call   1060 <printArray@plt>
lea    0xea3(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0x0,%al
call   1040 <getchar@plt>
movsbl %al,%edx
xor    %eax,%eax
mov    $0x1,%ecx
cmp    $0x73,%edx
cmove  %ecx,%eax
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
je     11a8 <linearSearch+0x68>
mov    $0x0,%al
call   1040 <getchar@plt>
mov    -0x18(%rbp),%edx
mov    $0x20,%eax
mov    $0xa,%ecx
cmp    $0x0,%edx
cmovne %ecx,%eax
mov    %al,-0x19(%rbp)
movl   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    1253 <linearSearch+0x113>
mov    -0x20(%rbp),%esi
mov    -0x10(%rbp),%rax
movslq -0x20(%rbp),%rcx
mov    (%rax,%rcx,4),%edx
movsbl -0x19(%rbp),%ecx
lea    0xe5e(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x10(%rbp),%rax
movslq -0x20(%rbp),%rcx
mov    (%rax,%rcx,4),%eax
cmp    -0x14(%rbp),%eax
jne    123c <linearSearch+0xfc>
cmpl   $0x0,-0x18(%rbp)
jne    121d <linearSearch+0xdd>
lea    0xe60(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x14(%rbp),%esi
mov    -0x20(%rbp),%edx
lea    0xe31(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    126b <linearSearch+0x12b>
lea    -0x18(%rbp),%rdi
call   1050 <pause@plt>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    11c5 <linearSearch+0x85>
mov    -0x14(%rbp),%esi
lea    0xe1b(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<linearSearch>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,%r14d
mov    %rsi,%rbx
mov    %edi,%ebp
lea    0xe96(%rip),%rdi
mov    %edx,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
call   1070 <printArray@plt>
lea    0xe95(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
xor    %eax,%eax
call   1050 <getchar@plt>
mov    %eax,%r15d
xor    %eax,%eax
cmp    $0x73,%r15b
sete   %al
mov    %eax,0x4(%rsp)
jne    11ab <linearSearch+0x5b>
xor    %eax,%eax
call   1050 <getchar@plt>
test   %ebp,%ebp
jle    11fb <linearSearch+0xab>
cmp    $0x73,%r15b
mov    $0xa,%eax
mov    $0x20,%r15d
cmove  %eax,%r15d
mov    %ebp,%r13d
lea    0xe7c(%rip),%r12
xor    %ebp,%ebp
xchg   %ax,%ax
mov    (%rbx,%rbp,4),%edx
mov    %r12,%rdi
mov    %ebp,%esi
mov    %r15d,%ecx
xor    %eax,%eax
call   1040 <printf@plt>
cmp    %r14d,(%rbx,%rbp,4)
je     1213 <linearSearch+0xc3>
lea    0x4(%rsp),%rdi
call   1060 <pause@plt>
add    $0x1,%rbp
cmp    %rbp,%r13
jne    11d0 <linearSearch+0x80>
lea    0xe76(%rip),%rdi
mov    %r14d,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0xffffffff,%ebp
jmp    1237 <linearSearch+0xe7>
cmpl   $0x0,0x4(%rsp)
jne    1224 <linearSearch+0xd4>
mov    $0xa,%edi
call   1030 <putchar@plt>
lea    0xe30(%rip),%rdi
mov    %r14d,%esi
mov    %ebp,%edx
xor    %eax,%eax
call   1040 <printf@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [25 x i8] c"LINEAR SEARCH for %d in \00"
@.str.1 = [47 x i8] c"Press ENTER to step through, enter s to skip: \00"
@.str.2 = [19 x i8] c"ind: %d, val: %d%c\00"
@.str.3 = [2 x i8] c"\0A\00"
@.str.4 = [29 x i8] c"\0A%d was found at index %d!\0A\0A\00"
@.str.5 = [33 x i8] c"\0A%d was not found in the array\0A\0A\00"
define i32 @linearSearch(i32 %0, i32* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8
%10 = alloca i32
store i32 %0, i32* %5
store i32* %1, i32** %6
store i32 %2, i32* %7
%11 = load i32, i32* %7
%12 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i32 %11)
%13 = load i32, i32* %5
%14 = load i32*, i32** %6
%15 = call i32 @printArray(i32 %13, i32* %14)
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.1, i64 0, i64 0))
%17 = call signext i8 (...) @getchar()
%18 = sext i8 %17 to i32
%19 = icmp eq i32 %18, 115
%20 = zext i1 %19 to i64
%21 = select i1 %19, i32 1, i32 0
store i32 %21, i32* %8
%22 = load i32, i32* %8
%23 = icmp ne i32 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %3
%25 = call signext i8 (...) @getchar()
br label %26
26: ; preds = %24, %3
%27 = load i32, i32* %8
%28 = icmp ne i32 %27, 0
%29 = zext i1 %28 to i64
%30 = select i1 %28, i32 10, i32 32
%31 = trunc i32 %30 to i8
store i8 %31, i8* %9
store i32 0, i32* %10
br label %32
32: ; preds = %63, %26
%33 = load i32, i32* %10
%34 = load i32, i32* %5
%35 = icmp slt i32 %33, %34
br i1 %35, label %36, label %67
36: ; preds = %32
%37 = load i32, i32* %10
%38 = load i32*, i32** %6
%39 = load i32, i32* %10
%40 = sext i32 %39 to i64
%41 = getelementptr inbounds i32, i32* %38, i64 %40
%42 = load i32, i32* %41
%43 = load i8, i8* %9
%44 = sext i8 %43 to i32
%45 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i32 %37, i32 %42, i32 %44)
%46 = load i32*, i32** %6
%47 = load i32, i32* %10
%48 = sext i32 %47 to i64
%49 = getelementptr inbounds i32, i32* %46, i64 %48
%50 = load i32, i32* %49
%51 = load i32, i32* %7
%52 = icmp eq i32 %50, %51
br i1 %52, label %53, label %63
53: ; preds = %36
%54 = load i32, i32* %8
%55 = icmp ne i32 %54, 0
br i1 %55, label %58, label %56
56: ; preds = %53
%57 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
br label %58
58: ; preds = %56, %53
%59 = load i32, i32* %7
%60 = load i32, i32* %10
%61 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.4, i64 0, i64 0), i32 %59, i32 %60)
%62 = load i32, i32* %10
store i32 %62, i32* %4
br label %70
63: ; preds = %36
%64 = call i32 @pause(i32* %8)
%65 = load i32, i32* %10
%66 = add nsw i32 %65, 1
store i32 %66, i32* %10
br label %32
67: ; preds = %32
%68 = load i32, i32* %7
%69 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.5, i64 0, i64 0), i32 %68)
store i32 -1, i32* %4
br label %70
70: ; preds = %67, %58
%71 = load i32, i32* %4
ret i32 %71
}

LLVM IR (-O2):
@.str = [25 x i8] c"LINEAR SEARCH for %d in \00"
@.str.1 = [47 x i8] c"Press ENTER to step through, enter s to skip: \00"
@.str.2 = [19 x i8] c"ind: %d, val: %d%c\00"
@.str.4 = [29 x i8] c"\0A%d was found at index %d!\0A\0A\00"
@.str.5 = [33 x i8] c"\0A%d was not found in the array\0A\0A\00"
define i32 @linearSearch(i32 %0, i32* %1, i32 %2) {
%4 = alloca i32
%5 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i32 %2)
%6 = tail call i32 @printArray(i32 %0, i32* %1)
%7 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([47 x i8], [47 x i8]* @.str.1, i64 0, i64 0))
%8 = bitcast i32* %4 to i8*
%9 = tail call signext i8 (...) @getchar()
%10 = icmp eq i8 %9, 115
%11 = zext i1 %10 to i32
store i32 %11, i32* %4
br i1 %10, label %12, label %14
12: ; preds = %3
%13 = tail call signext i8 (...) @getchar()
br label %14
14: ; preds = %12, %3
%15 = icmp sgt i32 %0, 0
br i1 %15, label %16, label %39
16: ; preds = %14
%17 = select i1 %10, i32 10, i32 32
%18 = zext i32 %0 to i64
br label %19
19: ; preds = %16, %35
%20 = phi i64 [ 0, %16 ], [ %37, %35 ]
%21 = getelementptr inbounds i32, i32* %1, i64 %20
%22 = load i32, i32* %21
%23 = trunc i64 %20 to i32
%24 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i32 %23, i32 %22, i32 %17)
%25 = load i32, i32* %21
%26 = icmp eq i32 %25, %2
br i1 %26, label %27, label %35
27: ; preds = %19
%28 = trunc i64 %20 to i32
%29 = load i32, i32* %4
%30 = icmp eq i32 %29, 0
br i1 %30, label %31, label %33
31: ; preds = %27
%32 = call i32 @putchar(i32 10)
br label %33
33: ; preds = %31, %27
%34 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @.str.4, i64 0, i64 0), i32 %2, i32 %28)
br label %41
35: ; preds = %19
%36 = call i32 @pause(i32* %4)
%37 = add nuw nsw i64 %20, 1
%38 = icmp eq i64 %37, %18
br i1 %38, label %39, label %19
39: ; preds = %35, %14
%40 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @.str.5, i64 0, i64 0), i32 %2)
br label %41
41: ; preds = %39, %33
%42 = phi i32 [ %28, %33 ], [ -1, %39 ]
ret i32 %42
}