#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_2__ TYPE_1__;

/* Type definitions */
struct TYPE_2__ {
  int /*<<< orphan*/ s_addr;
};
struct sockaddr_in {
  int /*<<< orphan*/ sin_port;
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
struct in_addr {
  int /*<<< orphan*/ s_addr;
};
typedef int /*<<< orphan*/ sockname;
typedef int socklen_t;
typedef int /*<<< orphan*/ serv_addr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ bcopy(char *, char *, int);
scalar_t__ connect(int, struct sockaddr *, int);
int dup2(int, int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
scalar_t__ getsockname(int, struct sockaddr *, int *);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ inet_aton(char *, struct in_addr *);
int /*<<< orphan*/ log_stderr(char *, ...);
int /*<<< orphan*/ log_stdout(char *, ...);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdout;
char *strchr(char *, char);
scalar_t__ strcmp(char *, char *);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
long strtol(char *, char **, int);
void redirect_to_socket(char *sockloc) {
  int sock, sock2;
  int port;
  char *hostname;
  char *portname;
  char *p;
  char buf[(4096)];
  int numstreams = 1;
  p = strchr(sockloc, ':');
  if (p == NULL) {
    log_stderr("1700 invalid socket, missing port: %s\n", sockloc);
    exit(1);
  }
  *p = '\0';
  portname = p + 1;
  hostname = sockloc;
  p = strchr(portname, '?');
  if (p != NULL) {
    *p++ = '\0';
    if (strcmp(p, "splitstreams") == 0) {
      numstreams = 2;
    }
  }
  log_stdout("1701 host[%s] port[%s] streams[%d]\n", hostname, portname,
             numstreams);
  char *en = 0;
  long lport = strtol(portname, &en, 10);
  if (*en) {
    log_stderr("1702 Port[%s] is not numeric.\n", portname);
    exit(1);
  }
  port = lport;
  struct in_addr ip;
  if (!inet_aton(hostname, &ip)) {
    log_stderr("1703 Can't parse IP address %s\n", hostname);
    exit(1);
  }
  log_stdout("1704 addr: %x\n", ip.s_addr);
  struct sockaddr_in serv_addr;
  memset(&serv_addr, 0, sizeof(serv_addr));
  serv_addr.sin_family = AF_INET;
  bcopy((char *)&ip.s_addr, (char *)&serv_addr.sin_addr.s_addr,
        sizeof(serv_addr.sin_addr.s_addr));
  serv_addr.sin_port = htons(port);
  log_stdout("1705 About to connect\n");
  sock = socket(AF_INET, SOCK_STREAM, 0);
  if (connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) {
    perror("1707 Error connecting to console socket.");
    exit(1);
  }
  log_stdout("1706 Connected\n");
  struct sockaddr sockname;
  socklen_t namelen = sizeof(sockname);
  if (getsockname(sock, &sockname, &namelen) == 0) {
    struct sockaddr_in *sin = (struct sockaddr_in *)&sockname;
    log_stdout("1708 Local port is %d\n", sin->sin_port);
  } else {
    perror("1709 Cannot get local socket name, ignoring error.");
  }
  if (numstreams == 1) {
    sock2 = sock;
  } else {
    log_stdout("1705 About to connect for stderr\n");
    sock2 = socket(AF_INET, SOCK_STREAM, 0);
    if (sock2 == -1) {
      log_stdout("1707 Error creating stderr socket %s\n", strerror(errno));
    }
    if (connect(sock2, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) {
      perror("1707 Error connecting to stderr socket.");
      exit(1);
    }
    log_stdout("1706 Connected stderr\n");
    if (getsockname(sock2, &sockname, &namelen) == 0) {
      struct sockaddr_in *sin2 = (struct sockaddr_in *)&sockname;
      log_stdout("1708 Local stderr port is %d\n", sin2->sin_port);
    } else {
      perror("1709 Cannot get local stderr socket name, ignoring error.");
    }
  }
  fflush(stdout);
  fflush(stderr);
  int rc0 = dup2(sock, 0);
  int rc1 = dup2(sock, 1);
  int rc2 = dup2(sock2, 2);
  if (numstreams == 2) {
    log_stdout("1500 Stream: STDOUT\n");
    log_stderr("1500 Stream: STDERR\n");
  }
}


x86 assembly (clang, -O0):
<redirect_to_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1090,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x1,-0x1034(%rbp)
mov    -0x8(%rbp),%rdi
mov    $0x3a,%esi
call   1060 <strchr@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1262 <redirect_to_socket+0x52>
mov    -0x8(%rbp),%rsi
lea    0xdaf(%rip),%rdi
mov    $0x0,%al
call   1030 <log_stderr@plt>
mov    $0x1,%edi
call   1100 <exit@plt>
mov    -0x30(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x30(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x28(%rbp),%rdi
mov    $0x3f,%esi
call   1060 <strchr@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     12d5 <redirect_to_socket+0xc5>
mov    -0x30(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x30(%rbp)
movb   $0x0,(%rax)
mov    -0x30(%rbp),%rdi
lea    0xd70(%rip),%rsi
call   1080 <strcmp@plt>
cmp    $0x0,%rax
jne    12d0 <redirect_to_socket+0xc0>
movl   $0x2,-0x1034(%rbp)
jmp    12d5 <redirect_to_socket+0xc5>
mov    -0x20(%rbp),%rsi
mov    -0x28(%rbp),%rdx
mov    -0x1034(%rbp),%ecx
lea    0xd4a(%rip),%rdi
mov    $0x0,%al
call   10d0 <log_stdout@plt>
movq   $0x0,-0x1040(%rbp)
mov    -0x28(%rbp),%rdi
lea    -0x1040(%rbp),%rsi
mov    $0xa,%edx
call   1090 <strtol@plt>
mov    %rax,-0x1048(%rbp)
mov    -0x1040(%rbp),%rax
cmpb   $0x0,(%rax)
je     1344 <redirect_to_socket+0x134>
mov    -0x28(%rbp),%rsi
lea    0xd25(%rip),%rdi
mov    $0x0,%al
call   1030 <log_stderr@plt>
mov    $0x1,%edi
call   1100 <exit@plt>
mov    -0x1048(%rbp),%rax
mov    %eax,-0x14(%rbp)
mov    -0x20(%rbp),%rdi
lea    -0x1050(%rbp),%rsi
call   10a0 <inet_aton@plt>
cmp    $0x0,%eax
jne    1383 <redirect_to_socket+0x173>
mov    -0x20(%rbp),%rsi
lea    0xd05(%rip),%rdi
mov    $0x0,%al
call   1030 <log_stderr@plt>
mov    $0x1,%edi
call   1100 <exit@plt>
mov    -0x1050(%rbp),%esi
lea    0xd07(%rip),%rdi
mov    $0x0,%al
call   10d0 <log_stdout@plt>
lea    -0x1060(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1070 <memset@plt>
mov    0x2c37(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1058(%rbp)
lea    -0x1050(%rbp),%rdi
lea    -0x1060(%rbp),%rsi
add    $0x4,%rsi
mov    $0x4,%edx
call   10c0 <bcopy@plt>
mov    -0x14(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,-0x1060(%rbp)
lea    0xcbc(%rip),%rdi
mov    $0x0,%al
call   10d0 <log_stdout@plt>
mov    0x2bf0(%rip),%rax
mov    (%rax),%edi
mov    0x2bcf(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1130 <socket@plt>
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x1060(%rbp),%rsi
mov    $0xc,%edx
call   1110 <connect@plt>
cmp    $0x0,%rax
jge    1441 <redirect_to_socket+0x231>
lea    0xc8b(%rip),%rdi
call   10e0 <perror@plt>
mov    $0x1,%edi
call   1100 <exit@plt>
lea    0xc9e(%rip),%rdi
mov    $0x0,%al
call   10d0 <log_stdout@plt>
movl   $0x4,-0x106c(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x1068(%rbp),%rsi
lea    -0x106c(%rbp),%rdx
call   10f0 <getsockname@plt>
cmp    $0x0,%rax
jne    14a3 <redirect_to_socket+0x293>
lea    -0x1068(%rbp),%rax
mov    %rax,-0x1078(%rbp)
mov    -0x1078(%rbp),%rax
mov    (%rax),%esi
lea    0xc5f(%rip),%rdi
mov    $0x0,%al
call   10d0 <log_stdout@plt>
jmp    14af <redirect_to_socket+0x29f>
lea    0xc63(%rip),%rdi
call   10e0 <perror@plt>
cmpl   $0x1,-0x1034(%rbp)
jne    14c7 <redirect_to_socket+0x2b7>
mov    -0xc(%rbp),%eax
mov    %eax,-0x10(%rbp)
jmp    15b6 <redirect_to_socket+0x3a6>
lea    0xc72(%rip),%rdi
mov    $0x0,%al
call   10d0 <log_stdout@plt>
mov    0x2b0c(%rip),%rax
mov    (%rax),%edi
mov    0x2aeb(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1130 <socket@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0xffffffff,-0x10(%rbp)
jne    1519 <redirect_to_socket+0x309>
mov    0x2ac6(%rip),%rax
mov    (%rax),%edi
call   1120 <strerror@plt>
mov    %eax,%esi
lea    0xc50(%rip),%rdi
mov    $0x0,%al
call   10d0 <log_stdout@plt>
mov    -0x10(%rbp),%edi
lea    -0x1060(%rbp),%rsi
mov    $0xc,%edx
call   1110 <connect@plt>
cmp    $0x0,%rax
jge    154d <redirect_to_socket+0x33d>
lea    0xc4a(%rip),%rdi
call   10e0 <perror@plt>
mov    $0x1,%edi
call   1100 <exit@plt>
lea    0xc5c(%rip),%rdi
mov    $0x0,%al
call   10d0 <log_stdout@plt>
mov    -0x10(%rbp),%edi
lea    -0x1068(%rbp),%rsi
lea    -0x106c(%rbp),%rdx
call   10f0 <getsockname@plt>
cmp    $0x0,%rax
jne    15a5 <redirect_to_socket+0x395>
lea    -0x1068(%rbp),%rax
mov    %rax,-0x1080(%rbp)
mov    -0x1080(%rbp),%rax
mov    (%rax),%esi
lea    0xc2e(%rip),%rdi
mov    $0x0,%al
call   10d0 <log_stdout@plt>
jmp    15b1 <redirect_to_socket+0x3a1>
lea    0xc39(%rip),%rdi
call   10e0 <perror@plt>
jmp    15b6 <redirect_to_socket+0x3a6>
mov    0x2a03(%rip),%rax
mov    (%rax),%edi
call   10b0 <fflush@plt>
mov    0x2a0d(%rip),%rax
mov    (%rax),%edi
call   10b0 <fflush@plt>
mov    -0xc(%rbp),%edi
xor    %esi,%esi
call   1050 <dup2@plt>
mov    %eax,-0x1084(%rbp)
mov    -0xc(%rbp),%edi
mov    $0x1,%esi
call   1050 <dup2@plt>
mov    %eax,-0x1088(%rbp)
mov    -0x10(%rbp),%edi
mov    $0x2,%esi
call   1050 <dup2@plt>
mov    %eax,-0x108c(%rbp)
cmpl   $0x2,-0x1034(%rbp)
jne    1631 <redirect_to_socket+0x421>
lea    0xc03(%rip),%rdi
mov    $0x0,%al
call   10d0 <log_stdout@plt>
lea    0xc0a(%rip),%rdi
mov    $0x0,%al
call   1030 <log_stderr@plt>
add    $0x1090,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<redirect_to_socket>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rdi,%r14
mov    $0x3a,%esi
call   1060 <strchr@plt>
test   %rax,%rax
je     14b9 <redirect_to_socket+0x2b9>
mov    %rax,%rbp
movb   $0x0,(%rax)
add    $0x1,%rbp
mov    %rbp,%rdi
mov    $0x3f,%esi
call   1060 <strchr@plt>
mov    $0x1,%ebx
mov    $0x1,%r12b
test   %rax,%rax
je     1263 <redirect_to_socket+0x63>
mov    %rax,%rdi
add    $0x1,%rdi
movb   $0x0,(%rax)
lea    0xdce(%rip),%rsi
call   1080 <strcmp@plt>
test   %rax,%rax
je     1268 <redirect_to_socket+0x68>
xor    %r15d,%r15d
jmp    1273 <redirect_to_socket+0x73>
mov    $0x2,%ebx
mov    $0x1,%r15b
xor    %r12d,%r12d
lea    0xdba(%rip),%rdi
mov    %r14,%rsi
mov    %rbp,%rdx
mov    %ebx,%ecx
xor    %eax,%eax
call   10c0 <log_stdout@plt>
movq   $0x0,0x20(%rsp)
lea    0x20(%rsp),%rsi
mov    %rbp,%rdi
mov    $0xa,%edx
call   1090 <strtol@plt>
mov    %rax,%rbx
mov    0x20(%rsp),%rax
cmpb   $0x0,(%rax)
jne    14c2 <redirect_to_socket+0x2c2>
lea    0x8(%rsp),%rsi
mov    %r14,%rdi
call   10a0 <inet_aton@plt>
test   %eax,%eax
je     14ce <redirect_to_socket+0x2ce>
mov    0x8(%rsp),%esi
lea    0xdc2(%rip),%rdi
xor    %eax,%eax
call   10c0 <log_stdout@plt>
lea    0x10(%rsp),%rbp
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1070 <memset@plt>
mov    0x2cf1(%rip),%r14
mov    (%r14),%eax
mov    %eax,0x18(%rsp)
mov    0x8(%rsp),%eax
mov    %eax,0x14(%rsp)
mov    %ebx,%edi
call   1040 <htons@plt>
mov    %eax,0x10(%rsp)
lea    0xd8e(%rip),%rdi
xor    %eax,%eax
call   10c0 <log_stdout@plt>
mov    (%r14),%edi
mov    0x2ca7(%rip),%r13
mov    0x0(%r13),%esi
xor    %edx,%edx
call   1120 <socket@plt>
mov    %eax,%ebx
mov    %eax,%edi
mov    %rbp,%rsi
mov    $0xc,%edx
call   1100 <connect@plt>
test   %rax,%rax
js     14e9 <redirect_to_socket+0x2e9>
lea    0xd91(%rip),%rdi
xor    %eax,%eax
call   10c0 <log_stdout@plt>
movl   $0x4,0xc(%rsp)
mov    %rsp,%rsi
lea    0xc(%rsp),%rdx
mov    %ebx,%edi
call   10e0 <getsockname@plt>
test   %rax,%rax
jne    1497 <redirect_to_socket+0x297>
mov    (%rsp),%esi
lea    0xd70(%rip),%rdi
xor    %eax,%eax
call   10c0 <log_stdout@plt>
mov    %ebx,%ebp
test   %r12b,%r12b
jne    142a <redirect_to_socket+0x22a>
lea    0xda1(%rip),%rdi
xor    %eax,%eax
call   10c0 <log_stdout@plt>
mov    (%r14),%edi
mov    0x0(%r13),%esi
xor    %edx,%edx
call   1120 <socket@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
jne    13d9 <redirect_to_socket+0x1d9>
mov    0x2c06(%rip),%rax
mov    (%rax),%edi
call   1110 <strerror@plt>
lea    0xd92(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   10c0 <log_stdout@plt>
lea    0x10(%rsp),%rsi
mov    %ebp,%edi
mov    $0xc,%edx
call   1100 <connect@plt>
test   %rax,%rax
js     14f2 <redirect_to_socket+0x2f2>
lea    0xdb6(%rip),%rdi
xor    %eax,%eax
call   10c0 <log_stdout@plt>
mov    %rsp,%rsi
lea    0xc(%rsp),%rdx
mov    %ebp,%edi
call   10e0 <getsockname@plt>
test   %rax,%rax
jne    14a8 <redirect_to_socket+0x2a8>
mov    (%rsp),%esi
lea    0xda4(%rip),%rdi
xor    %eax,%eax
call   10c0 <log_stdout@plt>
mov    0x2b8f(%rip),%rax
mov    (%rax),%edi
call   10b0 <fflush@plt>
mov    0x2b99(%rip),%rax
mov    (%rax),%edi
call   10b0 <fflush@plt>
mov    %ebx,%edi
xor    %esi,%esi
call   1050 <dup2@plt>
mov    %ebx,%edi
mov    $0x1,%esi
call   1050 <dup2@plt>
mov    %ebp,%edi
mov    $0x2,%esi
call   1050 <dup2@plt>
test   %r15b,%r15b
je     1488 <redirect_to_socket+0x288>
lea    0xdac(%rip),%rdi
xor    %eax,%eax
call   10c0 <log_stdout@plt>
lea    0xdb3(%rip),%rdi
xor    %eax,%eax
call   1030 <log_stderr@plt>
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xc6f(%rip),%rdi
call   10d0 <perror@plt>
jmp    138d <redirect_to_socket+0x18d>
lea    0xd36(%rip),%rdi
call   10d0 <perror@plt>
jmp    142a <redirect_to_socket+0x22a>
lea    0xb40(%rip),%rdi
jmp    14d5 <redirect_to_socket+0x2d5>
lea    0xb8f(%rip),%rdi
mov    %rbp,%rsi
jmp    14d8 <redirect_to_socket+0x2d8>
lea    0xba2(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1030 <log_stderr@plt>
mov    $0x1,%edi
call   10f0 <exit@plt>
lea    0xbcd(%rip),%rdi
jmp    14f9 <redirect_to_socket+0x2f9>
lea    0xc8f(%rip),%rdi
call   10d0 <perror@plt>
mov    $0x1,%edi
call   10f0 <exit@plt>

LLVM IR (-O0):
%struct.in_addr = type { i32 }
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [39 x i8] c"1700 invalid socket, missing port: %s\0A\00"
@.str.1 = [13 x i8] c"splitstreams\00"
@.str.2 = [36 x i8] c"1701 host[%s] port[%s] streams[%d]\0A\00"
@.str.3 = [31 x i8] c"1702 Port[%s] is not numeric.\0A\00"
@.str.4 = [32 x i8] c"1703 Can't parse IP address %s\0A\00"
@.str.5 = [15 x i8] c"1704 addr: %x\0A\00"
@AF_INET = global i32 0
@.str.6 = [23 x i8] c"1705 About to connect\0A\00"
@SOCK_STREAM = global i32 0
@.str.7 = [41 x i8] c"1707 Error connecting to console socket.\00"
@.str.8 = [16 x i8] c"1706 Connected\0A\00"
@.str.9 = [23 x i8] c"1708 Local port is %d\0A\00"
@.str.10 = [51 x i8] c"1709 Cannot get local socket name, ignoring error.\00"
@.str.11 = [34 x i8] c"1705 About to connect for stderr\0A\00"
@.str.12 = [38 x i8] c"1707 Error creating stderr socket %s\0A\00"
@errno = global i32 0
@.str.13 = [40 x i8] c"1707 Error connecting to stderr socket.\00"
@.str.14 = [23 x i8] c"1706 Connected stderr\0A\00"
@.str.15 = [30 x i8] c"1708 Local stderr port is %d\0A\00"
@.str.16 = [58 x i8] c"1709 Cannot get local stderr socket name, ignoring error.\00"
@stdout = global i32 0
@stderr = global i32 0
@.str.17 = [21 x i8] c"1500 Stream: STDOUT\0A\00"
@.str.18 = [21 x i8] c"1500 Stream: STDERR\0A\00"
define void @redirect_to_socket(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca [4096 x i8]
%10 = alloca i32
%11 = alloca i8*
%12 = alloca i64
%13 = alloca %struct.in_addr
%14 = alloca %struct.sockaddr_in
%15 = alloca %struct.sockaddr
%16 = alloca i32
%17 = alloca %struct.sockaddr_in*
%18 = alloca %struct.sockaddr_in*
%19 = alloca i32
%20 = alloca i32
%21 = alloca i32
store i8* %0, i8** %2
store i32 1, i32* %10
%22 = load i8*, i8** %2
%23 = call i8* @strchr(i8* %22, i8 signext 58)
store i8* %23, i8** %8
%24 = load i8*, i8** %8
%25 = icmp eq i8* %24, null
br i1 %25, label %26, label %30
26: ; preds = %1
%27 = load i8*, i8** %2
%28 = call i32 (i8*, ...) @log_stderr(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str, i64 0, i64 0), i8* %27)
%29 = call i32 @exit(i32 1)
unreachable
30: ; preds = %1
%31 = load i8*, i8** %8
store i8 0, i8* %31
%32 = load i8*, i8** %8
%33 = getelementptr inbounds i8, i8* %32, i64 1
store i8* %33, i8** %7
%34 = load i8*, i8** %2
store i8* %34, i8** %6
%35 = load i8*, i8** %7
%36 = call i8* @strchr(i8* %35, i8 signext 63)
store i8* %36, i8** %8
%37 = load i8*, i8** %8
%38 = icmp ne i8* %37, null
br i1 %38, label %39, label %47
39: ; preds = %30
%40 = load i8*, i8** %8
%41 = getelementptr inbounds i8, i8* %40, i32 1
store i8* %41, i8** %8
store i8 0, i8* %40
%42 = load i8*, i8** %8
%43 = call i64 @strcmp(i8* %42, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
%44 = icmp eq i64 %43, 0
br i1 %44, label %45, label %46
45: ; preds = %39
store i32 2, i32* %10
br label %46
46: ; preds = %45, %39
br label %47
47: ; preds = %46, %30
%48 = load i8*, i8** %6
%49 = load i8*, i8** %7
%50 = load i32, i32* %10
%51 = call i32 (i8*, ...) @log_stdout(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0), i8* %48, i8* %49, i32 %50)
store i8* null, i8** %11
%52 = load i8*, i8** %7
%53 = call i64 @strtol(i8* %52, i8** %11, i32 10)
store i64 %53, i64* %12
%54 = load i8*, i8** %11
%55 = load i8, i8* %54
%56 = icmp ne i8 %55, 0
br i1 %56, label %57, label %61
57: ; preds = %47
%58 = load i8*, i8** %7
%59 = call i32 (i8*, ...) @log_stderr(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0), i8* %58)
%60 = call i32 @exit(i32 1)
unreachable
61: ; preds = %47
%62 = load i64, i64* %12
%63 = trunc i64 %62 to i32
store i32 %63, i32* %5
%64 = load i8*, i8** %6
%65 = call i32 @inet_aton(i8* %64, %struct.in_addr* %13)
%66 = icmp ne i32 %65, 0
br i1 %66, label %71, label %67
67: ; preds = %61
%68 = load i8*, i8** %6
%69 = call i32 (i8*, ...) @log_stderr(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.4, i64 0, i64 0), i8* %68)
%70 = call i32 @exit(i32 1)
unreachable
71: ; preds = %61
%72 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %13, i32 0, i32 0
%73 = load i32, i32* %72
%74 = call i32 (i8*, ...) @log_stdout(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.5, i64 0, i64 0), i32 %73)
%75 = call i32 @memset(%struct.sockaddr_in* %14, i32 0, i32 12)
%76 = load i32, i32* @AF_INET
%77 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %14, i32 0, i32 2
store i32 %76, i32* %77
%78 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %13, i32 0, i32 0
%79 = bitcast i32* %78 to i8*
%80 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %14, i32 0, i32 1
%81 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %80, i32 0, i32 0
%82 = bitcast i32* %81 to i8*
%83 = call i32 @bcopy(i8* %79, i8* %82, i32 4)
%84 = load i32, i32* %5
%85 = call i32 @htons(i32 %84)
%86 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %14, i32 0, i32 0
store i32 %85, i32* %86
%87 = call i32 (i8*, ...) @log_stdout(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.6, i64 0, i64 0))
%88 = load i32, i32* @AF_INET
%89 = load i32, i32* @SOCK_STREAM
%90 = call i32 @socket(i32 %88, i32 %89, i32 0)
store i32 %90, i32* %3
%91 = load i32, i32* %3
%92 = bitcast %struct.sockaddr_in* %14 to %struct.sockaddr*
%93 = call i64 @connect(i32 %91, %struct.sockaddr* %92, i32 12)
%94 = icmp slt i64 %93, 0
br i1 %94, label %95, label %98
95: ; preds = %71
%96 = call i32 @perror(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.7, i64 0, i64 0))
%97 = call i32 @exit(i32 1)
unreachable
98: ; preds = %71
%99 = call i32 (i8*, ...) @log_stdout(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.8, i64 0, i64 0))
store i32 4, i32* %16
%100 = load i32, i32* %3
%101 = call i64 @getsockname(i32 %100, %struct.sockaddr* %15, i32* %16)
%102 = icmp eq i64 %101, 0
br i1 %102, label %103, label %109
103: ; preds = %98
%104 = bitcast %struct.sockaddr* %15 to %struct.sockaddr_in*
store %struct.sockaddr_in* %104, %struct.sockaddr_in** %17
%105 = load %struct.sockaddr_in*, %struct.sockaddr_in** %17
%106 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %105, i32 0, i32 0
%107 = load i32, i32* %106
%108 = call i32 (i8*, ...) @log_stdout(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.9, i64 0, i64 0), i32 %107)
br label %111
109: ; preds = %98
%110 = call i32 @perror(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.10, i64 0, i64 0))
br label %111
111: ; preds = %109, %103
%112 = load i32, i32* %10
%113 = icmp eq i32 %112, 1
br i1 %113, label %114, label %116
114: ; preds = %111
%115 = load i32, i32* %3
store i32 %115, i32* %4
br label %149
116: ; preds = %111
%117 = call i32 (i8*, ...) @log_stdout(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.11, i64 0, i64 0))
%118 = load i32, i32* @AF_INET
%119 = load i32, i32* @SOCK_STREAM
%120 = call i32 @socket(i32 %118, i32 %119, i32 0)
store i32 %120, i32* %4
%121 = load i32, i32* %4
%122 = icmp eq i32 %121, -1
br i1 %122, label %123, label %127
123: ; preds = %116
%124 = load i32, i32* @errno
%125 = call i32 @strerror(i32 %124)
%126 = call i32 (i8*, ...) @log_stdout(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.12, i64 0, i64 0), i32 %125)
br label %127
127: ; preds = %123, %116
%128 = load i32, i32* %4
%129 = bitcast %struct.sockaddr_in* %14 to %struct.sockaddr*
%130 = call i64 @connect(i32 %128, %struct.sockaddr* %129, i32 12)
%131 = icmp slt i64 %130, 0
br i1 %131, label %132, label %135
132: ; preds = %127
%133 = call i32 @perror(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.13, i64 0, i64 0))
%134 = call i32 @exit(i32 1)
unreachable
135: ; preds = %127
%136 = call i32 (i8*, ...) @log_stdout(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.14, i64 0, i64 0))
%137 = load i32, i32* %4
%138 = call i64 @getsockname(i32 %137, %struct.sockaddr* %15, i32* %16)
%139 = icmp eq i64 %138, 0
br i1 %139, label %140, label %146
140: ; preds = %135
%141 = bitcast %struct.sockaddr* %15 to %struct.sockaddr_in*
store %struct.sockaddr_in* %141, %struct.sockaddr_in** %18
%142 = load %struct.sockaddr_in*, %struct.sockaddr_in** %18
%143 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %142, i32 0, i32 0
%144 = load i32, i32* %143
%145 = call i32 (i8*, ...) @log_stdout(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.15, i64 0, i64 0), i32 %144)
br label %148
146: ; preds = %135
%147 = call i32 @perror(i8* getelementptr inbounds ([58 x i8], [58 x i8]* @.str.16, i64 0, i64 0))
br label %148
148: ; preds = %146, %140
br label %149
149: ; preds = %148, %114
%150 = load i32, i32* @stdout
%151 = call i32 @fflush(i32 %150)
%152 = load i32, i32* @stderr
%153 = call i32 @fflush(i32 %152)
%154 = load i32, i32* %3
%155 = call i32 @dup2(i32 %154, i32 0)
store i32 %155, i32* %19
%156 = load i32, i32* %3
%157 = call i32 @dup2(i32 %156, i32 1)
store i32 %157, i32* %20
%158 = load i32, i32* %4
%159 = call i32 @dup2(i32 %158, i32 2)
store i32 %159, i32* %21
%160 = load i32, i32* %10
%161 = icmp eq i32 %160, 2
br i1 %161, label %162, label %165
162: ; preds = %149
%163 = call i32 (i8*, ...) @log_stdout(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.17, i64 0, i64 0))
%164 = call i32 (i8*, ...) @log_stderr(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.18, i64 0, i64 0))
br label %165
165: ; preds = %162, %149
ret void
}

LLVM IR (-O2):
%struct.in_addr = type { i32 }
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [39 x i8] c"1700 invalid socket, missing port: %s\0A\00"
@.str.1 = [13 x i8] c"splitstreams\00"
@.str.2 = [36 x i8] c"1701 host[%s] port[%s] streams[%d]\0A\00"
@.str.3 = [31 x i8] c"1702 Port[%s] is not numeric.\0A\00"
@.str.4 = [32 x i8] c"1703 Can't parse IP address %s\0A\00"
@.str.5 = [15 x i8] c"1704 addr: %x\0A\00"
@AF_INET = global i32 0
@.str.6 = [23 x i8] c"1705 About to connect\0A\00"
@SOCK_STREAM = global i32 0
@.str.7 = [41 x i8] c"1707 Error connecting to console socket.\00"
@.str.8 = [16 x i8] c"1706 Connected\0A\00"
@.str.9 = [23 x i8] c"1708 Local port is %d\0A\00"
@.str.10 = [51 x i8] c"1709 Cannot get local socket name, ignoring error.\00"
@.str.11 = [34 x i8] c"1705 About to connect for stderr\0A\00"
@.str.12 = [38 x i8] c"1707 Error creating stderr socket %s\0A\00"
@errno = global i32 0
@.str.13 = [40 x i8] c"1707 Error connecting to stderr socket.\00"
@.str.14 = [23 x i8] c"1706 Connected stderr\0A\00"
@.str.15 = [30 x i8] c"1708 Local stderr port is %d\0A\00"
@.str.16 = [58 x i8] c"1709 Cannot get local stderr socket name, ignoring error.\00"
@stdout = global i32 0
@stderr = global i32 0
@.str.17 = [21 x i8] c"1500 Stream: STDOUT\0A\00"
@.str.18 = [21 x i8] c"1500 Stream: STDERR\0A\00"
define void @redirect_to_socket(i8* %0) {
%2 = alloca i8*
%3 = alloca %struct.in_addr
%4 = alloca %struct.sockaddr_in
%5 = alloca %struct.sockaddr
%6 = alloca i32
%7 = tail call i8* @strchr(i8* dereferenceable(1) %0, i8 signext 58)
%8 = icmp eq i8* %7, null
br i1 %8, label %9, label %12
9: ; preds = %1
%10 = tail call i32 (i8*, ...) @log_stderr(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str, i64 0, i64 0), i8* %0)
%11 = tail call i32 @exit(i32 1)
unreachable
12: ; preds = %1
store i8 0, i8* %7
%13 = getelementptr inbounds i8, i8* %7, i64 1
%14 = tail call i8* @strchr(i8* %13, i8 signext 63)
%15 = icmp eq i8* %14, null
br i1 %15, label %21, label %16
16: ; preds = %12
%17 = getelementptr inbounds i8, i8* %14, i64 1
store i8 0, i8* %14
%18 = tail call i64 @strcmp(i8* %17, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
%19 = icmp eq i64 %18, 0
br i1 %19, label %20, label %21
20: ; preds = %16
br label %21
21: ; preds = %16, %20, %12
%22 = phi i1 [ true, %16 ], [ false, %20 ], [ true, %12 ]
%23 = phi i1 [ false, %16 ], [ true, %20 ], [ false, %12 ]
%24 = phi i32 [ 1, %16 ], [ 2, %20 ], [ 1, %12 ]
%25 = tail call i32 (i8*, ...) @log_stdout(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0), i8* %0, i8* %13, i32 %24)
%26 = bitcast i8** %2 to i8*
store i8* null, i8** %2
%27 = call i64 @strtol(i8* %13, i8** %2, i32 10)
%28 = load i8*, i8** %2
%29 = load i8, i8* %28
%30 = icmp eq i8 %29, 0
br i1 %30, label %34, label %31
31: ; preds = %21
%32 = tail call i32 (i8*, ...) @log_stderr(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0), i8* %13)
%33 = tail call i32 @exit(i32 1)
unreachable
34: ; preds = %21
%35 = bitcast %struct.in_addr* %3 to i8*
%36 = call i32 @inet_aton(i8* %0, %struct.in_addr* %3)
%37 = icmp eq i32 %36, 0
br i1 %37, label %38, label %41
38: ; preds = %34
%39 = call i32 (i8*, ...) @log_stderr(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.4, i64 0, i64 0), i8* %0)
%40 = call i32 @exit(i32 1)
unreachable
41: ; preds = %34
%42 = trunc i64 %27 to i32
%43 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %3, i64 0, i32 0
%44 = load i32, i32* %43
%45 = call i32 (i8*, ...) @log_stdout(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.5, i64 0, i64 0), i32 %44)
%46 = bitcast %struct.sockaddr_in* %4 to i8*
%47 = call i32 @memset(%struct.sockaddr_in* %4, i32 0, i32 12)
%48 = load i32, i32* @AF_INET
%49 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 2
store i32 %48, i32* %49
%50 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 1, i32 0
%51 = load i32, i32* %43
store i32 %51, i32* %50
%52 = call i32 @htons(i32 %42)
%53 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0
store i32 %52, i32* %53
%54 = call i32 (i8*, ...) @log_stdout(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.6, i64 0, i64 0))
%55 = load i32, i32* @AF_INET
%56 = load i32, i32* @SOCK_STREAM
%57 = call i32 @socket(i32 %55, i32 %56, i32 0)
%58 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%59 = call i64 @connect(i32 %57, %struct.sockaddr* %58, i32 12)
%60 = icmp slt i64 %59, 0
br i1 %60, label %61, label %64
61: ; preds = %41
%62 = call i32 @perror(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.7, i64 0, i64 0))
%63 = call i32 @exit(i32 1)
unreachable
64: ; preds = %41
%65 = call i32 (i8*, ...) @log_stdout(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.8, i64 0, i64 0))
%66 = bitcast %struct.sockaddr* %5 to i8*
%67 = bitcast i32* %6 to i8*
store i32 4, i32* %6
%68 = call i64 @getsockname(i32 %57, %struct.sockaddr* %5, i32* %6)
%69 = icmp eq i64 %68, 0
br i1 %69, label %70, label %74
70: ; preds = %64
%71 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %5, i64 0, i32 0
%72 = load i32, i32* %71
%73 = call i32 (i8*, ...) @log_stdout(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.9, i64 0, i64 0), i32 %72)
br label %76
74: ; preds = %64
%75 = call i32 @perror(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.10, i64 0, i64 0))
br label %76
76: ; preds = %74, %70
br i1 %22, label %103, label %77
77: ; preds = %76
%78 = call i32 (i8*, ...) @log_stdout(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.11, i64 0, i64 0))
%79 = load i32, i32* @AF_INET
%80 = load i32, i32* @SOCK_STREAM
%81 = call i32 @socket(i32 %79, i32 %80, i32 0)
%82 = icmp eq i32 %81, -1
br i1 %82, label %83, label %87
83: ; preds = %77
%84 = load i32, i32* @errno
%85 = call i32 @strerror(i32 %84)
%86 = call i32 (i8*, ...) @log_stdout(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.12, i64 0, i64 0), i32 %85)
br label %87
87: ; preds = %83, %77
%88 = call i64 @connect(i32 %81, %struct.sockaddr* %58, i32 12)
%89 = icmp slt i64 %88, 0
br i1 %89, label %90, label %93
90: ; preds = %87
%91 = call i32 @perror(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.13, i64 0, i64 0))
%92 = call i32 @exit(i32 1)
unreachable
93: ; preds = %87
%94 = call i32 (i8*, ...) @log_stdout(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.14, i64 0, i64 0))
%95 = call i64 @getsockname(i32 %81, %struct.sockaddr* %5, i32* %6)
%96 = icmp eq i64 %95, 0
br i1 %96, label %97, label %101
97: ; preds = %93
%98 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %5, i64 0, i32 0
%99 = load i32, i32* %98
%100 = call i32 (i8*, ...) @log_stdout(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.15, i64 0, i64 0), i32 %99)
br label %103
101: ; preds = %93
%102 = call i32 @perror(i8* getelementptr inbounds ([58 x i8], [58 x i8]* @.str.16, i64 0, i64 0))
br label %103
103: ; preds = %76, %97, %101
%104 = phi i32 [ %81, %97 ], [ %81, %101 ], [ %57, %76 ]
%105 = load i32, i32* @stdout
%106 = call i32 @fflush(i32 %105)
%107 = load i32, i32* @stderr
%108 = call i32 @fflush(i32 %107)
%109 = call i32 @dup2(i32 %57, i32 0)
%110 = call i32 @dup2(i32 %57, i32 1)
%111 = call i32 @dup2(i32 %104, i32 2)
br i1 %23, label %112, label %115
112: ; preds = %103
%113 = call i32 (i8*, ...) @log_stdout(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.17, i64 0, i64 0))
%114 = call i32 (i8*, ...) @log_stderr(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.18, i64 0, i64 0))
br label %115
115: ; preds = %112, %103
ret void
}