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

/* Variables and functions */
int /*<<< orphan*/ add_ent(struct list_head *, struct dirent *);
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ dup(int);
int /*<<< orphan*/ *fdopendir(int /*<<< orphan*/);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ sort_list(struct list_head *);
void load_dir(struct list_head *lst, int fd) {
  DIR *dir;
  struct dirent *res;
  dir = fdopendir(dup(fd));
  if (!dir)
    return;
  while ((res = readdir(dir)) != NULL)
    add_ent(lst, res);
  sort_list(lst);
  closedir(dir);
}


x86 assembly (clang, -O0):
<load_dir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0xc(%rbp),%edi
call   1030 <dup@plt>
mov    %eax,%edi
call   1070 <fdopendir@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1192 <load_dir+0x32>
jmp    11d2 <load_dir+0x72>
jmp    1197 <load_dir+0x37>
mov    -0x18(%rbp),%rdi
call   1050 <readdir@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     11c0 <load_dir+0x60>
mov    -0x8(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1080 <add_ent@plt>
jmp    1197 <load_dir+0x37>
mov    -0x8(%rbp),%rdi
call   1060 <sort_list@plt>
mov    -0x18(%rbp),%rdi
call   1040 <closedir@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<load_dir>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
mov    %esi,%edi
call   1030 <dup@plt>
mov    %eax,%edi
call   1070 <fdopendir@plt>
test   %rax,%rax
je     11b1 <load_dir+0x51>
mov    %rax,%rbx
nopl   (%rax)
mov    %rbx,%rdi
call   1050 <readdir@plt>
test   %rax,%rax
je     119a <load_dir+0x3a>
mov    %r14,%rdi
mov    %rax,%rsi
call   1080 <add_ent@plt>
jmp    1180 <load_dir+0x20>
mov    %r14,%rdi
call   1060 <sort_list@plt>
mov    %rbx,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1040 <closedir@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.list_head = type { i32 }
%struct.dirent = type { i32 }
define void @load_dir(%struct.list_head* %0, i32 %1) {
%3 = alloca %struct.list_head*
%4 = alloca i32
%5 = alloca i32*
%6 = alloca %struct.dirent*
store %struct.list_head* %0, %struct.list_head** %3
store i32 %1, i32* %4
%7 = load i32, i32* %4
%8 = call i32 @dup(i32 %7)
%9 = call i32* @fdopendir(i32 %8)
store i32* %9, i32** %5
%10 = load i32*, i32** %5
%11 = icmp ne i32* %10, null
br i1 %11, label %13, label %12
12: ; preds = %2
br label %27
13: ; preds = %2
br label %14
14: ; preds = %18, %13
%15 = load i32*, i32** %5
%16 = call %struct.dirent* @readdir(i32* %15)
store %struct.dirent* %16, %struct.dirent** %6
%17 = icmp ne %struct.dirent* %16, null
br i1 %17, label %18, label %22
18: ; preds = %14
%19 = load %struct.list_head*, %struct.list_head** %3
%20 = load %struct.dirent*, %struct.dirent** %6
%21 = call i32 @add_ent(%struct.list_head* %19, %struct.dirent* %20)
br label %14
22: ; preds = %14
%23 = load %struct.list_head*, %struct.list_head** %3
%24 = call i32 @sort_list(%struct.list_head* %23)
%25 = load i32*, i32** %5
%26 = call i32 @closedir(i32* %25)
br label %27
27: ; preds = %22, %12
ret void
}

LLVM IR (-O2):
%struct.list_head = type { i32 }
%struct.dirent = type { i32 }
define void @load_dir(%struct.list_head* %0, i32 %1) {
%3 = tail call i32 @dup(i32 %1)
%4 = tail call i32* @fdopendir(i32 %3)
%5 = icmp eq i32* %4, null
br i1 %5, label %17, label %6
6: ; preds = %2
%7 = tail call %struct.dirent* @readdir(i32* %4)
%8 = icmp eq %struct.dirent* %7, null
br i1 %8, label %14, label %9
9: ; preds = %6, %9
%10 = phi %struct.dirent* [ %12, %9 ], [ %7, %6 ]
%11 = tail call i32 @add_ent(%struct.list_head* %0, %struct.dirent* %10)
%12 = tail call %struct.dirent* @readdir(i32* %4)
%13 = icmp eq %struct.dirent* %12, null
br i1 %13, label %14, label %9
14: ; preds = %9, %6
%15 = tail call i32 @sort_list(%struct.list_head* %0)
%16 = tail call i32 @closedir(i32* %4)
br label %17
17: ; preds = %2, %14
ret void
}