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

/* Variables and functions */
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
scalar_t__ dprintf(int, char *, int /*<<< orphan*/);
int /*<<< orphan*/ *opendir(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int send_control_response(int, int, int);
int transmit_listing(char *filename, int data_sock, int control_sock) {
  int err;
  DIR *dir;
  struct dirent *ent;
  int data_length = 0;
  dir = opendir(filename);
  if (dir == NULL) {
    err = send_control_response(control_sock, 403, 0);
    return err;
  }
  while (true) {
    ent = readdir(dir);
    if (ent == NULL) {
      err = send_control_response(control_sock, 200, data_length);
      break;
    }
    data_length += dprintf(data_sock, "%s\n", ent->d_name);
  }
  closedir(dir);
  return err;
}


x86 assembly (clang, -O0):
<transmit_listing>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
movl   $0x0,-0x34(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    119e <transmit_listing+0x4e>
mov    -0x18(%rbp),%edi
mov    $0x193,%esi
xor    %edx,%edx
call   1060 <send_control_response@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1209 <transmit_listing+0xb9>
jmp    11a3 <transmit_listing+0x53>
mov    -0x28(%rbp),%rdi
call   1070 <readdir@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    11d3 <transmit_listing+0x83>
mov    -0x18(%rbp),%edi
mov    -0x34(%rbp),%edx
mov    $0xc8,%esi
call   1060 <send_control_response@plt>
mov    %eax,-0x1c(%rbp)
jmp    11fa <transmit_listing+0xaa>
mov    -0x14(%rbp),%edi
mov    -0x30(%rbp),%rax
mov    (%rax),%edx
lea    0xe1d(%rip),%rsi
call   1040 <dprintf@plt>
mov    %rax,%rcx
movslq -0x34(%rbp),%rax
add    %rcx,%rax
mov    %eax,-0x34(%rbp)
jmp    11a3 <transmit_listing+0x53>
mov    -0x28(%rbp),%rdi
call   1050 <closedir@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<transmit_listing>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %edx,%r14d
mov    %esi,%r15d
call   1030 <opendir@plt>
test   %rax,%rax
je     11d0 <transmit_listing+0x80>
mov    %rax,%rbx
mov    %rax,%rdi
call   1070 <readdir@plt>
xor    %ebp,%ebp
test   %rax,%rax
je     11ac <transmit_listing+0x5c>
lea    0xe7f(%rip),%r12
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    (%rax),%edx
mov    %r15d,%edi
mov    %r12,%rsi
call   1040 <dprintf@plt>
add    %eax,%ebp
mov    %rbx,%rdi
call   1070 <readdir@plt>
test   %rax,%rax
jne    1190 <transmit_listing+0x40>
mov    %r14d,%edi
mov    $0xc8,%esi
mov    %ebp,%edx
call   1060 <send_control_response@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1050 <closedir@plt>
mov    %ebp,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %r14d,%edi
mov    $0x193,%esi
xor    %edx,%edx
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1060 <send_control_response@plt>

LLVM IR (-O0):
%struct.dirent = type { i32 }
@.str = [4 x i8] c"%s\0A\00"
define i32 @transmit_listing(i8* %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32*
%10 = alloca %struct.dirent*
%11 = alloca i32
store i8* %0, i8** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
store i32 0, i32* %11
%12 = load i8*, i8** %5
%13 = call i32* @opendir(i8* %12)
store i32* %13, i32** %9
%14 = load i32*, i32** %9
%15 = icmp eq i32* %14, null
br i1 %15, label %16, label %20
16: ; preds = %3
%17 = load i32, i32* %7
%18 = call i32 @send_control_response(i32 %17, i32 403, i32 0)
store i32 %18, i32* %8
%19 = load i32, i32* %8
store i32 %19, i32* %4
br label %44
20: ; preds = %3
br label %21
21: ; preds = %20, %30
%22 = load i32*, i32** %9
%23 = call %struct.dirent* @readdir(i32* %22)
store %struct.dirent* %23, %struct.dirent** %10
%24 = load %struct.dirent*, %struct.dirent** %10
%25 = icmp eq %struct.dirent* %24, null
br i1 %25, label %26, label %30
26: ; preds = %21
%27 = load i32, i32* %7
%28 = load i32, i32* %11
%29 = call i32 @send_control_response(i32 %27, i32 200, i32 %28)
store i32 %29, i32* %8
br label %40
30: ; preds = %21
%31 = load i32, i32* %6
%32 = load %struct.dirent*, %struct.dirent** %10
%33 = getelementptr inbounds %struct.dirent, %struct.dirent* %32, i32 0, i32 0
%34 = load i32, i32* %33
%35 = call i64 @dprintf(i32 %31, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %34)
%36 = load i32, i32* %11
%37 = sext i32 %36 to i64
%38 = add nsw i64 %37, %35
%39 = trunc i64 %38 to i32
store i32 %39, i32* %11
br label %21
40: ; preds = %26
%41 = load i32*, i32** %9
%42 = call i32 @closedir(i32* %41)
%43 = load i32, i32* %8
store i32 %43, i32* %4
br label %44
44: ; preds = %40, %16
%45 = load i32, i32* %4
ret i32 %45
}

LLVM IR (-O2):
%struct.dirent = type { i32 }
@.str = [4 x i8] c"%s\0A\00"
define i32 @transmit_listing(i8* nocapture %0, i32 %1, i32 %2) {
%4 = tail call i32* @opendir(i8* %0)
%5 = icmp eq i32* %4, null
br i1 %5, label %9, label %6
6: ; preds = %3
%7 = tail call %struct.dirent* @readdir(i32* %4)
%8 = icmp eq %struct.dirent* %7, null
br i1 %8, label %11, label %15
9: ; preds = %3
%10 = tail call i32 @send_control_response(i32 %2, i32 403, i32 0)
br label %25
11: ; preds = %15, %6
%12 = phi i32 [ 0, %6 ], [ %22, %15 ]
%13 = tail call i32 @send_control_response(i32 %2, i32 200, i32 %12)
%14 = tail call i32 @closedir(i32* %4)
br label %25
15: ; preds = %6, %15
%16 = phi %struct.dirent* [ %23, %15 ], [ %7, %6 ]
%17 = phi i32 [ %22, %15 ], [ 0, %6 ]
%18 = getelementptr inbounds %struct.dirent, %struct.dirent* %16, i64 0, i32 0
%19 = load i32, i32* %18
%20 = tail call i64 @dprintf(i32 %1, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %19)
%21 = trunc i64 %20 to i32
%22 = add i32 %17, %21
%23 = tail call %struct.dirent* @readdir(i32* %4)
%24 = icmp eq %struct.dirent* %23, null
br i1 %24, label %11, label %15
25: ; preds = %11, %9
%26 = phi i32 [ %10, %9 ], [ %13, %11 ]
ret i32 %26
}