#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 dummy;
};
struct sockaddr {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ IPPROTO_TCP;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int connect(int, struct sockaddr *, int);
int /*<<< orphan*/ set_addr(struct sockaddr_in *, char const *, int);
int setnonblock(int);
int setreuse(int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int socket_connect(const char *address, int port) {
  int s, rv;
  struct sockaddr_in addr;
  s = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
  if (s == -1)
    return -1;
  rv = setreuse(s);
  if (rv == -1)
    return -1;
  set_addr(&addr, address, port);
  rv = connect(s, (struct sockaddr *)&addr, sizeof(struct sockaddr_in));
  if (rv == -1)
    return -1;
  rv = setnonblock(s);
  if (rv == -1)
    return -1;
  return s;
}


x86 assembly (clang, -O0):
<socket_connect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    0x2e6a(%rip),%rax
mov    (%rax),%edi
mov    0x2e69(%rip),%rax
mov    (%rax),%esi
mov    0x2e70(%rip),%rax
mov    (%rax),%edx
call   1070 <socket@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    1198 <socket_connect+0x48>
movl   $0xffffffff,-0x4(%rbp)
jmp    121a <socket_connect+0xca>
mov    -0x18(%rbp),%edi
call   1040 <setreuse@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    11b9 <socket_connect+0x69>
movl   $0xffffffff,-0x4(%rbp)
jmp    121a <socket_connect+0xca>
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
lea    -0x20(%rbp),%rdi
call   1030 <set_addr@plt>
mov    -0x18(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x4,%edx
call   1060 <connect@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    11f3 <socket_connect+0xa3>
movl   $0xffffffff,-0x4(%rbp)
jmp    121a <socket_connect+0xca>
mov    -0x18(%rbp),%edi
call   1050 <setnonblock@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    1214 <socket_connect+0xc4>
movl   $0xffffffff,-0x4(%rbp)
jmp    121a <socket_connect+0xca>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<socket_connect>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %esi,%r14d
mov    %rdi,%r15
mov    0x2e67(%rip),%rax
mov    (%rax),%edi
mov    0x2e66(%rip),%rax
mov    (%rax),%esi
mov    0x2e6d(%rip),%rax
mov    (%rax),%edx
call   1070 <socket@plt>
mov    $0xffffffff,%ebp
cmp    $0xffffffff,%eax
je     11d0 <socket_connect+0x80>
mov    %eax,%ebx
mov    %eax,%edi
call   1040 <setreuse@plt>
cmp    $0xffffffff,%eax
je     11d0 <socket_connect+0x80>
lea    0x8(%rsp),%r12
mov    %r12,%rdi
mov    %r15,%rsi
mov    %r14d,%edx
call   1030 <set_addr@plt>
mov    %ebx,%edi
mov    %r12,%rsi
mov    $0x4,%edx
call   1060 <connect@plt>
cmp    $0xffffffff,%eax
je     11d0 <socket_connect+0x80>
mov    %ebx,%edi
call   1050 <setnonblock@plt>
cmp    $0xffffffff,%eax
cmove  %eax,%ebx
mov    %ebx,%ebp
mov    %ebp,%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
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
define i32 @socket_connect(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca %struct.sockaddr_in
store i8* %0, i8** %4
store i32 %1, i32* %5
%9 = load i32, i32* @PF_INET
%10 = load i32, i32* @SOCK_STREAM
%11 = load i32, i32* @IPPROTO_TCP
%12 = call i32 @socket(i32 %9, i32 %10, i32 %11)
store i32 %12, i32* %6
%13 = load i32, i32* %6
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %16
15: ; preds = %2
store i32 -1, i32* %3
br label %40
16: ; preds = %2
%17 = load i32, i32* %6
%18 = call i32 @setreuse(i32 %17)
store i32 %18, i32* %7
%19 = load i32, i32* %7
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %22
21: ; preds = %16
store i32 -1, i32* %3
br label %40
22: ; preds = %16
%23 = load i8*, i8** %4
%24 = load i32, i32* %5
%25 = call i32 @set_addr(%struct.sockaddr_in* %8, i8* %23, i32 %24)
%26 = load i32, i32* %6
%27 = bitcast %struct.sockaddr_in* %8 to %struct.sockaddr*
%28 = call i32 @connect(i32 %26, %struct.sockaddr* %27, i32 4)
store i32 %28, i32* %7
%29 = load i32, i32* %7
%30 = icmp eq i32 %29, -1
br i1 %30, label %31, label %32
31: ; preds = %22
store i32 -1, i32* %3
br label %40
32: ; preds = %22
%33 = load i32, i32* %6
%34 = call i32 @setnonblock(i32 %33)
store i32 %34, i32* %7
%35 = load i32, i32* %7
%36 = icmp eq i32 %35, -1
br i1 %36, label %37, label %38
37: ; preds = %32
store i32 -1, i32* %3
br label %40
38: ; preds = %32
%39 = load i32, i32* %6
store i32 %39, i32* %3
br label %40
40: ; preds = %38, %37, %31, %21, %15
%41 = load i32, i32* %3
ret i32 %41
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32 }
%struct.sockaddr = type { i32 }
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
define i32 @socket_connect(i8* %0, i32 %1) {
%3 = alloca %struct.sockaddr_in
%4 = bitcast %struct.sockaddr_in* %3 to i8*
%5 = load i32, i32* @PF_INET
%6 = load i32, i32* @SOCK_STREAM
%7 = load i32, i32* @IPPROTO_TCP
%8 = tail call i32 @socket(i32 %5, i32 %6, i32 %7)
%9 = icmp eq i32 %8, -1
br i1 %9, label %22, label %10
10: ; preds = %2
%11 = tail call i32 @setreuse(i32 %8)
%12 = icmp eq i32 %11, -1
br i1 %12, label %22, label %13
13: ; preds = %10
%14 = call i32 @set_addr(%struct.sockaddr_in* %3, i8* %0, i32 %1)
%15 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%16 = call i32 @connect(i32 %8, %struct.sockaddr* %15, i32 4)
%17 = icmp eq i32 %16, -1
br i1 %17, label %22, label %18
18: ; preds = %13
%19 = call i32 @setnonblock(i32 %8)
%20 = icmp eq i32 %19, -1
%21 = select i1 %20, i32 -1, i32 %8
br label %22
22: ; preds = %18, %13, %10, %2
%23 = phi i32 [ -1, %2 ], [ -1, %10 ], [ -1, %13 ], [ %21, %18 ]
ret i32 %23
}