#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 */
struct stat {
  int dummy;
};
typedef int /*<<< orphan*/ path;

/* Variables and functions */
int /*<<< orphan*/ PATH_MAX;
int /*<<< orphan*/ snprintf(char *, int, char *, char *, char *);
scalar_t__ stat(char *, struct stat *);
int _search(char **searchpath, int length, char *name) {
  char path[PATH_MAX + 1];
  struct stat sb;
  int i;
  for (i = 0; i < length; i++) {
    snprintf(path, sizeof(path), "%s/%s", searchpath[i], name);
    if (stat(path, &sb) == 0)
      break;
  }
  return i < length ? i : -1;
}


x86 assembly (clang, -O0):
<_search>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    0x2eb6(%rip),%rax
mov    (%rax),%eax
add    $0x1,%eax
mov    %rax,-0x48(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x20(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x40(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x28(%rbp)
movl   $0x0,-0x34(%rbp)
mov    -0x34(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    11ce <_search+0xae>
mov    -0x40(%rbp),%rdi
mov    -0x48(%rbp),%rax
mov    %eax,%esi
mov    -0x8(%rbp),%rax
movslq -0x34(%rbp),%rcx
mov    (%rax,%rcx,8),%rcx
mov    -0x18(%rbp),%r8
lea    0xe66(%rip),%rdx
call   1030 <snprintf@plt>
mov    -0x40(%rbp),%rdi
lea    -0x30(%rbp),%rsi
call   1040 <stat@plt>
cmp    $0x0,%rax
jne    11bb <_search+0x9b>
jmp    11ce <_search+0xae>
jmp    11c0 <_search+0xa0>
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    116d <_search+0x4d>
mov    -0x34(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    11e5 <_search+0xc5>
mov    -0x34(%rbp),%eax
mov    %eax,-0x4c(%rbp)
jmp    11f2 <_search+0xd2>
mov    $0xffffffff,%eax
mov    %eax,-0x4c(%rbp)
jmp    11f2 <_search+0xd2>
mov    -0x4c(%rbp),%eax
mov    -0x20(%rbp),%rcx
mov    %rcx,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_search>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdx,-0x30(%rbp)
mov    0x2eb4(%rip),%rax
mov    (%rax),%r13d
add    $0x1,%r13d
mov    %rsp,%rbx
lea    0xf(%r13),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
mov    $0xffffffff,%eax
test   %esi,%esi
jle    11ae <_search+0x8e>
mov    %rdi,%r15
mov    %esi,%r14d
xor    %r12d,%r12d
cs nopw 0x0(%rax,%rax,1)
mov    (%r15,%r12,8),%rcx
mov    %rbx,%rdi
mov    %r13d,%esi
lea    0xe7f(%rip),%rdx
mov    -0x30(%rbp),%r8
call   1030 <snprintf@plt>
mov    %rbx,%rdi
lea    -0x38(%rbp),%rsi
call   1040 <stat@plt>
test   %rax,%rax
je     11ab <_search+0x8b>
add    $0x1,%r12
cmp    %r12,%r14
jne    1170 <_search+0x50>
mov    $0xffffffff,%eax
jmp    11ae <_search+0x8e>
mov    %r12d,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@PATH_MAX = global i32 0
@.str = [6 x i8] c"%s/%s\00"
define i32 @_search(i8** %0, i32 %1, i8* %2) {
%4 = alloca i8**
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i64
%9 = alloca %struct.stat
%10 = alloca i32
store i8** %0, i8*** %4
store i32 %1, i32* %5
store i8* %2, i8** %6
%11 = load i32, i32* @PATH_MAX
%12 = add nsw i32 %11, 1
%13 = zext i32 %12 to i64
%14 = call i8* @llvm.stacksave()
store i8* %14, i8** %7
%15 = alloca i8, i64 %13
store i64 %13, i64* %8
store i32 0, i32* %10
br label %16
16: ; preds = %33, %3
%17 = load i32, i32* %10
%18 = load i32, i32* %5
%19 = icmp slt i32 %17, %18
br i1 %19, label %20, label %36
20: ; preds = %16
%21 = trunc i64 %13 to i32
%22 = load i8**, i8*** %4
%23 = load i32, i32* %10
%24 = sext i32 %23 to i64
%25 = getelementptr inbounds i8*, i8** %22, i64 %24
%26 = load i8*, i8** %25
%27 = load i8*, i8** %6
%28 = call i32 @snprintf(i8* %15, i32 %21, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %26, i8* %27)
%29 = call i64 @stat(i8* %15, %struct.stat* %9)
%30 = icmp eq i64 %29, 0
br i1 %30, label %31, label %32
31: ; preds = %20
br label %36
32: ; preds = %20
br label %33
33: ; preds = %32
%34 = load i32, i32* %10
%35 = add nsw i32 %34, 1
store i32 %35, i32* %10
br label %16
36: ; preds = %31, %16
%37 = load i32, i32* %10
%38 = load i32, i32* %5
%39 = icmp slt i32 %37, %38
br i1 %39, label %40, label %42
40: ; preds = %36
%41 = load i32, i32* %10
br label %43
42: ; preds = %36
br label %43
43: ; preds = %42, %40
%44 = phi i32 [ %41, %40 ], [ -1, %42 ]
%45 = load i8*, i8** %7
ret i32 %44
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@PATH_MAX = global i32 0
@.str = [6 x i8] c"%s/%s\00"
define i32 @_search(i8** nocapture %0, i32 %1, i8* %2) {
%4 = alloca %struct.stat
%5 = load i32, i32* @PATH_MAX
%6 = add nsw i32 %5, 1
%7 = zext i32 %6 to i64
%8 = alloca i8, i64 %7
%9 = bitcast %struct.stat* %4 to i8*
%10 = icmp sgt i32 %1, 0
br i1 %10, label %11, label %25
11: ; preds = %3
%12 = zext i32 %1 to i64
br label %13
13: ; preds = %11, %20
%14 = phi i64 [ 0, %11 ], [ %21, %20 ]
%15 = getelementptr inbounds i8*, i8** %0, i64 %14
%16 = load i8*, i8** %15
%17 = call i32 @snprintf(i8* %8, i32 %6, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %16, i8* %2)
%18 = call i64 @stat(i8* %8, %struct.stat* %4)
%19 = icmp eq i64 %18, 0
br i1 %19, label %23, label %20
20: ; preds = %13
%21 = add nuw nsw i64 %14, 1
%22 = icmp eq i64 %21, %12
br i1 %22, label %25, label %13
23: ; preds = %13
%24 = trunc i64 %14 to i32
br label %25
25: ; preds = %20, %23, %3
%26 = phi i32 [ -1, %3 ], [ %24, %23 ], [ -1, %20 ]
ret i32 %26
}