#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*/ buffer;

/* Variables and functions */
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
scalar_t__ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int openSocketConnection(char *, char *);
int /*<<< orphan*/ perror(char *);
int recv(int, char **, int, int /*<<< orphan*/);
int /*<<< orphan*/ sscanf(char *, char *, char *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strlen(char *);
scalar_t__ strncasecmp(char *, char *, int /*<<< orphan*/);
int openOTPServerSocket(char *Hostname, char *PortStr) {
  char buffer[1024];
  char newPortStr[16];
  int sSock, tSock;
  int recvcount;
  memset(buffer, 0, sizeof(buffer));
  if ((sSock = openSocketConnection(Hostname, PortStr)) < 0) {
    perror("Open socket");
    exit(1);
  }
  recvcount = recv(sSock, &buffer, sizeof(buffer), 0);
  if (recvcount <= 0) {
    perror("Received no data from server");
    return (-1);
  }
  close(sSock);
  if ((strncasecmp(buffer, "RCN", strlen("RCN"))) != 0) {
    fprintf(stderr, "Received unrecognized command from server \"%s\"\n",
            buffer);
    return (-1);
  }
  sscanf(buffer, "%*3s %s", newPortStr);
  tSock = openSocketConnection(Hostname, newPortStr);
  if (tSock < 0) {
    perror("Reconnect socket");
    exit(1);
  }
  0 && fprintf(stderr, "Reconnecting on port %s\n", newPortStr);
  return (tSock);
}


x86 assembly (clang, -O0):
<openOTPServerSocket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x450,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
lea    -0x420(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1050 <memset@plt>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1070 <openSocketConnection@plt>
mov    %eax,-0x434(%rbp)
cmp    $0x0,%eax
jge    11f8 <openOTPServerSocket+0x58>
lea    0xe17(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    -0x434(%rbp),%edi
lea    -0x420(%rbp),%rsi
mov    $0x400,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0x43c(%rbp)
cmpl   $0x0,-0x43c(%rbp)
jg     123c <openOTPServerSocket+0x9c>
lea    0xde1(%rip),%rdi
call   10b0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1304 <openOTPServerSocket+0x164>
mov    -0x434(%rbp),%edi
call   1060 <close@plt>
lea    -0x420(%rbp),%rax
mov    %rax,-0x448(%rbp)
lea    0xdcd(%rip),%rdi
call   1040 <strlen@plt>
mov    -0x448(%rbp),%rdi
mov    %eax,%edx
lea    0xdb8(%rip),%rsi
call   1090 <strncasecmp@plt>
cmp    $0x0,%rax
je     12a8 <openOTPServerSocket+0x108>
mov    0x2d59(%rip),%rax
mov    (%rax),%edi
lea    -0x420(%rbp),%rdx
lea    0xd96(%rip),%rsi
call   1080 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1304 <openOTPServerSocket+0x164>
lea    -0x420(%rbp),%rdi
lea    -0x430(%rbp),%rdx
lea    0xda0(%rip),%rsi
call   10a0 <sscanf@plt>
mov    -0x10(%rbp),%rdi
lea    -0x430(%rbp),%rsi
call   1070 <openSocketConnection@plt>
mov    %eax,-0x438(%rbp)
cmpl   $0x0,-0x438(%rbp)
jge    12fb <openOTPServerSocket+0x15b>
lea    0xd79(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    -0x438(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x450,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<openOTPServerSocket>:
push   %rbp
push   %rbx
sub    $0x418,%rsp
mov    %rsi,%rbp
mov    %rdi,%rbx
lea    0x10(%rsp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1050 <memset@plt>
mov    %rbx,%rdi
mov    %rbp,%rsi
call   1070 <openSocketConnection@plt>
test   %eax,%eax
js     1275 <openOTPServerSocket+0xd5>
mov    %eax,%ebp
lea    0x10(%rsp),%rsi
mov    %eax,%edi
mov    $0x400,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
test   %eax,%eax
jle    1267 <openOTPServerSocket+0xc7>
mov    %ebp,%edi
call   1060 <close@plt>
lea    0xe2f(%rip),%rbp
mov    %rbp,%rdi
call   1040 <strlen@plt>
lea    0x10(%rsp),%rdi
mov    %rbp,%rsi
mov    %eax,%edx
call   1090 <strncasecmp@plt>
test   %rax,%rax
je     1237 <openOTPServerSocket+0x97>
mov    0x2dc3(%rip),%rax
mov    (%rax),%edi
lea    0xe07(%rip),%rsi
lea    0x10(%rsp),%rdx
call   1080 <fprintf@plt>
mov    $0xffffffff,%eax
jmp    125d <openOTPServerSocket+0xbd>
lea    0xe1f(%rip),%rsi
lea    0x10(%rsp),%rdi
mov    %rsp,%rbp
mov    %rbp,%rdx
call   10a0 <sscanf@plt>
mov    %rbx,%rdi
mov    %rbp,%rsi
call   1070 <openSocketConnection@plt>
test   %eax,%eax
js     127e <openOTPServerSocket+0xde>
add    $0x418,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xd9e(%rip),%rdi
call   10b0 <perror@plt>
jmp    1230 <openOTPServerSocket+0x90>
lea    0xd84(%rip),%rdi
jmp    1285 <openOTPServerSocket+0xe5>
lea    0xde0(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
@.str = [12 x i8] c"Open socket\00"
@.str.1 = [29 x i8] c"Received no data from server\00"
@.str.2 = [4 x i8] c"RCN\00"
@stderr = global i32 0
@.str.3 = [48 x i8] c"Received unrecognized command from server \22%s\22\0A\00"
@.str.4 = [8 x i8] c"%*3s %s\00"
@.str.5 = [17 x i8] c"Reconnect socket\00"
define i32 @openOTPServerSocket(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca [1024 x i8]
%7 = alloca [16 x i8]
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
%11 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%12 = call i32 @memset(i8* %11, i32 0, i32 1024)
%13 = load i8*, i8** %4
%14 = load i8*, i8** %5
%15 = call i32 @openSocketConnection(i8* %13, i8* %14)
store i32 %15, i32* %8
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %2
%18 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%19 = call i32 @exit(i32 1)
unreachable
20: ; preds = %2
%21 = load i32, i32* %8
%22 = bitcast [1024 x i8]* %6 to i8**
%23 = call i32 @recv(i32 %21, i8** %22, i32 1024, i32 0)
store i32 %23, i32* %10
%24 = load i32, i32* %10
%25 = icmp sle i32 %24, 0
br i1 %25, label %26, label %28
26: ; preds = %20
%27 = call i32 @perror(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %3
br label %53
28: ; preds = %20
%29 = load i32, i32* %8
%30 = call i32 @close(i32 %29)
%31 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%32 = call i32 @strlen(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0))
%33 = call i64 @strncasecmp(i8* %31, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 %32)
%34 = icmp ne i64 %33, 0
br i1 %34, label %35, label %39
35: ; preds = %28
%36 = load i32, i32* @stderr
%37 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%38 = call i64 @fprintf(i32 %36, i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.3, i64 0, i64 0), i8* %37)
store i32 -1, i32* %3
br label %53
39: ; preds = %28
%40 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%41 = getelementptr inbounds [16 x i8], [16 x i8]* %7, i64 0, i64 0
%42 = call i32 @sscanf(i8* %40, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0), i8* %41)
%43 = load i8*, i8** %4
%44 = getelementptr inbounds [16 x i8], [16 x i8]* %7, i64 0, i64 0
%45 = call i32 @openSocketConnection(i8* %43, i8* %44)
store i32 %45, i32* %9
%46 = load i32, i32* %9
%47 = icmp slt i32 %46, 0
br i1 %47, label %48, label %51
48: ; preds = %39
%49 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.5, i64 0, i64 0))
%50 = call i32 @exit(i32 1)
unreachable
51: ; preds = %39
%52 = load i32, i32* %9
store i32 %52, i32* %3
br label %53
53: ; preds = %51, %35, %26
%54 = load i32, i32* %3
ret i32 %54
}

LLVM IR (-O2):
@.str = [12 x i8] c"Open socket\00"
@.str.1 = [29 x i8] c"Received no data from server\00"
@.str.2 = [4 x i8] c"RCN\00"
@stderr = global i32 0
@.str.3 = [48 x i8] c"Received unrecognized command from server \22%s\22\0A\00"
@.str.4 = [8 x i8] c"%*3s %s\00"
@.str.5 = [17 x i8] c"Reconnect socket\00"
define i32 @openOTPServerSocket(i8* %0, i8* %1) {
%3 = alloca [1024 x i8]
%4 = alloca [16 x i8]
%5 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%6 = getelementptr inbounds [16 x i8], [16 x i8]* %4, i64 0, i64 0
%7 = call i32 @memset(i8* %5, i32 0, i32 1024)
%8 = call i32 @openSocketConnection(i8* %0, i8* %1)
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %13
10: ; preds = %2
%11 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%12 = call i32 @exit(i32 1)
unreachable
13: ; preds = %2
%14 = bitcast [1024 x i8]* %3 to i8**
%15 = call i32 @recv(i32 %8, i8** %14, i32 1024, i32 0)
%16 = icmp slt i32 %15, 1
br i1 %16, label %17, label %19
17: ; preds = %13
%18 = call i32 @perror(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0))
br label %34
19: ; preds = %13
%20 = call i32 @close(i32 %8)
%21 = call i32 @strlen(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0))
%22 = call i64 @strncasecmp(i8* %5, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 %21)
%23 = icmp eq i64 %22, 0
br i1 %23, label %27, label %24
24: ; preds = %19
%25 = load i32, i32* @stderr
%26 = call i64 @fprintf(i32 %25, i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.3, i64 0, i64 0), i8* %5)
br label %34
27: ; preds = %19
%28 = call i32 @sscanf(i8* %5, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0), i8* %6)
%29 = call i32 @openSocketConnection(i8* %0, i8* %6)
%30 = icmp slt i32 %29, 0
br i1 %30, label %31, label %34
31: ; preds = %27
%32 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.5, i64 0, i64 0))
%33 = call i32 @exit(i32 1)
unreachable
34: ; preds = %27, %24, %17
%35 = phi i32 [ -1, %17 ], [ -1, %24 ], [ %29, %27 ]
ret i32 %35
}