#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*/ trueval;
struct sockaddr_in {
  int dummy;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ socketaddr;

/* Variables and functions */
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEADDR;
scalar_t__ connect(int, struct sockaddr *, int);
int /*<<< orphan*/ prototype(char const *);
int /*<<< orphan*/ resolveproto(char const *);
int /*<<< orphan*/ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/,
                              int *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ socketaddr_host(struct sockaddr_in *, char const *);
int /*<<< orphan*/ socketaddr_init(struct sockaddr_in *);
int /*<<< orphan*/ socketaddr_service(struct sockaddr_in *, char const *,
                                      char const *);
int clientconnect(const char *host, const char *port, const char *proto) {
  struct sockaddr_in socketaddr;
  int sockid;
  int trueval = 1;
  socketaddr_init(&socketaddr);
  socketaddr_service(&socketaddr, port, proto);
  socketaddr_host(&socketaddr, host);
  sockid = socket(PF_INET, prototype(proto), resolveproto(proto));
  if (sockid < 0) {
    return -1;
  }
  setsockopt(sockid, SOL_SOCKET, SO_REUSEADDR, &trueval, sizeof(trueval));
  if (connect(sockid, (struct sockaddr *)&socketaddr, sizeof(socketaddr)) < 0) {
    return -1;
  }
  return sockid;
}


x86 assembly (clang, -O0):
<clientconnect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movl   $0x1,-0x30(%rbp)
lea    -0x28(%rbp),%rdi
call   1070 <socketaddr_init@plt>
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
lea    -0x28(%rbp),%rdi
call   1060 <socketaddr_service@plt>
mov    -0x10(%rbp),%rsi
lea    -0x28(%rbp),%rdi
call   1040 <socketaddr_host@plt>
mov    0x2e0f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x38(%rbp)
mov    -0x20(%rbp),%rdi
call   1050 <prototype@plt>
mov    %eax,-0x34(%rbp)
mov    -0x20(%rbp),%rdi
call   1090 <resolveproto@plt>
mov    -0x38(%rbp),%edi
mov    -0x34(%rbp),%esi
mov    %eax,%edx
call   10a0 <socket@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jge    1209 <clientconnect+0x89>
movl   $0xffffffff,-0x4(%rbp)
jmp    125a <clientconnect+0xda>
mov    -0x2c(%rbp),%edi
mov    0x2dd5(%rip),%rax
mov    (%rax),%esi
mov    0x2db4(%rip),%rax
mov    (%rax),%edx
lea    -0x30(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    -0x2c(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0x4,%edx
call   1080 <connect@plt>
cmp    $0x0,%rax
jge    1254 <clientconnect+0xd4>
movl   $0xffffffff,-0x4(%rbp)
jmp    125a <clientconnect+0xda>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<clientconnect>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rdx,%rbx
mov    %rsi,%r14
mov    %rdi,%r15
movl   $0x1,0xc(%rsp)
lea    0x8(%rsp),%r12
mov    %r12,%rdi
call   1070 <socketaddr_init@plt>
mov    %r12,%rdi
mov    %r14,%rsi
mov    %rbx,%rdx
call   1060 <socketaddr_service@plt>
mov    %r12,%rdi
mov    %r15,%rsi
call   1040 <socketaddr_host@plt>
mov    0x2e0e(%rip),%rax
mov    (%rax),%r14d
mov    %rbx,%rdi
call   1050 <prototype@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1090 <resolveproto@plt>
mov    %r14d,%edi
mov    %ebp,%esi
mov    %eax,%edx
call   10a0 <socket@plt>
test   %eax,%eax
js     1233 <clientconnect+0xb3>
mov    %eax,%ebx
mov    0x2df0(%rip),%rax
mov    (%rax),%esi
mov    0x2dcf(%rip),%rax
mov    (%rax),%edx
lea    0xc(%rsp),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
mov    $0x4,%edx
call   1080 <connect@plt>
test   %rax,%rax
mov    $0xffffffff,%eax
cmovns %ebx,%eax
jmp    1238 <clientconnect+0xb8>
mov    $0xffffffff,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32 }
%struct.sockaddr = type { i32 }
@PF_INET = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
define i32 @clientconnect(i8* %0, i8* %1, i8* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca %struct.sockaddr_in
%9 = alloca i32
%10 = alloca i32
store i8* %0, i8** %5
store i8* %1, i8** %6
store i8* %2, i8** %7
store i32 1, i32* %10
%11 = call i32 @socketaddr_init(%struct.sockaddr_in* %8)
%12 = load i8*, i8** %6
%13 = load i8*, i8** %7
%14 = call i32 @socketaddr_service(%struct.sockaddr_in* %8, i8* %12, i8* %13)
%15 = load i8*, i8** %5
%16 = call i32 @socketaddr_host(%struct.sockaddr_in* %8, i8* %15)
%17 = load i32, i32* @PF_INET
%18 = load i8*, i8** %7
%19 = call i32 @prototype(i8* %18)
%20 = load i8*, i8** %7
%21 = call i32 @resolveproto(i8* %20)
%22 = call i32 @socket(i32 %17, i32 %19, i32 %21)
store i32 %22, i32* %9
%23 = load i32, i32* %9
%24 = icmp slt i32 %23, 0
br i1 %24, label %25, label %26
25: ; preds = %3
store i32 -1, i32* %4
br label %38
26: ; preds = %3
%27 = load i32, i32* %9
%28 = load i32, i32* @SOL_SOCKET
%29 = load i32, i32* @SO_REUSEADDR
%30 = call i32 @setsockopt(i32 %27, i32 %28, i32 %29, i32* %10, i32 4)
%31 = load i32, i32* %9
%32 = bitcast %struct.sockaddr_in* %8 to %struct.sockaddr*
%33 = call i64 @connect(i32 %31, %struct.sockaddr* %32, i32 4)
%34 = icmp slt i64 %33, 0
br i1 %34, label %35, label %36
35: ; preds = %26
store i32 -1, i32* %4
br label %38
36: ; preds = %26
%37 = load i32, i32* %9
store i32 %37, i32* %4
br label %38
38: ; preds = %36, %35, %25
%39 = load i32, i32* %4
ret i32 %39
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32 }
%struct.sockaddr = type { i32 }
@PF_INET = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
define i32 @clientconnect(i8* %0, i8* %1, i8* %2) {
%4 = alloca %struct.sockaddr_in
%5 = alloca i32
%6 = bitcast %struct.sockaddr_in* %4 to i8*
%7 = bitcast i32* %5 to i8*
store i32 1, i32* %5
%8 = call i32 @socketaddr_init(%struct.sockaddr_in* %4)
%9 = call i32 @socketaddr_service(%struct.sockaddr_in* %4, i8* %1, i8* %2)
%10 = call i32 @socketaddr_host(%struct.sockaddr_in* %4, i8* %0)
%11 = load i32, i32* @PF_INET
%12 = call i32 @prototype(i8* %2)
%13 = call i32 @resolveproto(i8* %2)
%14 = call i32 @socket(i32 %11, i32 %12, i32 %13)
%15 = icmp slt i32 %14, 0
br i1 %15, label %24, label %16
16: ; preds = %3
%17 = load i32, i32* @SOL_SOCKET
%18 = load i32, i32* @SO_REUSEADDR
%19 = call i32 @setsockopt(i32 %14, i32 %17, i32 %18, i32* %5, i32 4)
%20 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%21 = call i64 @connect(i32 %14, %struct.sockaddr* %20, i32 4)
%22 = icmp slt i64 %21, 0
%23 = select i1 %22, i32 -1, i32 %14
br label %24
24: ; preds = %16, %3
%25 = phi i32 [ -1, %3 ], [ %23, %16 ]
ret i32 %25
}