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

/* Variables and functions */
int /*<<< orphan*/ AI_PASSIVE;
int EAI_SYSTEM;
int /*<<< orphan*/ LOG_DEBUG;
int /*<<< orphan*/ LOG_ERR;
int /*<<< orphan*/ LOG_WARNING;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEADDR;
scalar_t__ bind(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ free(int *);
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
int /*<<< orphan*/ gai_strerror(int);
int getaddrinfo(char *, char const *, struct addrinfo *, struct addrinfo **);
int *malloc(int);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
scalar_t__ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, int const *,
                      int);
int socket(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
int /*<<< orphan*/ syslog(int /*<<< orphan*/, char *, ...);
int *socksetup(int af, char *bindname, const char *bindport) {
  struct addrinfo hints, *res, *r;
  int error, maxs, *s, *socks;
  const int on = 1;
  memset(&hints, 0, sizeof(hints));
  hints.ai_flags = AI_PASSIVE;
  hints.ai_family = af;
  hints.ai_socktype = SOCK_STREAM;
  error = getaddrinfo(bindname, bindport, &hints, &res);
  if (error) {
    syslog(LOG_ERR, "%s", gai_strerror(error));
    if (error == EAI_SYSTEM)
      syslog(LOG_ERR, "%s", strerror(errno));
    return NULL;
  }
  for (maxs = 0, r = res; r; r = r->ai_next, maxs++)
    ;
  socks = malloc((maxs + 1) * sizeof(int));
  if (!socks) {
    freeaddrinfo(res);
    syslog(LOG_ERR, "couldn't allocate memory for sockets");
    return NULL;
  }
  *socks = 0;
  s = socks + 1;
  for (r = res; r; r = r->ai_next) {
    *s = socket(r->ai_family, r->ai_socktype, r->ai_protocol);
    if (*s < 0) {
      syslog(LOG_DEBUG, "control socket: %m");
      continue;
    }
    if (setsockopt(*s, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0)
      syslog(LOG_WARNING, "control setsockopt (SO_REUSEADDR): %m");
    if (bind(*s, r->ai_addr, r->ai_addrlen) < 0) {
      syslog(LOG_DEBUG, "control bind: %m");
      close(*s);
      continue;
    }
    (*socks)++;
    s++;
  }
  if (res)
    freeaddrinfo(res);
  if (*socks == 0) {
    syslog(LOG_ERR, "control socket: Couldn't bind to any socket");
    free(socks);
    return NULL;
  }
  return (socks);
}


x86 assembly (clang, -O0):
<socksetup>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %edi,-0xc(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movl   $0x1,-0x74(%rbp)
lea    -0x48(%rbp),%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1060 <memset@plt>
mov    0x2dec(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x48(%rbp)
mov    0x2dba(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x38(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
lea    -0x48(%rbp),%rdx
lea    -0x50(%rbp),%rcx
call   10b0 <getaddrinfo@plt>
mov    %eax,-0x5c(%rbp)
cmpl   $0x0,-0x5c(%rbp)
je     12a0 <socksetup+0xe0>
mov    0x2d64(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x78(%rbp)
mov    -0x5c(%rbp),%edi
call   1050 <gai_strerror@plt>
mov    -0x78(%rbp),%edi
mov    %eax,%edx
lea    0xdb3(%rip),%rsi
mov    $0x0,%al
call   1090 <syslog@plt>
mov    -0x5c(%rbp),%eax
mov    0x2d7a(%rip),%rcx
cmp    (%rcx),%eax
jne    1293 <socksetup+0xd3>
mov    0x2d2b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x7c(%rbp)
mov    0x2d3f(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
mov    -0x7c(%rbp),%edi
mov    %eax,%edx
lea    0xd74(%rip),%rsi
mov    $0x0,%al
call   1090 <syslog@plt>
movq   $0x0,-0x8(%rbp)
jmp    14ab <socksetup+0x2eb>
movl   $0x0,-0x60(%rbp)
mov    -0x50(%rbp),%rax
mov    %rax,-0x58(%rbp)
cmpq   $0x0,-0x58(%rbp)
je     12d9 <socksetup+0x119>
jmp    12bf <socksetup+0xff>
mov    -0x58(%rbp),%rax
mov    0x18(%rax),%rax
mov    %rax,-0x58(%rbp)
mov    -0x60(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x60(%rbp)
jmp    12af <socksetup+0xef>
mov    -0x60(%rbp),%eax
add    $0x1,%eax
cltq
shl    $0x2,%rax
mov    %eax,%edi
call   1080 <malloc@plt>
mov    %rax,-0x70(%rbp)
cmpq   $0x0,-0x70(%rbp)
jne    1328 <socksetup+0x168>
mov    -0x50(%rbp),%rdi
call   10d0 <freeaddrinfo@plt>
mov    0x2c8d(%rip),%rax
mov    (%rax),%edi
lea    0xcef(%rip),%rsi
mov    $0x0,%al
call   1090 <syslog@plt>
movq   $0x0,-0x8(%rbp)
jmp    14ab <socksetup+0x2eb>
mov    -0x70(%rbp),%rax
movl   $0x0,(%rax)
mov    -0x70(%rbp),%rax
add    $0x4,%rax
mov    %rax,-0x68(%rbp)
mov    -0x50(%rbp),%rax
mov    %rax,-0x58(%rbp)
cmpq   $0x0,-0x58(%rbp)
je     1455 <socksetup+0x295>
mov    -0x58(%rbp),%rax
mov    (%rax),%edi
mov    -0x58(%rbp),%rax
mov    0x10(%rax),%esi
mov    -0x58(%rbp),%rax
mov    0xc(%rax),%edx
call   10e0 <socket@plt>
mov    %eax,%ecx
mov    -0x68(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x68(%rbp),%rax
cmpl   $0x0,(%rax)
jge    139b <socksetup+0x1db>
mov    0x2c22(%rip),%rax
mov    (%rax),%edi
lea    0xc99(%rip),%rsi
mov    $0x0,%al
call   1090 <syslog@plt>
jmp    1444 <socksetup+0x284>
mov    -0x68(%rbp),%rax
mov    (%rax),%edi
mov    0x2c40(%rip),%rax
mov    (%rax),%esi
mov    0x2bff(%rip),%rax
mov    (%rax),%edx
lea    -0x74(%rbp),%rcx
mov    $0x4,%r8d
call   1040 <setsockopt@plt>
cmp    $0x0,%rax
jge    13e3 <socksetup+0x223>
mov    0x2bfd(%rip),%rax
mov    (%rax),%edi
lea    0xc5f(%rip),%rsi
mov    $0x0,%al
call   1090 <syslog@plt>
mov    -0x68(%rbp),%rax
mov    (%rax),%edi
mov    -0x58(%rbp),%rax
mov    0x8(%rax),%esi
mov    -0x58(%rbp),%rax
mov    0x4(%rax),%edx
call   10a0 <bind@plt>
cmp    $0x0,%rax
jge    142d <socksetup+0x26d>
mov    0x2b9b(%rip),%rax
mov    (%rax),%edi
lea    0xc4b(%rip),%rsi
mov    $0x0,%al
call   1090 <syslog@plt>
mov    -0x68(%rbp),%rax
mov    (%rax),%edi
call   1070 <close@plt>
jmp    1444 <socksetup+0x284>
mov    -0x70(%rbp),%rax
mov    (%rax),%ecx
add    $0x1,%ecx
mov    %ecx,(%rax)
mov    -0x68(%rbp),%rax
add    $0x4,%rax
mov    %rax,-0x68(%rbp)
mov    -0x58(%rbp),%rax
mov    0x18(%rax),%rax
mov    %rax,-0x58(%rbp)
jmp    1346 <socksetup+0x186>
cmpq   $0x0,-0x50(%rbp)
je     1469 <socksetup+0x2a9>
mov    -0x50(%rbp),%rdi
call   10d0 <freeaddrinfo@plt>
mov    -0x70(%rbp),%rax
cmpl   $0x0,(%rax)
jne    14a3 <socksetup+0x2e3>
mov    0x2b1b(%rip),%rax
mov    (%rax),%edi
lea    0xbec(%rip),%rsi
mov    $0x0,%al
call   1090 <syslog@plt>
mov    -0x70(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    14ab <socksetup+0x2eb>
mov    -0x70(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<socksetup>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    %rdx,%r14
mov    %rsi,%rbp
mov    %edi,%ebx
movl   $0x1,0x4(%rsp)
lea    0x10(%rsp),%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1060 <memset@plt>
mov    0x2de7(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x30(%rsp)
mov    %ebx,0x10(%rsp)
mov    0x2db6(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x20(%rsp)
lea    0x8(%rsp),%rcx
mov    %rbp,%rdi
mov    %r14,%rsi
mov    %r15,%rdx
call   10b0 <getaddrinfo@plt>
test   %eax,%eax
je     1289 <socksetup+0xc9>
mov    %eax,%ebx
mov    0x2d68(%rip),%r14
mov    (%r14),%ebp
mov    %eax,%edi
call   1050 <gai_strerror@plt>
lea    0xdbf(%rip),%rsi
xor    %r13d,%r13d
mov    %ebp,%edi
mov    %eax,%edx
xor    %eax,%eax
call   1090 <syslog@plt>
mov    0x2d82(%rip),%rax
cmp    (%rax),%ebx
jne    1415 <socksetup+0x255>
mov    (%r14),%ebx
mov    0x2d50(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
lea    0xd8a(%rip),%rsi
xor    %r13d,%r13d
mov    %ebx,%edi
mov    %eax,%edx
xor    %eax,%eax
call   1090 <syslog@plt>
jmp    1415 <socksetup+0x255>
mov    0x8(%rsp),%rbx
mov    $0x4,%edi
test   %rbx,%rbx
je     13a4 <socksetup+0x1e4>
mov    %rbx,%rax
nop
mov    0x18(%rax),%rax
add    $0x4,%edi
test   %rax,%rax
jne    12a0 <socksetup+0xe0>
call   1080 <malloc@plt>
test   %rax,%rax
je     13b9 <socksetup+0x1f9>
mov    %rax,%r13
movl   $0x0,(%rax)
test   %rbx,%rbx
je     13f3 <socksetup+0x233>
mov    %r13,%r14
add    $0x4,%r14
mov    0x2d0e(%rip),%r15
mov    0x2ccf(%rip),%rbp
lea    0x4(%rsp),%r12
jmp    1314 <socksetup+0x154>
nopl   0x0(%rax,%rax,1)
mov    0x2cb1(%rip),%rax
mov    (%rax),%edi
lea    0xd28(%rip),%rsi
xor    %eax,%eax
call   1090 <syslog@plt>
mov    0x18(%rbx),%rbx
test   %rbx,%rbx
je     13dd <socksetup+0x21d>
mov    0x10(%rbx),%esi
mov    (%rbx),%edi
mov    0xc(%rbx),%edx
call   10e0 <socket@plt>
mov    %eax,(%r14)
test   %eax,%eax
js     12f0 <socksetup+0x130>
mov    (%r15),%esi
mov    0x0(%rbp),%edx
mov    %eax,%edi
mov    %r12,%rcx
mov    $0x4,%r8d
call   1040 <setsockopt@plt>
test   %rax,%rax
jns    135a <socksetup+0x19a>
mov    0x2c86(%rip),%rax
mov    (%rax),%edi
lea    0xce8(%rip),%rsi
xor    %eax,%eax
call   1090 <syslog@plt>
mov    (%r14),%edi
mov    0x4(%rbx),%edx
mov    0x8(%rbx),%esi
call   10a0 <bind@plt>
test   %rax,%rax
js     1380 <socksetup+0x1c0>
addl   $0x1,0x0(%r13)
add    $0x4,%r14
jmp    1307 <socksetup+0x147>
nopl   0x0(%rax,%rax,1)
mov    0x2c21(%rip),%rax
mov    (%rax),%edi
lea    0xcd1(%rip),%rsi
xor    %eax,%eax
call   1090 <syslog@plt>
mov    (%r14),%edi
call   1070 <close@plt>
jmp    1307 <socksetup+0x147>
call   1080 <malloc@plt>
test   %rax,%rax
je     13b9 <socksetup+0x1f9>
mov    %rax,%r13
movl   $0x0,(%rax)
jmp    13f3 <socksetup+0x233>
mov    %rbx,%rdi
call   10d0 <freeaddrinfo@plt>
mov    0x2bd0(%rip),%rax
mov    (%rax),%edi
lea    0xc32(%rip),%rsi
xor    %r13d,%r13d
xor    %eax,%eax
call   1090 <syslog@plt>
jmp    1415 <socksetup+0x255>
mov    0x8(%rsp),%rdi
test   %rdi,%rdi
je     13ec <socksetup+0x22c>
call   10d0 <freeaddrinfo@plt>
cmpl   $0x0,0x0(%r13)
jne    1415 <socksetup+0x255>
mov    0x2b9e(%rip),%rax
mov    (%rax),%edi
lea    0xc6f(%rip),%rsi
xor    %eax,%eax
call   1090 <syslog@plt>
mov    %r13,%rdi
call   1030 <free@plt>
xor    %r13d,%r13d
mov    %r13,%rax
add    $0x38,%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*, i32 }
@AI_PASSIVE = global i32 0
@SOCK_STREAM = global i32 0
@LOG_ERR = global i32 0
@.str = [3 x i8] c"%s\00"
@EAI_SYSTEM = global i32 0
@errno = global i32 0
@.str.1 = [37 x i8] c"couldn't allocate memory for sockets\00"
@LOG_DEBUG = global i32 0
@.str.2 = [19 x i8] c"control socket: %m\00"
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@LOG_WARNING = global i32 0
@.str.3 = [38 x i8] c"control setsockopt (SO_REUSEADDR): %m\00"
@.str.4 = [17 x i8] c"control bind: %m\00"
@.str.5 = [44 x i8] c"control socket: Couldn't bind to any socket\00"
define i32* @socksetup(i32 %0, i8* %1, i8* %2) {
%4 = alloca i32*
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca %struct.addrinfo
%9 = alloca %struct.addrinfo*
%10 = alloca %struct.addrinfo*
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32*
%14 = alloca i32*
%15 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i8* %2, i8** %7
store i32 1, i32* %15
%16 = call i32 @memset(%struct.addrinfo* %8, i32 0, i32 40)
%17 = load i32, i32* @AI_PASSIVE
%18 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %8, i32 0, i32 6
store i32 %17, i32* %18
%19 = load i32, i32* %5
%20 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %8, i32 0, i32 0
store i32 %19, i32* %20
%21 = load i32, i32* @SOCK_STREAM
%22 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %8, i32 0, i32 4
store i32 %21, i32* %22
%23 = load i8*, i8** %6
%24 = load i8*, i8** %7
%25 = call i32 @getaddrinfo(i8* %23, i8* %24, %struct.addrinfo* %8, %struct.addrinfo** %9)
store i32 %25, i32* %11
%26 = load i32, i32* %11
%27 = icmp ne i32 %26, 0
br i1 %27, label %28, label %42
28: ; preds = %3
%29 = load i32, i32* @LOG_ERR
%30 = load i32, i32* %11
%31 = call i32 @gai_strerror(i32 %30)
%32 = call i32 (i32, i8*, ...) @syslog(i32 %29, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %31)
%33 = load i32, i32* %11
%34 = load i32, i32* @EAI_SYSTEM
%35 = icmp eq i32 %33, %34
br i1 %35, label %36, label %41
36: ; preds = %28
%37 = load i32, i32* @LOG_ERR
%38 = load i32, i32* @errno
%39 = call i32 @strerror(i32 %38)
%40 = call i32 (i32, i8*, ...) @syslog(i32 %37, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %39)
br label %41
41: ; preds = %36, %28
store i32* null, i32** %4
br label %148
42: ; preds = %3
store i32 0, i32* %12
%43 = load %struct.addrinfo*, %struct.addrinfo** %9
store %struct.addrinfo* %43, %struct.addrinfo** %10
br label %44
44: ; preds = %48, %42
%45 = load %struct.addrinfo*, %struct.addrinfo** %10
%46 = icmp ne %struct.addrinfo* %45, null
br i1 %46, label %47, label %54
47: ; preds = %44
br label %48
48: ; preds = %47
%49 = load %struct.addrinfo*, %struct.addrinfo** %10
%50 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %49, i32 0, i32 5
%51 = load %struct.addrinfo*, %struct.addrinfo** %50
store %struct.addrinfo* %51, %struct.addrinfo** %10
%52 = load i32, i32* %12
%53 = add nsw i32 %52, 1
store i32 %53, i32* %12
br label %44
54: ; preds = %44
%55 = load i32, i32* %12
%56 = add nsw i32 %55, 1
%57 = sext i32 %56 to i64
%58 = mul i64 %57, 4
%59 = trunc i64 %58 to i32
%60 = call i32* @malloc(i32 %59)
store i32* %60, i32** %14
%61 = load i32*, i32** %14
%62 = icmp ne i32* %61, null
br i1 %62, label %68, label %63
63: ; preds = %54
%64 = load %struct.addrinfo*, %struct.addrinfo** %9
%65 = call i32 @freeaddrinfo(%struct.addrinfo* %64)
%66 = load i32, i32* @LOG_ERR
%67 = call i32 (i32, i8*, ...) @syslog(i32 %66, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0))
store i32* null, i32** %4
br label %148
68: ; preds = %54
%69 = load i32*, i32** %14
store i32 0, i32* %69
%70 = load i32*, i32** %14
%71 = getelementptr inbounds i32, i32* %70, i64 1
store i32* %71, i32** %13
%72 = load %struct.addrinfo*, %struct.addrinfo** %9
store %struct.addrinfo* %72, %struct.addrinfo** %10
br label %73
73: ; preds = %127, %68
%74 = load %struct.addrinfo*, %struct.addrinfo** %10
%75 = icmp ne %struct.addrinfo* %74, null
br i1 %75, label %76, label %131
76: ; preds = %73
%77 = load %struct.addrinfo*, %struct.addrinfo** %10
%78 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %77, i32 0, i32 0
%79 = load i32, i32* %78
%80 = load %struct.addrinfo*, %struct.addrinfo** %10
%81 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %80, i32 0, i32 4
%82 = load i32, i32* %81
%83 = load %struct.addrinfo*, %struct.addrinfo** %10
%84 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %83, i32 0, i32 3
%85 = load i32, i32* %84
%86 = call i32 @socket(i32 %79, i32 %82, i32 %85)
%87 = load i32*, i32** %13
store i32 %86, i32* %87
%88 = load i32*, i32** %13
%89 = load i32, i32* %88
%90 = icmp slt i32 %89, 0
br i1 %90, label %91, label %94
91: ; preds = %76
%92 = load i32, i32* @LOG_DEBUG
%93 = call i32 (i32, i8*, ...) @syslog(i32 %92, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0))
br label %127
94: ; preds = %76
%95 = load i32*, i32** %13
%96 = load i32, i32* %95
%97 = load i32, i32* @SOL_SOCKET
%98 = load i32, i32* @SO_REUSEADDR
%99 = call i64 @setsockopt(i32 %96, i32 %97, i32 %98, i32* %15, i32 4)
%100 = icmp slt i64 %99, 0
br i1 %100, label %101, label %104
101: ; preds = %94
%102 = load i32, i32* @LOG_WARNING
%103 = call i32 (i32, i8*, ...) @syslog(i32 %102, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.3, i64 0, i64 0))
br label %104
104: ; preds = %101, %94
%105 = load i32*, i32** %13
%106 = load i32, i32* %105
%107 = load %struct.addrinfo*, %struct.addrinfo** %10
%108 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %107, i32 0, i32 2
%109 = load i32, i32* %108
%110 = load %struct.addrinfo*, %struct.addrinfo** %10
%111 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %110, i32 0, i32 1
%112 = load i32, i32* %111
%113 = call i64 @bind(i32 %106, i32 %109, i32 %112)
%114 = icmp slt i64 %113, 0
br i1 %114, label %115, label %121
115: ; preds = %104
%116 = load i32, i32* @LOG_DEBUG
%117 = call i32 (i32, i8*, ...) @syslog(i32 %116, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0))
%118 = load i32*, i32** %13
%119 = load i32, i32* %118
%120 = call i32 @close(i32 %119)
br label %127
121: ; preds = %104
%122 = load i32*, i32** %14
%123 = load i32, i32* %122
%124 = add nsw i32 %123, 1
store i32 %124, i32* %122
%125 = load i32*, i32** %13
%126 = getelementptr inbounds i32, i32* %125, i32 1
store i32* %126, i32** %13
br label %127
127: ; preds = %121, %115, %91
%128 = load %struct.addrinfo*, %struct.addrinfo** %10
%129 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %128, i32 0, i32 5
%130 = load %struct.addrinfo*, %struct.addrinfo** %129
store %struct.addrinfo* %130, %struct.addrinfo** %10
br label %73
131: ; preds = %73
%132 = load %struct.addrinfo*, %struct.addrinfo** %9
%133 = icmp ne %struct.addrinfo* %132, null
br i1 %133, label %134, label %137
134: ; preds = %131
%135 = load %struct.addrinfo*, %struct.addrinfo** %9
%136 = call i32 @freeaddrinfo(%struct.addrinfo* %135)
br label %137
137: ; preds = %134, %131
%138 = load i32*, i32** %14
%139 = load i32, i32* %138
%140 = icmp eq i32 %139, 0
br i1 %140, label %141, label %146
141: ; preds = %137
%142 = load i32, i32* @LOG_ERR
%143 = call i32 (i32, i8*, ...) @syslog(i32 %142, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.5, i64 0, i64 0))
%144 = load i32*, i32** %14
%145 = call i32 @free(i32* %144)
store i32* null, i32** %4
br label %148
146: ; preds = %137
%147 = load i32*, i32** %14
store i32* %147, i32** %4
br label %148
148: ; preds = %146, %141, %63, %41
%149 = load i32*, i32** %4
ret i32* %149
}

LLVM IR (-O2):
%struct.addrinfo = type { i32, i32, i32, i32, i32, %struct.addrinfo*, i32 }
@AI_PASSIVE = global i32 0
@SOCK_STREAM = global i32 0
@LOG_ERR = global i32 0
@.str = [3 x i8] c"%s\00"
@EAI_SYSTEM = global i32 0
@errno = global i32 0
@.str.1 = [37 x i8] c"couldn't allocate memory for sockets\00"
@LOG_DEBUG = global i32 0
@.str.2 = [19 x i8] c"control socket: %m\00"
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@LOG_WARNING = global i32 0
@.str.3 = [38 x i8] c"control setsockopt (SO_REUSEADDR): %m\00"
@.str.4 = [17 x i8] c"control bind: %m\00"
@.str.5 = [44 x i8] c"control socket: Couldn't bind to any socket\00"
define noalias i32* @socksetup(i32 %0, i8* %1, i8* %2) {
%4 = alloca %struct.addrinfo
%5 = alloca %struct.addrinfo*
%6 = alloca i32
%7 = bitcast %struct.addrinfo* %4 to i8*
%8 = bitcast %struct.addrinfo** %5 to i8*
%9 = bitcast i32* %6 to i8*
store i32 1, i32* %6
%10 = call i32 @memset(%struct.addrinfo* %4, i32 0, i32 40)
%11 = load i32, i32* @AI_PASSIVE
%12 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i64 0, i32 6
store i32 %11, i32* %12
%13 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i64 0, i32 0
store i32 %0, i32* %13
%14 = load i32, i32* @SOCK_STREAM
%15 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i64 0, i32 4
store i32 %14, i32* %15
%16 = call i32 @getaddrinfo(i8* %1, i8* %2, %struct.addrinfo* %4, %struct.addrinfo** %5)
%17 = icmp eq i32 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %3
%19 = load %struct.addrinfo*, %struct.addrinfo** %5
%20 = icmp eq %struct.addrinfo* %19, null
br i1 %20, label %43, label %32
21: ; preds = %3
%22 = load i32, i32* @LOG_ERR
%23 = call i32 @gai_strerror(i32 %16)
%24 = call i32 (i32, i8*, ...) @syslog(i32 %22, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %23)
%25 = load i32, i32* @EAI_SYSTEM
%26 = icmp eq i32 %16, %25
br i1 %26, label %27, label %111
27: ; preds = %21
%28 = load i32, i32* @LOG_ERR
%29 = load i32, i32* @errno
%30 = call i32 @strerror(i32 %29)
%31 = call i32 (i32, i8*, ...) @syslog(i32 %28, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %30)
br label %111
32: ; preds = %18, %32
%33 = phi %struct.addrinfo* [ %37, %32 ], [ %19, %18 ]
%34 = phi i32 [ %36, %32 ], [ 1, %18 ]
%35 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %33, i64 0, i32 5
%36 = add i32 %34, 1
%37 = load %struct.addrinfo*, %struct.addrinfo** %35
%38 = icmp eq %struct.addrinfo* %37, null
br i1 %38, label %39, label %32
39: ; preds = %32
%40 = shl i32 %36, 2
%41 = call i32* @malloc(i32 %40)
%42 = icmp eq i32* %41, null
br i1 %42, label %47, label %51
43: ; preds = %18
%44 = call i32* @malloc(i32 4)
%45 = icmp eq i32* %44, null
br i1 %45, label %47, label %46
46: ; preds = %43
store i32 0, i32* %44
br label %106
47: ; preds = %43, %39
%48 = call i32 @freeaddrinfo(%struct.addrinfo* %19)
%49 = load i32, i32* @LOG_ERR
%50 = call i32 (i32, i8*, ...) @syslog(i32 %49, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0))
br label %111
51: ; preds = %39
store i32 0, i32* %41
br i1 %20, label %106, label %52
52: ; preds = %51
%53 = getelementptr inbounds i32, i32* %41, i64 1
br label %54
54: ; preds = %52, %93
%55 = phi %struct.addrinfo* [ %96, %93 ], [ %19, %52 ]
%56 = phi i32* [ %94, %93 ], [ %53, %52 ]
%57 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %55, i64 0, i32 0
%58 = load i32, i32* %57
%59 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %55, i64 0, i32 4
%60 = load i32, i32* %59
%61 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %55, i64 0, i32 3
%62 = load i32, i32* %61
%63 = call i32 @socket(i32 %58, i32 %60, i32 %62)
store i32 %63, i32* %56
%64 = icmp slt i32 %63, 0
br i1 %64, label %65, label %68
65: ; preds = %54
%66 = load i32, i32* @LOG_DEBUG
%67 = call i32 (i32, i8*, ...) @syslog(i32 %66, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0))
br label %93
68: ; preds = %54
%69 = load i32, i32* @SOL_SOCKET
%70 = load i32, i32* @SO_REUSEADDR
%71 = call i64 @setsockopt(i32 %63, i32 %69, i32 %70, i32* %6, i32 4)
%72 = icmp slt i64 %71, 0
br i1 %72, label %73, label %76
73: ; preds = %68
%74 = load i32, i32* @LOG_WARNING
%75 = call i32 (i32, i8*, ...) @syslog(i32 %74, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.3, i64 0, i64 0))
br label %76
76: ; preds = %73, %68
%77 = load i32, i32* %56
%78 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %55, i64 0, i32 2
%79 = load i32, i32* %78
%80 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %55, i64 0, i32 1
%81 = load i32, i32* %80
%82 = call i64 @bind(i32 %77, i32 %79, i32 %81)
%83 = icmp slt i64 %82, 0
br i1 %83, label %84, label %89
84: ; preds = %76
%85 = load i32, i32* @LOG_DEBUG
%86 = call i32 (i32, i8*, ...) @syslog(i32 %85, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0))
%87 = load i32, i32* %56
%88 = call i32 @close(i32 %87)
br label %93
89: ; preds = %76
%90 = load i32, i32* %41
%91 = add nsw i32 %90, 1
store i32 %91, i32* %41
%92 = getelementptr inbounds i32, i32* %56, i64 1
br label %93
93: ; preds = %89, %84, %65
%94 = phi i32* [ %56, %65 ], [ %56, %84 ], [ %92, %89 ]
%95 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %55, i64 0, i32 5
%96 = load %struct.addrinfo*, %struct.addrinfo** %95
%97 = icmp eq %struct.addrinfo* %96, null
br i1 %97, label %98, label %54
98: ; preds = %93
%99 = load %struct.addrinfo*, %struct.addrinfo** %5
%100 = icmp eq %struct.addrinfo* %99, null
br i1 %100, label %103, label %101
101: ; preds = %98
%102 = call i32 @freeaddrinfo(%struct.addrinfo* %99)
br label %103
103: ; preds = %101, %98
%104 = load i32, i32* %41
%105 = icmp eq i32 %104, 0
br i1 %105, label %106, label %111
106: ; preds = %51, %46, %103
%107 = phi i32* [ %41, %103 ], [ %41, %51 ], [ %44, %46 ]
%108 = load i32, i32* @LOG_ERR
%109 = call i32 (i32, i8*, ...) @syslog(i32 %108, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.5, i64 0, i64 0))
%110 = call i32 @free(i32* %107)
br label %111
111: ; preds = %103, %21, %27, %106, %47
%112 = phi i32* [ null, %106 ], [ null, %47 ], [ null, %27 ], [ null, %21 ], [ %41, %103 ]
ret i32* %112
}