#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 */
typedef int /*<<< orphan*/ buffer;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ FILENAME_MAX;
int /*<<< orphan*/ *fopen(char const *, char const *);
int /*<<< orphan*/ snprintf(char *, int, char *, char const *, char const *);
FILE *open_file(const char *name, const char *mode, const char (*searches)[260],
                int count) {
  int i;
  for (i = 0; i < count; i++) {
    char buffer[FILENAME_MAX + 260];
    FILE *fp;
    snprintf(buffer, sizeof(buffer), "%s/%s", searches[i], name);
    if ((fp = fopen(buffer, mode)))
      return fp;
  }
  return !count ? fopen(name, mode) : NULL;
}


x86 assembly (clang, -O0):
<open_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %ecx,-0x24(%rbp)
movl   $0x0,-0x28(%rbp)
mov    -0x28(%rbp),%eax
cmp    -0x24(%rbp),%eax
jge    1204 <open_file+0xe4>
mov    0x2e97(%rip),%rax
mov    (%rax),%eax
add    $0x104,%eax
mov    %rsp,%rcx
mov    %rcx,-0x30(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,-0x50(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x38(%rbp)
mov    %eax,%esi
mov    -0x20(%rbp),%rcx
movslq -0x28(%rbp),%rax
imul   $0x104,%rax,%rax
add    %rax,%rcx
mov    -0x10(%rbp),%r8
lea    0xe66(%rip),%rdx
call   1030 <snprintf@plt>
mov    -0x50(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1040 <fopen@plt>
mov    %rax,-0x40(%rbp)
cmp    $0x0,%rax
je     11ce <open_file+0xae>
mov    -0x40(%rbp),%rax
mov    %rax,-0x8(%rbp)
movl   $0x1,-0x44(%rbp)
jmp    11d5 <open_file+0xb5>
movl   $0x0,-0x44(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,%rsp
mov    -0x44(%rbp),%eax
test   %eax,%eax
je     11f1 <open_file+0xd1>
jmp    11ec <open_file+0xcc>
jmp    1237 <open_file+0x117>
jmp    11f6 <open_file+0xd6>
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
jmp    113e <open_file+0x1e>
cmpl   $0x0,-0x24(%rbp)
jne    1224 <open_file+0x104>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1040 <fopen@plt>
mov    %rax,-0x58(%rbp)
jmp    122f <open_file+0x10f>
xor    %eax,%eax
mov    %rax,-0x58(%rbp)
jmp    122f <open_file+0x10f>
mov    -0x58(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_file>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,-0x38(%rbp)
mov    %rdi,%r12
mov    %ecx,-0x2c(%rbp)
test   %ecx,%ecx
jle    11aa <open_file+0x8a>
mov    %rdx,%rbx
mov    -0x2c(%rbp),%r13d
cs nopw 0x0(%rax,%rax,1)
mov    %rsp,%r14
mov    0x2e8e(%rip),%rax
mov    (%rax),%esi
mov    $0x104,%eax
add    %eax,%esi
lea    0xf(%rsi),%rax
and    $0xfffffffffffffff0,%rax
mov    %rsp,%r15
sub    %rax,%r15
mov    %r15,%rsp
mov    %r15,%rdi
lea    0xe82(%rip),%rdx
mov    %rbx,%rcx
mov    %r12,%r8
call   1030 <snprintf@plt>
mov    %r15,%rdi
mov    -0x38(%rbp),%rsi
call   1040 <fopen@plt>
test   %rax,%rax
mov    %r14,%rsp
jne    11c0 <open_file+0xa0>
add    $0x104,%rbx
add    $0xffffffffffffffff,%r13
jne    1150 <open_file+0x30>
cmpl   $0x0,-0x2c(%rbp)
je     11b4 <open_file+0x94>
xor    %eax,%eax
jmp    11c0 <open_file+0xa0>
mov    %r12,%rdi
mov    -0x38(%rbp),%rsi
call   1040 <fopen@plt>
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@FILENAME_MAX = global i32 0
@.str = [6 x i8] c"%s/%s\00"
define i32* @open_file(i8* %0, i8* %1, [260 x i8]* %2, i32 %3) {
%5 = alloca i32*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca [260 x i8]*
%9 = alloca i32
%10 = alloca i32
%11 = alloca i8*
%12 = alloca i64
%13 = alloca i32*
%14 = alloca i32
store i8* %0, i8** %6
store i8* %1, i8** %7
store [260 x i8]* %2, [260 x i8]** %8
store i32 %3, i32* %9
store i32 0, i32* %10
br label %15
15: ; preds = %43, %4
%16 = load i32, i32* %10
%17 = load i32, i32* %9
%18 = icmp slt i32 %16, %17
br i1 %18, label %19, label %46
19: ; preds = %15
%20 = load i32, i32* @FILENAME_MAX
%21 = add nsw i32 %20, 260
%22 = zext i32 %21 to i64
%23 = call i8* @llvm.stacksave()
store i8* %23, i8** %11
%24 = alloca i8, i64 %22
store i64 %22, i64* %12
%25 = trunc i64 %22 to i32
%26 = load [260 x i8]*, [260 x i8]** %8
%27 = load i32, i32* %10
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds [260 x i8], [260 x i8]* %26, i64 %28
%30 = getelementptr inbounds [260 x i8], [260 x i8]* %29, i64 0, i64 0
%31 = load i8*, i8** %6
%32 = call i32 @snprintf(i8* %24, i32 %25, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %30, i8* %31)
%33 = load i8*, i8** %7
%34 = call i32* @fopen(i8* %24, i8* %33)
store i32* %34, i32** %13
%35 = icmp ne i32* %34, null
br i1 %35, label %36, label %38
36: ; preds = %19
%37 = load i32*, i32** %13
store i32* %37, i32** %5
store i32 1, i32* %14
br label %39
38: ; preds = %19
store i32 0, i32* %14
br label %39
39: ; preds = %38, %36
%40 = load i8*, i8** %11
%41 = load i32, i32* %14
switch i32 %41, label %58 [
i32 0, label %42
i32 1, label %56
]
42: ; preds = %39
br label %43
43: ; preds = %42
%44 = load i32, i32* %10
%45 = add nsw i32 %44, 1
store i32 %45, i32* %10
br label %15
46: ; preds = %15
%47 = load i32, i32* %9
%48 = icmp ne i32 %47, 0
br i1 %48, label %53, label %49
49: ; preds = %46
%50 = load i8*, i8** %6
%51 = load i8*, i8** %7
%52 = call i32* @fopen(i8* %50, i8* %51)
br label %54
53: ; preds = %46
br label %54
54: ; preds = %53, %49
%55 = phi i32* [ %52, %49 ], [ null, %53 ]
store i32* %55, i32** %5
br label %56
56: ; preds = %54, %39
%57 = load i32*, i32** %5
ret i32* %57
58: ; preds = %39
unreachable
}

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