#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_storage {
  int dummy;
};
struct sockaddr_in6 {
  scalar_t__ sin6_family;
  int /*<<< orphan*/ sin6_addr;
  void *sin6_port;
};
struct sockaddr_in {
  scalar_t__ sin_family;
  int /*<<< orphan*/ sin_addr;
  void *sin_port;
};
struct sockaddr {
  int dummy;
};
typedef scalar_t__ sa_family_t;
typedef int /*<<< orphan*/ in_port_t;

/* Variables and functions */
scalar_t__ const AF_INET;
scalar_t__ const AF_INET6;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ ab_terror(char *);
scalar_t__ connect(int const, struct sockaddr *, int);
void *htons(int /*<<< orphan*/ const);
int /*<<< orphan*/ inet_pton(scalar_t__ const, char *, int /*<<< orphan*/ *);
int socket(scalar_t__ const, int /*<<< orphan*/, int /*<<< orphan*/);
int serverconnect(sa_family_t const domain, char const *const address,
                  in_port_t const port) {
  int const server = socket(domain, SOCK_STREAM, 0);
  struct sockaddr_storage conninfo;
  struct sockaddr_in *const conninfo4 = (struct sockaddr_in *const)&conninfo;
  struct sockaddr_in6 *const conninfo6 = (struct sockaddr_in6 *const)&conninfo;
  if (server == -1) {
    ab_terror("socket("
              "domain, "
              "SOCK_STREAM, "
              "0"
              ")");
    return (-1);
  }
  if (domain == AF_INET) {
    conninfo4->sin_family = AF_INET;
    conninfo4->sin_port = htons(port);
    inet_pton(AF_INET, address == NULL ? "127.0.0.1" : address,
              &conninfo4->sin_addr);
  }
  if (domain == AF_INET6) {
    conninfo6->sin6_family = AF_INET6;
    conninfo6->sin6_port = htons(port);
    inet_pton(AF_INET6, address == NULL ? "127.0.0.1" : address,
              &conninfo6->sin6_addr);
  }
  if (connect(server, (struct sockaddr *)&conninfo,
              sizeof(struct sockaddr_storage))) {
    ab_terror("connect("
              "server, "
              "(struct sockaddr *)&conninfo, "
              "sizeof(struct sockaddr_storage)"
              ")");
    return (-1);
  }
  return (server);
}


x86 assembly (clang, -O0):
<serverconnect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e62(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
mov    %eax,-0x20(%rbp)
lea    -0x28(%rbp),%rax
mov    %rax,-0x30(%rbp)
lea    -0x28(%rbp),%rax
mov    %rax,-0x38(%rbp)
cmpl   $0xffffffff,-0x20(%rbp)
jne    11ac <serverconnect+0x5c>
lea    0xe65(%rip),%rdi
call   1040 <ab_terror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12e1 <serverconnect+0x191>
mov    -0x10(%rbp),%rax
mov    0x2e31(%rip),%rcx
cmp    (%rcx),%rax
jne    122a <serverconnect+0xda>
mov    0x2e21(%rip),%rax
mov    (%rax),%rcx
mov    -0x30(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x1c(%rbp),%edi
call   1030 <htons@plt>
mov    %rax,%rcx
mov    -0x30(%rbp),%rax
mov    %rcx,0x10(%rax)
mov    0x2dfd(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    120d <serverconnect+0xbd>
lea    0xe1b(%rip),%rax
mov    %rax,-0x48(%rbp)
jmp    1215 <serverconnect+0xc5>
mov    -0x18(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x40(%rbp),%rdi
mov    -0x48(%rbp),%rsi
mov    -0x30(%rbp),%rdx
add    $0x8,%rdx
call   1050 <inet_pton@plt>
mov    -0x10(%rbp),%rax
mov    0x2dab(%rip),%rcx
cmp    (%rcx),%rax
jne    12a8 <serverconnect+0x158>
mov    0x2d9b(%rip),%rax
mov    (%rax),%rcx
mov    -0x38(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x1c(%rbp),%edi
call   1030 <htons@plt>
mov    %rax,%rcx
mov    -0x38(%rbp),%rax
mov    %rcx,0x10(%rax)
mov    0x2d77(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x50(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    128b <serverconnect+0x13b>
lea    0xd9d(%rip),%rax
mov    %rax,-0x58(%rbp)
jmp    1293 <serverconnect+0x143>
mov    -0x18(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    -0x50(%rbp),%rdi
mov    -0x58(%rbp),%rsi
mov    -0x38(%rbp),%rdx
add    $0x8,%rdx
call   1050 <inet_pton@plt>
mov    -0x20(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0x4,%edx
call   1060 <connect@plt>
cmp    $0x0,%rax
je     12db <serverconnect+0x18b>
lea    0xd5f(%rip),%rdi
call   1040 <ab_terror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12e1 <serverconnect+0x191>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<serverconnect>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%r14d
mov    %rsi,%r15
mov    %rdi,%rbp
mov    0x2e79(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
cmp    $0xffffffff,%eax
je     11f0 <serverconnect+0xa0>
mov    %eax,%ebx
test   %rbp,%rbp
jne    11d3 <serverconnect+0x83>
movq   $0x0,(%rsp)
mov    %r14d,%edi
call   1030 <htons@plt>
mov    %rax,0x10(%rsp)
test   %r15,%r15
lea    0xe84(%rip),%rbp
cmovne %r15,%rbp
lea    0x8(%rsp),%r15
xor    %edi,%edi
mov    %rbp,%rsi
mov    %r15,%rdx
call   1050 <inet_pton@plt>
movq   $0x0,(%rsp)
mov    %r14d,%edi
call   1030 <htons@plt>
mov    %rax,0x10(%rsp)
xor    %edi,%edi
mov    %rbp,%rsi
mov    %r15,%rdx
call   1050 <inet_pton@plt>
mov    %rsp,%rsi
mov    %ebx,%edi
mov    $0x4,%edx
call   1060 <connect@plt>
test   %rax,%rax
je     1201 <serverconnect+0xb1>
lea    0xe3b(%rip),%rdi
jmp    11f7 <serverconnect+0xa7>
lea    0xe09(%rip),%rdi
call   1040 <ab_terror@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_storage = type { i32 }
%struct.sockaddr_in = type { i64, i32, i8* }
%struct.sockaddr_in6 = type { i64, i32, i8* }
%struct.sockaddr = type { i32 }
@SOCK_STREAM = global i32 0
@.str = [31 x i8] c"socket(domain, SOCK_STREAM, 0)\00"
@AF_INET = constant i64 0
@.str.1 = [10 x i8] c"127.0.0.1\00"
@AF_INET6 = constant i64 0
@.str.2 = [79 x i8] c"connect(server, (struct sockaddr *)&conninfo, sizeof(struct sockaddr_storage))\00"
define i32 @serverconnect(i64 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i64
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca %struct.sockaddr_storage
%10 = alloca %struct.sockaddr_in*
%11 = alloca %struct.sockaddr_in6*
store i64 %0, i64* %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%12 = load i64, i64* %5
%13 = load i32, i32* @SOCK_STREAM
%14 = call i32 @socket(i64 %12, i32 %13, i32 0)
store i32 %14, i32* %8
%15 = bitcast %struct.sockaddr_storage* %9 to %struct.sockaddr_in*
store %struct.sockaddr_in* %15, %struct.sockaddr_in** %10
%16 = bitcast %struct.sockaddr_storage* %9 to %struct.sockaddr_in6*
store %struct.sockaddr_in6* %16, %struct.sockaddr_in6** %11
%17 = load i32, i32* %8
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %21
19: ; preds = %3
%20 = call i32 @ab_terror(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %4
br label %76
21: ; preds = %3
%22 = load i64, i64* %5
%23 = load i64, i64* @AF_INET
%24 = icmp eq i64 %22, %23
br i1 %24, label %25, label %44
25: ; preds = %21
%26 = load i64, i64* @AF_INET
%27 = load %struct.sockaddr_in*, %struct.sockaddr_in** %10
%28 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %27, i32 0, i32 0
store i64 %26, i64* %28
%29 = load i32, i32* %7
%30 = call i8* @htons(i32 %29)
%31 = load %struct.sockaddr_in*, %struct.sockaddr_in** %10
%32 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %31, i32 0, i32 2
store i8* %30, i8** %32
%33 = load i64, i64* @AF_INET
%34 = load i8*, i8** %6
%35 = icmp eq i8* %34, null
br i1 %35, label %36, label %37
36: ; preds = %25
br label %39
37: ; preds = %25
%38 = load i8*, i8** %6
br label %39
39: ; preds = %37, %36
%40 = phi i8* [ getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), %36 ], [ %38, %37 ]
%41 = load %struct.sockaddr_in*, %struct.sockaddr_in** %10
%42 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %41, i32 0, i32 1
%43 = call i32 @inet_pton(i64 %33, i8* %40, i32* %42)
br label %44
44: ; preds = %39, %21
%45 = load i64, i64* %5
%46 = load i64, i64* @AF_INET6
%47 = icmp eq i64 %45, %46
br i1 %47, label %48, label %67
48: ; preds = %44
%49 = load i64, i64* @AF_INET6
%50 = load %struct.sockaddr_in6*, %struct.sockaddr_in6** %11
%51 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %50, i32 0, i32 0
store i64 %49, i64* %51
%52 = load i32, i32* %7
%53 = call i8* @htons(i32 %52)
%54 = load %struct.sockaddr_in6*, %struct.sockaddr_in6** %11
%55 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %54, i32 0, i32 2
store i8* %53, i8** %55
%56 = load i64, i64* @AF_INET6
%57 = load i8*, i8** %6
%58 = icmp eq i8* %57, null
br i1 %58, label %59, label %60
59: ; preds = %48
br label %62
60: ; preds = %48
%61 = load i8*, i8** %6
br label %62
62: ; preds = %60, %59
%63 = phi i8* [ getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), %59 ], [ %61, %60 ]
%64 = load %struct.sockaddr_in6*, %struct.sockaddr_in6** %11
%65 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %64, i32 0, i32 1
%66 = call i32 @inet_pton(i64 %56, i8* %63, i32* %65)
br label %67
67: ; preds = %62, %44
%68 = load i32, i32* %8
%69 = bitcast %struct.sockaddr_storage* %9 to %struct.sockaddr*
%70 = call i64 @connect(i32 %68, %struct.sockaddr* %69, i32 4)
%71 = icmp ne i64 %70, 0
br i1 %71, label %72, label %74
72: ; preds = %67
%73 = call i32 @ab_terror(i8* getelementptr inbounds ([79 x i8], [79 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %4
br label %76
74: ; preds = %67
%75 = load i32, i32* %8
store i32 %75, i32* %4
br label %76
76: ; preds = %74, %72, %19
%77 = load i32, i32* %4
ret i32 %77
}

LLVM IR (-O2):
%struct.sockaddr_storage = type { i32 }
%struct.sockaddr = type { i32 }
@SOCK_STREAM = global i32 0
@.str = [31 x i8] c"socket(domain, SOCK_STREAM, 0)\00"
@AF_INET = constant i64 0
@.str.1 = [10 x i8] c"127.0.0.1\00"
@AF_INET6 = constant i64 0
@.str.2 = [79 x i8] c"connect(server, (struct sockaddr *)&conninfo, sizeof(struct sockaddr_storage))\00"
define i32 @serverconnect(i64 %0, i8* %1, i32 %2) {
%4 = alloca %struct.sockaddr_storage
%5 = load i32, i32* @SOCK_STREAM
%6 = tail call i32 @socket(i64 %0, i32 %5, i32 0)
%7 = bitcast %struct.sockaddr_storage* %4 to i8*
%8 = icmp eq i32 %6, -1
br i1 %8, label %9, label %11
9: ; preds = %3
%10 = tail call i32 @ab_terror(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0))
br label %30
11: ; preds = %3
%12 = icmp eq i64 %0, 0
br i1 %12, label %13, label %24
13: ; preds = %11
%14 = bitcast %struct.sockaddr_storage* %4 to i64*
store i64 0, i64* %14
%15 = tail call i8* @htons(i32 %2)
%16 = getelementptr inbounds %struct.sockaddr_storage, %struct.sockaddr_storage* %4, i64 4
%17 = bitcast %struct.sockaddr_storage* %16 to i8**
store i8* %15, i8** %17
%18 = icmp eq i8* %1, null
%19 = select i1 %18, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8* %1
%20 = getelementptr inbounds %struct.sockaddr_storage, %struct.sockaddr_storage* %4, i64 2, i32 0
%21 = call i32 @inet_pton(i64 0, i8* %19, i32* %20)
store i64 0, i64* %14
%22 = call i8* @htons(i32 %2)
store i8* %22, i8** %17
%23 = call i32 @inet_pton(i64 0, i8* %19, i32* %20)
br label %24
24: ; preds = %11, %13
%25 = bitcast %struct.sockaddr_storage* %4 to %struct.sockaddr*
%26 = call i64 @connect(i32 %6, %struct.sockaddr* %25, i32 4)
%27 = icmp eq i64 %26, 0
br i1 %27, label %30, label %28
28: ; preds = %24
%29 = call i32 @ab_terror(i8* getelementptr inbounds ([79 x i8], [79 x i8]* @.str.2, i64 0, i64 0))
br label %30
30: ; preds = %24, %28, %9
%31 = phi i32 [ -1, %9 ], [ -1, %28 ], [ %6, %24 ]
ret i32 %31
}