#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 /*<<< orphan*/ d_name;
};

/* Variables and functions */
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ free(char *);
char *malloc(scalar_t__);
int /*<<< orphan*/ stderr;
int strcoll(char *, char *);
scalar_t__ strlen(int /*<<< orphan*/);
int /*<<< orphan*/ strtolower(int /*<<< orphan*/, char *);
int ialphasort(const void *a, const void *b) {
  struct dirent **a2 = (struct dirent **)a;
  struct dirent **b2 = (struct dirent **)b;
  char *a_lower = malloc(strlen((*a2)->d_name) + 1);
  char *b_lower = malloc(strlen((*b2)->d_name) + 1);
  if (a_lower == NULL || b_lower == NULL) {
    fprintf(stderr, "unable to allocate memory\n");
    exit(1);
  }
  strtolower((*a2)->d_name, a_lower);
  strtolower((*b2)->d_name, b_lower);
  int result = strcoll(a_lower, b_lower);
  free(a_lower);
  free(b_lower);
  return result;
}


x86 assembly (clang, -O0):
<ialphasort>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
mov    (%rax),%edi
call   1040 <strlen@plt>
mov    %rax,%rdi
add    $0x1,%rdi
call   1070 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
mov    (%rax),%edi
call   1040 <strlen@plt>
mov    %rax,%rdi
add    $0x1,%rdi
call   1070 <malloc@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     11e2 <ialphasort+0x72>
cmpq   $0x0,-0x30(%rbp)
jne    1201 <ialphasort+0x91>
mov    0x2df7(%rip),%rax
mov    (%rax),%edi
lea    0xe0e(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
mov    (%rax),%edi
mov    -0x28(%rbp),%rsi
call   1050 <strtolower@plt>
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
mov    (%rax),%edi
mov    -0x30(%rbp),%rsi
call   1050 <strtolower@plt>
mov    -0x28(%rbp),%rdi
mov    -0x30(%rbp),%rsi
call   1080 <strcoll@plt>
mov    %eax,-0x34(%rbp)
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    -0x34(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ialphasort>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rsi,%r14
mov    %rdi,%r15
mov    (%rdi),%rax
mov    (%rax),%edi
call   1040 <strlen@plt>
lea    0x1(%rax),%rdi
call   1070 <malloc@plt>
mov    %rax,%r12
mov    (%r14),%rax
mov    (%rax),%edi
call   1040 <strlen@plt>
lea    0x1(%rax),%rdi
call   1070 <malloc@plt>
test   %r12,%r12
je     11f6 <ialphasort+0x86>
mov    %rax,%rbx
test   %rax,%rax
je     11f6 <ialphasort+0x86>
mov    (%r15),%rax
mov    (%rax),%edi
mov    %r12,%rsi
call   1050 <strtolower@plt>
mov    (%r14),%rax
mov    (%rax),%edi
mov    %rbx,%rsi
call   1050 <strtolower@plt>
mov    %r12,%rdi
mov    %rbx,%rsi
call   1080 <strcoll@plt>
mov    %eax,%ebp
mov    %r12,%rdi
call   1030 <free@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %ebp,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2de3(%rip),%rax
mov    (%rax),%edi
lea    0xdfa(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
%struct.dirent = type { i32 }
@stderr = global i32 0
@.str = [27 x i8] c"unable to allocate memory\0A\00"
define i32 @ialphasort(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca %struct.dirent**
%6 = alloca %struct.dirent**
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i32
store i8* %0, i8** %3
store i8* %1, i8** %4
%10 = load i8*, i8** %3
%11 = bitcast i8* %10 to %struct.dirent**
store %struct.dirent** %11, %struct.dirent*** %5
%12 = load i8*, i8** %4
%13 = bitcast i8* %12 to %struct.dirent**
store %struct.dirent** %13, %struct.dirent*** %6
%14 = load %struct.dirent**, %struct.dirent*** %5
%15 = load %struct.dirent*, %struct.dirent** %14
%16 = getelementptr inbounds %struct.dirent, %struct.dirent* %15, i32 0, i32 0
%17 = load i32, i32* %16
%18 = call i64 @strlen(i32 %17)
%19 = add nsw i64 %18, 1
%20 = call i8* @malloc(i64 %19)
store i8* %20, i8** %7
%21 = load %struct.dirent**, %struct.dirent*** %6
%22 = load %struct.dirent*, %struct.dirent** %21
%23 = getelementptr inbounds %struct.dirent, %struct.dirent* %22, i32 0, i32 0
%24 = load i32, i32* %23
%25 = call i64 @strlen(i32 %24)
%26 = add nsw i64 %25, 1
%27 = call i8* @malloc(i64 %26)
store i8* %27, i8** %8
%28 = load i8*, i8** %7
%29 = icmp eq i8* %28, null
br i1 %29, label %33, label %30
30: ; preds = %2
%31 = load i8*, i8** %8
%32 = icmp eq i8* %31, null
br i1 %32, label %33, label %37
33: ; preds = %30, %2
%34 = load i32, i32* @stderr
%35 = call i32 @fprintf(i32 %34, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
%36 = call i32 @exit(i32 1)
unreachable
37: ; preds = %30
%38 = load %struct.dirent**, %struct.dirent*** %5
%39 = load %struct.dirent*, %struct.dirent** %38
%40 = getelementptr inbounds %struct.dirent, %struct.dirent* %39, i32 0, i32 0
%41 = load i32, i32* %40
%42 = load i8*, i8** %7
%43 = call i32 @strtolower(i32 %41, i8* %42)
%44 = load %struct.dirent**, %struct.dirent*** %6
%45 = load %struct.dirent*, %struct.dirent** %44
%46 = getelementptr inbounds %struct.dirent, %struct.dirent* %45, i32 0, i32 0
%47 = load i32, i32* %46
%48 = load i8*, i8** %8
%49 = call i32 @strtolower(i32 %47, i8* %48)
%50 = load i8*, i8** %7
%51 = load i8*, i8** %8
%52 = call i32 @strcoll(i8* %50, i8* %51)
store i32 %52, i32* %9
%53 = load i8*, i8** %7
%54 = call i32 @free(i8* %53)
%55 = load i8*, i8** %8
%56 = call i32 @free(i8* %55)
%57 = load i32, i32* %9
ret i32 %57
}

LLVM IR (-O2):
%struct.dirent = type { i32 }
@stderr = global i32 0
@.str = [27 x i8] c"unable to allocate memory\0A\00"
define i32 @ialphasort(i8* nocapture %0, i8* nocapture %1) {
%3 = bitcast i8* %0 to %struct.dirent**
%4 = bitcast i8* %1 to %struct.dirent**
%5 = load %struct.dirent*, %struct.dirent** %3
%6 = getelementptr inbounds %struct.dirent, %struct.dirent* %5, i64 0, i32 0
%7 = load i32, i32* %6
%8 = tail call i64 @strlen(i32 %7)
%9 = add nsw i64 %8, 1
%10 = tail call i8* @malloc(i64 %9)
%11 = load %struct.dirent*, %struct.dirent** %4
%12 = getelementptr inbounds %struct.dirent, %struct.dirent* %11, i64 0, i32 0
%13 = load i32, i32* %12
%14 = tail call i64 @strlen(i32 %13)
%15 = add nsw i64 %14, 1
%16 = tail call i8* @malloc(i64 %15)
%17 = icmp eq i8* %10, null
%18 = icmp eq i8* %16, null
%19 = or i1 %17, %18
br i1 %19, label %20, label %24
20: ; preds = %2
%21 = load i32, i32* @stderr
%22 = tail call i32 @fprintf(i32 %21, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
%23 = tail call i32 @exit(i32 1)
unreachable
24: ; preds = %2
%25 = load %struct.dirent*, %struct.dirent** %3
%26 = getelementptr inbounds %struct.dirent, %struct.dirent* %25, i64 0, i32 0
%27 = load i32, i32* %26
%28 = tail call i32 @strtolower(i32 %27, i8* %10)
%29 = load %struct.dirent*, %struct.dirent** %4
%30 = getelementptr inbounds %struct.dirent, %struct.dirent* %29, i64 0, i32 0
%31 = load i32, i32* %30
%32 = tail call i32 @strtolower(i32 %31, i8* %16)
%33 = tail call i32 @strcoll(i8* %10, i8* %16)
%34 = tail call i32 @free(i8* %10)
%35 = tail call i32 @free(i8* %16)
ret i32 %33
}