#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 file {
  int dummy;
};

/* Variables and functions */
struct file *op_open(unsigned int, unsigned int, int /*<<< orphan*/);
void openfiles(unsigned testcode, struct file **files, unsigned opennum,
               unsigned totnum) {
  unsigned i;
  for (i = 0; i < opennum; i++) {
    files[i] = op_open(testcode + 1, i, 0);
  }
  for (; i < totnum; i++) {
    files[i] = NULL;
  }
}


x86 assembly (clang, -O0):
<openfiles>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jae    1164 <openfiles+0x54>
mov    -0x4(%rbp),%edi
add    $0x1,%edi
mov    -0x1c(%rbp),%esi
xor    %edx,%edx
call   1030 <op_open@plt>
mov    %rax,%rdx
mov    -0x10(%rbp),%rax
mov    -0x1c(%rbp),%ecx
mov    %rdx,(%rax,%rcx,8)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    112c <openfiles+0x1c>
jmp    1169 <openfiles+0x59>
mov    -0x1c(%rbp),%eax
cmp    -0x18(%rbp),%eax
jae    1192 <openfiles+0x82>
mov    -0x10(%rbp),%rax
mov    -0x1c(%rbp),%ecx
movq   $0x0,(%rax,%rcx,8)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1169 <openfiles+0x59>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<openfiles>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %ecx,%r14d
mov    %edx,%r15d
mov    %rsi,%r12
mov    %edx,%r13d
test   %edx,%edx
je     1168 <openfiles+0x48>
mov    %edi,%ebx
add    $0x1,%ebx
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %ebx,%edi
mov    %ebp,%esi
xor    %edx,%edx
call   1040 <op_open@plt>
mov    %rax,(%r12,%rbp,8)
add    $0x1,%rbp
cmp    %rbp,%r13
jne    1150 <openfiles+0x30>
cmp    %r14d,%r15d
jae    1186 <openfiles+0x66>
lea    (%r12,%r13,8),%rdi
not    %r15d
add    %r14d,%r15d
lea    0x8(,%r15,8),%rdx
xor    %esi,%esi
call   1030 <memset@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.file = type { i32 }
define void @openfiles(i32 %0, %struct.file** %1, i32 %2, i32 %3) {
%5 = alloca i32
%6 = alloca %struct.file**
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i32 %0, i32* %5
store %struct.file** %1, %struct.file*** %6
store i32 %2, i32* %7
store i32 %3, i32* %8
store i32 0, i32* %9
br label %10
10: ; preds = %23, %4
%11 = load i32, i32* %9
%12 = load i32, i32* %7
%13 = icmp ult i32 %11, %12
br i1 %13, label %14, label %26
14: ; preds = %10
%15 = load i32, i32* %5
%16 = add i32 %15, 1
%17 = load i32, i32* %9
%18 = call %struct.file* @op_open(i32 %16, i32 %17, i32 0)
%19 = load %struct.file**, %struct.file*** %6
%20 = load i32, i32* %9
%21 = zext i32 %20 to i64
%22 = getelementptr inbounds %struct.file*, %struct.file** %19, i64 %21
store %struct.file* %18, %struct.file** %22
br label %23
23: ; preds = %14
%24 = load i32, i32* %9
%25 = add i32 %24, 1
store i32 %25, i32* %9
br label %10
26: ; preds = %10
br label %27
27: ; preds = %36, %26
%28 = load i32, i32* %9
%29 = load i32, i32* %8
%30 = icmp ult i32 %28, %29
br i1 %30, label %31, label %39
31: ; preds = %27
%32 = load %struct.file**, %struct.file*** %6
%33 = load i32, i32* %9
%34 = zext i32 %33 to i64
%35 = getelementptr inbounds %struct.file*, %struct.file** %32, i64 %34
store %struct.file* null, %struct.file** %35
br label %36
36: ; preds = %31
%37 = load i32, i32* %9
%38 = add i32 %37, 1
store i32 %38, i32* %9
br label %27
39: ; preds = %27
ret void
}

LLVM IR (-O2):
%struct.file = type { i32 }
define void @openfiles(i32 %0, %struct.file** nocapture writeonly %1, i32 %2, i32 %3) {
%5 = icmp eq i32 %2, 0
br i1 %5, label %9, label %6
6: ; preds = %4
%7 = add i32 %0, 1
%8 = zext i32 %2 to i64
br label %20
9: ; preds = %20, %4
%10 = icmp ult i32 %2, %3
br i1 %10, label %11, label %27
11: ; preds = %9
%12 = zext i32 %2 to i64
%13 = getelementptr %struct.file*, %struct.file** %1, i64 %12
%14 = bitcast %struct.file** %13 to i8*
%15 = xor i32 %2, -1
%16 = add i32 %15, %3
%17 = zext i32 %16 to i64
%18 = shl nuw nsw i64 %17, 3
%19 = add nuw nsw i64 %18, 8
br label %27
20: ; preds = %6, %20
%21 = phi i64 [ 0, %6 ], [ %25, %20 ]
%22 = trunc i64 %21 to i32
%23 = tail call %struct.file* @op_open(i32 %7, i32 %22, i32 0)
%24 = getelementptr inbounds %struct.file*, %struct.file** %1, i64 %21
store %struct.file* %23, %struct.file** %24
%25 = add nuw nsw i64 %21, 1
%26 = icmp eq i64 %25, %8
br i1 %26, label %9, label %20
27: ; preds = %11, %9
ret void
}