#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 ai_flags;
  int ai_socktype;
  int ai_family;
  struct addrinfo *ai_next;
  int /*<<< orphan*/ ai_addrlen;
  int /*<<< orphan*/ ai_addr;
  int /*<<< orphan*/ ai_protocol;
};
typedef int socklen_t;
typedef int /*<<< orphan*/ rhints;
typedef int /*<<< orphan*/ remote_addr;
typedef int /*<<< orphan*/ on;
typedef int /*<<< orphan*/ lhints;

/* Variables and functions */
int AI_PASSIVE;
int SOCK_DGRAM;
int SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEADDR;
int accept(int, struct sockaddr *, int *);
scalar_t__ bind(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
scalar_t__ connect(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *, ...);
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
int getaddrinfo(char const *, char const *, struct addrinfo *,
                struct addrinfo **);
scalar_t__ listen(int, int);
int /*<<< orphan*/ memset(struct addrinfo *, char, int);
int /*<<< orphan*/ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/,
                              char *, int);
int socket(int, int, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
scalar_t__ strcmp(char const *, char *);
int socketopen(int family, int type, const char *localpname,
               const char *remotepname, const char *remotehostname) {
  struct addrinfo *lres, *lres0;
  struct addrinfo lhints;
  struct addrinfo *rres, *rres0;
  struct addrinfo rhints;
  int lerror, rerror;
  int socket_fd = (-1);
  int any_remote_host = (strcmp(remotehostname, "0") == 0);
  memset(&lhints, '\0', sizeof(lhints));
  lhints.ai_flags = AI_PASSIVE | 0;
  lhints.ai_socktype = type;
  lhints.ai_family = family;
  lerror = getaddrinfo(NULL, localpname, &lhints, &lres);
  if (lerror) {
    if (strcmp(localpname, "0") != 0) {
      fprintf(stderr, "local port %s invalid in `/inet'", localpname);
      exit(1);
    }
    lres0 = NULL;
    lres = &lhints;
  } else
    lres0 = lres;
  while (lres != NULL) {
    memset(&rhints, '\0', sizeof(rhints));
    rhints.ai_flags = lhints.ai_flags;
    rhints.ai_socktype = lhints.ai_socktype;
    rhints.ai_family = lhints.ai_family;
    rhints.ai_protocol = lhints.ai_protocol;
    rerror = getaddrinfo(any_remote_host ? NULL : remotehostname, remotepname,
                         &rhints, &rres);
    if (rerror) {
      if (lres0 != NULL)
        freeaddrinfo(lres0);
      fprintf(stderr, "remote host and port information (%s, %s) invalid",
              remotehostname, remotepname);
      exit(1);
    }
    rres0 = rres;
    socket_fd = (-1);
    while (rres != NULL) {
      socket_fd = socket(rres->ai_family, rres->ai_socktype, rres->ai_protocol);
      if (socket_fd < 0 || socket_fd == (-1))
        goto nextrres;
      if (type == SOCK_STREAM) {
        int on = 1;
        setsockopt(socket_fd, SOL_SOCKET, SO_REUSEADDR, (char *)&on,
                   sizeof(on));
      }
      if (bind(socket_fd, lres->ai_addr, lres->ai_addrlen) != 0)
        goto nextrres;
      if (!any_remote_host) {
        if (connect(socket_fd, rres->ai_addr, rres->ai_addrlen) == 0)
          break;
      } else {
        if (type == SOCK_STREAM) {
          int clientsocket_fd = (-1);
          struct sockaddr_storage remote_addr;
          socklen_t namelen = sizeof(remote_addr);
          if (listen(socket_fd, 1) >= 0 &&
              (clientsocket_fd =
                   accept(socket_fd, (struct sockaddr *)&remote_addr,
                          &namelen)) >= 0) {
            close(socket_fd);
            socket_fd = clientsocket_fd;
            break;
          }
        } else if (type == SOCK_DGRAM) {
        }
      }
    nextrres:
      if (socket_fd != (-1))
        close(socket_fd);
      socket_fd = (-1);
      rres = rres->ai_next;
    }
    freeaddrinfo(rres0);
    if (socket_fd != (-1))
      break;
    lres = lres->ai_next;
  }
  if (lres0)
    freeaddrinfo(lres0);
  return socket_fd;
}


x86 assembly (clang, -O0):
<socketopen>:
push   %rbp
mov    %rsp,%rbp
sub    $0xc0,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %rdx,-0x10(%rbp)
mov    %rcx,-0x18(%rbp)
mov    %r8,-0x20(%rbp)
movl   $0xffffffff,-0x9c(%rbp)
ff ff ff
mov    -0x20(%rbp),%rdi
lea    0xdfe(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
sete   %al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0xa0(%rbp)
lea    -0x58(%rbp),%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1040 <memset@plt>
mov    0x2db0(%rip),%rax
mov    (%rax),%eax
or     $0x0,%eax
mov    %eax,-0x58(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x54(%rbp)
mov    -0x4(%rbp),%eax
mov    %eax,-0x50(%rbp)
mov    -0x10(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edi
lea    -0x58(%rbp),%rdx
lea    -0x28(%rbp),%rcx
call   10d0 <getaddrinfo@plt>
mov    %eax,-0x94(%rbp)
cmpl   $0x0,-0x94(%rbp)
je     12c0 <socketopen+0xf0>
mov    -0x10(%rbp),%rdi
lea    0xd89(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
je     12ab <socketopen+0xdb>
mov    0x2d3b(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xd68(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
movq   $0x0,-0x30(%rbp)
lea    -0x58(%rbp),%rax
mov    %rax,-0x28(%rbp)
jmp    12c8 <socketopen+0xf8>
mov    -0x28(%rbp),%rax
mov    %rax,-0x30(%rbp)
jmp    12cd <socketopen+0xfd>
cmpq   $0x0,-0x28(%rbp)
je     15af <socketopen+0x3df>
lea    -0x90(%rbp),%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1040 <memset@plt>
mov    -0x58(%rbp),%eax
mov    %eax,-0x90(%rbp)
mov    -0x54(%rbp),%eax
mov    %eax,-0x8c(%rbp)
mov    -0x50(%rbp),%eax
mov    %eax,-0x88(%rbp)
mov    -0x38(%rbp),%eax
mov    %eax,-0x70(%rbp)
cmpl   $0x0,-0xa0(%rbp)
je     1327 <socketopen+0x157>
xor    %eax,%eax
mov    %rax,-0xc0(%rbp)
jmp    1332 <socketopen+0x162>
mov    -0x20(%rbp),%rax
mov    %rax,-0xc0(%rbp)
mov    -0xc0(%rbp),%rdi
mov    -0x18(%rbp),%rsi
lea    -0x90(%rbp),%rdx
lea    -0x60(%rbp),%rcx
call   10d0 <getaddrinfo@plt>
mov    %eax,-0x98(%rbp)
cmpl   $0x0,-0x98(%rbp)
je     139d <socketopen+0x1cd>
cmpq   $0x0,-0x30(%rbp)
je     1374 <socketopen+0x1a4>
mov    -0x30(%rbp),%rdi
call   10e0 <freeaddrinfo@plt>
mov    0x2c4d(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%rdx
mov    -0x18(%rbp),%rcx
lea    0xc97(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0x60(%rbp),%rax
mov    %rax,-0x68(%rbp)
movl   $0xffffffff,-0x9c(%rbp)
ff ff ff
cmpq   $0x0,-0x60(%rbp)
je     1583 <socketopen+0x3b3>
mov    -0x60(%rbp),%rax
mov    0x8(%rax),%edi
mov    -0x60(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x60(%rbp),%rax
mov    0x20(%rax),%edx
call   10f0 <socket@plt>
mov    %eax,-0x9c(%rbp)
cmpl   $0x0,-0x9c(%rbp)
jl     13f4 <socketopen+0x224>
cmpl   $0xffffffff,-0x9c(%rbp)
jne    13f9 <socketopen+0x229>
jmp    1550 <socketopen+0x380>
mov    -0x8(%rbp),%eax
mov    0x2bbd(%rip),%rcx
cmp    (%rcx),%eax
jne    143f <socketopen+0x26f>
movl   $0x1,-0xa4(%rbp)
mov    -0x9c(%rbp),%edi
mov    0x2bc6(%rip),%rax
mov    (%rax),%esi
mov    0x2b8d(%rip),%rax
mov    (%rax),%edx
lea    -0xa4(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    -0x9c(%rbp),%edi
mov    -0x28(%rbp),%rax
mov    0x1c(%rax),%esi
mov    -0x28(%rbp),%rax
mov    0x18(%rax),%edx
call   1090 <bind@plt>
cmp    $0x0,%rax
je     1467 <socketopen+0x297>
jmp    1550 <socketopen+0x380>
cmpl   $0x0,-0xa0(%rbp)
jne    14a1 <socketopen+0x2d1>
mov    -0x9c(%rbp),%edi
mov    -0x60(%rbp),%rax
mov    0x1c(%rax),%esi
mov    -0x60(%rbp),%rax
mov    0x18(%rax),%edx
call   10c0 <connect@plt>
cmp    $0x0,%rax
jne    149c <socketopen+0x2cc>
jmp    1583 <socketopen+0x3b3>
jmp    154b <socketopen+0x37b>
mov    -0x8(%rbp),%eax
mov    0x2b15(%rip),%rcx
cmp    (%rcx),%eax
jne    152a <socketopen+0x35a>
movl   $0xffffffff,-0xa8(%rbp)
ff ff ff
movl   $0x4,-0xb4(%rbp)
mov    -0x9c(%rbp),%edi
mov    $0x1,%esi
call   1080 <listen@plt>
cmp    $0x0,%rax
jl     1525 <socketopen+0x355>
mov    -0x9c(%rbp),%edi
lea    -0xb0(%rbp),%rsi
lea    -0xb4(%rbp),%rdx
call   10a0 <accept@plt>
mov    %eax,-0xa8(%rbp)
cmp    $0x0,%eax
jl     1525 <socketopen+0x355>
mov    -0x9c(%rbp),%edi
call   1050 <close@plt>
mov    -0xa8(%rbp),%eax
mov    %eax,-0x9c(%rbp)
jmp    1583 <socketopen+0x3b3>
jmp    1546 <socketopen+0x376>
mov    -0x8(%rbp),%eax
mov    0x2aa4(%rip),%rcx
cmp    (%rcx),%eax
jne    1541 <socketopen+0x371>
jmp    1541 <socketopen+0x371>
jmp    1546 <socketopen+0x376>
jmp    154b <socketopen+0x37b>
jmp    1550 <socketopen+0x380>
cmpl   $0xffffffff,-0x9c(%rbp)
je     1568 <socketopen+0x398>
mov    -0x9c(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x9c(%rbp)
ff ff ff
mov    -0x60(%rbp),%rax
mov    0x10(%rax),%rax
mov    %rax,-0x60(%rbp)
jmp    13af <socketopen+0x1df>
mov    -0x68(%rbp),%rdi
call   10e0 <freeaddrinfo@plt>
cmpl   $0xffffffff,-0x9c(%rbp)
je     159e <socketopen+0x3ce>
jmp    15af <socketopen+0x3df>
mov    -0x28(%rbp),%rax
mov    0x10(%rax),%rax
mov    %rax,-0x28(%rbp)
jmp    12cd <socketopen+0xfd>
cmpq   $0x0,-0x30(%rbp)
je     15c3 <socketopen+0x3f3>
mov    -0x30(%rbp),%rdi
call   10e0 <freeaddrinfo@plt>
mov    -0x9c(%rbp),%eax
add    $0xc0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<socketopen>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x98,%rsp
mov    %rcx,0x38(%rsp)
mov    %rdx,%r14
mov    %esi,%ebp
mov    %edi,%r12d
lea    0xe0b(%rip),%rsi
mov    %r8,0x30(%rsp)
mov    %r8,%rdi
call   1060 <strcmp@plt>
mov    %rax,%r13
lea    0x48(%rsp),%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1040 <memset@plt>
mov    0x2dc0(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x48(%rsp)
mov    %ebp,0x4c(%rsp)
mov    %r12d,0x50(%rsp)
lea    0x8(%rsp),%rcx
xor    %edi,%edi
mov    %r14,%rsi
mov    %r15,%rdx
call   10d0 <getaddrinfo@plt>
test   %eax,%eax
je     1266 <socketopen+0x96>
lea    0xdb4(%rip),%rsi
mov    %r14,%rdi
call   1060 <strcmp@plt>
test   %rax,%rax
jne    14c2 <socketopen+0x2f2>
mov    %r15,0x8(%rsp)
xor    %eax,%eax
jmp    1274 <socketopen+0xa4>
mov    0x8(%rsp),%rax
test   %rax,%rax
je     1441 <socketopen+0x271>
mov    %rax,0x18(%rsp)
test   %r13,%r13
mov    0x30(%rsp),%rax
cmove  %r13,%rax
mov    %rax,0x40(%rsp)
mov    0x2d37(%rip),%r12
mov    0x2d50(%rip),%rbx
jmp    12c1 <socketopen+0xf1>
nopw   0x0(%rax,%rax,1)
mov    0x20(%rsp),%rdi
call   10e0 <freeaddrinfo@plt>
mov    0x8(%rsp),%rax
mov    0x10(%rax),%rax
mov    %rax,0x8(%rsp)
test   %rax,%rax
je     1439 <socketopen+0x269>
mov    %rbx,%r15
lea    0x70(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1040 <memset@plt>
mov    0x48(%rsp),%eax
mov    %eax,0x70(%rsp)
mov    0x4c(%rsp),%rax
mov    %rax,0x74(%rsp)
mov    0x68(%rsp),%eax
mov    %eax,0x90(%rsp)
mov    0x40(%rsp),%rdi
mov    0x38(%rsp),%rsi
mov    %rbx,%rdx
lea    0x10(%rsp),%rcx
call   10d0 <getaddrinfo@plt>
test   %eax,%eax
jne    1485 <socketopen+0x2b5>
mov    0x10(%rsp),%rax
mov    %rax,0x20(%rsp)
test   %rax,%rax
mov    %r15,%rbx
je     12a0 <socketopen+0xd0>
mov    0x20(%rsp),%rax
jmp    1377 <socketopen+0x1a7>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    0x8(%rsp),%rax
mov    0x18(%rax),%edx
mov    0x1c(%rax),%esi
mov    %r14d,%edi
call   1090 <bind@plt>
test   %rax,%rax
je     13d0 <socketopen+0x200>
mov    %r14d,%edi
call   1050 <close@plt>
mov    0x10(%rsp),%rax
mov    0x10(%rax),%rax
mov    %rax,0x10(%rsp)
test   %rax,%rax
je     12a0 <socketopen+0xd0>
mov    0x4(%rax),%esi
mov    0x8(%rax),%edi
mov    0x20(%rax),%edx
call   10f0 <socket@plt>
mov    %eax,%r14d
test   %eax,%eax
js     13c0 <socketopen+0x1f0>
cmp    %ebp,(%r12)
jne    1340 <socketopen+0x170>
movl   $0x1,(%rsp)
mov    (%rbx),%esi
mov    0x2c1e(%rip),%rax
mov    (%rax),%edx
mov    %r14d,%edi
mov    %rsp,%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
jmp    1340 <socketopen+0x170>
nopw   0x0(%rax,%rax,1)
cmp    $0xffffffff,%r14d
jne    1358 <socketopen+0x188>
jmp    1360 <socketopen+0x190>
nopl   0x0(%rax,%rax,1)
test   %r13,%r13
je     13f3 <socketopen+0x223>
mov    0x10(%rsp),%rax
mov    0x18(%rax),%edx
mov    0x1c(%rax),%esi
mov    %r14d,%edi
call   10c0 <connect@plt>
test   %rax,%rax
jne    1358 <socketopen+0x188>
jmp    1457 <socketopen+0x287>
cmp    %ebp,(%r12)
jne    1358 <socketopen+0x188>
mov    %r13,%rbx
movl   $0x4,0x2c(%rsp)
mov    %r14d,%edi
mov    $0x1,%esi
call   1080 <listen@plt>
test   %rax,%rax
js     142e <socketopen+0x25e>
mov    %r14d,%edi
mov    %rsp,%rsi
lea    0x2c(%rsp),%rdx
call   10a0 <accept@plt>
test   %eax,%eax
jns    1449 <socketopen+0x279>
mov    %rbx,%r13
mov    %r15,%rbx
jmp    1358 <socketopen+0x188>
mov    $0xffffffff,%r14d
jmp    1461 <socketopen+0x291>
mov    $0xffffffff,%r14d
jmp    1470 <socketopen+0x2a0>
mov    %eax,%r13d
mov    %r14d,%edi
call   1050 <close@plt>
mov    %r13d,%r14d
mov    0x20(%rsp),%rdi
call   10e0 <freeaddrinfo@plt>
mov    0x18(%rsp),%rdi
test   %rdi,%rdi
je     1470 <socketopen+0x2a0>
call   10e0 <freeaddrinfo@plt>
mov    %r14d,%eax
add    $0x98,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
cmpq   $0x0,0x18(%rsp)
je     1497 <socketopen+0x2c7>
mov    0x18(%rsp),%rdi
call   10e0 <freeaddrinfo@plt>
mov    0x2b32(%rip),%rax
mov    (%rax),%edi
lea    0xb7c(%rip),%rsi
mov    0x30(%rsp),%rdx
mov    0x38(%rsp),%rcx
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    0x2b07(%rip),%rax
mov    (%rax),%edi
lea    0xb30(%rip),%rsi
mov    %r14,%rdx
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
%struct.addrinfo = type { i32, i32, i32, %struct.addrinfo*, i32, i32, i32 }
%struct.sockaddr_storage = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [2 x i8] c"0\00"
@AI_PASSIVE = global i32 0
@stderr = global i32 0
@.str.1 = [33 x i8] c"local port %s invalid in `/inet'\00"
@.str.2 = [50 x i8] c"remote host and port information (%s, %s) invalid\00"
@SOCK_STREAM = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@SOCK_DGRAM = global i32 0
define i32 @socketopen(i32 %0, i32 %1, i8* %2, i8* %3, i8* %4) {
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca %struct.addrinfo*
%12 = alloca %struct.addrinfo*
%13 = alloca %struct.addrinfo
%14 = alloca %struct.addrinfo*
%15 = alloca %struct.addrinfo*
%16 = alloca %struct.addrinfo
%17 = alloca i32
%18 = alloca i32
%19 = alloca i32
%20 = alloca i32
%21 = alloca i32
%22 = alloca i32
%23 = alloca %struct.sockaddr_storage
%24 = alloca i32
store i32 %0, i32* %6
store i32 %1, i32* %7
store i8* %2, i8** %8
store i8* %3, i8** %9
store i8* %4, i8** %10
store i32 -1, i32* %19
%25 = load i8*, i8** %10
%26 = call i64 @strcmp(i8* %25, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%27 = icmp eq i64 %26, 0
%28 = zext i1 %27 to i32
store i32 %28, i32* %20
%29 = call i32 @memset(%struct.addrinfo* %13, i8 signext 0, i32 40)
%30 = load i32, i32* @AI_PASSIVE
%31 = or i32 %30, 0
%32 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %13, i32 0, i32 0
store i32 %31, i32* %32
%33 = load i32, i32* %7
%34 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %13, i32 0, i32 1
store i32 %33, i32* %34
%35 = load i32, i32* %6
%36 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %13, i32 0, i32 2
store i32 %35, i32* %36
%37 = load i8*, i8** %8
%38 = call i32 @getaddrinfo(i8* null, i8* %37, %struct.addrinfo* %13, %struct.addrinfo** %11)
store i32 %38, i32* %17
%39 = load i32, i32* %17
%40 = icmp ne i32 %39, 0
br i1 %40, label %41, label %51
41: ; preds = %5
%42 = load i8*, i8** %8
%43 = call i64 @strcmp(i8* %42, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%44 = icmp ne i64 %43, 0
br i1 %44, label %45, label %50
45: ; preds = %41
%46 = load i32, i32* @stderr
%47 = load i8*, i8** %8
%48 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %46, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i8* %47)
%49 = call i32 @exit(i32 1)
unreachable
50: ; preds = %41
store %struct.addrinfo* null, %struct.addrinfo** %12
store %struct.addrinfo* %13, %struct.addrinfo** %11
br label %53
51: ; preds = %5
%52 = load %struct.addrinfo*, %struct.addrinfo** %11
store %struct.addrinfo* %52, %struct.addrinfo** %12
br label %53
53: ; preds = %51, %50
br label %54
54: ; preds = %194, %53
%55 = load %struct.addrinfo*, %struct.addrinfo** %11
%56 = icmp ne %struct.addrinfo* %55, null
br i1 %56, label %57, label %198
57: ; preds = %54
%58 = call i32 @memset(%struct.addrinfo* %16, i8 signext 0, i32 40)
%59 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %13, i32 0, i32 0
%60 = load i32, i32* %59
%61 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %16, i32 0, i32 0
store i32 %60, i32* %61
%62 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %13, i32 0, i32 1
%63 = load i32, i32* %62
%64 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %16, i32 0, i32 1
store i32 %63, i32* %64
%65 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %13, i32 0, i32 2
%66 = load i32, i32* %65
%67 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %16, i32 0, i32 2
store i32 %66, i32* %67
%68 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %13, i32 0, i32 6
%69 = load i32, i32* %68
%70 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %16, i32 0, i32 6
store i32 %69, i32* %70
%71 = load i32, i32* %20
%72 = icmp ne i32 %71, 0
br i1 %72, label %73, label %74
73: ; preds = %57
br label %76
74: ; preds = %57
%75 = load i8*, i8** %10
br label %76
76: ; preds = %74, %73
%77 = phi i8* [ null, %73 ], [ %75, %74 ]
%78 = load i8*, i8** %9
%79 = call i32 @getaddrinfo(i8* %77, i8* %78, %struct.addrinfo* %16, %struct.addrinfo** %14)
store i32 %79, i32* %18
%80 = load i32, i32* %18
%81 = icmp ne i32 %80, 0
br i1 %81, label %82, label %94
82: ; preds = %76
%83 = load %struct.addrinfo*, %struct.addrinfo** %12
%84 = icmp ne %struct.addrinfo* %83, null
br i1 %84, label %85, label %88
85: ; preds = %82
%86 = load %struct.addrinfo*, %struct.addrinfo** %12
%87 = call i32 @freeaddrinfo(%struct.addrinfo* %86)
br label %88
88: ; preds = %85, %82
%89 = load i32, i32* @stderr
%90 = load i8*, i8** %10
%91 = load i8*, i8** %9
%92 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %89, i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.2, i64 0, i64 0), i8* %90, i8* %91)
%93 = call i32 @exit(i32 1)
unreachable
94: ; preds = %76
%95 = load %struct.addrinfo*, %struct.addrinfo** %14
store %struct.addrinfo* %95, %struct.addrinfo** %15
store i32 -1, i32* %19
br label %96
96: ; preds = %184, %94
%97 = load %struct.addrinfo*, %struct.addrinfo** %14
%98 = icmp ne %struct.addrinfo* %97, null
br i1 %98, label %99, label %188
99: ; preds = %96
%100 = load %struct.addrinfo*, %struct.addrinfo** %14
%101 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %100, i32 0, i32 2
%102 = load i32, i32* %101
%103 = load %struct.addrinfo*, %struct.addrinfo** %14
%104 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %103, i32 0, i32 1
%105 = load i32, i32* %104
%106 = load %struct.addrinfo*, %struct.addrinfo** %14
%107 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %106, i32 0, i32 6
%108 = load i32, i32* %107
%109 = call i32 @socket(i32 %102, i32 %105, i32 %108)
store i32 %109, i32* %19
%110 = load i32, i32* %19
%111 = icmp slt i32 %110, 0
br i1 %111, label %115, label %112
112: ; preds = %99
%113 = load i32, i32* %19
%114 = icmp eq i32 %113, -1
br i1 %114, label %115, label %116
115: ; preds = %112, %99
br label %178
116: ; preds = %112
%117 = load i32, i32* %7
%118 = load i32, i32* @SOCK_STREAM
%119 = icmp eq i32 %117, %118
br i1 %119, label %120, label %126
120: ; preds = %116
store i32 1, i32* %21
%121 = load i32, i32* %19
%122 = load i32, i32* @SOL_SOCKET
%123 = load i32, i32* @SO_REUSEADDR
%124 = bitcast i32* %21 to i8*
%125 = call i32 @setsockopt(i32 %121, i32 %122, i32 %123, i8* %124, i32 4)
br label %126
126: ; preds = %120, %116
%127 = load i32, i32* %19
%128 = load %struct.addrinfo*, %struct.addrinfo** %11
%129 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %128, i32 0, i32 5
%130 = load i32, i32* %129
%131 = load %struct.addrinfo*, %struct.addrinfo** %11
%132 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %131, i32 0, i32 4
%133 = load i32, i32* %132
%134 = call i64 @bind(i32 %127, i32 %130, i32 %133)
%135 = icmp ne i64 %134, 0
br i1 %135, label %136, label %137
136: ; preds = %126
br label %178
137: ; preds = %126
%138 = load i32, i32* %20
%139 = icmp ne i32 %138, 0
br i1 %139, label %152, label %140
140: ; preds = %137
%141 = load i32, i32* %19
%142 = load %struct.addrinfo*, %struct.addrinfo** %14
%143 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %142, i32 0, i32 5
%144 = load i32, i32* %143
%145 = load %struct.addrinfo*, %struct.addrinfo** %14
%146 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %145, i32 0, i32 4
%147 = load i32, i32* %146
%148 = call i64 @connect(i32 %141, i32 %144, i32 %147)
%149 = icmp eq i64 %148, 0
br i1 %149, label %150, label %151
150: ; preds = %140
br label %188
151: ; preds = %140
br label %177
152: ; preds = %137
%153 = load i32, i32* %7
%154 = load i32, i32* @SOCK_STREAM
%155 = icmp eq i32 %153, %154
br i1 %155, label %156, label %170
156: ; preds = %152
store i32 -1, i32* %22
store i32 4, i32* %24
%157 = load i32, i32* %19
%158 = call i64 @listen(i32 %157, i32 1)
%159 = icmp sge i64 %158, 0
br i1 %159, label %160, label %169
160: ; preds = %156
%161 = load i32, i32* %19
%162 = bitcast %struct.sockaddr_storage* %23 to %struct.sockaddr*
%163 = call i32 @accept(i32 %161, %struct.sockaddr* %162, i32* %24)
store i32 %163, i32* %22
%164 = icmp sge i32 %163, 0
br i1 %164, label %165, label %169
165: ; preds = %160
%166 = load i32, i32* %19
%167 = call i32 @close(i32 %166)
%168 = load i32, i32* %22
store i32 %168, i32* %19
br label %188
169: ; preds = %160, %156
br label %176
170: ; preds = %152
%171 = load i32, i32* %7
%172 = load i32, i32* @SOCK_DGRAM
%173 = icmp eq i32 %171, %172
br i1 %173, label %174, label %175
174: ; preds = %170
br label %175
175: ; preds = %174, %170
br label %176
176: ; preds = %175, %169
br label %177
177: ; preds = %176, %151
br label %178
178: ; preds = %177, %136, %115
%179 = load i32, i32* %19
%180 = icmp ne i32 %179, -1
br i1 %180, label %181, label %184
181: ; preds = %178
%182 = load i32, i32* %19
%183 = call i32 @close(i32 %182)
br label %184
184: ; preds = %181, %178
store i32 -1, i32* %19
%185 = load %struct.addrinfo*, %struct.addrinfo** %14
%186 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %185, i32 0, i32 3
%187 = load %struct.addrinfo*, %struct.addrinfo** %186
store %struct.addrinfo* %187, %struct.addrinfo** %14
br label %96
188: ; preds = %165, %150, %96
%189 = load %struct.addrinfo*, %struct.addrinfo** %15
%190 = call i32 @freeaddrinfo(%struct.addrinfo* %189)
%191 = load i32, i32* %19
%192 = icmp ne i32 %191, -1
br i1 %192, label %193, label %194
193: ; preds = %188
br label %198
194: ; preds = %188
%195 = load %struct.addrinfo*, %struct.addrinfo** %11
%196 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %195, i32 0, i32 3
%197 = load %struct.addrinfo*, %struct.addrinfo** %196
store %struct.addrinfo* %197, %struct.addrinfo** %11
br label %54
198: ; preds = %193, %54
%199 = load %struct.addrinfo*, %struct.addrinfo** %12
%200 = icmp ne %struct.addrinfo* %199, null
br i1 %200, label %201, label %204
201: ; preds = %198
%202 = load %struct.addrinfo*, %struct.addrinfo** %12
%203 = call i32 @freeaddrinfo(%struct.addrinfo* %202)
br label %204
204: ; preds = %201, %198
%205 = load i32, i32* %19
ret i32 %205
}

LLVM IR (-O2):
%struct.addrinfo = type { i32, i32, i32, %struct.addrinfo*, i32, i32, i32 }
%struct.sockaddr_storage = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [2 x i8] c"0\00"
@AI_PASSIVE = global i32 0
@stderr = global i32 0
@.str.1 = [33 x i8] c"local port %s invalid in `/inet'\00"
@.str.2 = [50 x i8] c"remote host and port information (%s, %s) invalid\00"
@SOCK_STREAM = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@SOCK_DGRAM = global i32 0
define i32 @socketopen(i32 %0, i32 %1, i8* %2, i8* %3, i8* %4) {
%6 = alloca %struct.addrinfo*
%7 = alloca %struct.addrinfo
%8 = alloca %struct.addrinfo*
%9 = alloca %struct.addrinfo
%10 = alloca i32
%11 = alloca %struct.sockaddr_storage
%12 = alloca i32
%13 = bitcast %struct.addrinfo** %6 to i8*
%14 = bitcast %struct.addrinfo* %7 to i8*
%15 = bitcast %struct.addrinfo** %8 to i8*
%16 = bitcast %struct.addrinfo* %9 to i8*
%17 = tail call i64 @strcmp(i8* %4, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%18 = icmp eq i64 %17, 0
%19 = call i32 @memset(%struct.addrinfo* %7, i8 signext 0, i32 40)
%20 = load i32, i32* @AI_PASSIVE
%21 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %7, i64 0, i32 0
store i32 %20, i32* %21
%22 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %7, i64 0, i32 1
store i32 %1, i32* %22
%23 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %7, i64 0, i32 2
store i32 %0, i32* %23
%24 = call i32 @getaddrinfo(i8* null, i8* %2, %struct.addrinfo* %7, %struct.addrinfo** %6)
%25 = icmp eq i32 %24, 0
br i1 %25, label %34, label %26
26: ; preds = %5
%27 = call i64 @strcmp(i8* %2, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%28 = icmp eq i64 %27, 0
br i1 %28, label %33, label %29
29: ; preds = %26
%30 = load i32, i32* @stderr
%31 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %30, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i8* %2)
%32 = call i32 @exit(i32 1)
unreachable
33: ; preds = %26
store %struct.addrinfo* %7, %struct.addrinfo** %6
br label %37
34: ; preds = %5
%35 = load %struct.addrinfo*, %struct.addrinfo** %6
%36 = icmp eq %struct.addrinfo* %35, null
br i1 %36, label %137, label %37
37: ; preds = %33, %34
%38 = phi %struct.addrinfo* [ null, %33 ], [ %35, %34 ]
%39 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %9, i64 0, i32 0
%40 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %9, i64 0, i32 1
%41 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %9, i64 0, i32 2
%42 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %7, i64 0, i32 6
%43 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %9, i64 0, i32 6
%44 = select i1 %18, i8* null, i8* %4
%45 = bitcast i32* %10 to i8*
%46 = bitcast %struct.sockaddr_storage* %11 to i8*
%47 = bitcast i32* %12 to i8*
%48 = bitcast %struct.sockaddr_storage* %11 to %struct.sockaddr*
br label %49
49: ; preds = %37, %126
%50 = call i32 @memset(%struct.addrinfo* %9, i8 signext 0, i32 40)
%51 = load i32, i32* %21
store i32 %51, i32* %39
%52 = load i32, i32* %22
store i32 %52, i32* %40
%53 = load i32, i32* %23
store i32 %53, i32* %41
%54 = load i32, i32* %42
store i32 %54, i32* %43
%55 = call i32 @getaddrinfo(i8* %44, i8* %3, %struct.addrinfo* %9, %struct.addrinfo** %8)
%56 = icmp eq i32 %55, 0
br i1 %56, label %65, label %57
57: ; preds = %49
%58 = icmp eq %struct.addrinfo* %38, null
br i1 %58, label %61, label %59
59: ; preds = %57
%60 = call i32 @freeaddrinfo(%struct.addrinfo* %38)
br label %61
61: ; preds = %59, %57
%62 = load i32, i32* @stderr
%63 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %62, i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.2, i64 0, i64 0), i8* %4, i8* %3)
%64 = call i32 @exit(i32 1)
unreachable
65: ; preds = %49
%66 = load %struct.addrinfo*, %struct.addrinfo** %8
%67 = icmp eq %struct.addrinfo* %66, null
br i1 %67, label %126, label %68
68: ; preds = %65, %118
%69 = phi %struct.addrinfo* [ %121, %118 ], [ %66, %65 ]
%70 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %69, i64 0, i32 2
%71 = load i32, i32* %70
%72 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %69, i64 0, i32 1
%73 = load i32, i32* %72
%74 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %69, i64 0, i32 6
%75 = load i32, i32* %74
%76 = call i32 @socket(i32 %71, i32 %73, i32 %75)
%77 = icmp slt i32 %76, 0
br i1 %77, label %114, label %78
78: ; preds = %68
%79 = load i32, i32* @SOCK_STREAM
%80 = icmp eq i32 %79, %1
br i1 %80, label %81, label %85
81: ; preds = %78
store i32 1, i32* %10
%82 = load i32, i32* @SOL_SOCKET
%83 = load i32, i32* @SO_REUSEADDR
%84 = call i32 @setsockopt(i32 %76, i32 %82, i32 %83, i8* %45, i32 4)
br label %85
85: ; preds = %81, %78
%86 = load %struct.addrinfo*, %struct.addrinfo** %6
%87 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %86, i64 0, i32 5
%88 = load i32, i32* %87
%89 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %86, i64 0, i32 4
%90 = load i32, i32* %89
%91 = call i64 @bind(i32 %76, i32 %88, i32 %90)
%92 = icmp eq i64 %91, 0
br i1 %92, label %93, label %116
93: ; preds = %85
br i1 %18, label %102, label %94
94: ; preds = %93
%95 = load %struct.addrinfo*, %struct.addrinfo** %8
%96 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %95, i64 0, i32 5
%97 = load i32, i32* %96
%98 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %95, i64 0, i32 4
%99 = load i32, i32* %98
%100 = call i64 @connect(i32 %76, i32 %97, i32 %99)
%101 = icmp eq i64 %100, 0
br i1 %101, label %123, label %116
102: ; preds = %93
%103 = load i32, i32* @SOCK_STREAM
%104 = icmp eq i32 %103, %1
br i1 %104, label %105, label %116
105: ; preds = %102
store i32 4, i32* %12
%106 = call i64 @listen(i32 %76, i32 1)
%107 = icmp sgt i64 %106, -1
br i1 %107, label %108, label %111
108: ; preds = %105
%109 = call i32 @accept(i32 %76, %struct.sockaddr* %48, i32* %12)
%110 = icmp sgt i32 %109, -1
br i1 %110, label %112, label %111
111: ; preds = %108, %105
br label %116
112: ; preds = %108
%113 = call i32 @close(i32 %76)
br label %123
114: ; preds = %68
%115 = icmp eq i32 %76, -1
br i1 %115, label %118, label %116
116: ; preds = %85, %94, %102, %111, %114
%117 = call i32 @close(i32 %76)
br label %118
118: ; preds = %116, %114
%119 = load %struct.addrinfo*, %struct.addrinfo** %8
%120 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %119, i64 0, i32 3
%121 = load %struct.addrinfo*, %struct.addrinfo** %120
store %struct.addrinfo* %121, %struct.addrinfo** %8
%122 = icmp eq %struct.addrinfo* %121, null
br i1 %122, label %126, label %68
123: ; preds = %94, %112
%124 = phi i32 [ %109, %112 ], [ %76, %94 ]
%125 = call i32 @freeaddrinfo(%struct.addrinfo* %66)
br label %132
126: ; preds = %118, %65
%127 = call i32 @freeaddrinfo(%struct.addrinfo* %66)
%128 = load %struct.addrinfo*, %struct.addrinfo** %6
%129 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %128, i64 0, i32 3
%130 = load %struct.addrinfo*, %struct.addrinfo** %129
store %struct.addrinfo* %130, %struct.addrinfo** %6
%131 = icmp eq %struct.addrinfo* %130, null
br i1 %131, label %132, label %49
132: ; preds = %126, %123
%133 = phi i32 [ %124, %123 ], [ -1, %126 ]
%134 = icmp eq %struct.addrinfo* %38, null
br i1 %134, label %137, label %135
135: ; preds = %132
%136 = call i32 @freeaddrinfo(%struct.addrinfo* %38)
br label %137
137: ; preds = %34, %135, %132
%138 = phi i32 [ %133, %135 ], [ %133, %132 ], [ -1, %34 ]
ret i32 %138
}