#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 open(char const *, int);
int try_open(const char *paths[], int mode) {
  int fd, i;
  for (i = 0; paths[i] != NULL; i++) {
    fd = open(paths[i], mode);
    if (fd != -1)
      break;
  }
  return fd;
}


x86 assembly (clang, -O0):
<try_open>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x8(%rbp),%rax
movslq -0x14(%rbp),%rcx
cmpq   $0x0,(%rax,%rcx,8)
je     1172 <try_open+0x62>
mov    -0x8(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
mov    -0xc(%rbp),%esi
call   1030 <open@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0xffffffff,-0x10(%rbp)
je     115f <try_open+0x4f>
jmp    1172 <try_open+0x62>
jmp    1164 <try_open+0x54>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1126 <try_open+0x16>
mov    -0x10(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<try_open>:
push   %rbp
push   %r14
push   %rbx
mov    %rdi,%rbx
mov    (%rdi),%rdi
mov    $0xffffffff,%r14d
test   %rdi,%rdi
je     114d <try_open+0x3d>
mov    %esi,%ebp
add    $0x8,%rbx
nopl   0x0(%rax,%rax,1)
mov    %ebp,%esi
call   1030 <open@plt>
cmp    $0xffffffff,%eax
jne    114a <try_open+0x3a>
mov    (%rbx),%rdi
add    $0x8,%rbx
test   %rdi,%rdi
jne    1130 <try_open+0x20>
jmp    114d <try_open+0x3d>
mov    %eax,%r14d
mov    %r14d,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
define i32 @try_open(i8** %0, i32 %1) {
%3 = alloca i8**
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i8** %0, i8*** %3
store i32 %1, i32* %4
store i32 0, i32* %6
br label %7
7: ; preds = %26, %2
%8 = load i8**, i8*** %3
%9 = load i32, i32* %6
%10 = sext i32 %9 to i64
%11 = getelementptr inbounds i8*, i8** %8, i64 %10
%12 = load i8*, i8** %11
%13 = icmp ne i8* %12, null
br i1 %13, label %14, label %29
14: ; preds = %7
%15 = load i8**, i8*** %3
%16 = load i32, i32* %6
%17 = sext i32 %16 to i64
%18 = getelementptr inbounds i8*, i8** %15, i64 %17
%19 = load i8*, i8** %18
%20 = load i32, i32* %4
%21 = call i32 @open(i8* %19, i32 %20)
store i32 %21, i32* %5
%22 = load i32, i32* %5
%23 = icmp ne i32 %22, -1
br i1 %23, label %24, label %25
24: ; preds = %14
br label %29
25: ; preds = %14
br label %26
26: ; preds = %25
%27 = load i32, i32* %6
%28 = add nsw i32 %27, 1
store i32 %28, i32* %6
br label %7
29: ; preds = %24, %7
%30 = load i32, i32* %5
ret i32 %30
}

LLVM IR (-O2):
define i32 @try_open(i8** nocapture %0, i32 %1) {
%3 = load i8*, i8** %0
%4 = icmp eq i8* %3, null
br i1 %4, label %15, label %9
5: ; preds = %9
%6 = getelementptr inbounds i8*, i8** %0, i64 %14
%7 = load i8*, i8** %6
%8 = icmp eq i8* %7, null
br i1 %8, label %15, label %9
9: ; preds = %2, %5
%10 = phi i64 [ %14, %5 ], [ 0, %2 ]
%11 = phi i8* [ %7, %5 ], [ %3, %2 ]
%12 = tail call i32 @open(i8* %11, i32 %1)
%13 = icmp eq i32 %12, -1
%14 = add nuw i64 %10, 1
br i1 %13, label %5, label %15
15: ; preds = %5, %9, %2
%16 = phi i32 [ -1, %2 ], [ %12, %9 ], [ -1, %5 ]
ret i32 %16
}