#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 {
  int dummy;
};
struct addrinfo {
  int /*<<< orphan*/ ai_addrlen;
  int /*<<< orphan*/ ai_addr;
  int /*<<< orphan*/ ai_protocol;
  int /*<<< orphan*/ ai_socktype;
  int /*<<< orphan*/ ai_family;
  struct addrinfo *ai_next;
  scalar_t__ ai_flags;
};
typedef int socklen_t;
typedef int /*<<< orphan*/ serv;
typedef int /*<<< orphan*/ sa;
typedef int /*<<< orphan*/ optval;
typedef int /*<<< orphan*/ host;

/* Variables and functions */
int NI_MAXHOST;
int NI_MAXSERV;
int NI_NUMERICHOST;
int NI_NUMERICSERV;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_SNDBUF;
int /*<<< orphan*/ close(int);
int connect(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ err(int, char *, ...);
int errno;
int /*<<< orphan*/ errx(int, char *, char const *, ...);
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
int /*<<< orphan*/ gai_strerror(int);
int getaddrinfo(char const *, char const *, struct addrinfo *,
                struct addrinfo **);
int getnameinfo(struct sockaddr *, int, char *, int, char *, int, int);
int getpeername(int, struct sockaddr *, int *);
int /*<<< orphan*/ printf(char *, char *, char *);
int setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, int *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int socket_connect(struct addrinfo *hints, const char *hostname,
                   const char *port) {
  char host[NI_MAXHOST], serv[NI_MAXSERV];
  struct sockaddr_storage sa;
  socklen_t salen;
  struct addrinfo *res, *res0;
  const char *cause = NULL;
  int optval, error, save_errno, cs;
  hints->ai_flags = 0;
  error = getaddrinfo(hostname, port, hints, &res0);
  if (error)
    errx(1, "getaddrinfo %s %s: %s", hostname, port, gai_strerror(error));
  cs = -1;
  for (res = res0; res; res = res->ai_next) {
    cs = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
    if (cs == -1) {
      cause = "connect socket";
      continue;
    }
    optval = 100000;
    if (setsockopt(cs, SOL_SOCKET, SO_SNDBUF, &optval, sizeof(optval)) == -1)
      err(1, "setsockopt sndbuf");
    if (connect(cs, res->ai_addr, res->ai_addrlen) == -1) {
      cause = "connect";
      save_errno = errno;
      close(cs);
      errno = save_errno;
      cs = -1;
      continue;
    }
    break;
  }
  if (cs == -1)
    err(1, "%s", cause);
  salen = sizeof(sa);
  if (getpeername(cs, (struct sockaddr *)&sa, &salen) == -1)
    err(1, "connect getpeername");
  error = getnameinfo((struct sockaddr *)&sa, salen, host, sizeof(host), serv,
                      sizeof(serv), NI_NUMERICHOST | NI_NUMERICSERV);
  if (error)
    errx(1, "connect getnameinfo: %s", gai_strerror(error));
  printf("connect %s %s\n", host, serv);
  freeaddrinfo(res0);
  return cs;
}


x86 assembly (clang, -O0):
<socket_connect>:
push   %rbp
mov    %rsp,%rbp
sub    $0xa0,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    0x2dd2(%rip),%rax
mov    (%rax),%eax
mov    %rax,-0x88(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x20(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x80(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x28(%rbp)
mov    0x2dbf(%rip),%rax
mov    (%rax),%eax
mov    %rax,-0x78(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x70(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x30(%rbp)
movq   $0x0,-0x58(%rbp)
mov    -0x8(%rbp),%rax
movq   $0x0,0x20(%rax)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x8(%rbp),%rdx
lea    -0x50(%rbp),%rcx
call   10c0 <getaddrinfo@plt>
mov    %eax,-0x60(%rbp)
cmpl   $0x0,-0x60(%rbp)
je     12ab <socket_connect+0xeb>
mov    -0x10(%rbp),%rax
mov    %rax,-0x98(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x90(%rbp)
mov    -0x60(%rbp),%edi
call   1070 <gai_strerror@plt>
mov    -0x98(%rbp),%rdx
mov    -0x90(%rbp),%rcx
mov    %eax,%r8d
mov    $0x1,%edi
lea    0xd5c(%rip),%rsi
mov    $0x0,%al
call   1050 <errx@plt>
movl   $0xffffffff,-0x68(%rbp)
mov    -0x50(%rbp),%rax
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x48(%rbp)
je     13ae <socket_connect+0x1ee>
mov    -0x48(%rbp),%rax
mov    0x10(%rax),%edi
mov    -0x48(%rbp),%rax
mov    0xc(%rax),%esi
mov    -0x48(%rbp),%rax
mov    0x8(%rax),%edx
call   10e0 <socket@plt>
mov    %eax,-0x68(%rbp)
cmpl   $0xffffffff,-0x68(%rbp)
jne    12fc <socket_connect+0x13c>
lea    0xd23(%rip),%rax
mov    %rax,-0x58(%rbp)
jmp    139d <socket_connect+0x1dd>
movl   $0x186a0,-0x5c(%rbp)
mov    -0x68(%rbp),%edi
mov    0x2cd3(%rip),%rax
mov    (%rax),%esi
mov    0x2cc2(%rip),%rax
mov    (%rax),%edx
lea    -0x5c(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0xffffffff,%eax
jne    1343 <socket_connect+0x183>
mov    $0x1,%edi
lea    0xce9(%rip),%rsi
mov    $0x0,%al
call   10a0 <err@plt>
mov    -0x68(%rbp),%edi
mov    -0x48(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x48(%rbp),%rax
mov    (%rax),%edx
call   10b0 <connect@plt>
cmp    $0xffffffff,%eax
jne    1398 <socket_connect+0x1d8>
lea    0xccf(%rip),%rax
mov    %rax,-0x58(%rbp)
mov    0x2c45(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x64(%rbp)
mov    -0x68(%rbp),%edi
call   1080 <close@plt>
mov    -0x64(%rbp),%ecx
mov    0x2c2e(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x68(%rbp)
jmp    139d <socket_connect+0x1dd>
jmp    13ae <socket_connect+0x1ee>
mov    -0x48(%rbp),%rax
mov    0x18(%rax),%rax
mov    %rax,-0x48(%rbp)
jmp    12ba <socket_connect+0xfa>
cmpl   $0xffffffff,-0x68(%rbp)
jne    13cf <socket_connect+0x20f>
mov    -0x58(%rbp),%rdx
mov    $0x1,%edi
lea    0xca0(%rip),%rsi
mov    $0x0,%al
call   10a0 <err@plt>
movl   $0x4,-0x3c(%rbp)
mov    -0x68(%rbp),%edi
lea    -0x38(%rbp),%rsi
lea    -0x3c(%rbp),%rdx
call   1040 <getpeername@plt>
cmp    $0xffffffff,%eax
jne    1402 <socket_connect+0x242>
mov    $0x1,%edi
lea    0xc44(%rip),%rsi
mov    $0x0,%al
call   10a0 <err@plt>
mov    -0x70(%rbp),%r8
mov    -0x80(%rbp),%rdx
mov    -0x78(%rbp),%rax
mov    -0x88(%rbp),%rcx
lea    -0x38(%rbp),%rdi
mov    -0x3c(%rbp),%esi
mov    %eax,%r9d
mov    0x2b9a(%rip),%rax
mov    (%rax),%eax
mov    0x2bb9(%rip),%r10
or     (%r10),%eax
sub    $0x10,%rsp
mov    %eax,(%rsp)
call   1090 <getnameinfo@plt>
add    $0x10,%rsp
mov    %eax,-0x60(%rbp)
cmpl   $0x0,-0x60(%rbp)
je     146d <socket_connect+0x2ad>
mov    -0x60(%rbp),%edi
call   1070 <gai_strerror@plt>
movslq %eax,%rdx
mov    $0x1,%edi
lea    0xbed(%rip),%rsi
mov    $0x0,%al
call   1050 <errx@plt>
mov    -0x70(%rbp),%rdx
mov    -0x80(%rbp),%rsi
lea    0xbef(%rip),%rdi
call   1060 <printf@plt>
mov    -0x50(%rbp),%rdi
call   10d0 <freeaddrinfo@plt>
mov    -0x68(%rbp),%eax
mov    -0x20(%rbp),%rcx
mov    %rcx,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<socket_connect>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    %rdx,%r15
mov    %rsi,%r14
mov    %rdi,%rdx
mov    0x2dcf(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rbx
mov    %rax,-0x48(%rbp)
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
mov    0x2dd1(%rip),%rax
mov    (%rax),%r12d
mov    %rsp,%rcx
lea    0xf(%r12),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rcx
mov    %rcx,-0x50(%rbp)
mov    %rcx,%rsp
movq   $0x0,0x20(%rdi)
lea    -0x60(%rbp),%rcx
mov    %rsi,%rdi
mov    %r15,%rsi
call   10c0 <getaddrinfo@plt>
test   %eax,%eax
je     1256 <socket_connect+0x96>
mov    %eax,%edi
call   1070 <gai_strerror@plt>
lea    0xdbf(%rip),%rsi
mov    $0x1,%edi
mov    %r14,%rdx
mov    %r15,%rcx
mov    %eax,%r8d
xor    %eax,%eax
call   1050 <errx@plt>
mov    %r12,-0x40(%rbp)
mov    %rbx,-0x58(%rbp)
mov    -0x60(%rbp),%r12
test   %r12,%r12
je     131e <socket_connect+0x15e>
lea    0xda4(%rip),%r14
mov    0x2d3f(%rip),%r13
jmp    128e <socket_connect+0xce>
nopl   0x0(%rax,%rax,1)
mov    0x18(%r12),%r12
test   %r12,%r12
je     1320 <socket_connect+0x160>
mov    0x10(%r12),%edi
mov    0x8(%r12),%edx
mov    0xc(%r12),%esi
call   10e0 <socket@plt>
mov    %r14,%rdx
cmp    $0xffffffff,%eax
je     1280 <socket_connect+0xc0>
mov    %eax,%ebx
movl   $0x186a0,-0x34(%rbp)
mov    0x2d26(%rip),%rax
mov    (%rax),%esi
mov    0x2d15(%rip),%rax
mov    (%rax),%edx
mov    %ebx,%edi
lea    -0x34(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0xffffffff,%eax
jne    12ee <socket_connect+0x12e>
mov    $0x1,%edi
lea    0xd3e(%rip),%rsi
xor    %eax,%eax
call   10a0 <err@plt>
mov    (%r12),%edx
mov    0x4(%r12),%esi
mov    %ebx,%edi
call   10b0 <connect@plt>
cmp    $0xffffffff,%eax
jne    1338 <socket_connect+0x178>
mov    0x0(%r13),%r15d
mov    %ebx,%edi
call   1080 <close@plt>
mov    %r15d,0x0(%r13)
lea    0xd1e(%rip),%rdx
jmp    1280 <socket_connect+0xc0>
xor    %edx,%edx
lea    0xd41(%rip),%rsi
mov    $0x1,%edi
xor    %eax,%eax
call   10a0 <err@plt>
mov    $0xffffffff,%ebx
mov    -0x58(%rbp),%r14
mov    -0x50(%rbp),%r15
mov    -0x48(%rbp),%r12
mov    -0x40(%rbp),%r13
movl   $0x4,-0x2c(%rbp)
lea    -0x30(%rbp),%rsi
lea    -0x2c(%rbp),%rdx
mov    %ebx,%edi
call   1040 <getpeername@plt>
cmp    $0xffffffff,%eax
jne    1376 <socket_connect+0x1b6>
lea    0xcd5(%rip),%rsi
mov    $0x1,%edi
xor    %eax,%eax
call   10a0 <err@plt>
mov    -0x2c(%rbp),%esi
mov    0x2c40(%rip),%rdx
mov    0x2c61(%rip),%rcx
mov    (%rcx),%eax
or     (%rdx),%eax
sub    $0x8,%rsp
lea    -0x30(%rbp),%rdi
mov    %r14,%rdx
mov    %r12d,%ecx
mov    %r15,%r8
mov    %r13d,%r9d
push   %rax
call   1090 <getnameinfo@plt>
add    $0x10,%rsp
test   %eax,%eax
je     13ca <socket_connect+0x20a>
mov    %eax,%edi
call   1070 <gai_strerror@plt>
movslq %eax,%rdx
lea    0xc95(%rip),%rsi
mov    $0x1,%edi
xor    %eax,%eax
call   1050 <errx@plt>
lea    0xc9a(%rip),%rdi
mov    %r14,%rsi
mov    %r15,%rdx
call   1060 <printf@plt>
mov    -0x60(%rbp),%rdi
call   10d0 <freeaddrinfo@plt>
mov    %ebx,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.addrinfo = type { i32, i32, i32, i32, i32, %struct.addrinfo*, i64 }
%struct.sockaddr_storage = type { i32 }
%struct.sockaddr = type { i32 }
@NI_MAXHOST = global i32 0
@NI_MAXSERV = global i32 0
@.str = [22 x i8] c"getaddrinfo %s %s: %s\00"
@.str.1 = [15 x i8] c"connect socket\00"
@SOL_SOCKET = global i32 0
@SO_SNDBUF = global i32 0
@.str.2 = [18 x i8] c"setsockopt sndbuf\00"
@.str.3 = [8 x i8] c"connect\00"
@errno = global i32 0
@.str.4 = [3 x i8] c"%s\00"
@.str.5 = [20 x i8] c"connect getpeername\00"
@NI_NUMERICHOST = global i32 0
@NI_NUMERICSERV = global i32 0
@.str.6 = [24 x i8] c"connect getnameinfo: %s\00"
@.str.7 = [15 x i8] c"connect %s %s\0A\00"
define i32 @socket_connect(%struct.addrinfo* %0, i8* %1, i8* %2) {
%4 = alloca %struct.addrinfo*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i64
%10 = alloca %struct.sockaddr_storage
%11 = alloca i32
%12 = alloca %struct.addrinfo*
%13 = alloca %struct.addrinfo*
%14 = alloca i8*
%15 = alloca i32
%16 = alloca i32
%17 = alloca i32
%18 = alloca i32
store %struct.addrinfo* %0, %struct.addrinfo** %4
store i8* %1, i8** %5
store i8* %2, i8** %6
%19 = load i32, i32* @NI_MAXHOST
%20 = zext i32 %19 to i64
%21 = call i8* @llvm.stacksave()
store i8* %21, i8** %7
%22 = alloca i8, i64 %20
store i64 %20, i64* %8
%23 = load i32, i32* @NI_MAXSERV
%24 = zext i32 %23 to i64
%25 = alloca i8, i64 %24
store i64 %24, i64* %9
store i8* null, i8** %14
%26 = load %struct.addrinfo*, %struct.addrinfo** %4
%27 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %26, i32 0, i32 6
store i64 0, i64* %27
%28 = load i8*, i8** %5
%29 = load i8*, i8** %6
%30 = load %struct.addrinfo*, %struct.addrinfo** %4
%31 = call i32 @getaddrinfo(i8* %28, i8* %29, %struct.addrinfo* %30, %struct.addrinfo** %13)
store i32 %31, i32* %16
%32 = load i32, i32* %16
%33 = icmp ne i32 %32, 0
br i1 %33, label %34, label %40
34: ; preds = %3
%35 = load i8*, i8** %5
%36 = load i8*, i8** %6
%37 = load i32, i32* %16
%38 = call i32 @gai_strerror(i32 %37)
%39 = call i32 (i32, i8*, i8*, ...) @errx(i32 1, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* %35, i8* %36, i32 %38)
br label %40
40: ; preds = %34, %3
store i32 -1, i32* %18
%41 = load %struct.addrinfo*, %struct.addrinfo** %13
store %struct.addrinfo* %41, %struct.addrinfo** %12
br label %42
42: ; preds = %83, %40
%43 = load %struct.addrinfo*, %struct.addrinfo** %12
%44 = icmp ne %struct.addrinfo* %43, null
br i1 %44, label %45, label %87
45: ; preds = %42
%46 = load %struct.addrinfo*, %struct.addrinfo** %12
%47 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %46, i32 0, i32 4
%48 = load i32, i32* %47
%49 = load %struct.addrinfo*, %struct.addrinfo** %12
%50 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %49, i32 0, i32 3
%51 = load i32, i32* %50
%52 = load %struct.addrinfo*, %struct.addrinfo** %12
%53 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %52, i32 0, i32 2
%54 = load i32, i32* %53
%55 = call i32 @socket(i32 %48, i32 %51, i32 %54)
store i32 %55, i32* %18
%56 = load i32, i32* %18
%57 = icmp eq i32 %56, -1
br i1 %57, label %58, label %59
58: ; preds = %45
store i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i8** %14
br label %83
59: ; preds = %45
store i32 100000, i32* %15
%60 = load i32, i32* %18
%61 = load i32, i32* @SOL_SOCKET
%62 = load i32, i32* @SO_SNDBUF
%63 = call i32 @setsockopt(i32 %60, i32 %61, i32 %62, i32* %15, i32 4)
%64 = icmp eq i32 %63, -1
br i1 %64, label %65, label %67
65: ; preds = %59
%66 = call i32 (i32, i8*, ...) @err(i32 1, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0))
br label %67
67: ; preds = %65, %59
%68 = load i32, i32* %18
%69 = load %struct.addrinfo*, %struct.addrinfo** %12
%70 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %69, i32 0, i32 1
%71 = load i32, i32* %70
%72 = load %struct.addrinfo*, %struct.addrinfo** %12
%73 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %72, i32 0, i32 0
%74 = load i32, i32* %73
%75 = call i32 @connect(i32 %68, i32 %71, i32 %74)
%76 = icmp eq i32 %75, -1
br i1 %76, label %77, label %82
77: ; preds = %67
store i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i8** %14
%78 = load i32, i32* @errno
store i32 %78, i32* %17
%79 = load i32, i32* %18
%80 = call i32 @close(i32 %79)
%81 = load i32, i32* %17
store i32 %81, i32* @errno
store i32 -1, i32* %18
br label %83
82: ; preds = %67
br label %87
83: ; preds = %77, %58
%84 = load %struct.addrinfo*, %struct.addrinfo** %12
%85 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %84, i32 0, i32 5
%86 = load %struct.addrinfo*, %struct.addrinfo** %85
store %struct.addrinfo* %86, %struct.addrinfo** %12
br label %42
87: ; preds = %82, %42
%88 = load i32, i32* %18
%89 = icmp eq i32 %88, -1
br i1 %89, label %90, label %93
90: ; preds = %87
%91 = load i8*, i8** %14
%92 = call i32 (i32, i8*, ...) @err(i32 1, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i8* %91)
br label %93
93: ; preds = %90, %87
store i32 4, i32* %11
%94 = load i32, i32* %18
%95 = bitcast %struct.sockaddr_storage* %10 to %struct.sockaddr*
%96 = call i32 @getpeername(i32 %94, %struct.sockaddr* %95, i32* %11)
%97 = icmp eq i32 %96, -1
br i1 %97, label %98, label %100
98: ; preds = %93
%99 = call i32 (i32, i8*, ...) @err(i32 1, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.5, i64 0, i64 0))
br label %100
100: ; preds = %98, %93
%101 = bitcast %struct.sockaddr_storage* %10 to %struct.sockaddr*
%102 = load i32, i32* %11
%103 = trunc i64 %20 to i32
%104 = trunc i64 %24 to i32
%105 = load i32, i32* @NI_NUMERICHOST
%106 = load i32, i32* @NI_NUMERICSERV
%107 = or i32 %105, %106
%108 = call i32 @getnameinfo(%struct.sockaddr* %101, i32 %102, i8* %22, i32 %103, i8* %25, i32 %104, i32 %107)
store i32 %108, i32* %16
%109 = load i32, i32* %16
%110 = icmp ne i32 %109, 0
br i1 %110, label %111, label %117
111: ; preds = %100
%112 = load i32, i32* %16
%113 = call i32 @gai_strerror(i32 %112)
%114 = sext i32 %113 to i64
%115 = inttoptr i64 %114 to i8*
%116 = call i32 (i32, i8*, i8*, ...) @errx(i32 1, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.6, i64 0, i64 0), i8* %115)
br label %117
117: ; preds = %111, %100
%118 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.7, i64 0, i64 0), i8* %22, i8* %25)
%119 = load %struct.addrinfo*, %struct.addrinfo** %13
%120 = call i32 @freeaddrinfo(%struct.addrinfo* %119)
%121 = load i32, i32* %18
%122 = load i8*, i8** %7
ret i32 %121
}

LLVM IR (-O2):
%struct.addrinfo = type { i32, i32, i32, i32, i32, %struct.addrinfo*, i64 }
%struct.sockaddr_storage = type { i32 }
%struct.sockaddr = type { i32 }
@NI_MAXHOST = global i32 0
@NI_MAXSERV = global i32 0
@.str = [22 x i8] c"getaddrinfo %s %s: %s\00"
@.str.1 = [15 x i8] c"connect socket\00"
@SOL_SOCKET = global i32 0
@SO_SNDBUF = global i32 0
@.str.2 = [18 x i8] c"setsockopt sndbuf\00"
@.str.3 = [8 x i8] c"connect\00"
@errno = global i32 0
@.str.4 = [3 x i8] c"%s\00"
@.str.5 = [20 x i8] c"connect getpeername\00"
@NI_NUMERICHOST = global i32 0
@NI_NUMERICSERV = global i32 0
@.str.6 = [24 x i8] c"connect getnameinfo: %s\00"
@.str.7 = [15 x i8] c"connect %s %s\0A\00"
define i32 @socket_connect(%struct.addrinfo* %0, i8* %1, i8* %2) {
%4 = alloca %struct.sockaddr_storage
%5 = alloca i32
%6 = alloca %struct.addrinfo*
%7 = alloca i32
%8 = load i32, i32* @NI_MAXHOST
%9 = zext i32 %8 to i64
%10 = alloca i8, i64 %9
%11 = load i32, i32* @NI_MAXSERV
%12 = zext i32 %11 to i64
%13 = alloca i8, i64 %12
%14 = bitcast %struct.sockaddr_storage* %4 to i8*
%15 = bitcast i32* %5 to i8*
%16 = bitcast %struct.addrinfo** %6 to i8*
%17 = bitcast i32* %7 to i8*
%18 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %0, i64 0, i32 6
store i64 0, i64* %18
%19 = call i32 @getaddrinfo(i8* %1, i8* %2, %struct.addrinfo* %0, %struct.addrinfo** %6)
%20 = icmp eq i32 %19, 0
br i1 %20, label %24, label %21
21: ; preds = %3
%22 = call i32 @gai_strerror(i32 %19)
%23 = call i32 (i32, i8*, i8*, ...) @errx(i32 1, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* %1, i8* %2, i32 %22)
br label %24
24: ; preds = %21, %3
%25 = load %struct.addrinfo*, %struct.addrinfo** %6
%26 = icmp eq %struct.addrinfo* %25, null
br i1 %26, label %59, label %27
27: ; preds = %24, %54
%28 = phi %struct.addrinfo* [ %57, %54 ], [ %25, %24 ]
%29 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %28, i64 0, i32 4
%30 = load i32, i32* %29
%31 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %28, i64 0, i32 3
%32 = load i32, i32* %31
%33 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %28, i64 0, i32 2
%34 = load i32, i32* %33
%35 = call i32 @socket(i32 %30, i32 %32, i32 %34)
%36 = icmp eq i32 %35, -1
br i1 %36, label %54, label %37
37: ; preds = %27
store i32 100000, i32* %7
%38 = load i32, i32* @SOL_SOCKET
%39 = load i32, i32* @SO_SNDBUF
%40 = call i32 @setsockopt(i32 %35, i32 %38, i32 %39, i32* %7, i32 4)
%41 = icmp eq i32 %40, -1
br i1 %41, label %42, label %44
42: ; preds = %37
%43 = call i32 (i32, i8*, ...) @err(i32 1, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0))
br label %44
44: ; preds = %42, %37
%45 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %28, i64 0, i32 1
%46 = load i32, i32* %45
%47 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %28, i64 0, i32 0
%48 = load i32, i32* %47
%49 = call i32 @connect(i32 %35, i32 %46, i32 %48)
%50 = icmp eq i32 %49, -1
br i1 %50, label %51, label %62
51: ; preds = %44
%52 = load i32, i32* @errno
%53 = call i32 @close(i32 %35)
store i32 %52, i32* @errno
br label %54
54: ; preds = %27, %51
%55 = phi i8* [ getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), %51 ], [ getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), %27 ]
%56 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %28, i64 0, i32 5
%57 = load %struct.addrinfo*, %struct.addrinfo** %56
%58 = icmp eq %struct.addrinfo* %57, null
br i1 %58, label %59, label %27
59: ; preds = %54, %24
%60 = phi i8* [ null, %24 ], [ %55, %54 ]
%61 = call i32 (i32, i8*, ...) @err(i32 1, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i8* %60)
br label %62
62: ; preds = %44, %59
%63 = phi i32 [ -1, %59 ], [ %35, %44 ]
store i32 4, i32* %5
%64 = bitcast %struct.sockaddr_storage* %4 to %struct.sockaddr*
%65 = call i32 @getpeername(i32 %63, %struct.sockaddr* %64, i32* %5)
%66 = icmp eq i32 %65, -1
br i1 %66, label %67, label %69
67: ; preds = %62
%68 = call i32 (i32, i8*, ...) @err(i32 1, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.5, i64 0, i64 0))
br label %69
69: ; preds = %67, %62
%70 = load i32, i32* %5
%71 = load i32, i32* @NI_NUMERICHOST
%72 = load i32, i32* @NI_NUMERICSERV
%73 = or i32 %72, %71
%74 = call i32 @getnameinfo(%struct.sockaddr* %64, i32 %70, i8* %10, i32 %8, i8* %13, i32 %11, i32 %73)
%75 = icmp eq i32 %74, 0
br i1 %75, label %81, label %76
76: ; preds = %69
%77 = call i32 @gai_strerror(i32 %74)
%78 = sext i32 %77 to i64
%79 = inttoptr i64 %78 to i8*
%80 = call i32 (i32, i8*, i8*, ...) @errx(i32 1, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.6, i64 0, i64 0), i8* %79)
br label %81
81: ; preds = %76, %69
%82 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.7, i64 0, i64 0), i8* %10, i8* %13)
%83 = load %struct.addrinfo*, %struct.addrinfo** %6
%84 = call i32 @freeaddrinfo(%struct.addrinfo* %83)
ret i32 %63
}