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

/* Variables and functions */
int sam3HandshakeInternal(int);
int sam3tcpConnect(char *, int, int /*<<< orphan*/ *);
int sam3Handshake(const char *hostname, int port, uint32_t *ip) {
  int fd;
  if ((fd = sam3tcpConnect(
           (hostname == NULL || !hostname[0] ? "localhost" : hostname),
           (port < 1 || port > 65535 ? 7656 : port), ip)) < 0)
    return -1;
  return sam3HandshakeInternal(fd);
}


x86 assembly (clang, -O0):
<sam3Handshake>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     114b <sam3Handshake+0x2b>
mov    -0x10(%rbp),%rax
cmpb   $0x0,(%rax)
jne    115b <sam3Handshake+0x3b>
lea    0xeae(%rip),%rax
mov    %rax,-0x30(%rbp)
jmp    1163 <sam3Handshake+0x43>
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,-0x38(%rbp)
cmpl   $0x1,-0x14(%rbp)
jl     1182 <sam3Handshake+0x62>
cmpl   $0xffff,-0x14(%rbp)
jle    118f <sam3Handshake+0x6f>
mov    $0x1de8,%eax
mov    %eax,-0x3c(%rbp)
jmp    1195 <sam3Handshake+0x75>
mov    -0x14(%rbp),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x38(%rbp),%rdi
mov    -0x3c(%rbp),%esi
mov    -0x20(%rbp),%rdx
call   1040 <sam3tcpConnect@plt>
mov    %eax,-0x24(%rbp)
cmp    $0x0,%eax
jge    11bd <sam3Handshake+0x9d>
movl   $0xffffffff,-0x4(%rbp)
jmp    11c8 <sam3Handshake+0xa8>
mov    -0x24(%rbp),%edi
call   1030 <sam3HandshakeInternal@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sam3Handshake>:
push   %rax
test   %rdi,%rdi
je     1139 <sam3Handshake+0x19>
mov    %rdi,%rax
cmpb   $0x0,(%rdi)
lea    0xecd(%rip),%rdi
cmovne %rax,%rdi
jmp    1140 <sam3Handshake+0x20>
lea    0xec0(%rip),%rdi
lea    -0x10000(%rsi),%eax
cmp    $0xffff0001,%eax
mov    $0x1de8,%eax
cmovae %esi,%eax
mov    %eax,%esi
call   1040 <sam3tcpConnect@plt>
test   %eax,%eax
js     1166 <sam3Handshake+0x46>
mov    %eax,%edi
pop    %rax
jmp    1030 <sam3HandshakeInternal@plt>
mov    $0xffffffff,%eax
pop    %rcx
ret

LLVM IR (-O0):
@.str = [10 x i8] c"localhost\00"
define i32 @sam3Handshake(i8* %0, i32 %1, i32* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32*
%8 = alloca i32
store i8* %0, i8** %5
store i32 %1, i32* %6
store i32* %2, i32** %7
%9 = load i8*, i8** %5
%10 = icmp eq i8* %9, null
br i1 %10, label %16, label %11
11: ; preds = %3
%12 = load i8*, i8** %5
%13 = getelementptr inbounds i8, i8* %12, i64 0
%14 = load i8, i8* %13
%15 = icmp ne i8 %14, 0
br i1 %15, label %17, label %16
16: ; preds = %11, %3
br label %19
17: ; preds = %11
%18 = load i8*, i8** %5
br label %19
19: ; preds = %17, %16
%20 = phi i8* [ getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), %16 ], [ %18, %17 ]
%21 = load i32, i32* %6
%22 = icmp slt i32 %21, 1
br i1 %22, label %26, label %23
23: ; preds = %19
%24 = load i32, i32* %6
%25 = icmp sgt i32 %24, 65535
br i1 %25, label %26, label %27
26: ; preds = %23, %19
br label %29
27: ; preds = %23
%28 = load i32, i32* %6
br label %29
29: ; preds = %27, %26
%30 = phi i32 [ 7656, %26 ], [ %28, %27 ]
%31 = load i32*, i32** %7
%32 = call i32 @sam3tcpConnect(i8* %20, i32 %30, i32* %31)
store i32 %32, i32* %8
%33 = icmp slt i32 %32, 0
br i1 %33, label %34, label %35
34: ; preds = %29
store i32 -1, i32* %4
br label %38
35: ; preds = %29
%36 = load i32, i32* %8
%37 = call i32 @sam3HandshakeInternal(i32 %36)
store i32 %37, i32* %4
br label %38
38: ; preds = %35, %34
%39 = load i32, i32* %4
ret i32 %39
}

LLVM IR (-O2):
@.str = [10 x i8] c"localhost\00"
define i32 @sam3Handshake(i8* %0, i32 %1, i32* %2) {
%4 = icmp eq i8* %0, null
br i1 %4, label %9, label %5
5: ; preds = %3
%6 = load i8, i8* %0
%7 = icmp eq i8 %6, 0
%8 = select i1 %7, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* %0
br label %9
9: ; preds = %5, %3
%10 = phi i8* [ getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), %3 ], [ %8, %5 ]
%11 = add i32 %1, -65536
%12 = icmp ult i32 %11, -65535
%13 = select i1 %12, i32 7656, i32 %1
%14 = tail call i32 @sam3tcpConnect(i8* %10, i32 %13, i32* %2)
%15 = icmp slt i32 %14, 0
br i1 %15, label %18, label %16
16: ; preds = %9
%17 = tail call i32 @sam3HandshakeInternal(i32 %14)
br label %18
18: ; preds = %9, %16
%19 = phi i32 [ %17, %16 ], [ -1, %9 ]
ret i32 %19
}