#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*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int validate_files(char **files, int count) {
  FILE *f;
  int i;
  for (i = 0; i < count; i++) {
    if (NULL == (f = fopen(files[i], "rb"))) {
      return i;
    }
    fclose(f);
  }
  return -1;
}


x86 assembly (clang, -O0):
<validate_files>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    118e <validate_files+0x6e>
mov    -0x10(%rbp),%rax
movslq -0x24(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
lea    0xeab(%rip),%rsi
call   1040 <fopen@plt>
mov    %rax,%rcx
mov    %rcx,-0x20(%rbp)
xor    %eax,%eax
cmp    %rcx,%rax
jne    1177 <validate_files+0x57>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1195 <validate_files+0x75>
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    1136 <validate_files+0x16>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<validate_files>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    $0xffffffff,%r14d
test   %esi,%esi
jle    1167 <validate_files+0x47>
mov    %rdi,%r12
mov    %esi,%ebx
xor    %ebp,%ebp
lea    0xec0(%rip),%r15
mov    (%r12,%rbp,8),%rdi
mov    %r15,%rsi
call   1040 <fopen@plt>
test   %rax,%rax
je     1164 <validate_files+0x44>
mov    %rax,%rdi
call   1030 <fclose@plt>
add    $0x1,%rbp
cmp    %rbp,%rbx
jne    1140 <validate_files+0x20>
jmp    1167 <validate_files+0x47>
mov    %ebp,%r14d
mov    %r14d,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"rb\00"
define i32 @validate_files(i8** %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8**
%5 = alloca i32
%6 = alloca i32*
%7 = alloca i32
store i8** %0, i8*** %4
store i32 %1, i32* %5
store i32 0, i32* %7
br label %8
8: ; preds = %25, %2
%9 = load i32, i32* %7
%10 = load i32, i32* %5
%11 = icmp slt i32 %9, %10
br i1 %11, label %12, label %28
12: ; preds = %8
%13 = load i8**, i8*** %4
%14 = load i32, i32* %7
%15 = sext i32 %14 to i64
%16 = getelementptr inbounds i8*, i8** %13, i64 %15
%17 = load i8*, i8** %16
%18 = call i32* @fopen(i8* %17, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %18, i32** %6
%19 = icmp eq i32* null, %18
br i1 %19, label %20, label %22
20: ; preds = %12
%21 = load i32, i32* %7
store i32 %21, i32* %3
br label %29
22: ; preds = %12
%23 = load i32*, i32** %6
%24 = call i32 @fclose(i32* %23)
br label %25
25: ; preds = %22
%26 = load i32, i32* %7
%27 = add nsw i32 %26, 1
store i32 %27, i32* %7
br label %8
28: ; preds = %8
store i32 -1, i32* %3
br label %29
29: ; preds = %28, %20
%30 = load i32, i32* %3
ret i32 %30
}

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
define i32 @validate_files(i8** nocapture %0, i32 %1) {
%3 = icmp sgt i32 %1, 0
br i1 %3, label %4, label %18
4: ; preds = %2
%5 = zext i32 %1 to i64
br label %6
6: ; preds = %4, %12
%7 = phi i64 [ 0, %4 ], [ %14, %12 ]
%8 = getelementptr inbounds i8*, i8** %0, i64 %7
%9 = load i8*, i8** %8
%10 = tail call i32* @fopen(i8* %9, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%11 = icmp eq i32* %10, null
br i1 %11, label %16, label %12
12: ; preds = %6
%13 = tail call i32 @fclose(i32* %10)
%14 = add nuw nsw i64 %7, 1
%15 = icmp eq i64 %14, %5
br i1 %15, label %18, label %6
16: ; preds = %6
%17 = trunc i64 %7 to i32
br label %18
18: ; preds = %12, %16, %2
%19 = phi i32 [ -1, %2 ], [ %17, %16 ], [ -1, %12 ]
ret i32 %19
}