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

/* Variables and functions */
int /*<<< orphan*/ bmp_free_list(struct bmp_handle **, int);
struct bmp_handle *bmp_open(char *);
struct bmp_handle **malloc(int);
int /*<<< orphan*/ strcat(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
struct bmp_handle **open_files(char **file_list, int to_open, char *dir) {
  int i;
  struct bmp_handle **bmp_list = malloc(to_open * sizeof(struct bmp_handle *));
  char tmp_filename[255] = {0};
  if (bmp_list == NULL) {
    return NULL;
  }
  for (i = 0; i < to_open; i++) {
    strcpy(tmp_filename, dir);
    strcat(tmp_filename, "/");
    strcat(tmp_filename, file_list[i]);
    bmp_list[i] = bmp_open(tmp_filename);
    if (bmp_list[i] == NULL) {
      bmp_free_list(bmp_list, i);
      return NULL;
    }
  }
  return bmp_list;
}


x86 assembly (clang, -O0):
<open_files>:
push   %rbp
mov    %rsp,%rbp
sub    $0x130,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
movslq -0x14(%rbp),%rax
shl    $0x3,%rax
mov    %eax,%edi
call   1060 <malloc@plt>
mov    %rax,-0x30(%rbp)
lea    -0x130(%rbp),%rdi
xor    %esi,%esi
mov    $0xff,%edx
call   1050 <memset@plt>
cmpq   $0x0,-0x30(%rbp)
jne    11b4 <open_files+0x54>
movq   $0x0,-0x8(%rbp)
jmp    1264 <open_files+0x104>
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    125c <open_files+0xfc>
lea    -0x130(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1040 <strcpy@plt>
lea    -0x130(%rbp),%rdi
lea    0xe1b(%rip),%rsi
call   1070 <strcat@plt>
lea    -0x130(%rbp),%rdi
mov    -0x10(%rbp),%rax
movslq -0x24(%rbp),%rcx
mov    (%rax,%rcx,8),%rsi
call   1070 <strcat@plt>
lea    -0x130(%rbp),%rdi
call   1030 <bmp_open@plt>
mov    %rax,%rdx
mov    -0x30(%rbp),%rax
movslq -0x24(%rbp),%rcx
mov    %rdx,(%rax,%rcx,8)
mov    -0x30(%rbp),%rax
movslq -0x24(%rbp),%rcx
cmpq   $0x0,(%rax,%rcx,8)
jne    1249 <open_files+0xe9>
mov    -0x30(%rbp),%rdi
mov    -0x24(%rbp),%esi
call   1080 <bmp_free_list@plt>
movq   $0x0,-0x8(%rbp)
jmp    1264 <open_files+0x104>
jmp    124e <open_files+0xee>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    11bb <open_files+0x5b>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x130,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_files>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x108,%rsp
mov    %rdx,%r14
mov    %esi,%ebx
mov    %rdi,%r15
lea    0x0(,%rbx,8),%edi
call   1050 <malloc@plt>
xorps  %xmm0,%xmm0
movups %xmm0,0xef(%rsp)
movaps %xmm0,0xe0(%rsp)
movaps %xmm0,0xd0(%rsp)
movaps %xmm0,0xc0(%rsp)
movaps %xmm0,0xb0(%rsp)
movaps %xmm0,0xa0(%rsp)
movaps %xmm0,0x90(%rsp)
movaps %xmm0,0x80(%rsp)
movaps %xmm0,0x70(%rsp)
movaps %xmm0,0x60(%rsp)
movaps %xmm0,0x50(%rsp)
movaps %xmm0,0x40(%rsp)
movaps %xmm0,0x30(%rsp)
movaps %xmm0,0x20(%rsp)
movaps %xmm0,0x10(%rsp)
movaps %xmm0,(%rsp)
test   %rax,%rax
je     124c <open_files+0xfc>
mov    %rax,%r12
test   %ebx,%ebx
jle    124f <open_files+0xff>
mov    %ebx,%ebp
xor    %ebx,%ebx
mov    %rsp,%r13
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %r13,%rdi
mov    %r14,%rsi
call   1040 <strcpy@plt>
mov    %r13,%rdi
lea    0xdeb(%rip),%rsi
call   1060 <strcat@plt>
mov    (%r15,%rbx,8),%rsi
mov    %r13,%rdi
call   1060 <strcat@plt>
mov    %r13,%rdi
call   1030 <bmp_open@plt>
mov    %rax,(%r12,%rbx,8)
test   %rax,%rax
je     1242 <open_files+0xf2>
add    $0x1,%rbx
cmp    %rbx,%rbp
jne    1200 <open_files+0xb0>
jmp    124f <open_files+0xff>
mov    %r12,%rdi
mov    %ebx,%esi
call   1070 <bmp_free_list@plt>
xor    %r12d,%r12d
mov    %r12,%rax
add    $0x108,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.bmp_handle = type { i32 }
@.str = [2 x i8] c"/\00"
define %struct.bmp_handle** @open_files(i8** %0, i32 %1, i8* %2) {
%4 = alloca %struct.bmp_handle**
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca %struct.bmp_handle**
%10 = alloca [255 x i8]
store i8** %0, i8*** %5
store i32 %1, i32* %6
store i8* %2, i8** %7
%11 = load i32, i32* %6
%12 = sext i32 %11 to i64
%13 = mul i64 %12, 8
%14 = trunc i64 %13 to i32
%15 = call %struct.bmp_handle** @malloc(i32 %14)
store %struct.bmp_handle** %15, %struct.bmp_handle*** %9
%16 = bitcast [255 x i8]* %10 to i8*
%17 = load %struct.bmp_handle**, %struct.bmp_handle*** %9
%18 = icmp eq %struct.bmp_handle** %17, null
br i1 %18, label %19, label %20
19: ; preds = %3
store %struct.bmp_handle** null, %struct.bmp_handle*** %4
br label %60
20: ; preds = %3
store i32 0, i32* %8
br label %21
21: ; preds = %55, %20
%22 = load i32, i32* %8
%23 = load i32, i32* %6
%24 = icmp slt i32 %22, %23
br i1 %24, label %25, label %58
25: ; preds = %21
%26 = getelementptr inbounds [255 x i8], [255 x i8]* %10, i64 0, i64 0
%27 = load i8*, i8** %7
%28 = call i32 @strcpy(i8* %26, i8* %27)
%29 = getelementptr inbounds [255 x i8], [255 x i8]* %10, i64 0, i64 0
%30 = call i32 @strcat(i8* %29, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%31 = getelementptr inbounds [255 x i8], [255 x i8]* %10, i64 0, i64 0
%32 = load i8**, i8*** %5
%33 = load i32, i32* %8
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds i8*, i8** %32, i64 %34
%36 = load i8*, i8** %35
%37 = call i32 @strcat(i8* %31, i8* %36)
%38 = getelementptr inbounds [255 x i8], [255 x i8]* %10, i64 0, i64 0
%39 = call %struct.bmp_handle* @bmp_open(i8* %38)
%40 = load %struct.bmp_handle**, %struct.bmp_handle*** %9
%41 = load i32, i32* %8
%42 = sext i32 %41 to i64
%43 = getelementptr inbounds %struct.bmp_handle*, %struct.bmp_handle** %40, i64 %42
store %struct.bmp_handle* %39, %struct.bmp_handle** %43
%44 = load %struct.bmp_handle**, %struct.bmp_handle*** %9
%45 = load i32, i32* %8
%46 = sext i32 %45 to i64
%47 = getelementptr inbounds %struct.bmp_handle*, %struct.bmp_handle** %44, i64 %46
%48 = load %struct.bmp_handle*, %struct.bmp_handle** %47
%49 = icmp eq %struct.bmp_handle* %48, null
br i1 %49, label %50, label %54
50: ; preds = %25
%51 = load %struct.bmp_handle**, %struct.bmp_handle*** %9
%52 = load i32, i32* %8
%53 = call i32 @bmp_free_list(%struct.bmp_handle** %51, i32 %52)
store %struct.bmp_handle** null, %struct.bmp_handle*** %4
br label %60
54: ; preds = %25
br label %55
55: ; preds = %54
%56 = load i32, i32* %8
%57 = add nsw i32 %56, 1
store i32 %57, i32* %8
br label %21
58: ; preds = %21
%59 = load %struct.bmp_handle**, %struct.bmp_handle*** %9
store %struct.bmp_handle** %59, %struct.bmp_handle*** %4
br label %60
60: ; preds = %58, %50, %19
%61 = load %struct.bmp_handle**, %struct.bmp_handle*** %4
ret %struct.bmp_handle** %61
}

LLVM IR (-O2):
%struct.bmp_handle = type { i32 }
@.str = [2 x i8] c"/\00"
define %struct.bmp_handle** @open_files(i8** nocapture %0, i32 %1, i8* %2) {
%4 = alloca [255 x i8]
%5 = shl i32 %1, 3
%6 = tail call %struct.bmp_handle** @malloc(i32 %5)
%7 = getelementptr inbounds [255 x i8], [255 x i8]* %4, i64 0, i64 0
%8 = icmp eq %struct.bmp_handle** %6, null
br i1 %8, label %29, label %9
9: ; preds = %3
%10 = icmp sgt i32 %1, 0
br i1 %10, label %11, label %29
11: ; preds = %9
%12 = zext i32 %1 to i64
br label %13
13: ; preds = %11, %26
%14 = phi i64 [ 0, %11 ], [ %27, %26 ]
%15 = call i32 @strcpy(i8* %7, i8* %2)
%16 = call i32 @strcat(i8* %7, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%17 = getelementptr inbounds i8*, i8** %0, i64 %14
%18 = load i8*, i8** %17
%19 = call i32 @strcat(i8* %7, i8* %18)
%20 = call %struct.bmp_handle* @bmp_open(i8* %7)
%21 = getelementptr inbounds %struct.bmp_handle*, %struct.bmp_handle** %6, i64 %14
store %struct.bmp_handle* %20, %struct.bmp_handle** %21
%22 = icmp eq %struct.bmp_handle* %20, null
br i1 %22, label %23, label %26
23: ; preds = %13
%24 = trunc i64 %14 to i32
%25 = call i32 @bmp_free_list(%struct.bmp_handle** %6, i32 %24)
br label %29
26: ; preds = %13
%27 = add nuw nsw i64 %14, 1
%28 = icmp eq i64 %27, %12
br i1 %28, label %29, label %13
29: ; preds = %26, %9, %3, %23
%30 = phi %struct.bmp_handle** [ null, %23 ], [ null, %3 ], [ %6, %9 ], [ %6, %26 ]
ret %struct.bmp_handle** %30
}