#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 */
typedef int /*<<< orphan*/ value;
struct in_addr {
  int /*<<< orphan*/ s_addr;
};
struct sockaddr_in {
  scalar_t__ sin_family;
  int /*<<< orphan*/ sin_port;
  struct in_addr sin_addr;
};
struct sockaddr {
  int dummy;
};
struct hostent {
  scalar_t__ h_addrtype;
  scalar_t__ h_addr;
};

/* Variables and functions */
scalar_t__ AF_INET;
int EINVAL;
int ENOMEM;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEADDR;
int atoi(char *);
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ free(char *);
struct hostent *gethostbyname(char *);
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
scalar_t__ inet_aton(char *, struct in_addr *);
scalar_t__ listen(int, int);
int /*<<< orphan*/ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/,
                              int *, int);
int socket(scalar_t__, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
char *strchr(char *, char);
char *strdup(char *);
char *strerror(int);
int net_listen(char *name, int port, int flags) {
  int p, len, fd, se;
  char *ptr, *host, *copy;
  struct hostent *he;
  struct sockaddr_in sa;
  int value;
  p = 0;
  ptr = NULL;
  copy = NULL;
  host = NULL;
  if (name) {
    ptr = strchr(name, ':');
    if (ptr != NULL) {
      p = atoi(ptr + 1);
      if (ptr > name) {
        len = ptr - name;
        copy = strdup(name);
        if (copy == NULL) {
          if (flags & 0x01)
            fprintf(stderr, "listen: unable to duplicate string\n");
          errno = ENOMEM;
          return -1;
        }
        copy[len] = '\0';
        host = copy;
      }
    } else {
      p = atoi(name);
      if (p > 0) {
        if (strchr(name, '.')) {
          host = name;
          p = 0;
        }
      } else {
        host = name;
      }
    }
  }
  if (port > 0) {
    p = port;
  }
  if (p == 0) {
    if (!(flags & 0x10)) {
      p = 7147;
    }
  }
  if (host) {
    if (inet_aton(host, &(sa.sin_addr)) == 0) {
      he = gethostbyname(host);
      if ((he == NULL) || (he->h_addrtype != AF_INET)) {
        if (flags & 0x01)
          fprintf(stderr, "listen: unable to map %s to ipv4 address\n", host);
        if (copy) {
          free(copy);
        }
        errno = EINVAL;
        return -1;
      }
      sa.sin_addr = *(struct in_addr *)he->h_addr;
    }
  } else {
    sa.sin_addr.s_addr = htonl(INADDR_ANY);
  }
  if (copy) {
    free(copy);
  }
  sa.sin_port = htons(p);
  sa.sin_family = AF_INET;
  fd = socket(AF_INET, SOCK_STREAM, 0);
  if (fd < 0) {
    if (flags & 0x01) {
      se = errno;
      fprintf(stderr, "listen: unable to allocate socket: %s\n",
              strerror(errno));
      errno = se;
    }
    return -1;
  }
  value = 1;
  setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &value, sizeof(value));
  if (flags & 0x02) {
    fprintf(stderr, "listen: about to bind %u\n", p);
  }
  len = sizeof(struct sockaddr_in);
  if (bind(fd, (struct sockaddr *)(&sa), len)) {
    se = errno;
    close(fd);
    if (flags & 0x01)
      fprintf(stderr, "listen: bind to %u failed: %s\n", p, strerror(errno));
    errno = se;
    return -1;
  }
  if (listen(fd, 3)) {
    se = errno;
    close(fd);
    if (flags & 0x01)
      fprintf(stderr, "listen: unable to listen on port %u: %s\n", p,
              strerror(errno));
    errno = se;
    return -1;
  }
  if (flags & 0x02) {
    fprintf(stderr, "listen: ready for connections\n");
  }
  return fd;
}


x86 assembly (clang, -O0):
<net_listen>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
movq   $0x0,-0x30(%rbp)
movq   $0x0,-0x40(%rbp)
movq   $0x0,-0x38(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     133e <net_listen+0x14e>
mov    -0x10(%rbp),%rdi
mov    $0x3a,%esi
call   1060 <strchr@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     12ea <net_listen+0xfa>
mov    -0x30(%rbp),%rdi
add    $0x1,%rdi
call   10e0 <atoi@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x30(%rbp),%rax
cmp    -0x10(%rbp),%rax
jbe    12e5 <net_listen+0xf5>
mov    -0x30(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   10f0 <strdup@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
jne    12d1 <net_listen+0xe1>
mov    -0x18(%rbp),%eax
and    $0x1,%eax
cmp    $0x0,%eax
je     12b3 <net_listen+0xc3>
mov    0x2d1d(%rip),%rax
mov    (%rax),%edi
lea    0xd54(%rip),%rsi
mov    $0x0,%al
call   10a0 <fprintf@plt>
mov    0x2d0e(%rip),%rax
mov    (%rax),%ecx
mov    0x2ced(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    1697 <net_listen+0x4a7>
mov    -0x40(%rbp),%rax
movslq -0x20(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x40(%rbp),%rax
mov    %rax,-0x38(%rbp)
jmp    1339 <net_listen+0x149>
mov    -0x10(%rbp),%rdi
call   10e0 <atoi@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jle    132c <net_listen+0x13c>
mov    -0x10(%rbp),%rdi
mov    $0x2e,%esi
call   1060 <strchr@plt>
cmp    $0x0,%rax
je     1327 <net_listen+0x137>
mov    -0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
movl   $0x0,-0x1c(%rbp)
jmp    1334 <net_listen+0x144>
mov    -0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
jmp    1339 <net_listen+0x149>
jmp    133e <net_listen+0x14e>
cmpl   $0x0,-0x14(%rbp)
jle    134e <net_listen+0x15e>
mov    -0x14(%rbp),%eax
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jne    1373 <net_listen+0x183>
mov    -0x18(%rbp),%eax
and    $0x10,%eax
cmp    $0x0,%eax
jne    136e <net_listen+0x17e>
movl   $0x1beb,-0x1c(%rbp)
jmp    1373 <net_listen+0x183>
cmpq   $0x0,-0x38(%rbp)
je     1436 <net_listen+0x246>
mov    -0x38(%rbp),%rdi
lea    -0x58(%rbp),%rsi
add    $0xc,%rsi
call   10b0 <inet_aton@plt>
cmp    $0x0,%rax
jne    1431 <net_listen+0x241>
mov    -0x38(%rbp),%rdi
call   1090 <gethostbyname@plt>
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x48(%rbp)
je     13c8 <net_listen+0x1d8>
mov    -0x48(%rbp),%rax
mov    (%rax),%rax
mov    0x2c21(%rip),%rcx
cmp    (%rcx),%rax
je     1424 <net_listen+0x234>
mov    -0x18(%rbp),%eax
and    $0x1,%eax
cmp    $0x0,%eax
je     13f2 <net_listen+0x202>
mov    0x2be2(%rip),%rax
mov    (%rax),%edi
mov    -0x38(%rbp),%rdx
lea    0xc39(%rip),%rsi
mov    $0x0,%al
call   10a0 <fprintf@plt>
cmpq   $0x0,-0x40(%rbp)
je     1406 <net_listen+0x216>
mov    -0x40(%rbp),%rdi
call   1030 <free@plt>
mov    0x2b9b(%rip),%rax
mov    (%rax),%ecx
mov    0x2b9a(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    1697 <net_listen+0x4a7>
mov    -0x48(%rbp),%rax
mov    0x8(%rax),%rax
mov    (%rax),%eax
mov    %eax,-0x4c(%rbp)
jmp    1447 <net_listen+0x257>
mov    0x2b9b(%rip),%rax
mov    (%rax),%edi
call   1070 <htonl@plt>
mov    %eax,-0x4c(%rbp)
cmpq   $0x0,-0x40(%rbp)
je     145b <net_listen+0x26b>
mov    -0x40(%rbp),%rdi
call   1030 <free@plt>
mov    -0x1c(%rbp),%edi
call   1050 <htons@plt>
mov    %eax,-0x50(%rbp)
mov    0x2b73(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x58(%rbp)
mov    0x2b65(%rip),%rax
mov    (%rax),%rdi
mov    0x2b33(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1110 <socket@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    14fc <net_listen+0x30c>
mov    -0x18(%rbp),%eax
and    $0x1,%eax
cmp    $0x0,%eax
je     14f0 <net_listen+0x300>
mov    0x2aff(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    0x2b03(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x60(%rbp)
mov    0x2ae7(%rip),%rax
mov    (%rax),%edi
call   1100 <strerror@plt>
mov    -0x60(%rbp),%edi
mov    %rax,%rdx
lea    0xb71(%rip),%rsi
mov    $0x0,%al
call   10a0 <fprintf@plt>
mov    -0x28(%rbp),%ecx
mov    0x2ac2(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    1697 <net_listen+0x4a7>
movl   $0x1,-0x5c(%rbp)
mov    -0x24(%rbp),%edi
mov    0x2adb(%rip),%rax
mov    (%rax),%esi
mov    0x2a8a(%rip),%rax
mov    (%rax),%edx
lea    -0x5c(%rbp),%rcx
mov    $0x4,%r8d
call   1040 <setsockopt@plt>
mov    -0x18(%rbp),%eax
and    $0x2,%eax
cmp    $0x0,%eax
je     1550 <net_listen+0x360>
mov    0x2a83(%rip),%rax
mov    (%rax),%edi
mov    -0x1c(%rbp),%edx
lea    0xb2c(%rip),%rsi
mov    $0x0,%al
call   10a0 <fprintf@plt>
movl   $0x10,-0x20(%rbp)
mov    -0x24(%rbp),%edi
lea    -0x58(%rbp),%rsi
mov    -0x20(%rbp),%edx
call   10d0 <bind@plt>
cmp    $0x0,%rax
je     15e2 <net_listen+0x3f2>
mov    0x2a39(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    -0x24(%rbp),%edi
call   1080 <close@plt>
mov    -0x18(%rbp),%eax
and    $0x1,%eax
cmp    $0x0,%eax
je     15ca <net_listen+0x3da>
mov    0x2a26(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x68(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x64(%rbp)
mov    0x2a04(%rip),%rax
mov    (%rax),%edi
call   1100 <strerror@plt>
mov    -0x68(%rbp),%edi
mov    -0x64(%rbp),%edx
mov    %rax,%rcx
lea    0xacc(%rip),%rsi
mov    $0x0,%al
call   10a0 <fprintf@plt>
mov    -0x28(%rbp),%ecx
mov    0x29dc(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    1697 <net_listen+0x4a7>
mov    -0x24(%rbp),%edi
mov    $0x3,%esi
call   10c0 <listen@plt>
cmp    $0x0,%rax
je     166b <net_listen+0x47b>
mov    0x29b0(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    -0x24(%rbp),%edi
call   1080 <close@plt>
mov    -0x18(%rbp),%eax
and    $0x1,%eax
cmp    $0x0,%eax
je     1653 <net_listen+0x463>
mov    0x299d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x70(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x6c(%rbp)
mov    0x297b(%rip),%rax
mov    (%rax),%edi
call   1100 <strerror@plt>
mov    -0x70(%rbp),%edi
mov    -0x6c(%rbp),%edx
mov    %rax,%rcx
lea    0xa62(%rip),%rsi
mov    $0x0,%al
call   10a0 <fprintf@plt>
mov    -0x28(%rbp),%ecx
mov    0x2953(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    1697 <net_listen+0x4a7>
mov    -0x18(%rbp),%eax
and    $0x2,%eax
cmp    $0x0,%eax
je     1691 <net_listen+0x4a1>
mov    0x293f(%rip),%rax
mov    (%rax),%edi
lea    0xa4d(%rip),%rsi
mov    $0x0,%al
call   10a0 <fprintf@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<net_listen>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %edx,(%rsp)
mov    %esi,%r13d
xor    %r15d,%r15d
test   %rdi,%rdi
je     1255 <net_listen+0x65>
mov    %rdi,%rbp
mov    $0x3a,%esi
call   1060 <strchr@plt>
test   %rax,%rax
je     125e <net_listen+0x6e>
mov    %rax,%r14
lea    0x1(%rax),%rdi
call   10e0 <atoi@plt>
mov    %eax,%ebx
cmp    %rbp,%r14
jbe    1257 <net_listen+0x67>
mov    %rbp,%rdi
call   10f0 <strdup@plt>
test   %rax,%rax
je     14aa <net_listen+0x2ba>
mov    %rax,%r12
sub    %ebp,%r14d
movslq %r14d,%rax
movb   $0x0,(%r12,%rax,1)
mov    %r12,%rbp
jmp    1287 <net_listen+0x97>
xor    %ebx,%ebx
xor    %ebp,%ebp
xor    %r12d,%r12d
jmp    1287 <net_listen+0x97>
mov    %rbp,%rdi
call   10e0 <atoi@plt>
mov    %eax,%ebx
xor    %r12d,%r12d
test   %eax,%eax
jle    1287 <net_listen+0x97>
mov    %rbp,%rdi
mov    $0x2e,%esi
call   1060 <strchr@plt>
test   %rax,%rax
cmovne %r12d,%ebx
cmove  %rax,%rbp
test   %r13d,%r13d
cmovg  %r13d,%ebx
mov    (%rsp),%r13d
test   $0x10,%r13b
mov    $0x1beb,%eax
cmove  %eax,%r15d
test   %ebx,%ebx
cmovne %ebx,%r15d
test   %rbp,%rbp
je     12e8 <net_listen+0xf8>
lea    0x14(%rsp),%rsi
mov    %rbp,%rdi
call   10b0 <inet_aton@plt>
test   %rax,%rax
jne    12fa <net_listen+0x10a>
mov    %rbp,%rdi
call   1090 <gethostbyname@plt>
test   %rax,%rax
je     1474 <net_listen+0x284>
mov    (%rax),%rcx
mov    0x2d09(%rip),%rdx
cmp    (%rdx),%rcx
jne    1474 <net_listen+0x284>
mov    0x8(%rax),%rax
mov    (%rax),%eax
jmp    12f6 <net_listen+0x106>
mov    0x2ce9(%rip),%rax
mov    (%rax),%edi
call   1070 <htonl@plt>
mov    %eax,0x14(%rsp)
test   %r12,%r12
je     1307 <net_listen+0x117>
mov    %r12,%rdi
call   1030 <free@plt>
mov    %r15d,%edi
call   1050 <htons@plt>
mov    %eax,0x10(%rsp)
mov    0x2cc6(%rip),%rax
mov    (%rax),%rdi
mov    %rdi,0x8(%rsp)
mov    0x2c8f(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1110 <socket@plt>
test   %eax,%eax
js     13d6 <net_listen+0x1e6>
mov    %eax,%ebx
movl   $0x1,0x4(%rsp)
mov    0x2c9d(%rip),%rax
mov    (%rax),%esi
mov    0x2c4c(%rip),%rax
mov    (%rax),%edx
lea    0x4(%rsp),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1040 <setsockopt@plt>
test   $0x2,%r13b
je     1388 <net_listen+0x198>
mov    0x2c4b(%rip),%rax
mov    (%rax),%edi
lea    0xcf7(%rip),%rsi
mov    %r15d,%edx
xor    %eax,%eax
call   10a0 <fprintf@plt>
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
mov    $0x10,%edx
call   10d0 <bind@plt>
test   %rax,%rax
je     141c <net_listen+0x22c>
mov    0x2c0b(%rip),%r14
mov    (%r14),%ebp
mov    %ebx,%edi
call   1080 <close@plt>
test   $0x1,%r13b
je     146f <net_listen+0x27f>
mov    0x2c00(%rip),%rax
mov    (%rax),%ebx
mov    (%r14),%edi
call   1100 <strerror@plt>
lea    0xcbe(%rip),%rsi
jmp    1460 <net_listen+0x270>
mov    $0xffffffff,%ebx
test   $0x1,%r13b
je     14de <net_listen+0x2ee>
mov    0x2bc4(%rip),%r15
mov    (%r15),%ebp
mov    0x2bca(%rip),%rax
mov    (%rax),%r14d
mov    %ebp,%edi
call   1100 <strerror@plt>
lea    0xc47(%rip),%rsi
mov    %r14d,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   10a0 <fprintf@plt>
mov    %ebp,(%r15)
jmp    14de <net_listen+0x2ee>
mov    %ebx,%edi
mov    $0x3,%esi
call   10c0 <listen@plt>
test   %rax,%rax
je     14ef <net_listen+0x2ff>
mov    0x2b78(%rip),%r14
mov    (%r14),%ebp
mov    %ebx,%edi
call   1080 <close@plt>
test   $0x1,%r13b
je     146f <net_listen+0x27f>
mov    0x2b71(%rip),%rax
mov    (%rax),%ebx
mov    (%r14),%edi
call   1100 <strerror@plt>
lea    0xc4e(%rip),%rsi
mov    %ebx,%edi
mov    %r15d,%edx
mov    %rax,%rcx
xor    %eax,%eax
call   10a0 <fprintf@plt>
mov    %ebp,(%r14)
jmp    14d9 <net_listen+0x2e9>
test   $0x1,%r13b
je     1494 <net_listen+0x2a4>
mov    0x2b3f(%rip),%rax
mov    (%rax),%edi
lea    0xb9a(%rip),%rsi
mov    %rbp,%rdx
xor    %eax,%eax
call   10a0 <fprintf@plt>
test   %r12,%r12
je     14a1 <net_listen+0x2b1>
mov    %r12,%rdi
call   1030 <free@plt>
mov    0x2b00(%rip),%rax
jmp    14ce <net_listen+0x2de>
testb  $0x1,(%rsp)
je     14c7 <net_listen+0x2d7>
mov    0x2b09(%rip),%rax
mov    (%rax),%edi
lea    0xb40(%rip),%rsi
xor    %eax,%eax
call   10a0 <fprintf@plt>
mov    0x2afa(%rip),%rax
mov    (%rax),%eax
mov    0x2ad9(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
test   $0x2,%r13b
je     14de <net_listen+0x2ee>
mov    0x2ac4(%rip),%rax
mov    (%rax),%edi
lea    0xbd2(%rip),%rsi
xor    %eax,%eax
call   10a0 <fprintf@plt>
jmp    14de <net_listen+0x2ee>

LLVM IR (-O0):
%struct.hostent = type { i64, i64 }
%struct.sockaddr_in = type { i64, i32, %struct.in_addr }
%struct.in_addr = type { i32 }
%struct.sockaddr = type { i32 }
@stderr = global i32 0
@.str = [36 x i8] c"listen: unable to duplicate string\0A\00"
@ENOMEM = global i32 0
@errno = global i32 0
@AF_INET = global i64 0
@.str.1 = [42 x i8] c"listen: unable to map %s to ipv4 address\0A\00"
@EINVAL = global i32 0
@INADDR_ANY = global i32 0
@SOCK_STREAM = global i32 0
@.str.2 = [39 x i8] c"listen: unable to allocate socket: %s\0A\00"
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.3 = [26 x i8] c"listen: about to bind %u\0A\00"
@.str.4 = [31 x i8] c"listen: bind to %u failed: %s\0A\00"
@.str.5 = [41 x i8] c"listen: unable to listen on port %u: %s\0A\00"
@.str.6 = [31 x i8] c"listen: ready for connections\0A\00"
define i32 @net_listen(i8* %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i8*
%13 = alloca i8*
%14 = alloca i8*
%15 = alloca %struct.hostent*
%16 = alloca %struct.sockaddr_in
%17 = alloca i32
store i8* %0, i8** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
store i32 0, i32* %8
store i8* null, i8** %12
store i8* null, i8** %14
store i8* null, i8** %13
%18 = load i8*, i8** %5
%19 = icmp ne i8* %18, null
br i1 %19, label %20, label %75
20: ; preds = %3
%21 = load i8*, i8** %5
%22 = call i8* @strchr(i8* %21, i8 signext 58)
store i8* %22, i8** %12
%23 = load i8*, i8** %12
%24 = icmp ne i8* %23, null
br i1 %24, label %25, label %59
25: ; preds = %20
%26 = load i8*, i8** %12
%27 = getelementptr inbounds i8, i8* %26, i64 1
%28 = call i32 @atoi(i8* %27)
store i32 %28, i32* %8
%29 = load i8*, i8** %12
%30 = load i8*, i8** %5
%31 = icmp ugt i8* %29, %30
br i1 %31, label %32, label %58
32: ; preds = %25
%33 = load i8*, i8** %12
%34 = load i8*, i8** %5
%35 = ptrtoint i8* %33 to i64
%36 = ptrtoint i8* %34 to i64
%37 = sub i64 %35, %36
%38 = trunc i64 %37 to i32
store i32 %38, i32* %9
%39 = load i8*, i8** %5
%40 = call i8* @strdup(i8* %39)
store i8* %40, i8** %14
%41 = load i8*, i8** %14
%42 = icmp eq i8* %41, null
br i1 %42, label %43, label %52
43: ; preds = %32
%44 = load i32, i32* %7
%45 = and i32 %44, 1
%46 = icmp ne i32 %45, 0
br i1 %46, label %47, label %50
47: ; preds = %43
%48 = load i32, i32* @stderr
%49 = call i32 (i32, i8*, ...) @fprintf(i32 %48, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0))
br label %50
50: ; preds = %47, %43
%51 = load i32, i32* @ENOMEM
store i32 %51, i32* @errno
store i32 -1, i32* %4
br label %228
52: ; preds = %32
%53 = load i8*, i8** %14
%54 = load i32, i32* %9
%55 = sext i32 %54 to i64
%56 = getelementptr inbounds i8, i8* %53, i64 %55
store i8 0, i8* %56
%57 = load i8*, i8** %14
store i8* %57, i8** %13
br label %58
58: ; preds = %52, %25
br label %74
59: ; preds = %20
%60 = load i8*, i8** %5
%61 = call i32 @atoi(i8* %60)
store i32 %61, i32* %8
%62 = load i32, i32* %8
%63 = icmp sgt i32 %62, 0
br i1 %63, label %64, label %71
64: ; preds = %59
%65 = load i8*, i8** %5
%66 = call i8* @strchr(i8* %65, i8 signext 46)
%67 = icmp ne i8* %66, null
br i1 %67, label %68, label %70
68: ; preds = %64
%69 = load i8*, i8** %5
store i8* %69, i8** %13
store i32 0, i32* %8
br label %70
70: ; preds = %68, %64
br label %73
71: ; preds = %59
%72 = load i8*, i8** %5
store i8* %72, i8** %13
br label %73
73: ; preds = %71, %70
br label %74
74: ; preds = %73, %58
br label %75
75: ; preds = %74, %3
%76 = load i32, i32* %6
%77 = icmp sgt i32 %76, 0
br i1 %77, label %78, label %80
78: ; preds = %75
%79 = load i32, i32* %6
store i32 %79, i32* %8
br label %80
80: ; preds = %78, %75
%81 = load i32, i32* %8
%82 = icmp eq i32 %81, 0
br i1 %82, label %83, label %89
83: ; preds = %80
%84 = load i32, i32* %7
%85 = and i32 %84, 16
%86 = icmp ne i32 %85, 0
br i1 %86, label %88, label %87
87: ; preds = %83
store i32 7147, i32* %8
br label %88
88: ; preds = %87, %83
br label %89
89: ; preds = %88, %80
%90 = load i8*, i8** %13
%91 = icmp ne i8* %90, null
br i1 %91, label %92, label %133
92: ; preds = %89
%93 = load i8*, i8** %13
%94 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %16, i32 0, i32 2
%95 = call i64 @inet_aton(i8* %93, %struct.in_addr* %94)
%96 = icmp eq i64 %95, 0
br i1 %96, label %97, label %132
97: ; preds = %92
%98 = load i8*, i8** %13
%99 = call %struct.hostent* @gethostbyname(i8* %98)
store %struct.hostent* %99, %struct.hostent** %15
%100 = load %struct.hostent*, %struct.hostent** %15
%101 = icmp eq %struct.hostent* %100, null
br i1 %101, label %108, label %102
102: ; preds = %97
%103 = load %struct.hostent*, %struct.hostent** %15
%104 = getelementptr inbounds %struct.hostent, %struct.hostent* %103, i32 0, i32 0
%105 = load i64, i64* %104
%106 = load i64, i64* @AF_INET
%107 = icmp ne i64 %105, %106
br i1 %107, label %108, label %124
108: ; preds = %102, %97
%109 = load i32, i32* %7
%110 = and i32 %109, 1
%111 = icmp ne i32 %110, 0
br i1 %111, label %112, label %116
112: ; preds = %108
%113 = load i32, i32* @stderr
%114 = load i8*, i8** %13
%115 = call i32 (i32, i8*, ...) @fprintf(i32 %113, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.1, i64 0, i64 0), i8* %114)
br label %116
116: ; preds = %112, %108
%117 = load i8*, i8** %14
%118 = icmp ne i8* %117, null
br i1 %118, label %119, label %122
119: ; preds = %116
%120 = load i8*, i8** %14
%121 = call i32 @free(i8* %120)
br label %122
122: ; preds = %119, %116
%123 = load i32, i32* @EINVAL
store i32 %123, i32* @errno
store i32 -1, i32* %4
br label %228
124: ; preds = %102
%125 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %16, i32 0, i32 2
%126 = load %struct.hostent*, %struct.hostent** %15
%127 = getelementptr inbounds %struct.hostent, %struct.hostent* %126, i32 0, i32 1
%128 = load i64, i64* %127
%129 = inttoptr i64 %128 to %struct.in_addr*
%130 = bitcast %struct.in_addr* %125 to i8*
%131 = bitcast %struct.in_addr* %129 to i8*
br label %132
132: ; preds = %124, %92
br label %138
133: ; preds = %89
%134 = load i32, i32* @INADDR_ANY
%135 = call i32 @htonl(i32 %134)
%136 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %16, i32 0, i32 2
%137 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %136, i32 0, i32 0
store i32 %135, i32* %137
br label %138
138: ; preds = %133, %132
%139 = load i8*, i8** %14
%140 = icmp ne i8* %139, null
br i1 %140, label %141, label %144
141: ; preds = %138
%142 = load i8*, i8** %14
%143 = call i32 @free(i8* %142)
br label %144
144: ; preds = %141, %138
%145 = load i32, i32* %8
%146 = call i32 @htons(i32 %145)
%147 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %16, i32 0, i32 1
store i32 %146, i32* %147
%148 = load i64, i64* @AF_INET
%149 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %16, i32 0, i32 0
store i64 %148, i64* %149
%150 = load i64, i64* @AF_INET
%151 = load i32, i32* @SOCK_STREAM
%152 = call i32 @socket(i64 %150, i32 %151, i32 0)
store i32 %152, i32* %10
%153 = load i32, i32* %10
%154 = icmp slt i32 %153, 0
br i1 %154, label %155, label %167
155: ; preds = %144
%156 = load i32, i32* %7
%157 = and i32 %156, 1
%158 = icmp ne i32 %157, 0
br i1 %158, label %159, label %166
159: ; preds = %155
%160 = load i32, i32* @errno
store i32 %160, i32* %11
%161 = load i32, i32* @stderr
%162 = load i32, i32* @errno
%163 = call i8* @strerror(i32 %162)
%164 = call i32 (i32, i8*, ...) @fprintf(i32 %161, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.2, i64 0, i64 0), i8* %163)
%165 = load i32, i32* %11
store i32 %165, i32* @errno
br label %166
166: ; preds = %159, %155
store i32 -1, i32* %4
br label %228
167: ; preds = %144
store i32 1, i32* %17
%168 = load i32, i32* %10
%169 = load i32, i32* @SOL_SOCKET
%170 = load i32, i32* @SO_REUSEADDR
%171 = call i32 @setsockopt(i32 %168, i32 %169, i32 %170, i32* %17, i32 4)
%172 = load i32, i32* %7
%173 = and i32 %172, 2
%174 = icmp ne i32 %173, 0
br i1 %174, label %175, label %179
175: ; preds = %167
%176 = load i32, i32* @stderr
%177 = load i32, i32* %8
%178 = call i32 (i32, i8*, ...) @fprintf(i32 %176, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0), i32 %177)
br label %179
179: ; preds = %175, %167
store i32 16, i32* %9
%180 = load i32, i32* %10
%181 = bitcast %struct.sockaddr_in* %16 to %struct.sockaddr*
%182 = load i32, i32* %9
%183 = call i64 @bind(i32 %180, %struct.sockaddr* %181, i32 %182)
%184 = icmp ne i64 %183, 0
br i1 %184, label %185, label %200
185: ; preds = %179
%186 = load i32, i32* @errno
store i32 %186, i32* %11
%187 = load i32, i32* %10
%188 = call i32 @close(i32 %187)
%189 = load i32, i32* %7
%190 = and i32 %189, 1
%191 = icmp ne i32 %190, 0
br i1 %191, label %192, label %198
192: ; preds = %185
%193 = load i32, i32* @stderr
%194 = load i32, i32* %8
%195 = load i32, i32* @errno
%196 = call i8* @strerror(i32 %195)
%197 = call i32 (i32, i8*, ...) @fprintf(i32 %193, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.4, i64 0, i64 0), i32 %194, i8* %196)
br label %198
198: ; preds = %192, %185
%199 = load i32, i32* %11
store i32 %199, i32* @errno
store i32 -1, i32* %4
br label %228
200: ; preds = %179
%201 = load i32, i32* %10
%202 = call i64 @listen(i32 %201, i32 3)
%203 = icmp ne i64 %202, 0
br i1 %203, label %204, label %219
204: ; preds = %200
%205 = load i32, i32* @errno
store i32 %205, i32* %11
%206 = load i32, i32* %10
%207 = call i32 @close(i32 %206)
%208 = load i32, i32* %7
%209 = and i32 %208, 1
%210 = icmp ne i32 %209, 0
br i1 %210, label %211, label %217
211: ; preds = %204
%212 = load i32, i32* @stderr
%213 = load i32, i32* %8
%214 = load i32, i32* @errno
%215 = call i8* @strerror(i32 %214)
%216 = call i32 (i32, i8*, ...) @fprintf(i32 %212, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.5, i64 0, i64 0), i32 %213, i8* %215)
br label %217
217: ; preds = %211, %204
%218 = load i32, i32* %11
store i32 %218, i32* @errno
store i32 -1, i32* %4
br label %228
219: ; preds = %200
%220 = load i32, i32* %7
%221 = and i32 %220, 2
%222 = icmp ne i32 %221, 0
br i1 %222, label %223, label %226
223: ; preds = %219
%224 = load i32, i32* @stderr
%225 = call i32 (i32, i8*, ...) @fprintf(i32 %224, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.6, i64 0, i64 0))
br label %226
226: ; preds = %223, %219
%227 = load i32, i32* %10
store i32 %227, i32* %4
br label %228
228: ; preds = %226, %217, %198, %166, %122, %50
%229 = load i32, i32* %4
ret i32 %229
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i64, i32, %struct.in_addr }
%struct.in_addr = type { i32 }
%struct.hostent = type { i64, i64 }
%struct.sockaddr = type { i32 }
@stderr = global i32 0
@.str = [36 x i8] c"listen: unable to duplicate string\0A\00"
@ENOMEM = global i32 0
@errno = global i32 0
@AF_INET = global i64 0
@.str.1 = [42 x i8] c"listen: unable to map %s to ipv4 address\0A\00"
@EINVAL = global i32 0
@INADDR_ANY = global i32 0
@SOCK_STREAM = global i32 0
@.str.2 = [39 x i8] c"listen: unable to allocate socket: %s\0A\00"
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.3 = [26 x i8] c"listen: about to bind %u\0A\00"
@.str.4 = [31 x i8] c"listen: bind to %u failed: %s\0A\00"
@.str.5 = [41 x i8] c"listen: unable to listen on port %u: %s\0A\00"
@.str.6 = [31 x i8] c"listen: ready for connections\0A\00"
define i32 @net_listen(i8* %0, i32 %1, i32 %2) {
%4 = alloca %struct.sockaddr_in
%5 = alloca i32
%6 = bitcast %struct.sockaddr_in* %4 to i8*
%7 = bitcast i32* %5 to i8*
%8 = icmp eq i8* %0, null
br i1 %8, label %42, label %9
9: ; preds = %3
%10 = tail call i8* @strchr(i8* %0, i8 signext 58)
%11 = icmp eq i8* %10, null
br i1 %11, label %34, label %12
12: ; preds = %9
%13 = getelementptr inbounds i8, i8* %10, i64 1
%14 = tail call i32 @atoi(i8* %13)
%15 = icmp ugt i8* %10, %0
br i1 %15, label %16, label %42
16: ; preds = %12
%17 = tail call i8* @strdup(i8* %0)
%18 = icmp eq i8* %17, null
br i1 %18, label %19, label %27
19: ; preds = %16
%20 = and i32 %2, 1
%21 = icmp eq i32 %20, 0
br i1 %21, label %25, label %22
22: ; preds = %19
%23 = load i32, i32* @stderr
%24 = tail call i32 (i32, i8*, ...) @fprintf(i32 %23, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0))
br label %25
25: ; preds = %22, %19
%26 = load i32, i32* @ENOMEM
store i32 %26, i32* @errno
br label %150
27: ; preds = %16
%28 = ptrtoint i8* %10 to i64
%29 = ptrtoint i8* %0 to i64
%30 = sub i64 %28, %29
%31 = shl i64 %30, 32
%32 = ashr exact i64 %31, 32
%33 = getelementptr inbounds i8, i8* %17, i64 %32
store i8 0, i8* %33
br label %42
34: ; preds = %9
%35 = tail call i32 @atoi(i8* %0)
%36 = icmp sgt i32 %35, 0
br i1 %36, label %37, label %42
37: ; preds = %34
%38 = tail call i8* @strchr(i8* %0, i8 signext 46)
%39 = icmp eq i8* %38, null
%40 = select i1 %39, i32 %35, i32 0
%41 = select i1 %39, i8* null, i8* %0
br label %42
42: ; preds = %37, %34, %27, %12, %3
%43 = phi i32 [ %14, %27 ], [ %14, %12 ], [ 0, %3 ], [ %35, %34 ], [ %40, %37 ]
%44 = phi i8* [ %17, %27 ], [ null, %12 ], [ null, %3 ], [ %0, %34 ], [ %41, %37 ]
%45 = phi i8* [ %17, %27 ], [ null, %12 ], [ null, %3 ], [ null, %34 ], [ null, %37 ]
%46 = icmp sgt i32 %1, 0
%47 = select i1 %46, i32 %1, i32 %43
%48 = icmp eq i32 %47, 0
%49 = and i32 %2, 16
%50 = icmp eq i32 %49, 0
%51 = select i1 %50, i32 7147, i32 0
%52 = select i1 %48, i32 %51, i32 %47
%53 = icmp eq i8* %44, null
br i1 %53, label %84, label %54
54: ; preds = %42
%55 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 2
%56 = call i64 @inet_aton(i8* %44, %struct.in_addr* %55)
%57 = icmp eq i64 %56, 0
br i1 %57, label %58, label %88
58: ; preds = %54
%59 = call %struct.hostent* @gethostbyname(i8* %44)
%60 = icmp eq %struct.hostent* %59, null
br i1 %60, label %66, label %61
61: ; preds = %58
%62 = getelementptr inbounds %struct.hostent, %struct.hostent* %59, i64 0, i32 0
%63 = load i64, i64* %62
%64 = load i64, i64* @AF_INET
%65 = icmp eq i64 %63, %64
br i1 %65, label %78, label %66
66: ; preds = %61, %58
%67 = and i32 %2, 1
%68 = icmp eq i32 %67, 0
br i1 %68, label %72, label %69
69: ; preds = %66
%70 = load i32, i32* @stderr
%71 = call i32 (i32, i8*, ...) @fprintf(i32 %70, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.1, i64 0, i64 0), i8* %44)
br label %72
72: ; preds = %69, %66
%73 = icmp eq i8* %45, null
br i1 %73, label %76, label %74
74: ; preds = %72
%75 = call i32 @free(i8* %45)
br label %76
76: ; preds = %74, %72
%77 = load i32, i32* @EINVAL
store i32 %77, i32* @errno
br label %150
78: ; preds = %61
%79 = getelementptr inbounds %struct.hostent, %struct.hostent* %59, i64 0, i32 1
%80 = load i64, i64* %79
%81 = inttoptr i64 %80 to i32*
%82 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %55, i64 0, i32 0
%83 = load i32, i32* %81
store i32 %83, i32* %82
br label %88
84: ; preds = %42
%85 = load i32, i32* @INADDR_ANY
%86 = tail call i32 @htonl(i32 %85)
%87 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 2, i32 0
store i32 %86, i32* %87
br label %88
88: ; preds = %54, %78, %84
%89 = icmp eq i8* %45, null
br i1 %89, label %92, label %90
90: ; preds = %88
%91 = call i32 @free(i8* %45)
br label %92
92: ; preds = %90, %88
%93 = call i32 @htons(i32 %52)
%94 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 1
store i32 %93, i32* %94
%95 = load i64, i64* @AF_INET
%96 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0
store i64 %95, i64* %96
%97 = load i32, i32* @SOCK_STREAM
%98 = call i32 @socket(i64 %95, i32 %97, i32 0)
%99 = icmp slt i32 %98, 0
br i1 %99, label %100, label %108
100: ; preds = %92
%101 = and i32 %2, 1
%102 = icmp eq i32 %101, 0
br i1 %102, label %150, label %103
103: ; preds = %100
%104 = load i32, i32* @errno
%105 = load i32, i32* @stderr
%106 = call i8* @strerror(i32 %104)
%107 = call i32 (i32, i8*, ...) @fprintf(i32 %105, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.2, i64 0, i64 0), i8* %106)
store i32 %104, i32* @errno
br label %150
108: ; preds = %92
store i32 1, i32* %5
%109 = load i32, i32* @SOL_SOCKET
%110 = load i32, i32* @SO_REUSEADDR
%111 = call i32 @setsockopt(i32 %98, i32 %109, i32 %110, i32* %5, i32 4)
%112 = and i32 %2, 2
%113 = icmp eq i32 %112, 0
br i1 %113, label %117, label %114
114: ; preds = %108
%115 = load i32, i32* @stderr
%116 = call i32 (i32, i8*, ...) @fprintf(i32 %115, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0), i32 %52)
br label %117
117: ; preds = %114, %108
%118 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%119 = call i64 @bind(i32 %98, %struct.sockaddr* %118, i32 16)
%120 = icmp eq i64 %119, 0
br i1 %120, label %132, label %121
121: ; preds = %117
%122 = load i32, i32* @errno
%123 = call i32 @close(i32 %98)
%124 = and i32 %2, 1
%125 = icmp eq i32 %124, 0
br i1 %125, label %131, label %126
126: ; preds = %121
%127 = load i32, i32* @stderr
%128 = load i32, i32* @errno
%129 = call i8* @strerror(i32 %128)
%130 = call i32 (i32, i8*, ...) @fprintf(i32 %127, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.4, i64 0, i64 0), i32 %52, i8* %129)
br label %131
131: ; preds = %126, %121
store i32 %122, i32* @errno
br label %150
132: ; preds = %117
%133 = call i64 @listen(i32 %98, i32 3)
%134 = icmp eq i64 %133, 0
br i1 %134, label %146, label %135
135: ; preds = %132
%136 = load i32, i32* @errno
%137 = call i32 @close(i32 %98)
%138 = and i32 %2, 1
%139 = icmp eq i32 %138, 0
br i1 %139, label %145, label %140
140: ; preds = %135
%141 = load i32, i32* @stderr
%142 = load i32, i32* @errno
%143 = call i8* @strerror(i32 %142)
%144 = call i32 (i32, i8*, ...) @fprintf(i32 %141, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.5, i64 0, i64 0), i32 %52, i8* %143)
br label %145
145: ; preds = %140, %135
store i32 %136, i32* @errno
br label %150
146: ; preds = %132
br i1 %113, label %150, label %147
147: ; preds = %146
%148 = load i32, i32* @stderr
%149 = call i32 (i32, i8*, ...) @fprintf(i32 %148, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.6, i64 0, i64 0))
br label %150
150: ; preds = %146, %147, %100, %103, %145, %131, %76, %25
%151 = phi i32 [ -1, %25 ], [ -1, %76 ], [ -1, %131 ], [ -1, %145 ], [ -1, %103 ], [ -1, %100 ], [ %98, %147 ], [ %98, %146 ]
ret i32 %151
}