#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 EXIT_FAILURE;
int EXIT_SUCCESS;
int /*<<< orphan*/ print_array(int, int *);
int /*<<< orphan*/ printf(char *, ...);
int linear_search(int _n, int _array[_n], int _target) {
  int i;
  if (_n <= 0) {
    printf("Array is of invalid length!\n");
    return EXIT_FAILURE;
  }
  printf("Linear search example on ");
  print_array(_n, _array);
  printf("\n");
  for (i = 0; i < _n; i++) {
    if (_array[i] == _target) {
      printf("Found target at index %d!\n", i);
      return EXIT_SUCCESS;
    }
  }
  printf("Array doesn't contain target!\n");
  return EXIT_FAILURE;
}


x86 assembly (clang, -O0):
<linear_search>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
cmpl   $0x0,-0x8(%rbp)
jg     115b <linear_search+0x3b>
lea    0xebd(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2e8f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    11f9 <linear_search+0xd9>
lea    0xebb(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
call   1030 <print_array@plt>
lea    0xed4(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    11df <linear_search+0xbf>
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
mov    (%rax,%rcx,4),%eax
cmp    -0x14(%rbp),%eax
jne    11cc <linear_search+0xac>
mov    -0x18(%rbp),%esi
lea    0xe83(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2e16(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    11f9 <linear_search+0xd9>
jmp    11d1 <linear_search+0xb1>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    118a <linear_search+0x6a>
lea    0xe6c(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2dec(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<linear_search>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
test   %edi,%edi
jle    1198 <linear_search+0x58>
mov    %edx,%r14d
mov    %rsi,%rbx
mov    %edi,%r15d
lea    0xea5(%rip),%rdi
xor    %ebp,%ebp
xor    %eax,%eax
call   1060 <printf@plt>
mov    %r15d,%edi
mov    %rbx,%rsi
call   1050 <print_array@plt>
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    %r15d,%eax
nopl   0x0(%rax)
cmp    %r14d,(%rbx,%rbp,4)
je     11ad <linear_search+0x6d>
add    $0x1,%rbp
cmp    %rbp,%rax
jne    1180 <linear_search+0x40>
lea    0xe9f(%rip),%rdi
jmp    119f <linear_search+0x5f>
lea    0xeb4(%rip),%rdi
call   1040 <puts@plt>
mov    0x2e35(%rip),%rax
jmp    11c4 <linear_search+0x84>
lea    0xe66(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    0x2e14(%rip),%rax
mov    (%rax),%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [29 x i8] c"Array is of invalid length!\0A\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [26 x i8] c"Linear search example on \00"
@.str.2 = [2 x i8] c"\0A\00"
@.str.3 = [27 x i8] c"Found target at index %d!\0A\00"
@EXIT_SUCCESS = global i32 0
@.str.4 = [31 x i8] c"Array doesn't contain target!\0A\00"
define i32 @linear_search(i32 %0, i32* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32*
%7 = alloca i32
%8 = alloca i32
store i32 %0, i32* %5
store i32* %1, i32** %6
store i32 %2, i32* %7
%9 = load i32, i32* %5
%10 = zext i32 %9 to i64
%11 = load i32, i32* %5
%12 = icmp sle i32 %11, 0
br i1 %12, label %13, label %16
13: ; preds = %3
%14 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0))
%15 = load i32, i32* @EXIT_FAILURE
store i32 %15, i32* %4
br label %45
16: ; preds = %3
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0))
%18 = load i32, i32* %5
%19 = load i32*, i32** %6
%20 = call i32 @print_array(i32 %18, i32* %19)
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32 0, i32* %8
br label %22
22: ; preds = %39, %16
%23 = load i32, i32* %8
%24 = load i32, i32* %5
%25 = icmp slt i32 %23, %24
br i1 %25, label %26, label %42
26: ; preds = %22
%27 = load i32*, i32** %6
%28 = load i32, i32* %8
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds i32, i32* %27, i64 %29
%31 = load i32, i32* %30
%32 = load i32, i32* %7
%33 = icmp eq i32 %31, %32
br i1 %33, label %34, label %38
34: ; preds = %26
%35 = load i32, i32* %8
%36 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.3, i64 0, i64 0), i32 %35)
%37 = load i32, i32* @EXIT_SUCCESS
store i32 %37, i32* %4
br label %45
38: ; preds = %26
br label %39
39: ; preds = %38
%40 = load i32, i32* %8
%41 = add nsw i32 %40, 1
store i32 %41, i32* %8
br label %22
42: ; preds = %22
%43 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.4, i64 0, i64 0))
%44 = load i32, i32* @EXIT_FAILURE
store i32 %44, i32* %4
br label %45
45: ; preds = %42, %34, %13
%46 = load i32, i32* %4
ret i32 %46
}

LLVM IR (-O2):
@EXIT_FAILURE = global i32 0
@.str.1 = [26 x i8] c"Linear search example on \00"
@.str.3 = [27 x i8] c"Found target at index %d!\0A\00"
@EXIT_SUCCESS = global i32 0
@str = [30 x i8] c"Array doesn't contain target!\00"
@str.5 = [28 x i8] c"Array is of invalid length!\00"
define i32 @linear_search(i32 %0, i32* %1, i32 %2) {
%4 = icmp slt i32 %0, 1
br i1 %4, label %5, label %7
5: ; preds = %3
%6 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @str.5, i64 0, i64 0))
br label %25
7: ; preds = %3
%8 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0))
%9 = tail call i32 @print_array(i32 %0, i32* %1)
%10 = tail call i32 @putchar(i32 10)
%11 = zext i32 %0 to i64
br label %12
12: ; preds = %7, %20
%13 = phi i64 [ 0, %7 ], [ %21, %20 ]
%14 = getelementptr inbounds i32, i32* %1, i64 %13
%15 = load i32, i32* %14
%16 = icmp eq i32 %15, %2
br i1 %16, label %17, label %20
17: ; preds = %12
%18 = trunc i64 %13 to i32
%19 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @.str.3, i64 0, i64 0), i32 %18)
br label %25
20: ; preds = %12
%21 = add nuw nsw i64 %13, 1
%22 = icmp eq i64 %21, %11
br i1 %22, label %23, label %12
23: ; preds = %20
%24 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @str, i64 0, i64 0))
br label %25
25: ; preds = %23, %17, %5
%26 = phi i32* [ @EXIT_FAILURE, %5 ], [ @EXIT_SUCCESS, %17 ], [ @EXIT_FAILURE, %23 ]
%27 = load i32, i32* %26
ret i32 %27
}