#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 sockaddr_in {
  int /*<<< orphan*/ sin_addr;
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ servaddr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
scalar_t__ connect(int, struct sockaddr *, int);
int /*<<< orphan*/ htons(int);
scalar_t__ inet_pton(int /*<<< orphan*/, char const *, int /*<<< orphan*/ *);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int seed_tcp_dial(const char *host, int port) {
  int sockfd;
  struct sockaddr_in servaddr;
  if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
    return -1;
  memset(&servaddr, '0', sizeof(servaddr));
  servaddr.sin_family = AF_INET;
  servaddr.sin_port = htons(port);
  if (inet_pton(AF_INET, host, &servaddr.sin_addr) < 0)
    return -1;
  if (connect(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0)
    return -1;
  return sockfd;
}


x86 assembly (clang, -O0):
<seed_tcp_dial>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    0x2e82(%rip),%rax
mov    (%rax),%edi
mov    0x2e69(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jge    1190 <seed_tcp_dial+0x40>
movl   $0xffffffff,-0x4(%rbp)
jmp    1213 <seed_tcp_dial+0xc3>
lea    -0x28(%rbp),%rdi
mov    $0x30,%esi
mov    $0xc,%edx
call   1040 <memset@plt>
mov    0x2e3e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x14(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0x24(%rbp)
mov    0x2e27(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rsi
lea    -0x28(%rbp),%rdx
call   1050 <inet_pton@plt>
cmp    $0x0,%rax
jge    11e6 <seed_tcp_dial+0x96>
movl   $0xffffffff,-0x4(%rbp)
jmp    1213 <seed_tcp_dial+0xc3>
mov    -0x18(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0xc,%edx
call   1060 <connect@plt>
cmp    $0x0,%rax
jge    120d <seed_tcp_dial+0xbd>
movl   $0xffffffff,-0x4(%rbp)
jmp    1213 <seed_tcp_dial+0xc3>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<seed_tcp_dial>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %esi,%r15d
mov    %rdi,%r14
mov    0x2e7d(%rip),%r13
mov    0x0(%r13),%edi
mov    0x2e62(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
mov    $0xffffffff,%ebp
test   %eax,%eax
js     11e5 <seed_tcp_dial+0x95>
mov    %eax,%ebx
lea    0x8(%rsp),%r12
mov    %r12,%rdi
mov    $0x30,%esi
mov    $0xc,%edx
call   1040 <memset@plt>
mov    0x0(%r13),%eax
mov    %eax,0x10(%rsp)
mov    %r15d,%edi
call   1030 <htons@plt>
mov    %eax,0xc(%rsp)
mov    0x0(%r13),%edi
mov    %r14,%rsi
mov    %r12,%rdx
call   1050 <inet_pton@plt>
test   %rax,%rax
js     11e5 <seed_tcp_dial+0x95>
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
mov    $0xc,%edx
call   1060 <connect@plt>
test   %rax,%rax
mov    $0xffffffff,%ebp
cmovns %ebx,%ebp
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
define i32 @seed_tcp_dial(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca %struct.sockaddr_in
store i8* %0, i8** %4
store i32 %1, i32* %5
%8 = load i32, i32* @AF_INET
%9 = load i32, i32* @SOCK_STREAM
%10 = call i32 @socket(i32 %8, i32 %9, i32 0)
store i32 %10, i32* %6
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %13
12: ; preds = %2
store i32 -1, i32* %3
br label %34
13: ; preds = %2
%14 = call i32 @memset(%struct.sockaddr_in* %7, i32 48, i32 12)
%15 = load i32, i32* @AF_INET
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 2
store i32 %15, i32* %16
%17 = load i32, i32* %5
%18 = call i32 @htons(i32 %17)
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 1
store i32 %18, i32* %19
%20 = load i32, i32* @AF_INET
%21 = load i8*, i8** %4
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 0
%23 = call i64 @inet_pton(i32 %20, i8* %21, i32* %22)
%24 = icmp slt i64 %23, 0
br i1 %24, label %25, label %26
25: ; preds = %13
store i32 -1, i32* %3
br label %34
26: ; preds = %13
%27 = load i32, i32* %6
%28 = bitcast %struct.sockaddr_in* %7 to %struct.sockaddr*
%29 = call i64 @connect(i32 %27, %struct.sockaddr* %28, i32 12)
%30 = icmp slt i64 %29, 0
br i1 %30, label %31, label %32
31: ; preds = %26
store i32 -1, i32* %3
br label %34
32: ; preds = %26
%33 = load i32, i32* %6
store i32 %33, i32* %3
br label %34
34: ; preds = %32, %31, %25, %12
%35 = load i32, i32* %3
ret i32 %35
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
define i32 @seed_tcp_dial(i8* %0, i32 %1) {
%3 = alloca %struct.sockaddr_in
%4 = bitcast %struct.sockaddr_in* %3 to i8*
%5 = load i32, i32* @AF_INET
%6 = load i32, i32* @SOCK_STREAM
%7 = tail call i32 @socket(i32 %5, i32 %6, i32 0)
%8 = icmp slt i32 %7, 0
br i1 %8, label %24, label %9
9: ; preds = %2
%10 = call i32 @memset(%struct.sockaddr_in* %3, i32 48, i32 12)
%11 = load i32, i32* @AF_INET
%12 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %11, i32* %12
%13 = call i32 @htons(i32 %1)
%14 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
store i32 %13, i32* %14
%15 = load i32, i32* @AF_INET
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
%17 = call i64 @inet_pton(i32 %15, i8* %0, i32* %16)
%18 = icmp slt i64 %17, 0
br i1 %18, label %24, label %19
19: ; preds = %9
%20 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%21 = call i64 @connect(i32 %7, %struct.sockaddr* %20, i32 12)
%22 = icmp slt i64 %21, 0
%23 = select i1 %22, i32 -1, i32 %7
br label %24
24: ; preds = %19, %9, %2
%25 = phi i32 [ -1, %2 ], [ -1, %9 ], [ %23, %19 ]
ret i32 %25
}