#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 */
typedef struct TYPE_2__ TYPE_1__;

/* Type definitions */
struct TYPE_2__ {
  unsigned long s_addr;
};
struct sockaddr_in {
  int /*<<< orphan*/ sin_port;
  TYPE_1__ sin_addr;
};
struct sockaddr {
  int dummy;
};
typedef int socklen_t;
typedef int /*<<< orphan*/ so;

/* Variables and functions */
scalar_t__ getsockname(int, struct sockaddr *, int *);
int /*<<< orphan*/ net_printf(int, char *, ...);
unsigned int ntohs(int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int ftpd_pasv(int cs, int dp) {
  struct sockaddr_in so;
  unsigned long int add;
  socklen_t solen;
  unsigned int port;
  int a1 = 0, a2 = 0, a3 = 0, a4 = 0, p1 = 0, p2 = 0;
  if (dp < 0 || cs < 0) {
    perror("ftpd_pasv");
    net_printf(cs, "521 PASV Connection Failed\n");
    return -1;
  }
  solen = sizeof(so);
  if (getsockname(cs, (struct sockaddr *)&so, &solen) < 0) {
    perror("getsockname(cs)");
    net_printf(cs, "521 PASV Connection Failed\n");
    return -1;
  }
  add = so.sin_addr.s_addr;
  a1 = (int)(add & 0xFF);
  add /= 256;
  a2 = (int)(add & 0xFF);
  add /= 256;
  a3 = (int)(add & 0xFF);
  add /= 256;
  a4 = (int)(add & 0xFF);
  add /= 256;
  if (getsockname(dp, (struct sockaddr *)&so, &solen) < 0) {
    perror("getsockname(datap)");
    net_printf(cs, "521 PASV Connection Failed\n");
    return -1;
  }
  port = ntohs(so.sin_port);
  p2 = (int)(port & 0xFF);
  port /= 256;
  p1 = (int)(port & 0xFF);
  port /= 256;
  net_printf(cs, "227 PASV OK (%d,%d,%d,%d,%d,%d)\n", a1, a2, a3, a4, p1, p2);
  return 0;
}


x86 assembly (clang, -O0):
<ftpd_pasv>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0x0,-0x34(%rbp)
movl   $0x0,-0x38(%rbp)
movl   $0x0,-0x3c(%rbp)
movl   $0x0,-0x40(%rbp)
movl   $0x0,-0x44(%rbp)
movl   $0x0,-0x48(%rbp)
cmpl   $0x0,-0xc(%rbp)
jl     118c <ftpd_pasv+0x4c>
cmpl   $0x0,-0x8(%rbp)
jge    11b5 <ftpd_pasv+0x75>
lea    0xe6d(%rip),%rdi
call   1040 <perror@plt>
mov    -0x8(%rbp),%edi
lea    0xe68(%rip),%rsi
mov    $0x0,%al
call   1060 <net_printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1316 <ftpd_pasv+0x1d6>
movl   $0x10,-0x2c(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x20(%rbp),%rsi
lea    -0x2c(%rbp),%rdx
call   1050 <getsockname@plt>
cmp    $0x0,%rax
jge    11ff <ftpd_pasv+0xbf>
lea    0xe49(%rip),%rdi
call   1040 <perror@plt>
mov    -0x8(%rbp),%edi
lea    0xe1e(%rip),%rsi
mov    $0x0,%al
call   1060 <net_printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1316 <ftpd_pasv+0x1d6>
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
and    $0xff,%rax
mov    %eax,-0x34(%rbp)
mov    -0x28(%rbp),%rax
shr    $0x8,%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
and    $0xff,%rax
mov    %eax,-0x38(%rbp)
mov    -0x28(%rbp),%rax
shr    $0x8,%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
and    $0xff,%rax
mov    %eax,-0x3c(%rbp)
mov    -0x28(%rbp),%rax
shr    $0x8,%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
and    $0xff,%rax
mov    %eax,-0x40(%rbp)
mov    -0x28(%rbp),%rax
shr    $0x8,%rax
mov    %rax,-0x28(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x20(%rbp),%rsi
lea    -0x2c(%rbp),%rdx
call   1050 <getsockname@plt>
cmp    $0x0,%rax
jge    12ae <ftpd_pasv+0x16e>
lea    0xdaa(%rip),%rdi
call   1040 <perror@plt>
mov    -0x8(%rbp),%edi
lea    0xd6f(%rip),%rsi
mov    $0x0,%al
call   1060 <net_printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1316 <ftpd_pasv+0x1d6>
mov    -0x20(%rbp),%edi
call   1030 <ntohs@plt>
mov    %eax,-0x30(%rbp)
mov    -0x30(%rbp),%eax
and    $0xff,%eax
mov    %eax,-0x48(%rbp)
mov    -0x30(%rbp),%eax
shr    $0x8,%eax
mov    %eax,-0x30(%rbp)
mov    -0x30(%rbp),%eax
and    $0xff,%eax
mov    %eax,-0x44(%rbp)
mov    -0x30(%rbp),%eax
shr    $0x8,%eax
mov    %eax,-0x30(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x34(%rbp),%edx
mov    -0x38(%rbp),%ecx
mov    -0x3c(%rbp),%r8d
mov    -0x40(%rbp),%r9d
mov    -0x44(%rbp),%r10d
mov    -0x48(%rbp),%eax
lea    0xd49(%rip),%rsi
mov    %r10d,(%rsp)
mov    %eax,0x8(%rsp)
mov    $0x0,%al
call   1060 <net_printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ftpd_pasv>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %edi,%r14d
mov    %esi,%eax
or     %edi,%eax
js     1205 <ftpd_pasv+0xc5>
mov    %esi,%ebp
movl   $0x10,0xc(%rsp)
lea    0x18(%rsp),%rsi
lea    0xc(%rsp),%rdx
mov    %r14d,%edi
call   1050 <getsockname@plt>
test   %rax,%rax
js     120e <ftpd_pasv+0xce>
mov    0x20(%rsp),%rbx
lea    0x18(%rsp),%rsi
lea    0xc(%rsp),%rdx
mov    %ebp,%edi
call   1050 <getsockname@plt>
test   %rax,%rax
js     1217 <ftpd_pasv+0xd7>
mov    %ebx,%eax
shr    $0x18,%eax
mov    %eax,0x14(%rsp)
mov    %ebx,%eax
shr    $0x10,%eax
movzbl %al,%eax
mov    %eax,0x10(%rsp)
movzbl %bh,%eax
mov    %eax,%r15d
movzbl %bl,%r13d
mov    0x18(%rsp),%edi
call   1030 <ntohs@plt>
movzbl %al,%ebp
movzbl %ah,%ebx
lea    0xe79(%rip),%rsi
xor    %r12d,%r12d
mov    %r14d,%edi
mov    %r13d,%edx
mov    0x14(%rsp),%r9d
mov    %r15d,%ecx
mov    0x10(%rsp),%r8d
xor    %eax,%eax
push   %rbp
push   %rbx
call   1060 <net_printf@plt>
add    $0x10,%rsp
mov    %r12d,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdf4(%rip),%rdi
jmp    121e <ftpd_pasv+0xde>
lea    0xe11(%rip),%rdi
jmp    121e <ftpd_pasv+0xde>
lea    0xe18(%rip),%rdi
call   1040 <perror@plt>
lea    0xde0(%rip),%rsi
mov    %r14d,%edi
xor    %eax,%eax
call   1060 <net_printf@plt>
mov    $0xffffffff,%r12d
jmp    11f3 <ftpd_pasv+0xb3>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i64 }
%struct.sockaddr = type { i32 }
@.str = [10 x i8] c"ftpd_pasv\00"
@.str.1 = [28 x i8] c"521 PASV Connection Failed\0A\00"
@.str.2 = [16 x i8] c"getsockname(cs)\00"
@.str.3 = [19 x i8] c"getsockname(datap)\00"
@.str.4 = [33 x i8] c"227 PASV OK (%d,%d,%d,%d,%d,%d)\0A\00"
define i32 @ftpd_pasv(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.sockaddr_in
%7 = alloca i64
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
%15 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
store i32 0, i32* %10
store i32 0, i32* %11
store i32 0, i32* %12
store i32 0, i32* %13
store i32 0, i32* %14
store i32 0, i32* %15
%16 = load i32, i32* %5
%17 = icmp slt i32 %16, 0
br i1 %17, label %21, label %18
18: ; preds = %2
%19 = load i32, i32* %4
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %25
21: ; preds = %18, %2
%22 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%23 = load i32, i32* %4
%24 = call i32 (i32, i8*, ...) @net_printf(i32 %23, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %3
br label %86
25: ; preds = %18
store i32 16, i32* %8
%26 = load i32, i32* %4
%27 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%28 = call i64 @getsockname(i32 %26, %struct.sockaddr* %27, i32* %8)
%29 = icmp slt i64 %28, 0
br i1 %29, label %30, label %34
30: ; preds = %25
%31 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
%32 = load i32, i32* %4
%33 = call i32 (i32, i8*, ...) @net_printf(i32 %32, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %3
br label %86
34: ; preds = %25
%35 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 1
%36 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %35, i32 0, i32 0
%37 = load i64, i64* %36
store i64 %37, i64* %7
%38 = load i64, i64* %7
%39 = and i64 %38, 255
%40 = trunc i64 %39 to i32
store i32 %40, i32* %10
%41 = load i64, i64* %7
%42 = udiv i64 %41, 256
store i64 %42, i64* %7
%43 = load i64, i64* %7
%44 = and i64 %43, 255
%45 = trunc i64 %44 to i32
store i32 %45, i32* %11
%46 = load i64, i64* %7
%47 = udiv i64 %46, 256
store i64 %47, i64* %7
%48 = load i64, i64* %7
%49 = and i64 %48, 255
%50 = trunc i64 %49 to i32
store i32 %50, i32* %12
%51 = load i64, i64* %7
%52 = udiv i64 %51, 256
store i64 %52, i64* %7
%53 = load i64, i64* %7
%54 = and i64 %53, 255
%55 = trunc i64 %54 to i32
store i32 %55, i32* %13
%56 = load i64, i64* %7
%57 = udiv i64 %56, 256
store i64 %57, i64* %7
%58 = load i32, i32* %5
%59 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%60 = call i64 @getsockname(i32 %58, %struct.sockaddr* %59, i32* %8)
%61 = icmp slt i64 %60, 0
br i1 %61, label %62, label %66
62: ; preds = %34
%63 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0))
%64 = load i32, i32* %4
%65 = call i32 (i32, i8*, ...) @net_printf(i32 %64, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %3
br label %86
66: ; preds = %34
%67 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 0
%68 = load i32, i32* %67
%69 = call i32 @ntohs(i32 %68)
store i32 %69, i32* %9
%70 = load i32, i32* %9
%71 = and i32 %70, 255
store i32 %71, i32* %15
%72 = load i32, i32* %9
%73 = udiv i32 %72, 256
store i32 %73, i32* %9
%74 = load i32, i32* %9
%75 = and i32 %74, 255
store i32 %75, i32* %14
%76 = load i32, i32* %9
%77 = udiv i32 %76, 256
store i32 %77, i32* %9
%78 = load i32, i32* %4
%79 = load i32, i32* %10
%80 = load i32, i32* %11
%81 = load i32, i32* %12
%82 = load i32, i32* %13
%83 = load i32, i32* %14
%84 = load i32, i32* %15
%85 = call i32 (i32, i8*, ...) @net_printf(i32 %78, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.4, i64 0, i64 0), i32 %79, i32 %80, i32 %81, i32 %82, i32 %83, i32 %84)
store i32 0, i32* %3
br label %86
86: ; preds = %66, %62, %30, %21
%87 = load i32, i32* %3
ret i32 %87
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i64 }
%struct.sockaddr = type { i32 }
@.str = [10 x i8] c"ftpd_pasv\00"
@.str.1 = [28 x i8] c"521 PASV Connection Failed\0A\00"
@.str.2 = [16 x i8] c"getsockname(cs)\00"
@.str.3 = [19 x i8] c"getsockname(datap)\00"
@.str.4 = [33 x i8] c"227 PASV OK (%d,%d,%d,%d,%d,%d)\0A\00"
define i32 @ftpd_pasv(i32 %0, i32 %1) {
%3 = alloca %struct.sockaddr_in
%4 = alloca i32
%5 = bitcast %struct.sockaddr_in* %3 to i8*
%6 = bitcast i32* %4 to i8*
%7 = or i32 %1, %0
%8 = icmp sgt i32 %7, -1
br i1 %8, label %12, label %9
9: ; preds = %2
%10 = tail call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%11 = tail call i32 (i32, i8*, ...) @net_printf(i32 %0, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0))
br label %42
12: ; preds = %2
store i32 16, i32* %4
%13 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%14 = call i64 @getsockname(i32 %0, %struct.sockaddr* %13, i32* %4)
%15 = icmp slt i64 %14, 0
br i1 %15, label %16, label %19
16: ; preds = %12
%17 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
%18 = call i32 (i32, i8*, ...) @net_printf(i32 %0, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0))
br label %42
19: ; preds = %12
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1, i32 0
%21 = load i64, i64* %20
%22 = call i64 @getsockname(i32 %1, %struct.sockaddr* %13, i32* %4)
%23 = icmp slt i64 %22, 0
br i1 %23, label %24, label %27
24: ; preds = %19
%25 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0))
%26 = call i32 (i32, i8*, ...) @net_printf(i32 %0, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0))
br label %42
27: ; preds = %19
%28 = trunc i64 %21 to i32
%29 = lshr i32 %28, 24
%30 = lshr i32 %28, 16
%31 = and i32 %30, 255
%32 = lshr i32 %28, 8
%33 = and i32 %32, 255
%34 = and i32 %28, 255
%35 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
%36 = load i32, i32* %35
%37 = call i32 @ntohs(i32 %36)
%38 = and i32 %37, 255
%39 = lshr i32 %37, 8
%40 = and i32 %39, 255
%41 = call i32 (i32, i8*, ...) @net_printf(i32 %0, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.4, i64 0, i64 0), i32 %34, i32 %33, i32 %31, i32 %29, i32 %40, i32 %38)
br label %42
42: ; preds = %27, %24, %16, %9
%43 = phi i32 [ -1, %9 ], [ -1, %16 ], [ -1, %24 ], [ 0, %27 ]
ret i32 %43
}