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

/* Variables and functions */
int const AF_INET;
int const AF_INET6;
int const AF_UNSPEC;
int EXIT_SUCCESS;
int EX_OSERR;
int /*<<< orphan*/ assert(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ free(struct dent *);
int fwrite(char *, int, int, int /*<<< orphan*/ *);
int lsdir_tree(char const *const, int /*<<< orphan*/ *);
int /*<<< orphan*/ *open_memstream(char **, size_t *);
int /*<<< orphan*/ warn(char *, ...);
int /*<<< orphan*/ warnx(char *, char const *const);
int main_c(const int net_fam, const char *const rootdir, const char *const host,
           const char *const port) {
  assert(net_fam == AF_INET || net_fam == AF_INET6 || net_fam == AF_UNSPEC);
  assert(rootdir != NULL);
  assert(host != NULL);
  assert(port != NULL);
  struct dent *dirtree = NULL;
  size_t dirtree_len = 0;
  FILE *dirtree_f = NULL;
  dirtree_f = open_memstream((char **)&dirtree, &dirtree_len);
  if (dirtree_f == NULL) {
    warn("Cannot allocate memory for dir tree");
    return (EX_OSERR);
  }
  if (lsdir_tree(rootdir, dirtree_f) == false ||
      fwrite("\0", 1, 1, dirtree_f) < 1) {
    warn("Cannot load dir listing for `%s'", rootdir);
    (void)fclose(dirtree_f);
    free(dirtree);
    return (EX_OSERR);
  }
  (void)fclose(dirtree_f);
  warnx("Loaded directory `%s'", rootdir);
  return (EXIT_SUCCESS);
}


x86 assembly (clang, -O0):
<main_c>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    -0x8(%rbp),%ecx
mov    $0x1,%al
mov    0x2e45(%rip),%rdx
cmp    (%rdx),%ecx
mov    %al,-0x39(%rbp)
je     11d7 <main_c+0x57>
mov    -0x8(%rbp),%ecx
mov    $0x1,%al
mov    0x2e1e(%rip),%rdx
cmp    (%rdx),%ecx
mov    %al,-0x39(%rbp)
je     11d7 <main_c+0x57>
mov    -0x8(%rbp),%eax
mov    0x2e11(%rip),%rcx
cmp    (%rcx),%eax
sete   %al
mov    %al,-0x39(%rbp)
mov    -0x39(%rbp),%al
and    $0x1,%al
movzbl %al,%edi
call   1080 <assert@plt>
cmpq   $0x0,-0x10(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1080 <assert@plt>
cmpq   $0x0,-0x18(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1080 <assert@plt>
cmpq   $0x0,-0x20(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1080 <assert@plt>
movq   $0x0,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
lea    -0x28(%rbp),%rdi
lea    -0x30(%rbp),%rsi
call   1040 <open_memstream@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    126d <main_c+0xed>
lea    0xdb9(%rip),%rdi
mov    $0x0,%al
call   1050 <warn@plt>
mov    0x2d5d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    12fd <main_c+0x17d>
mov    -0x10(%rbp),%rdi
mov    -0x38(%rbp),%rsi
call   1090 <lsdir_tree@plt>
cmp    $0x0,%eax
je     12a3 <main_c+0x123>
mov    -0x38(%rbp),%rcx
lea    0xd7e(%rip),%rdi
mov    $0x1,%edx
mov    %edx,%esi
call   10a0 <fwrite@plt>
cmp    $0x1,%eax
jge    12d8 <main_c+0x158>
mov    -0x10(%rbp),%rsi
lea    0xd84(%rip),%rdi
mov    $0x0,%al
call   1050 <warn@plt>
mov    -0x38(%rbp),%rdi
call   1060 <fclose@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    0x2cf2(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    12fd <main_c+0x17d>
mov    -0x38(%rbp),%rdi
call   1060 <fclose@plt>
mov    -0x10(%rbp),%rsi
lea    0xd67(%rip),%rdi
call   1070 <warnx@plt>
mov    0x2cd0(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main_c>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rcx,%r15
mov    %rdx,%rbx
mov    %rsi,%r14
xor    %eax,%eax
test   %edi,%edi
sete   %al
mov    %eax,%edi
call   1080 <assert@plt>
xor    %edi,%edi
test   %r14,%r14
setne  %dil
call   1080 <assert@plt>
xor    %edi,%edi
test   %rbx,%rbx
setne  %dil
call   1080 <assert@plt>
xor    %edi,%edi
test   %r15,%r15
setne  %dil
call   1080 <assert@plt>
movq   $0x0,(%rsp)
movq   $0x0,0x8(%rsp)
mov    %rsp,%rdi
lea    0x8(%rsp),%rsi
call   1040 <open_memstream@plt>
test   %rax,%rax
je     123c <main_c+0xbc>
mov    %rax,%rbx
mov    %r14,%rdi
mov    %rax,%rsi
call   1090 <lsdir_tree@plt>
test   %eax,%eax
je     124c <main_c+0xcc>
lea    0xe06(%rip),%rdi
mov    $0x1,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   10a0 <fwrite@plt>
test   %eax,%eax
jle    124c <main_c+0xcc>
mov    %rbx,%rdi
call   1060 <fclose@plt>
lea    0xe28(%rip),%rdi
mov    %r14,%rsi
call   1070 <warnx@plt>
mov    0x2da6(%rip),%rax
jmp    1275 <main_c+0xf5>
lea    0xdcb(%rip),%rdi
xor    %eax,%eax
call   1050 <warn@plt>
jmp    126e <main_c+0xee>
lea    0xddf(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1050 <warn@plt>
mov    %rbx,%rdi
call   1060 <fclose@plt>
mov    (%rsp),%rdi
call   1030 <free@plt>
mov    0x2d63(%rip),%rax
mov    (%rax),%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.dent = type { i32 }
@AF_INET = constant i32 0
@AF_INET6 = constant i32 0
@AF_UNSPEC = constant i32 0
@.str = [36 x i8] c"Cannot allocate memory for dir tree\00"
@EX_OSERR = global i32 0
@.str.1 = [2 x i8] zeroinitializer
@.str.2 = [33 x i8] c"Cannot load dir listing for `%s'\00"
@.str.3 = [22 x i8] c"Loaded directory `%s'\00"
@EXIT_SUCCESS = global i32 0
define i32 @main_c(i32 %0, i8* %1, i8* %2, i8* %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca %struct.dent*
%11 = alloca i64
%12 = alloca i32*
store i32 %0, i32* %6
store i8* %1, i8** %7
store i8* %2, i8** %8
store i8* %3, i8** %9
%13 = load i32, i32* %6
%14 = load i32, i32* @AF_INET
%15 = icmp eq i32 %13, %14
br i1 %15, label %24, label %16
16: ; preds = %4
%17 = load i32, i32* %6
%18 = load i32, i32* @AF_INET6
%19 = icmp eq i32 %17, %18
br i1 %19, label %24, label %20
20: ; preds = %16
%21 = load i32, i32* %6
%22 = load i32, i32* @AF_UNSPEC
%23 = icmp eq i32 %21, %22
br label %24
24: ; preds = %20, %16, %4
%25 = phi i1 [ true, %16 ], [ true, %4 ], [ %23, %20 ]
%26 = zext i1 %25 to i32
%27 = call i32 @assert(i32 %26)
%28 = load i8*, i8** %7
%29 = icmp ne i8* %28, null
%30 = zext i1 %29 to i32
%31 = call i32 @assert(i32 %30)
%32 = load i8*, i8** %8
%33 = icmp ne i8* %32, null
%34 = zext i1 %33 to i32
%35 = call i32 @assert(i32 %34)
%36 = load i8*, i8** %9
%37 = icmp ne i8* %36, null
%38 = zext i1 %37 to i32
%39 = call i32 @assert(i32 %38)
store %struct.dent* null, %struct.dent** %10
store i64 0, i64* %11
store i32* null, i32** %12
%40 = bitcast %struct.dent** %10 to i8**
%41 = call i32* @open_memstream(i8** %40, i64* %11)
store i32* %41, i32** %12
%42 = load i32*, i32** %12
%43 = icmp eq i32* %42, null
br i1 %43, label %44, label %47
44: ; preds = %24
%45 = call i32 (i8*, ...) @warn(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0))
%46 = load i32, i32* @EX_OSERR
store i32 %46, i32* %5
br label %70
47: ; preds = %24
%48 = load i8*, i8** %7
%49 = load i32*, i32** %12
%50 = call i32 @lsdir_tree(i8* %48, i32* %49)
%51 = icmp eq i32 %50, 0
br i1 %51, label %56, label %52
52: ; preds = %47
%53 = load i32*, i32** %12
%54 = call i32 @fwrite(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32 1, i32 1, i32* %53)
%55 = icmp slt i32 %54, 1
br i1 %55, label %56, label %64
56: ; preds = %52, %47
%57 = load i8*, i8** %7
%58 = call i32 (i8*, ...) @warn(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0), i8* %57)
%59 = load i32*, i32** %12
%60 = call i32 @fclose(i32* %59)
%61 = load %struct.dent*, %struct.dent** %10
%62 = call i32 @free(%struct.dent* %61)
%63 = load i32, i32* @EX_OSERR
store i32 %63, i32* %5
br label %70
64: ; preds = %52
%65 = load i32*, i32** %12
%66 = call i32 @fclose(i32* %65)
%67 = load i8*, i8** %7
%68 = call i32 @warnx(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0), i8* %67)
%69 = load i32, i32* @EXIT_SUCCESS
store i32 %69, i32* %5
br label %70
70: ; preds = %64, %56, %44
%71 = load i32, i32* %5
ret i32 %71
}

LLVM IR (-O2):
%struct.dent = type { i32 }
@AF_INET = constant i32 0
@AF_INET6 = constant i32 0
@AF_UNSPEC = constant i32 0
@.str = [36 x i8] c"Cannot allocate memory for dir tree\00"
@EX_OSERR = global i32 0
@.str.1 = [2 x i8] zeroinitializer
@.str.2 = [33 x i8] c"Cannot load dir listing for `%s'\00"
@.str.3 = [22 x i8] c"Loaded directory `%s'\00"
@EXIT_SUCCESS = global i32 0
define i32 @main_c(i32 %0, i8* %1, i8* readnone %2, i8* readnone %3) {
%5 = alloca %struct.dent*
%6 = alloca i64
%7 = icmp eq i32 %0, 0
%8 = zext i1 %7 to i32
%9 = tail call i32 @assert(i32 %8)
%10 = icmp ne i8* %1, null
%11 = zext i1 %10 to i32
%12 = tail call i32 @assert(i32 %11)
%13 = icmp ne i8* %2, null
%14 = zext i1 %13 to i32
%15 = tail call i32 @assert(i32 %14)
%16 = icmp ne i8* %3, null
%17 = zext i1 %16 to i32
%18 = tail call i32 @assert(i32 %17)
%19 = bitcast %struct.dent** %5 to i8*
store %struct.dent* null, %struct.dent** %5
%20 = bitcast i64* %6 to i8*
store i64 0, i64* %6
%21 = bitcast %struct.dent** %5 to i8**
%22 = call i32* @open_memstream(i8** %21, i64* %6)
%23 = icmp eq i32* %22, null
br i1 %23, label %24, label %26
24: ; preds = %4
%25 = call i32 (i8*, ...) @warn(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0))
br label %40
26: ; preds = %4
%27 = call i32 @lsdir_tree(i8* %1, i32* %22)
%28 = icmp eq i32 %27, 0
br i1 %28, label %32, label %29
29: ; preds = %26
%30 = call i32 @fwrite(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32 1, i32 1, i32* %22)
%31 = icmp slt i32 %30, 1
br i1 %31, label %32, label %37
32: ; preds = %29, %26
%33 = call i32 (i8*, ...) @warn(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0), i8* %1)
%34 = call i32 @fclose(i32* %22)
%35 = load %struct.dent*, %struct.dent** %5
%36 = call i32 @free(%struct.dent* %35)
br label %40
37: ; preds = %29
%38 = call i32 @fclose(i32* %22)
%39 = call i32 @warnx(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0), i8* %1)
br label %40
40: ; preds = %37, %32, %24
%41 = phi i32* [ @EX_OSERR, %24 ], [ @EX_OSERR, %32 ], [ @EXIT_SUCCESS, %37 ]
%42 = load i32, i32* %41
ret i32 %42
}