#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 */
typedef struct TYPE_4__ TYPE_2__;
typedef struct TYPE_3__ TYPE_1__;

/* Type definitions */
struct TYPE_4__ {
  int /*<<< orphan*/ s6_addr;
};
struct sockaddr_in6 {
  int sin6_family;
  void *sin6_port;
  TYPE_2__ sin6_addr;
};
struct TYPE_3__ {
  int /*<<< orphan*/ s_addr;
};
struct sockaddr_in {
  int sin_family;
  void *sin_port;
  TYPE_1__ sin_addr;
};
struct sockaddr {
  int dummy;
};

/* Variables and functions */
int AF_INET;
int AF_INET6;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ close(int);
int connect(int, struct sockaddr *, int);
void *htons(int);
int inet_pton(int, char const *, int /*<<< orphan*/ *);
int socket(int, int /*<<< orphan*/, int /*<<< orphan*/);
int simple_socket_connect(const char *address, int port) {
  int family;
  int sock;
  struct sockaddr_in host;
  struct sockaddr_in6 host6;
  if (inet_pton(AF_INET, address, &host.sin_addr.s_addr) == 1) {
    family = AF_INET;
    host.sin_family = family;
    host.sin_port = htons(port);
  } else if (inet_pton(AF_INET6, address, &host6.sin6_addr.s6_addr) == 1) {
    family = AF_INET6;
    host6.sin6_family = family;
    host6.sin6_port = htons(port);
  } else {
    return -1;
  }
  if ((sock = socket(family, SOCK_STREAM, 0)) == -1) {
    return -2;
  }
  int cr = -1;
  if (family == AF_INET) {
    cr = connect(sock, (struct sockaddr *)(&host), sizeof(struct sockaddr_in));
  } else if (family == AF_INET6) {
    cr =
        connect(sock, (struct sockaddr *)(&host6), sizeof(struct sockaddr_in6));
  }
  if (cr != 0) {
    close(sock);
    return -3;
  }
  return sock;
}


x86 assembly (clang, -O0):
<simple_socket_connect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    0x2e82(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rsi
lea    -0x38(%rbp),%rdx
add    $0x10,%rdx
call   1050 <inet_pton@plt>
cmp    $0x1,%eax
jne    11a5 <simple_socket_connect+0x55>
mov    0x2e5f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    -0x14(%rbp),%edi
call   1030 <htons@plt>
mov    %rax,-0x30(%rbp)
jmp    11fc <simple_socket_connect+0xac>
mov    0x2e34(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rsi
lea    -0x50(%rbp),%rdx
add    $0x10,%rdx
call   1050 <inet_pton@plt>
cmp    $0x1,%eax
jne    11eb <simple_socket_connect+0x9b>
mov    0x2e11(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%eax
mov    %eax,-0x50(%rbp)
mov    -0x14(%rbp),%edi
call   1030 <htons@plt>
mov    %rax,-0x48(%rbp)
jmp    11f7 <simple_socket_connect+0xa7>
movl   $0xffffffff,-0x4(%rbp)
jmp    12a8 <simple_socket_connect+0x158>
jmp    11fc <simple_socket_connect+0xac>
mov    -0x18(%rbp),%edi
mov    0x2dca(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0xffffffff,%eax
jne    1227 <simple_socket_connect+0xd7>
movl   $0xfffffffe,-0x4(%rbp)
jmp    12a8 <simple_socket_connect+0x158>
movl   $0xffffffff,-0x54(%rbp)
mov    -0x18(%rbp),%eax
mov    0x2db0(%rip),%rcx
cmp    (%rcx),%eax
jne    1259 <simple_socket_connect+0x109>
mov    -0x1c(%rbp),%edi
lea    -0x38(%rbp),%rsi
mov    $0x18,%edx
call   1060 <connect@plt>
mov    %eax,-0x54(%rbp)
jmp    1284 <simple_socket_connect+0x134>
mov    -0x18(%rbp),%eax
mov    0x2d7d(%rip),%rcx
cmp    (%rcx),%eax
jne    127f <simple_socket_connect+0x12f>
mov    -0x1c(%rbp),%edi
lea    -0x50(%rbp),%rsi
mov    $0x18,%edx
call   1060 <connect@plt>
mov    %eax,-0x54(%rbp)
jmp    1284 <simple_socket_connect+0x134>
cmpl   $0x0,-0x54(%rbp)
je     12a2 <simple_socket_connect+0x152>
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
movl   $0xfffffffd,-0x4(%rbp)
jmp    12a8 <simple_socket_connect+0x158>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<simple_socket_connect>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x38,%rsp
mov    %esi,%r14d
mov    %rdi,%rbx
mov    0x2e81(%rip),%r15
mov    (%r15),%edi
lea    0x30(%rsp),%rdx
mov    %rbx,%rsi
call   1050 <inet_pton@plt>
cmp    $0x1,%eax
jne    1192 <simple_socket_connect+0x42>
mov    (%r15),%ebp
mov    %ebp,0x20(%rsp)
mov    %r14d,%edi
call   1030 <htons@plt>
mov    %rax,0x28(%rsp)
jmp    11c7 <simple_socket_connect+0x77>
mov    0x2e47(%rip),%rbp
mov    0x0(%rbp),%edi
lea    0x18(%rsp),%rdx
mov    %rbx,%rsi
call   1050 <inet_pton@plt>
mov    $0xffffffff,%ebx
cmp    $0x1,%eax
jne    121f <simple_socket_connect+0xcf>
mov    0x0(%rbp),%ebp
mov    %ebp,0x8(%rsp)
mov    %r14d,%edi
call   1030 <htons@plt>
mov    %rax,0x10(%rsp)
mov    0x2e02(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
xor    %edx,%edx
call   1070 <socket@plt>
cmp    $0xffffffff,%eax
je     11ec <simple_socket_connect+0x9c>
mov    %eax,%ebx
cmp    (%r15),%ebp
jne    11f3 <simple_socket_connect+0xa3>
lea    0x20(%rsp),%rsi
jmp    1203 <simple_socket_connect+0xb3>
mov    $0xfffffffe,%ebx
jmp    121f <simple_socket_connect+0xcf>
mov    0x2de6(%rip),%rax
cmp    (%rax),%ebp
jne    1213 <simple_socket_connect+0xc3>
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
mov    $0x18,%edx
call   1060 <connect@plt>
test   %eax,%eax
je     121f <simple_socket_connect+0xcf>
mov    %ebx,%edi
call   1040 <close@plt>
mov    $0xfffffffd,%ebx
mov    %ebx,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i8*, %struct.TYPE_3__ }
%struct.TYPE_3__ = type { i32 }
%struct.sockaddr_in6 = type { i32, i8*, %struct.TYPE_4__ }
%struct.TYPE_4__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@AF_INET6 = global i32 0
@SOCK_STREAM = global i32 0
define i32 @simple_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
%9 = alloca %struct.sockaddr_in6
%10 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
%11 = load i32, i32* @AF_INET
%12 = load i8*, i8** %4
%13 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 2
%14 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %13, i32 0, i32 0
%15 = call i32 @inet_pton(i32 %11, i8* %12, i32* %14)
%16 = icmp eq i32 %15, 1
br i1 %16, label %17, label %24
17: ; preds = %2
%18 = load i32, i32* @AF_INET
store i32 %18, i32* %6
%19 = load i32, i32* %6
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 0
store i32 %19, i32* %20
%21 = load i32, i32* %5
%22 = call i8* @htons(i32 %21)
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 1
store i8* %22, i8** %23
br label %40
24: ; preds = %2
%25 = load i32, i32* @AF_INET6
%26 = load i8*, i8** %4
%27 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %9, i32 0, i32 2
%28 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %27, i32 0, i32 0
%29 = call i32 @inet_pton(i32 %25, i8* %26, i32* %28)
%30 = icmp eq i32 %29, 1
br i1 %30, label %31, label %38
31: ; preds = %24
%32 = load i32, i32* @AF_INET6
store i32 %32, i32* %6
%33 = load i32, i32* %6
%34 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %9, i32 0, i32 0
store i32 %33, i32* %34
%35 = load i32, i32* %5
%36 = call i8* @htons(i32 %35)
%37 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %9, i32 0, i32 1
store i8* %36, i8** %37
br label %39
38: ; preds = %24
store i32 -1, i32* %3
br label %71
39: ; preds = %31
br label %40
40: ; preds = %39, %17
%41 = load i32, i32* %6
%42 = load i32, i32* @SOCK_STREAM
%43 = call i32 @socket(i32 %41, i32 %42, i32 0)
store i32 %43, i32* %7
%44 = icmp eq i32 %43, -1
br i1 %44, label %45, label %46
45: ; preds = %40
store i32 -2, i32* %3
br label %71
46: ; preds = %40
store i32 -1, i32* %10
%47 = load i32, i32* %6
%48 = load i32, i32* @AF_INET
%49 = icmp eq i32 %47, %48
br i1 %49, label %50, label %54
50: ; preds = %46
%51 = load i32, i32* %7
%52 = bitcast %struct.sockaddr_in* %8 to %struct.sockaddr*
%53 = call i32 @connect(i32 %51, %struct.sockaddr* %52, i32 24)
store i32 %53, i32* %10
br label %63
54: ; preds = %46
%55 = load i32, i32* %6
%56 = load i32, i32* @AF_INET6
%57 = icmp eq i32 %55, %56
br i1 %57, label %58, label %62
58: ; preds = %54
%59 = load i32, i32* %7
%60 = bitcast %struct.sockaddr_in6* %9 to %struct.sockaddr*
%61 = call i32 @connect(i32 %59, %struct.sockaddr* %60, i32 24)
store i32 %61, i32* %10
br label %62
62: ; preds = %58, %54
br label %63
63: ; preds = %62, %50
%64 = load i32, i32* %10
%65 = icmp ne i32 %64, 0
br i1 %65, label %66, label %69
66: ; preds = %63
%67 = load i32, i32* %7
%68 = call i32 @close(i32 %67)
store i32 -3, i32* %3
br label %71
69: ; preds = %63
%70 = load i32, i32* %7
store i32 %70, i32* %3
br label %71
71: ; preds = %69, %66, %45, %38
%72 = load i32, i32* %3
ret i32 %72
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i8*, %struct.TYPE_3__ }
%struct.TYPE_3__ = type { i32 }
%struct.sockaddr_in6 = type { i32, i8*, %struct.TYPE_4__ }
%struct.TYPE_4__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@AF_INET6 = global i32 0
@SOCK_STREAM = global i32 0
define i32 @simple_socket_connect(i8* %0, i32 %1) {
%3 = alloca %struct.sockaddr_in
%4 = alloca %struct.sockaddr_in6
%5 = bitcast %struct.sockaddr_in* %3 to i8*
%6 = bitcast %struct.sockaddr_in6* %4 to i8*
%7 = load i32, i32* @AF_INET
%8 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2, i32 0
%9 = call i32 @inet_pton(i32 %7, i8* %0, i32* %8)
%10 = icmp eq i32 %9, 1
br i1 %10, label %11, label %16
11: ; preds = %2
%12 = load i32, i32* @AF_INET
%13 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
store i32 %12, i32* %13
%14 = call i8* @htons(i32 %1)
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
store i8* %14, i8** %15
br label %26
16: ; preds = %2
%17 = load i32, i32* @AF_INET6
%18 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %4, i64 0, i32 2, i32 0
%19 = call i32 @inet_pton(i32 %17, i8* %0, i32* %18)
%20 = icmp eq i32 %19, 1
br i1 %20, label %21, label %47
21: ; preds = %16
%22 = load i32, i32* @AF_INET6
%23 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %4, i64 0, i32 0
store i32 %22, i32* %23
%24 = call i8* @htons(i32 %1)
%25 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %4, i64 0, i32 1
store i8* %24, i8** %25
br label %26
26: ; preds = %21, %11
%27 = phi i32 [ %12, %11 ], [ %22, %21 ]
%28 = load i32, i32* @SOCK_STREAM
%29 = call i32 @socket(i32 %27, i32 %28, i32 0)
%30 = icmp eq i32 %29, -1
br i1 %30, label %47, label %31
31: ; preds = %26
%32 = load i32, i32* @AF_INET
%33 = icmp eq i32 %27, %32
br i1 %33, label %34, label %36
34: ; preds = %31
%35 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
br label %41
36: ; preds = %31
%37 = load i32, i32* @AF_INET6
%38 = icmp eq i32 %27, %37
br i1 %38, label %39, label %45
39: ; preds = %36
%40 = bitcast %struct.sockaddr_in6* %4 to %struct.sockaddr*
br label %41
41: ; preds = %39, %34
%42 = phi %struct.sockaddr* [ %40, %39 ], [ %35, %34 ]
%43 = call i32 @connect(i32 %29, %struct.sockaddr* %42, i32 24)
%44 = icmp eq i32 %43, 0
br i1 %44, label %47, label %45
45: ; preds = %36, %41
%46 = call i32 @close(i32 %29)
br label %47
47: ; preds = %45, %41, %26, %16
%48 = phi i32 [ -1, %16 ], [ -2, %26 ], [ -3, %45 ], [ %29, %41 ]
ret i32 %48
}