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

/* Variables and functions */
int /*<<< orphan*/ alphasort;
int /*<<< orphan*/ free(int /*<<< orphan*/ ******);
int scandir(char *, int /*<<< orphan*/ *******, int /*<<< orphan*/ *,
            int /*<<< orphan*/);
void no_memleak_scandir(void) {
  struct dirent **namelist;
  int n = scandir(".", &namelist, NULL, alphasort);
  if (n == -1) {
    return;
  }
  while (n--) {
    free(namelist[n]);
  }
  free(namelist);
}


x86 assembly (clang, -O0):
<no_memleak_scandir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
lea    -0x8(%rbp),%rsi
mov    0x2ead(%rip),%rax
mov    (%rax),%ecx
lea    0xec4(%rip),%rdi
xor    %eax,%eax
mov    %eax,%edx
call   1040 <scandir@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    1157 <no_memleak_scandir+0x37>
jmp    118f <no_memleak_scandir+0x6f>
jmp    115c <no_memleak_scandir+0x3c>
mov    -0xc(%rbp),%eax
mov    %eax,%ecx
add    $0xffffffff,%ecx
mov    %ecx,-0xc(%rbp)
cmp    $0x0,%eax
je     1186 <no_memleak_scandir+0x66>
mov    -0x8(%rbp),%rax
movslq -0xc(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
call   1030 <free@plt>
jmp    115c <no_memleak_scandir+0x3c>
mov    -0x8(%rbp),%rdi
call   1030 <free@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<no_memleak_scandir>:
push   %rbp
push   %rbx
push   %rax
mov    0x2eb6(%rip),%rax
mov    (%rax),%ecx
lea    0xecd(%rip),%rdi
mov    %rsp,%rsi
xor    %edx,%edx
call   1040 <scandir@plt>
cmp    $0xffffffff,%eax
je     116f <no_memleak_scandir+0x4f>
mov    %eax,%ebp
test   %eax,%eax
je     1166 <no_memleak_scandir+0x46>
movslq %ebp,%rbx
add    $0xffffffffffffffff,%rbx
nop
mov    (%rsp),%rax
mov    (%rax,%rbx,8),%rdi
call   1030 <free@plt>
add    $0xffffffffffffffff,%rbx
add    $0xffffffff,%ebp
jne    1150 <no_memleak_scandir+0x30>
mov    (%rsp),%rdi
call   1030 <free@plt>
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.dirent = type opaque
@.str = [2 x i8] c".\00"
@alphasort = global i32 0
define void @no_memleak_scandir() {
%1 = alloca %struct.dirent**
%2 = alloca i32
%3 = bitcast %struct.dirent*** %1 to i32*******
%4 = load i32, i32* @alphasort
%5 = call i32 @scandir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i32******* %3, i32* null, i32 %4)
store i32 %5, i32* %2
%6 = load i32, i32* %2
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %9
8: ; preds = %0
br label %26
9: ; preds = %0
br label %10
10: ; preds = %14, %9
%11 = load i32, i32* %2
%12 = add nsw i32 %11, -1
store i32 %12, i32* %2
%13 = icmp ne i32 %11, 0
br i1 %13, label %14, label %22
14: ; preds = %10
%15 = load %struct.dirent**, %struct.dirent*** %1
%16 = load i32, i32* %2
%17 = sext i32 %16 to i64
%18 = getelementptr inbounds %struct.dirent*, %struct.dirent** %15, i64 %17
%19 = load %struct.dirent*, %struct.dirent** %18
%20 = bitcast %struct.dirent* %19 to i32******
%21 = call i32 @free(i32****** %20)
br label %10
22: ; preds = %10
%23 = load %struct.dirent**, %struct.dirent*** %1
%24 = bitcast %struct.dirent** %23 to i32******
%25 = call i32 @free(i32****** %24)
br label %26
26: ; preds = %22, %8
ret void
}

LLVM IR (-O2):
%struct.dirent = type opaque
@.str = [2 x i8] c".\00"
@alphasort = global i32 0
define void @no_memleak_scandir() {
%1 = alloca %struct.dirent**
%2 = bitcast %struct.dirent*** %1 to i8*
%3 = bitcast %struct.dirent*** %1 to i32*******
%4 = load i32, i32* @alphasort
%5 = call i32 @scandir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i32******* %3, i32* null, i32 %4)
switch i32 %5, label %6 [
i32 -1, label %21
i32 0, label %18
]
6: ; preds = %0
%7 = sext i32 %5 to i64
br label %8
8: ; preds = %6, %8
%9 = phi i64 [ %7, %6 ], [ %10, %8 ]
%10 = add nsw i64 %9, -1
%11 = load %struct.dirent**, %struct.dirent*** %1
%12 = getelementptr inbounds %struct.dirent*, %struct.dirent** %11, i64 %10
%13 = bitcast %struct.dirent** %12 to i32*******
%14 = load i32******, i32******* %13
%15 = call i32 @free(i32****** %14)
%16 = trunc i64 %10 to i32
%17 = icmp eq i32 %16, 0
br i1 %17, label %18, label %8
18: ; preds = %8, %0
%19 = load i32******, i32******* %3
%20 = call i32 @free(i32****** %19)
br label %21
21: ; preds = %0, %18
ret void
}