#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 dirent {
  int dummy;
};
typedef int /*<<< orphan*/ DIR;

/* Variables and functions */
int curslen(struct dirent **);
struct dirent **fileslast(struct dirent **);
struct dirent **malloc(int);
struct dirent *readdir(int /*<<< orphan*/ *);
struct dirent **realloc(struct dirent **, int);
struct dirent **loadDirectory(DIR *d) {
  struct dirent **files;
  struct dirent *dir;
  struct dirent **lastfile;
  if (d) {
    if ((dir = readdir(d)) != NULL) {
      files = malloc(sizeof(struct dirent *) * 2);
      *files = dir;
      *(files + 1) = NULL;
    }
    while ((dir = readdir(d)) != NULL) {
      files = realloc(files, (curslen(files) + 2) * (sizeof(struct dirent *)));
      lastfile = fileslast(files);
      *(lastfile) = dir;
      *(++lastfile) = NULL;
    }
  }
  return files;
}


x86 assembly (clang, -O0):
<loadDirectory>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
je     1221 <loadDirectory+0xd1>
mov    -0x8(%rbp),%rdi
call   1040 <readdir@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
je     11a3 <loadDirectory+0x53>
mov    $0x10,%edi
call   1050 <malloc@plt>
mov    %rax,-0x10(%rbp)
mov    -0x18(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x10(%rbp),%rax
movq   $0x0,0x8(%rax)
jmp    11a8 <loadDirectory+0x58>
mov    -0x8(%rbp),%rdi
call   1040 <readdir@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
je     121c <loadDirectory+0xcc>
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <curslen@plt>
mov    -0x28(%rbp),%rdi
add    $0x2,%eax
cltq
shl    $0x3,%rax
mov    %eax,%esi
call   1070 <realloc@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <fileslast@plt>
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x20(%rbp),%rax
mov    %rax,%rcx
add    $0x8,%rcx
mov    %rcx,-0x20(%rbp)
movq   $0x0,0x8(%rax)
jmp    11a8 <loadDirectory+0x58>
jmp    1221 <loadDirectory+0xd1>
mov    -0x10(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<loadDirectory>:
push   %r15
push   %r14
push   %rbx
test   %rdi,%rdi
je     1184 <loadDirectory+0x34>
mov    %rdi,%r14
call   1040 <readdir@plt>
test   %rax,%rax
je     1186 <loadDirectory+0x36>
mov    %rax,%rbx
mov    $0x10,%edi
call   1050 <malloc@plt>
mov    %rax,%r15
mov    %rbx,(%rax)
movq   $0x0,0x8(%rax)
jmp    1186 <loadDirectory+0x36>
jmp    11dd <loadDirectory+0x8d>
mov    %r14,%rdi
call   1040 <readdir@plt>
test   %rax,%rax
je     11dd <loadDirectory+0x8d>
mov    %rax,%rbx
cs nopw 0x0(%rax,%rax,1)
mov    %r15,%rdi
call   1060 <curslen@plt>
lea    0x10(,%rax,8),%esi
mov    %r15,%rdi
call   1070 <realloc@plt>
mov    %rax,%r15
mov    %rax,%rdi
call   1030 <fileslast@plt>
mov    %rbx,(%rax)
movq   $0x0,0x8(%rax)
mov    %r14,%rdi
call   1040 <readdir@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    11a0 <loadDirectory+0x50>
mov    %r15,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
%struct.dirent = type { i32 }
define %struct.dirent** @loadDirectory(i32* %0) {
%2 = icmp eq i32* %0, null
br i1 %2, label %24, label %3
3: ; preds = %1
%4 = tail call %struct.dirent* @readdir(i32* %0)
%5 = icmp eq %struct.dirent* %4, null
br i1 %5, label %9, label %6
6: ; preds = %3
%7 = tail call %struct.dirent** @malloc(i32 16)
store %struct.dirent* %4, %struct.dirent** %7
%8 = getelementptr inbounds %struct.dirent*, %struct.dirent** %7, i64 1
store %struct.dirent* null, %struct.dirent** %8
br label %9
9: ; preds = %6, %3
%10 = phi %struct.dirent** [ %7, %6 ], [ undef, %3 ]
%11 = tail call %struct.dirent* @readdir(i32* %0)
%12 = icmp eq %struct.dirent* %11, null
br i1 %12, label %24, label %13
13: ; preds = %9, %13
%14 = phi %struct.dirent* [ %22, %13 ], [ %11, %9 ]
%15 = phi %struct.dirent** [ %19, %13 ], [ %10, %9 ]
%16 = tail call i32 @curslen(%struct.dirent** %15)
%17 = shl i32 %16, 3
%18 = add i32 %17, 16
%19 = tail call %struct.dirent** @realloc(%struct.dirent** %15, i32 %18)
%20 = tail call %struct.dirent** @fileslast(%struct.dirent** %19)
store %struct.dirent* %14, %struct.dirent** %20
%21 = getelementptr inbounds %struct.dirent*, %struct.dirent** %20, i64 1
store %struct.dirent* null, %struct.dirent** %21
%22 = tail call %struct.dirent* @readdir(i32* %0)
%23 = icmp eq %struct.dirent* %22, null
br i1 %23, label %24, label %13
24: ; preds = %13, %9, %1
%25 = phi %struct.dirent** [ undef, %1 ], [ %10, %9 ], [ %19, %13 ]
ret %struct.dirent** %25
}