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

/* Variables and functions */
int /*<<< orphan*/ close(int);
int netConnect(char *, int);
scalar_t__ readReadyWait(int, int);
int /*<<< orphan*/ safef(char *, int, char *, char *);
int /*<<< orphan*/ sendFtpCommand(int, char *, int /*<<< orphan*/ *, int *);
int openFtpControlSocket(char *host, int port, char *user, char *password) {
  int sd = netConnect(host, port);
  if (sd < 0)
    return -1;
  if (readReadyWait(sd, 1000000))
    sendFtpCommand(sd, "", NULL, NULL);
  char cmd[256];
  int retCode = 0;
  safef(cmd, sizeof(cmd), "USER %s\r\n", user);
  if (!sendFtpCommand(sd, cmd, NULL, &retCode)) {
    close(sd);
    return -1;
  }
  if (retCode == 331) {
    safef(cmd, sizeof(cmd), "PASS %s\r\n", password);
    if (!sendFtpCommand(sd, cmd, NULL, NULL)) {
      close(sd);
      return -1;
    }
  }
  if (!sendFtpCommand(sd, "TYPE I\r\n", NULL, NULL)) {
    close(sd);
    return -1;
  }
  return sd;
}


x86 assembly (clang, -O0):
<openFtpControlSocket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x140,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1070 <netConnect@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jge    118f <openFtpControlSocket+0x3f>
movl   $0xffffffff,-0x4(%rbp)
jmp    12b6 <openFtpControlSocket+0x166>
mov    -0x2c(%rbp),%edi
mov    $0xf4240,%esi
call   1060 <readReadyWait@plt>
cmp    $0x0,%rax
je     11bc <openFtpControlSocket+0x6c>
mov    -0x2c(%rbp),%edi
lea    0xe6c(%rip),%rsi
xor    %eax,%eax
mov    %eax,%ecx
mov    %rcx,%rdx
call   1040 <sendFtpCommand@plt>
movl   $0x0,-0x134(%rbp)
lea    -0x130(%rbp),%rdi
mov    -0x20(%rbp),%rcx
mov    $0x100,%esi
lea    0xe23(%rip),%rdx
call   1050 <safef@plt>
mov    -0x2c(%rbp),%edi
lea    -0x130(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
lea    -0x134(%rbp),%rcx
call   1040 <sendFtpCommand@plt>
cmp    $0x0,%eax
jne    1219 <openFtpControlSocket+0xc9>
mov    -0x2c(%rbp),%edi
call   1030 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12b6 <openFtpControlSocket+0x166>
cmpl   $0x14b,-0x134(%rbp)
jne    127d <openFtpControlSocket+0x12d>
lea    -0x130(%rbp),%rdi
mov    -0x28(%rbp),%rcx
mov    $0x100,%esi
lea    0xdca(%rip),%rdx
call   1050 <safef@plt>
mov    -0x2c(%rbp),%edi
lea    -0x130(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
mov    %rcx,%rdx
call   1040 <sendFtpCommand@plt>
cmp    $0x0,%eax
jne    1278 <openFtpControlSocket+0x128>
mov    -0x2c(%rbp),%edi
call   1030 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12b6 <openFtpControlSocket+0x166>
jmp    127d <openFtpControlSocket+0x12d>
mov    -0x2c(%rbp),%edi
lea    0xd8d(%rip),%rsi
xor    %eax,%eax
mov    %eax,%ecx
mov    %rcx,%rdx
call   1040 <sendFtpCommand@plt>
cmp    $0x0,%eax
jne    12b0 <openFtpControlSocket+0x160>
mov    -0x2c(%rbp),%edi
call   1030 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12b6 <openFtpControlSocket+0x166>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x140,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<openFtpControlSocket>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x118,%rsp
mov    %rcx,%r14
mov    %rdx,%r15
call   1070 <netConnect@plt>
test   %eax,%eax
js     1224 <openFtpControlSocket+0xd4>
mov    %eax,%ebx
mov    %eax,%edi
mov    $0xf4240,%esi
call   1060 <readReadyWait@plt>
test   %rax,%rax
je     1196 <openFtpControlSocket+0x46>
lea    0xe91(%rip),%rsi
mov    %ebx,%edi
xor    %edx,%edx
xor    %ecx,%ecx
call   1040 <sendFtpCommand@plt>
movl   $0x0,0xc(%rsp)
lea    0xe5b(%rip),%rdx
lea    0x10(%rsp),%r12
mov    %r12,%rdi
mov    $0x100,%esi
mov    %r15,%rcx
call   1050 <safef@plt>
lea    0xc(%rsp),%rcx
mov    %ebx,%edi
mov    %r12,%rsi
xor    %edx,%edx
call   1040 <sendFtpCommand@plt>
test   %eax,%eax
je     121d <openFtpControlSocket+0xcd>
cmpl   $0x14b,0xc(%rsp)
jne    1207 <openFtpControlSocket+0xb7>
lea    0xe2a(%rip),%rdx
lea    0x10(%rsp),%r15
mov    %r15,%rdi
mov    $0x100,%esi
mov    %r14,%rcx
call   1050 <safef@plt>
mov    %ebx,%edi
mov    %r15,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
call   1040 <sendFtpCommand@plt>
test   %eax,%eax
je     121d <openFtpControlSocket+0xcd>
lea    0xe06(%rip),%rsi
mov    %ebx,%edi
xor    %edx,%edx
xor    %ecx,%ecx
call   1040 <sendFtpCommand@plt>
test   %eax,%eax
jne    1229 <openFtpControlSocket+0xd9>
mov    %ebx,%edi
call   1030 <close@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x118,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [1 x i8] zeroinitializer
@.str.1 = [10 x i8] c"USER %s\0D\0A\00"
@.str.2 = [10 x i8] c"PASS %s\0D\0A\00"
@.str.3 = [9 x i8] c"TYPE I\0D\0A\00"
define i32 @openFtpControlSocket(i8* %0, i32 %1, i8* %2, i8* %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i32
%11 = alloca [256 x i8]
%12 = alloca i32
store i8* %0, i8** %6
store i32 %1, i32* %7
store i8* %2, i8** %8
store i8* %3, i8** %9
%13 = load i8*, i8** %6
%14 = load i32, i32* %7
%15 = call i32 @netConnect(i8* %13, i32 %14)
store i32 %15, i32* %10
%16 = load i32, i32* %10
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %19
18: ; preds = %4
store i32 -1, i32* %5
br label %61
19: ; preds = %4
%20 = load i32, i32* %10
%21 = call i64 @readReadyWait(i32 %20, i32 1000000)
%22 = icmp ne i64 %21, 0
br i1 %22, label %23, label %26
23: ; preds = %19
%24 = load i32, i32* %10
%25 = call i32 @sendFtpCommand(i32 %24, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0), i32* null, i32* null)
br label %26
26: ; preds = %23, %19
store i32 0, i32* %12
%27 = getelementptr inbounds [256 x i8], [256 x i8]* %11, i64 0, i64 0
%28 = load i8*, i8** %8
%29 = call i32 @safef(i8* %27, i32 256, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8* %28)
%30 = load i32, i32* %10
%31 = getelementptr inbounds [256 x i8], [256 x i8]* %11, i64 0, i64 0
%32 = call i32 @sendFtpCommand(i32 %30, i8* %31, i32* null, i32* %12)
%33 = icmp ne i32 %32, 0
br i1 %33, label %37, label %34
34: ; preds = %26
%35 = load i32, i32* %10
%36 = call i32 @close(i32 %35)
store i32 -1, i32* %5
br label %61
37: ; preds = %26
%38 = load i32, i32* %12
%39 = icmp eq i32 %38, 331
br i1 %39, label %40, label %52
40: ; preds = %37
%41 = getelementptr inbounds [256 x i8], [256 x i8]* %11, i64 0, i64 0
%42 = load i8*, i8** %9
%43 = call i32 @safef(i8* %41, i32 256, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i8* %42)
%44 = load i32, i32* %10
%45 = getelementptr inbounds [256 x i8], [256 x i8]* %11, i64 0, i64 0
%46 = call i32 @sendFtpCommand(i32 %44, i8* %45, i32* null, i32* null)
%47 = icmp ne i32 %46, 0
br i1 %47, label %51, label %48
48: ; preds = %40
%49 = load i32, i32* %10
%50 = call i32 @close(i32 %49)
store i32 -1, i32* %5
br label %61
51: ; preds = %40
br label %52
52: ; preds = %51, %37
%53 = load i32, i32* %10
%54 = call i32 @sendFtpCommand(i32 %53, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i32* null, i32* null)
%55 = icmp ne i32 %54, 0
br i1 %55, label %59, label %56
56: ; preds = %52
%57 = load i32, i32* %10
%58 = call i32 @close(i32 %57)
store i32 -1, i32* %5
br label %61
59: ; preds = %52
%60 = load i32, i32* %10
store i32 %60, i32* %5
br label %61
61: ; preds = %59, %56, %48, %34, %18
%62 = load i32, i32* %5
ret i32 %62
}

LLVM IR (-O2):
@.str = [1 x i8] zeroinitializer
@.str.1 = [10 x i8] c"USER %s\0D\0A\00"
@.str.2 = [10 x i8] c"PASS %s\0D\0A\00"
@.str.3 = [9 x i8] c"TYPE I\0D\0A\00"
define i32 @openFtpControlSocket(i8* %0, i32 %1, i8* %2, i8* %3) {
%5 = alloca [256 x i8]
%6 = alloca i32
%7 = tail call i32 @netConnect(i8* %0, i32 %1)
%8 = icmp slt i32 %7, 0
br i1 %8, label %34, label %9
9: ; preds = %4
%10 = tail call i64 @readReadyWait(i32 %7, i32 1000000)
%11 = icmp eq i64 %10, 0
br i1 %11, label %14, label %12
12: ; preds = %9
%13 = tail call i32 @sendFtpCommand(i32 %7, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0), i32* null, i32* null)
br label %14
14: ; preds = %12, %9
%15 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%16 = bitcast i32* %6 to i8*
store i32 0, i32* %6
%17 = call i32 @safef(i8* %15, i32 256, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8* %2)
%18 = call i32 @sendFtpCommand(i32 %7, i8* %15, i32* null, i32* %6)
%19 = icmp eq i32 %18, 0
br i1 %19, label %30, label %20
20: ; preds = %14
%21 = load i32, i32* %6
%22 = icmp eq i32 %21, 331
br i1 %22, label %23, label %27
23: ; preds = %20
%24 = call i32 @safef(i8* %15, i32 256, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i8* %3)
%25 = call i32 @sendFtpCommand(i32 %7, i8* %15, i32* null, i32* null)
%26 = icmp eq i32 %25, 0
br i1 %26, label %30, label %27
27: ; preds = %23, %20
%28 = call i32 @sendFtpCommand(i32 %7, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i32* null, i32* null)
%29 = icmp eq i32 %28, 0
br i1 %29, label %30, label %32
30: ; preds = %27, %23, %14
%31 = call i32 @close(i32 %7)
br label %32
32: ; preds = %30, %27
%33 = phi i32 [ %7, %27 ], [ -1, %30 ]
br label %34
34: ; preds = %4, %32
%35 = phi i32 [ %33, %32 ], [ -1, %4 ]
ret i32 %35
}