#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 stat {
  int /*<<< orphan*/ st_mode;
};

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
scalar_t__ S_ISDIR(int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
int ftpd_list(int, int, char *);
int /*<<< orphan*/ net_printf(int, char *, ...);
int open(char *, int /*<<< orphan*/, int /*<<< orphan*/);
int read(int, unsigned char *, int);
scalar_t__ stat(char *, struct stat *);
int /*<<< orphan*/ write(int, unsigned char *, int);
int ftpd_retr(int cs, int dp, char *filename) {
  struct stat sb;
  if (dp < 0)
    return 1;
  if (stat(filename, &sb) != 0) {
    net_printf(cs, "522 Data Connection Problem, did you use PASV?\n");
    return 1;
  } else {
    if (S_ISDIR(sb.st_mode)) {
      return ftpd_list(cs, dp, filename);
    } else {
      int fh;
      int len;
      unsigned char buf[1024];
      int totlen;
      totlen = 0;
      net_printf(cs, "150 Ready to download %s\n", filename);
      fh = open(filename, O_RDONLY, 0);
      if (fh < 0) {
        net_printf(cs, "522 Data Connection Problem, did you use PASV?\n");
        return 1;
      }
      while ((len = read(fh, buf, 1024 - 1)) > 0) {
        totlen += len;
        write(dp, buf, len);
      }
      close(fh);
      net_printf(cs, "226 Transfer OK - %d bytes received\n", totlen);
      return 0;
    }
  }
}


x86 assembly (clang, -O0):
<ftpd_retr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x440,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    11ab <ftpd_retr+0x2b>
movl   $0x1,-0x4(%rbp)
jmp    12d6 <ftpd_retr+0x156>
mov    -0x18(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1070 <stat@plt>
cmp    $0x0,%rax
je     11df <ftpd_retr+0x5f>
mov    -0x8(%rbp),%edi
lea    0xe34(%rip),%rsi
mov    $0x0,%al
call   1090 <net_printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12d6 <ftpd_retr+0x156>
mov    -0x20(%rbp),%edi
call   1040 <S_ISDIR@plt>
cmp    $0x0,%rax
je     1208 <ftpd_retr+0x88>
mov    -0x8(%rbp),%edi
mov    -0xc(%rbp),%esi
mov    -0x18(%rbp),%rdx
call   10a0 <ftpd_list@plt>
mov    %eax,-0x4(%rbp)
jmp    12d6 <ftpd_retr+0x156>
movl   $0x0,-0x434(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x18(%rbp),%rdx
lea    0xe10(%rip),%rsi
mov    $0x0,%al
call   1090 <net_printf@plt>
mov    -0x18(%rbp),%rdi
mov    0x2db6(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <open@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    1265 <ftpd_retr+0xe5>
mov    -0x8(%rbp),%edi
lea    0xdae(%rip),%rsi
mov    $0x0,%al
call   1090 <net_printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12d6 <ftpd_retr+0x156>
jmp    126a <ftpd_retr+0xea>
mov    -0x24(%rbp),%edi
lea    -0x430(%rbp),%rsi
mov    $0x3ff,%edx
call   1060 <read@plt>
mov    %eax,-0x28(%rbp)
cmp    $0x0,%eax
jle    12b0 <ftpd_retr+0x130>
mov    -0x28(%rbp),%eax
add    -0x434(%rbp),%eax
mov    %eax,-0x434(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x430(%rbp),%rsi
mov    -0x28(%rbp),%edx
call   1030 <write@plt>
jmp    126a <ftpd_retr+0xea>
mov    -0x24(%rbp),%edi
call   1050 <close@plt>
mov    -0x8(%rbp),%edi
mov    -0x434(%rbp),%edx
lea    0xd82(%rip),%rsi
mov    $0x0,%al
call   1090 <net_printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x440,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ftpd_retr>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x410,%rsp
mov    $0x1,%ebp
test   %esi,%esi
js     1298 <ftpd_retr+0x118>
mov    %rdx,%r15
mov    %esi,%r12d
mov    %edi,%r14d
lea    0x8(%rsp),%rsi
mov    %rdx,%rdi
call   1070 <stat@plt>
test   %rax,%rax
je     11cd <ftpd_retr+0x4d>
lea    0xe42(%rip),%rsi
mov    %r14d,%edi
xor    %eax,%eax
call   1090 <net_printf@plt>
jmp    1298 <ftpd_retr+0x118>
mov    0x8(%rsp),%edi
call   1040 <S_ISDIR@plt>
test   %rax,%rax
je     11f0 <ftpd_retr+0x70>
mov    %r14d,%edi
mov    %r12d,%esi
mov    %r15,%rdx
call   10a0 <ftpd_list@plt>
mov    %eax,%ebp
jmp    1298 <ftpd_retr+0x118>
lea    0xe39(%rip),%rsi
xor    %ebx,%ebx
mov    %r14d,%edi
mov    %r15,%rdx
xor    %eax,%eax
call   1090 <net_printf@plt>
mov    0x2ddb(%rip),%rax
mov    (%rax),%esi
mov    %r15,%rdi
xor    %edx,%edx
call   1080 <open@plt>
test   %eax,%eax
js     1282 <ftpd_retr+0x102>
mov    %eax,%r15d
lea    0x10(%rsp),%rsi
mov    %eax,%edi
mov    $0x3ff,%edx
call   1060 <read@plt>
test   %eax,%eax
jle    1263 <ftpd_retr+0xe3>
xor    %ebx,%ebx
lea    0x10(%rsp),%rbp
nopl   0x0(%rax)
add    %eax,%ebx
mov    %r12d,%edi
mov    %rbp,%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    %r15d,%edi
mov    %rbp,%rsi
mov    $0x3ff,%edx
call   1060 <read@plt>
test   %eax,%eax
jg     1240 <ftpd_retr+0xc0>
mov    %r15d,%edi
call   1050 <close@plt>
lea    0xdd8(%rip),%rsi
xor    %ebp,%ebp
mov    %r14d,%edi
mov    %ebx,%edx
xor    %eax,%eax
call   1090 <net_printf@plt>
jmp    1298 <ftpd_retr+0x118>
lea    0xd77(%rip),%rsi
mov    %r14d,%edi
xor    %eax,%eax
call   1090 <net_printf@plt>
mov    $0x1,%ebp
mov    %ebp,%eax
add    $0x410,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [48 x i8] c"522 Data Connection Problem, did you use PASV?\0A\00"
@.str.1 = [26 x i8] c"150 Ready to download %s\0A\00"
@O_RDONLY = global i32 0
@.str.2 = [37 x i8] c"226 Transfer OK - %d bytes received\0A\00"
define i32 @ftpd_retr(i32 %0, i32 %1, i8* %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca %struct.stat
%9 = alloca i32
%10 = alloca i32
%11 = alloca [1024 x i8]
%12 = alloca i32
store i32 %0, i32* %5
store i32 %1, i32* %6
store i8* %2, i8** %7
%13 = load i32, i32* %6
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %16
15: ; preds = %3
store i32 1, i32* %4
br label %65
16: ; preds = %3
%17 = load i8*, i8** %7
%18 = call i64 @stat(i8* %17, %struct.stat* %8)
%19 = icmp ne i64 %18, 0
br i1 %19, label %20, label %23
20: ; preds = %16
%21 = load i32, i32* %5
%22 = call i32 (i32, i8*, ...) @net_printf(i32 %21, i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %4
br label %65
23: ; preds = %16
%24 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 0
%25 = load i32, i32* %24
%26 = call i64 @S_ISDIR(i32 %25)
%27 = icmp ne i64 %26, 0
br i1 %27, label %28, label %33
28: ; preds = %23
%29 = load i32, i32* %5
%30 = load i32, i32* %6
%31 = load i8*, i8** %7
%32 = call i32 @ftpd_list(i32 %29, i32 %30, i8* %31)
store i32 %32, i32* %4
br label %65
33: ; preds = %23
store i32 0, i32* %12
%34 = load i32, i32* %5
%35 = load i8*, i8** %7
%36 = call i32 (i32, i8*, ...) @net_printf(i32 %34, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i8* %35)
%37 = load i8*, i8** %7
%38 = load i32, i32* @O_RDONLY
%39 = call i32 @open(i8* %37, i32 %38, i32 0)
store i32 %39, i32* %9
%40 = load i32, i32* %9
%41 = icmp slt i32 %40, 0
br i1 %41, label %42, label %45
42: ; preds = %33
%43 = load i32, i32* %5
%44 = call i32 (i32, i8*, ...) @net_printf(i32 %43, i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %4
br label %65
45: ; preds = %33
br label %46
46: ; preds = %51, %45
%47 = load i32, i32* %9
%48 = getelementptr inbounds [1024 x i8], [1024 x i8]* %11, i64 0, i64 0
%49 = call i32 @read(i32 %47, i8* %48, i32 1023)
store i32 %49, i32* %10
%50 = icmp sgt i32 %49, 0
br i1 %50, label %51, label %59
51: ; preds = %46
%52 = load i32, i32* %10
%53 = load i32, i32* %12
%54 = add nsw i32 %53, %52
store i32 %54, i32* %12
%55 = load i32, i32* %6
%56 = getelementptr inbounds [1024 x i8], [1024 x i8]* %11, i64 0, i64 0
%57 = load i32, i32* %10
%58 = call i32 @write(i32 %55, i8* %56, i32 %57)
br label %46
59: ; preds = %46
%60 = load i32, i32* %9
%61 = call i32 @close(i32 %60)
%62 = load i32, i32* %5
%63 = load i32, i32* %12
%64 = call i32 (i32, i8*, ...) @net_printf(i32 %62, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.2, i64 0, i64 0), i32 %63)
store i32 0, i32* %4
br label %65
65: ; preds = %59, %42, %28, %20, %15
%66 = load i32, i32* %4
ret i32 %66
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [48 x i8] c"522 Data Connection Problem, did you use PASV?\0A\00"
@.str.1 = [26 x i8] c"150 Ready to download %s\0A\00"
@O_RDONLY = global i32 0
@.str.2 = [37 x i8] c"226 Transfer OK - %d bytes received\0A\00"
define i32 @ftpd_retr(i32 %0, i32 %1, i8* %2) {
%4 = alloca %struct.stat
%5 = alloca [1024 x i8]
%6 = bitcast %struct.stat* %4 to i8*
%7 = icmp slt i32 %1, 0
br i1 %7, label %44, label %8
8: ; preds = %3
%9 = call i64 @stat(i8* %2, %struct.stat* %4)
%10 = icmp eq i64 %9, 0
br i1 %10, label %13, label %11
11: ; preds = %8
%12 = call i32 (i32, i8*, ...) @net_printf(i32 %0, i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str, i64 0, i64 0))
br label %44
13: ; preds = %8
%14 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 0
%15 = load i32, i32* %14
%16 = call i64 @S_ISDIR(i32 %15)
%17 = icmp eq i64 %16, 0
br i1 %17, label %20, label %18
18: ; preds = %13
%19 = call i32 @ftpd_list(i32 %0, i32 %1, i8* %2)
br label %44
20: ; preds = %13
%21 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%22 = call i32 (i32, i8*, ...) @net_printf(i32 %0, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i8* %2)
%23 = load i32, i32* @O_RDONLY
%24 = call i32 @open(i8* %2, i32 %23, i32 0)
%25 = icmp slt i32 %24, 0
br i1 %25, label %29, label %26
26: ; preds = %20
%27 = call i32 @read(i32 %24, i8* %21, i32 1023)
%28 = icmp sgt i32 %27, 0
br i1 %28, label %31, label %38
29: ; preds = %20
%30 = call i32 (i32, i8*, ...) @net_printf(i32 %0, i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str, i64 0, i64 0))
br label %42
31: ; preds = %26, %31
%32 = phi i32 [ %36, %31 ], [ %27, %26 ]
%33 = phi i32 [ %34, %31 ], [ 0, %26 ]
%34 = add nsw i32 %32, %33
%35 = call i32 @write(i32 %1, i8* %21, i32 %32)
%36 = call i32 @read(i32 %24, i8* %21, i32 1023)
%37 = icmp sgt i32 %36, 0
br i1 %37, label %31, label %38
38: ; preds = %31, %26
%39 = phi i32 [ 0, %26 ], [ %34, %31 ]
%40 = call i32 @close(i32 %24)
%41 = call i32 (i32, i8*, ...) @net_printf(i32 %0, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.2, i64 0, i64 0), i32 %39)
br label %42
42: ; preds = %38, %29
%43 = phi i32 [ 1, %29 ], [ 0, %38 ]
br label %44
44: ; preds = %3, %42, %18, %11
%45 = phi i32 [ 1, %11 ], [ %19, %18 ], [ %43, %42 ], [ 1, %3 ]
ret i32 %45
}