#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 close(int);
int errno;
int close_files(int fds[], int num_fds) {
  for (int index = 0; index < num_fds; ++index) {
    if (fds[index] != -1) {
      if (close(fds[index]) == -1) {
        return errno;
      }
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<close_files>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    1186 <close_files+0x76>
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
cmpl   $0xffffffff,(%rax,%rcx,4)
je     1173 <close_files+0x63>
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
mov    (%rax,%rcx,4),%edi
call   1030 <close@plt>
cmp    $0xffffffff,%eax
jne    116e <close_files+0x5e>
mov    0x2e7c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    118d <close_files+0x7d>
jmp    1173 <close_files+0x63>
jmp    1178 <close_files+0x68>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1126 <close_files+0x16>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<close_files>:
push   %r15
push   %r14
push   %rbx
test   %esi,%esi
jle    1157 <close_files+0x47>
mov    %rdi,%r14
mov    %esi,%r15d
xor    %ebx,%ebx
jmp    1139 <close_files+0x29>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
add    $0x1,%rbx
cmp    %rbx,%r15
je     1157 <close_files+0x47>
mov    (%r14,%rbx,4),%edi
cmp    $0xffffffff,%edi
je     1130 <close_files+0x20>
call   1030 <close@plt>
cmp    $0xffffffff,%eax
jne    1130 <close_files+0x20>
mov    0x2e8d(%rip),%rax
mov    (%rax),%eax
jmp    1159 <close_files+0x49>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
@errno = global i32 0
define i32 @close_files(i32* nocapture %0, i32 %1) {
%3 = icmp sgt i32 %1, 0
br i1 %3, label %4, label %19
4: ; preds = %2
%5 = zext i32 %1 to i64
br label %6
6: ; preds = %4, %16
%7 = phi i64 [ 0, %4 ], [ %17, %16 ]
%8 = getelementptr inbounds i32, i32* %0, i64 %7
%9 = load i32, i32* %8
%10 = icmp eq i32 %9, -1
br i1 %10, label %16, label %11
11: ; preds = %6
%12 = tail call i32 @close(i32 %9)
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %16
14: ; preds = %11
%15 = load i32, i32* @errno
br label %19
16: ; preds = %6, %11
%17 = add nuw nsw i64 %7, 1
%18 = icmp eq i64 %17, %5
br i1 %18, label %19, label %6
19: ; preds = %16, %2, %14
%20 = phi i32 [ %15, %14 ], [ 0, %2 ], [ 0, %16 ]
ret i32 %20
}