#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;
  struct hostent *he;
  struct sockaddr_in sa;
  int value;
  host = NULL;
  p = 0;
  if (name) {
    host = strdup(name);
    if (host == NULL) {
      if (flags & 0x1)
        fprintf(stderr, "listen: unable to duplicate string\n");
      errno = ENOMEM;
      return -1;
    }
    ptr = strchr(host, ':');
    if (ptr) {
      ptr[0] = '\0';
      p = atoi(ptr + 1);
    } else {
      p = atoi(name);
      free(host);
      host = NULL;
    }
  }
  if (port) {
    p = port;
  }
  if (p == 0) {
    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 & 0x1)
          fprintf(stderr, "listen: unable to map %s to ipv4 address\n", host);
        free(host);
        errno = EINVAL;
        return -1;
      }
      sa.sin_addr = *(struct in_addr *)he->h_addr;
    }
    free(host);
  } else {
    sa.sin_addr.s_addr = htonl(INADDR_ANY);
  }
  sa.sin_port = htons(p);
  sa.sin_family = AF_INET;
  fd = socket(AF_INET, SOCK_STREAM, 0);
  if (fd < 0) {
    if (flags & 0x1) {
      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 & 0x2) {
    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 & 0x1)
      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 & 0x1)
      fprintf(stderr, "listen: unable to listen on port %u: %s\n", p,
              strerror(errno));
    errno = se;
    return -1;
  }
  if (flags & 0x2) {
    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)
movq   $0x0,-0x38(%rbp)
movl   $0x0,-0x1c(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     12d3 <net_listen+0xe3>
mov    -0x10(%rbp),%rdi
call   10f0 <strdup@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    1278 <net_listen+0x88>
mov    -0x18(%rbp),%eax
and    $0x1,%eax
cmp    $0x0,%eax
je     125a <net_listen+0x6a>
mov    0x2d76(%rip),%rax
mov    (%rax),%edi
lea    0xdad(%rip),%rsi
mov    $0x0,%al
call   10a0 <fprintf@plt>
mov    0x2d67(%rip),%rax
mov    (%rax),%ecx
mov    0x2d46(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    1602 <net_listen+0x412>
mov    -0x38(%rbp),%rdi
mov    $0x3a,%esi
call   1060 <strchr@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     12b1 <net_listen+0xc1>
mov    -0x30(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x30(%rbp),%rdi
add    $0x1,%rdi
call   10e0 <atoi@plt>
mov    %eax,-0x1c(%rbp)
jmp    12ce <net_listen+0xde>
mov    -0x10(%rbp),%rdi
call   10e0 <atoi@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x38(%rbp)
jmp    12d3 <net_listen+0xe3>
cmpl   $0x0,-0x14(%rbp)
je     12e3 <net_listen+0xf3>
mov    -0x14(%rbp),%eax
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jne    12f4 <net_listen+0x104>
movl   $0x1beb,-0x1c(%rbp)
cmpq   $0x0,-0x38(%rbp)
je     13b5 <net_listen+0x1c5>
mov    -0x38(%rbp),%rdi
lea    -0x50(%rbp),%rsi
add    $0xc,%rsi
call   10b0 <inet_aton@plt>
cmp    $0x0,%rax
jne    13a7 <net_listen+0x1b7>
mov    -0x38(%rbp),%rdi
call   1090 <gethostbyname@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
je     1349 <net_listen+0x159>
mov    -0x40(%rbp),%rax
mov    (%rax),%rax
mov    0x2ca0(%rip),%rcx
cmp    (%rcx),%rax
je     139a <net_listen+0x1aa>
mov    -0x18(%rbp),%eax
and    $0x1,%eax
cmp    $0x0,%eax
je     1373 <net_listen+0x183>
mov    0x2c61(%rip),%rax
mov    (%rax),%edi
mov    -0x38(%rbp),%rdx
lea    0xcb8(%rip),%rsi
mov    $0x0,%al
call   10a0 <fprintf@plt>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
mov    0x2c25(%rip),%rax
mov    (%rax),%ecx
mov    0x2c24(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    1602 <net_listen+0x412>
mov    -0x40(%rbp),%rax
mov    0x8(%rax),%rax
mov    (%rax),%eax
mov    %eax,-0x44(%rbp)
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
jmp    13c6 <net_listen+0x1d6>
mov    0x2c1c(%rip),%rax
mov    (%rax),%edi
call   1070 <htonl@plt>
mov    %eax,-0x44(%rbp)
mov    -0x1c(%rbp),%edi
call   1050 <htons@plt>
mov    %eax,-0x48(%rbp)
mov    0x2c08(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x50(%rbp)
mov    0x2bfa(%rip),%rax
mov    (%rax),%rdi
mov    0x2bc8(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1110 <socket@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    1467 <net_listen+0x277>
mov    -0x18(%rbp),%eax
and    $0x1,%eax
cmp    $0x0,%eax
je     145b <net_listen+0x26b>
mov    0x2b94(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    0x2b98(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x58(%rbp)
mov    0x2b7c(%rip),%rax
mov    (%rax),%edi
call   1100 <strerror@plt>
mov    -0x58(%rbp),%edi
mov    %rax,%rdx
lea    0xc06(%rip),%rsi
mov    $0x0,%al
call   10a0 <fprintf@plt>
mov    -0x28(%rbp),%ecx
mov    0x2b57(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    1602 <net_listen+0x412>
movl   $0x1,-0x54(%rbp)
mov    -0x24(%rbp),%edi
mov    0x2b70(%rip),%rax
mov    (%rax),%esi
mov    0x2b1f(%rip),%rax
mov    (%rax),%edx
lea    -0x54(%rbp),%rcx
mov    $0x4,%r8d
call   1040 <setsockopt@plt>
mov    -0x18(%rbp),%eax
and    $0x2,%eax
cmp    $0x0,%eax
je     14bb <net_listen+0x2cb>
mov    0x2b18(%rip),%rax
mov    (%rax),%edi
mov    -0x1c(%rbp),%edx
lea    0xbc1(%rip),%rsi
mov    $0x0,%al
call   10a0 <fprintf@plt>
movl   $0x10,-0x20(%rbp)
mov    -0x24(%rbp),%edi
lea    -0x50(%rbp),%rsi
mov    -0x20(%rbp),%edx
call   10d0 <bind@plt>
cmp    $0x0,%rax
je     154d <net_listen+0x35d>
mov    0x2ace(%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     1535 <net_listen+0x345>
mov    0x2abb(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x60(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x5c(%rbp)
mov    0x2a99(%rip),%rax
mov    (%rax),%edi
call   1100 <strerror@plt>
mov    -0x60(%rbp),%edi
mov    -0x5c(%rbp),%edx
mov    %rax,%rcx
lea    0xb61(%rip),%rsi
mov    $0x0,%al
call   10a0 <fprintf@plt>
mov    -0x28(%rbp),%ecx
mov    0x2a71(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    1602 <net_listen+0x412>
mov    -0x24(%rbp),%edi
mov    $0x3,%esi
call   10c0 <listen@plt>
cmp    $0x0,%rax
je     15d6 <net_listen+0x3e6>
mov    0x2a45(%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     15be <net_listen+0x3ce>
mov    0x2a32(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x68(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x64(%rbp)
mov    0x2a10(%rip),%rax
mov    (%rax),%edi
call   1100 <strerror@plt>
mov    -0x68(%rbp),%edi
mov    -0x64(%rbp),%edx
mov    %rax,%rcx
lea    0xaf7(%rip),%rsi
mov    $0x0,%al
call   10a0 <fprintf@plt>
mov    -0x28(%rbp),%ecx
mov    0x29e8(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    1602 <net_listen+0x412>
mov    -0x18(%rbp),%eax
and    $0x2,%eax
cmp    $0x0,%eax
je     15fc <net_listen+0x40c>
mov    0x29d4(%rip),%rax
mov    (%rax),%edi
lea    0xae2(%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   %r12
push   %rbx
sub    $0x20,%rsp
mov    %edx,%r14d
mov    %esi,%ebp
test   %rdi,%rdi
je     1266 <net_listen+0x76>
mov    %rdi,%rbx
call   10f0 <strdup@plt>
test   %rax,%rax
je     126a <net_listen+0x7a>
mov    %rax,%r15
mov    %rax,%rdi
mov    $0x3a,%esi
call   1060 <strchr@plt>
test   %rax,%rax
je     1293 <net_listen+0xa3>
movb   $0x0,(%rax)
add    $0x1,%rax
mov    %rax,%rdi
call   10e0 <atoi@plt>
test   %ebp,%ebp
cmovne %ebp,%eax
test   %eax,%eax
mov    $0x1beb,%ebp
cmovne %eax,%ebp
lea    0x1c(%rsp),%rsi
mov    %r15,%rdi
call   10b0 <inet_aton@plt>
test   %rax,%rax
je     142f <net_listen+0x23f>
mov    %r15,%rdi
call   1030 <free@plt>
jmp    12c6 <net_listen+0xd6>
xor    %ebx,%ebx
jmp    12a5 <net_listen+0xb5>
test   $0x1,%r14b
je     1287 <net_listen+0x97>
mov    0x2d49(%rip),%rax
mov    (%rax),%edi
lea    0xd80(%rip),%rsi
xor    %eax,%eax
call   10a0 <fprintf@plt>
mov    0x2d3a(%rip),%rax
jmp    14a8 <net_listen+0x2b8>
mov    %rbx,%rdi
call   10e0 <atoi@plt>
mov    %eax,%ebx
mov    %r15,%rdi
call   1030 <free@plt>
test   %ebp,%ebp
cmovne %ebp,%ebx
test   %ebx,%ebx
mov    $0x1beb,%ebp
cmovne %ebx,%ebp
mov    0x2d1d(%rip),%rax
mov    (%rax),%edi
call   1070 <htonl@plt>
mov    %eax,0x1c(%rsp)
mov    %ebp,%edi
call   1050 <htons@plt>
mov    %eax,0x18(%rsp)
mov    0x2d08(%rip),%rax
mov    (%rax),%rdi
mov    %rdi,0x10(%rsp)
mov    0x2cd1(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1110 <socket@plt>
test   %eax,%eax
js     1393 <net_listen+0x1a3>
mov    %eax,%ebx
movl   $0x1,0xc(%rsp)
mov    0x2cdf(%rip),%rax
mov    (%rax),%esi
mov    0x2c8e(%rip),%rax
mov    (%rax),%edx
lea    0xc(%rsp),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1040 <setsockopt@plt>
test   $0x2,%r14b
je     1345 <net_listen+0x155>
mov    0x2c8d(%rip),%rax
mov    (%rax),%edi
lea    0xd39(%rip),%rsi
mov    %ebp,%edx
xor    %eax,%eax
call   10a0 <fprintf@plt>
lea    0x10(%rsp),%rsi
mov    %ebx,%edi
mov    $0x10,%edx
call   10d0 <bind@plt>
test   %rax,%rax
je     13d9 <net_listen+0x1e9>
mov    0x2c4e(%rip),%r15
mov    (%r15),%r12d
mov    %ebx,%edi
call   1080 <close@plt>
test   $0x1,%r14b
je     1427 <net_listen+0x237>
mov    0x2c43(%rip),%rax
mov    (%rax),%ebx
mov    (%r15),%edi
call   1100 <strerror@plt>
lea    0xd01(%rip),%rsi
jmp    1419 <net_listen+0x229>
mov    $0xffffffff,%ebx
test   $0x1,%r14b
je     14b8 <net_listen+0x2c8>
mov    0x2c07(%rip),%r15
mov    (%r15),%ebp
mov    0x2c0d(%rip),%rax
mov    (%rax),%r14d
mov    %ebp,%edi
call   1100 <strerror@plt>
lea    0xc8a(%rip),%rsi
mov    %r14d,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   10a0 <fprintf@plt>
mov    %ebp,(%r15)
jmp    14b8 <net_listen+0x2c8>
mov    %ebx,%edi
mov    $0x3,%esi
call   10c0 <listen@plt>
test   %rax,%rax
je     145a <net_listen+0x26a>
mov    0x2bbf(%rip),%r15
mov    (%r15),%r12d
mov    %ebx,%edi
call   1080 <close@plt>
test   $0x1,%r14b
je     1427 <net_listen+0x237>
mov    0x2bb8(%rip),%rax
mov    (%rax),%ebx
mov    (%r15),%edi
call   1100 <strerror@plt>
lea    0xc95(%rip),%rsi
mov    %ebx,%edi
mov    %ebp,%edx
mov    %rax,%rcx
xor    %eax,%eax
call   10a0 <fprintf@plt>
mov    %r12d,(%r15)
jmp    14b3 <net_listen+0x2c3>
mov    %r15,%rdi
call   1090 <gethostbyname@plt>
test   %rax,%rax
je     1479 <net_listen+0x289>
mov    (%rax),%rcx
mov    0x2b9a(%rip),%rdx
cmp    (%rdx),%rcx
jne    1479 <net_listen+0x289>
mov    0x8(%rax),%rax
mov    (%rax),%eax
mov    %eax,0x1c(%rsp)
jmp    125c <net_listen+0x6c>
test   $0x2,%r14b
je     14b8 <net_listen+0x2c8>
mov    0x2b59(%rip),%rax
mov    (%rax),%edi
lea    0xc67(%rip),%rsi
xor    %eax,%eax
call   10a0 <fprintf@plt>
jmp    14b8 <net_listen+0x2c8>
test   $0x1,%r14b
je     1499 <net_listen+0x2a9>
mov    0x2b3a(%rip),%rax
mov    (%rax),%edi
lea    0xb95(%rip),%rsi
mov    %r15,%rdx
xor    %eax,%eax
call   10a0 <fprintf@plt>
mov    %r15,%rdi
call   1030 <free@plt>
mov    0x2b00(%rip),%rax
mov    (%rax),%eax
mov    0x2aff(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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 %struct.hostent*
%15 = alloca %struct.sockaddr_in
%16 = alloca i32
store i8* %0, i8** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
store i8* null, i8** %13
store i32 0, i32* %8
%17 = load i8*, i8** %5
%18 = icmp ne i8* %17, null
br i1 %18, label %19, label %50
19: ; preds = %3
%20 = load i8*, i8** %5
%21 = call i8* @strdup(i8* %20)
store i8* %21, i8** %13
%22 = load i8*, i8** %13
%23 = icmp eq i8* %22, null
br i1 %23, label %24, label %33
24: ; preds = %19
%25 = load i32, i32* %7
%26 = and i32 %25, 1
%27 = icmp ne i32 %26, 0
br i1 %27, label %28, label %31
28: ; preds = %24
%29 = load i32, i32* @stderr
%30 = call i32 (i32, i8*, ...) @fprintf(i32 %29, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0))
br label %31
31: ; preds = %28, %24
%32 = load i32, i32* @ENOMEM
store i32 %32, i32* @errno
store i32 -1, i32* %4
br label %190
33: ; preds = %19
%34 = load i8*, i8** %13
%35 = call i8* @strchr(i8* %34, i8 signext 58)
store i8* %35, i8** %12
%36 = load i8*, i8** %12
%37 = icmp ne i8* %36, null
br i1 %37, label %38, label %44
38: ; preds = %33
%39 = load i8*, i8** %12
%40 = getelementptr inbounds i8, i8* %39, i64 0
store i8 0, i8* %40
%41 = load i8*, i8** %12
%42 = getelementptr inbounds i8, i8* %41, i64 1
%43 = call i32 @atoi(i8* %42)
store i32 %43, i32* %8
br label %49
44: ; preds = %33
%45 = load i8*, i8** %5
%46 = call i32 @atoi(i8* %45)
store i32 %46, i32* %8
%47 = load i8*, i8** %13
%48 = call i32 @free(i8* %47)
store i8* null, i8** %13
br label %49
49: ; preds = %44, %38
br label %50
50: ; preds = %49, %3
%51 = load i32, i32* %6
%52 = icmp ne i32 %51, 0
br i1 %52, label %53, label %55
53: ; preds = %50
%54 = load i32, i32* %6
store i32 %54, i32* %8
br label %55
55: ; preds = %53, %50
%56 = load i32, i32* %8
%57 = icmp eq i32 %56, 0
br i1 %57, label %58, label %59
58: ; preds = %55
store i32 7147, i32* %8
br label %59
59: ; preds = %58, %55
%60 = load i8*, i8** %13
%61 = icmp ne i8* %60, null
br i1 %61, label %62, label %101
62: ; preds = %59
%63 = load i8*, i8** %13
%64 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %15, i32 0, i32 2
%65 = call i64 @inet_aton(i8* %63, %struct.in_addr* %64)
%66 = icmp eq i64 %65, 0
br i1 %66, label %67, label %98
67: ; preds = %62
%68 = load i8*, i8** %13
%69 = call %struct.hostent* @gethostbyname(i8* %68)
store %struct.hostent* %69, %struct.hostent** %14
%70 = load %struct.hostent*, %struct.hostent** %14
%71 = icmp eq %struct.hostent* %70, null
br i1 %71, label %78, label %72
72: ; preds = %67
%73 = load %struct.hostent*, %struct.hostent** %14
%74 = getelementptr inbounds %struct.hostent, %struct.hostent* %73, i32 0, i32 0
%75 = load i64, i64* %74
%76 = load i64, i64* @AF_INET
%77 = icmp ne i64 %75, %76
br i1 %77, label %78, label %90
78: ; preds = %72, %67
%79 = load i32, i32* %7
%80 = and i32 %79, 1
%81 = icmp ne i32 %80, 0
br i1 %81, label %82, label %86
82: ; preds = %78
%83 = load i32, i32* @stderr
%84 = load i8*, i8** %13
%85 = call i32 (i32, i8*, ...) @fprintf(i32 %83, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.1, i64 0, i64 0), i8* %84)
br label %86
86: ; preds = %82, %78
%87 = load i8*, i8** %13
%88 = call i32 @free(i8* %87)
%89 = load i32, i32* @EINVAL
store i32 %89, i32* @errno
store i32 -1, i32* %4
br label %190
90: ; preds = %72
%91 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %15, i32 0, i32 2
%92 = load %struct.hostent*, %struct.hostent** %14
%93 = getelementptr inbounds %struct.hostent, %struct.hostent* %92, i32 0, i32 1
%94 = load i64, i64* %93
%95 = inttoptr i64 %94 to %struct.in_addr*
%96 = bitcast %struct.in_addr* %91 to i8*
%97 = bitcast %struct.in_addr* %95 to i8*
br label %98
98: ; preds = %90, %62
%99 = load i8*, i8** %13
%100 = call i32 @free(i8* %99)
br label %106
101: ; preds = %59
%102 = load i32, i32* @INADDR_ANY
%103 = call i32 @htonl(i32 %102)
%104 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %15, i32 0, i32 2
%105 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %104, i32 0, i32 0
store i32 %103, i32* %105
br label %106
106: ; preds = %101, %98
%107 = load i32, i32* %8
%108 = call i32 @htons(i32 %107)
%109 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %15, i32 0, i32 1
store i32 %108, i32* %109
%110 = load i64, i64* @AF_INET
%111 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %15, i32 0, i32 0
store i64 %110, i64* %111
%112 = load i64, i64* @AF_INET
%113 = load i32, i32* @SOCK_STREAM
%114 = call i32 @socket(i64 %112, i32 %113, i32 0)
store i32 %114, i32* %10
%115 = load i32, i32* %10
%116 = icmp slt i32 %115, 0
br i1 %116, label %117, label %129
117: ; preds = %106
%118 = load i32, i32* %7
%119 = and i32 %118, 1
%120 = icmp ne i32 %119, 0
br i1 %120, label %121, label %128
121: ; preds = %117
%122 = load i32, i32* @errno
store i32 %122, i32* %11
%123 = load i32, i32* @stderr
%124 = load i32, i32* @errno
%125 = call i8* @strerror(i32 %124)
%126 = call i32 (i32, i8*, ...) @fprintf(i32 %123, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.2, i64 0, i64 0), i8* %125)
%127 = load i32, i32* %11
store i32 %127, i32* @errno
br label %128
128: ; preds = %121, %117
store i32 -1, i32* %4
br label %190
129: ; preds = %106
store i32 1, i32* %16
%130 = load i32, i32* %10
%131 = load i32, i32* @SOL_SOCKET
%132 = load i32, i32* @SO_REUSEADDR
%133 = call i32 @setsockopt(i32 %130, i32 %131, i32 %132, i32* %16, i32 4)
%134 = load i32, i32* %7
%135 = and i32 %134, 2
%136 = icmp ne i32 %135, 0
br i1 %136, label %137, label %141
137: ; preds = %129
%138 = load i32, i32* @stderr
%139 = load i32, i32* %8
%140 = call i32 (i32, i8*, ...) @fprintf(i32 %138, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0), i32 %139)
br label %141
141: ; preds = %137, %129
store i32 16, i32* %9
%142 = load i32, i32* %10
%143 = bitcast %struct.sockaddr_in* %15 to %struct.sockaddr*
%144 = load i32, i32* %9
%145 = call i64 @bind(i32 %142, %struct.sockaddr* %143, i32 %144)
%146 = icmp ne i64 %145, 0
br i1 %146, label %147, label %162
147: ; preds = %141
%148 = load i32, i32* @errno
store i32 %148, i32* %11
%149 = load i32, i32* %10
%150 = call i32 @close(i32 %149)
%151 = load i32, i32* %7
%152 = and i32 %151, 1
%153 = icmp ne i32 %152, 0
br i1 %153, label %154, label %160
154: ; preds = %147
%155 = load i32, i32* @stderr
%156 = load i32, i32* %8
%157 = load i32, i32* @errno
%158 = call i8* @strerror(i32 %157)
%159 = call i32 (i32, i8*, ...) @fprintf(i32 %155, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.4, i64 0, i64 0), i32 %156, i8* %158)
br label %160
160: ; preds = %154, %147
%161 = load i32, i32* %11
store i32 %161, i32* @errno
store i32 -1, i32* %4
br label %190
162: ; preds = %141
%163 = load i32, i32* %10
%164 = call i64 @listen(i32 %163, i32 3)
%165 = icmp ne i64 %164, 0
br i1 %165, label %166, label %181
166: ; preds = %162
%167 = load i32, i32* @errno
store i32 %167, i32* %11
%168 = load i32, i32* %10
%169 = call i32 @close(i32 %168)
%170 = load i32, i32* %7
%171 = and i32 %170, 1
%172 = icmp ne i32 %171, 0
br i1 %172, label %173, label %179
173: ; preds = %166
%174 = load i32, i32* @stderr
%175 = load i32, i32* %8
%176 = load i32, i32* @errno
%177 = call i8* @strerror(i32 %176)
%178 = call i32 (i32, i8*, ...) @fprintf(i32 %174, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.5, i64 0, i64 0), i32 %175, i8* %177)
br label %179
179: ; preds = %173, %166
%180 = load i32, i32* %11
store i32 %180, i32* @errno
store i32 -1, i32* %4
br label %190
181: ; preds = %162
%182 = load i32, i32* %7
%183 = and i32 %182, 2
%184 = icmp ne i32 %183, 0
br i1 %184, label %185, label %188
185: ; preds = %181
%186 = load i32, i32* @stderr
%187 = call i32 (i32, i8*, ...) @fprintf(i32 %186, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.6, i64 0, i64 0))
br label %188
188: ; preds = %185, %181
%189 = load i32, i32* %10
store i32 %189, i32* %4
br label %190
190: ; preds = %188, %179, %160, %128, %86, %31
%191 = load i32, i32* %4
ret i32 %191
}

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 %61, label %9
9: ; preds = %3
%10 = tail call i8* @strdup(i8* %0)
%11 = icmp eq i8* %10, null
br i1 %11, label %12, label %20
12: ; preds = %9
%13 = and i32 %2, 1
%14 = icmp eq i32 %13, 0
br i1 %14, label %18, label %15
15: ; preds = %12
%16 = load i32, i32* @stderr
%17 = tail call i32 (i32, i8*, ...) @fprintf(i32 %16, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0))
br label %18
18: ; preds = %15, %12
%19 = load i32, i32* @ENOMEM
store i32 %19, i32* @errno
br label %129
20: ; preds = %9
%21 = tail call i8* @strchr(i8* %10, i8 signext 58)
%22 = icmp eq i8* %21, null
br i1 %22, label %23, label %26
23: ; preds = %20
%24 = tail call i32 @atoi(i8* %0)
%25 = tail call i32 @free(i8* %10)
br label %61
26: ; preds = %20
store i8 0, i8* %21
%27 = getelementptr inbounds i8, i8* %21, i64 1
%28 = tail call i32 @atoi(i8* %27)
%29 = icmp eq i32 %1, 0
%30 = select i1 %29, i32 %28, i32 %1
%31 = icmp eq i32 %30, 0
%32 = select i1 %31, i32 7147, i32 %30
%33 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 2
%34 = call i64 @inet_aton(i8* %10, %struct.in_addr* %33)
%35 = icmp eq i64 %34, 0
br i1 %35, label %36, label %59
36: ; preds = %26
%37 = call %struct.hostent* @gethostbyname(i8* %10)
%38 = icmp eq %struct.hostent* %37, null
br i1 %38, label %44, label %39
39: ; preds = %36
%40 = getelementptr inbounds %struct.hostent, %struct.hostent* %37, i64 0, i32 0
%41 = load i64, i64* %40
%42 = load i64, i64* @AF_INET
%43 = icmp eq i64 %41, %42
br i1 %43, label %53, label %44
44: ; preds = %39, %36
%45 = and i32 %2, 1
%46 = icmp eq i32 %45, 0
br i1 %46, label %50, label %47
47: ; preds = %44
%48 = load i32, i32* @stderr
%49 = call i32 (i32, i8*, ...) @fprintf(i32 %48, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.1, i64 0, i64 0), i8* %10)
br label %50
50: ; preds = %47, %44
%51 = call i32 @free(i8* %10)
%52 = load i32, i32* @EINVAL
store i32 %52, i32* @errno
br label %129
53: ; preds = %39
%54 = getelementptr inbounds %struct.hostent, %struct.hostent* %37, i64 0, i32 1
%55 = load i64, i64* %54
%56 = inttoptr i64 %55 to i32*
%57 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %33, i64 0, i32 0
%58 = load i32, i32* %56
store i32 %58, i32* %57
br label %59
59: ; preds = %53, %26
%60 = call i32 @free(i8* %10)
br label %70
61: ; preds = %23, %3
%62 = phi i32 [ 0, %3 ], [ %24, %23 ]
%63 = icmp eq i32 %1, 0
%64 = select i1 %63, i32 %62, i32 %1
%65 = icmp eq i32 %64, 0
%66 = select i1 %65, i32 7147, i32 %64
%67 = load i32, i32* @INADDR_ANY
%68 = tail call i32 @htonl(i32 %67)
%69 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 2, i32 0
store i32 %68, i32* %69
br label %70
70: ; preds = %61, %59
%71 = phi i32 [ %66, %61 ], [ %32, %59 ]
%72 = call i32 @htons(i32 %71)
%73 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 1
store i32 %72, i32* %73
%74 = load i64, i64* @AF_INET
%75 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0
store i64 %74, i64* %75
%76 = load i32, i32* @SOCK_STREAM
%77 = call i32 @socket(i64 %74, i32 %76, i32 0)
%78 = icmp slt i32 %77, 0
br i1 %78, label %79, label %87
79: ; preds = %70
%80 = and i32 %2, 1
%81 = icmp eq i32 %80, 0
br i1 %81, label %129, label %82
82: ; preds = %79
%83 = load i32, i32* @errno
%84 = load i32, i32* @stderr
%85 = call i8* @strerror(i32 %83)
%86 = call i32 (i32, i8*, ...) @fprintf(i32 %84, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.2, i64 0, i64 0), i8* %85)
store i32 %83, i32* @errno
br label %129
87: ; preds = %70
store i32 1, i32* %5
%88 = load i32, i32* @SOL_SOCKET
%89 = load i32, i32* @SO_REUSEADDR
%90 = call i32 @setsockopt(i32 %77, i32 %88, i32 %89, i32* %5, i32 4)
%91 = and i32 %2, 2
%92 = icmp eq i32 %91, 0
br i1 %92, label %96, label %93
93: ; preds = %87
%94 = load i32, i32* @stderr
%95 = call i32 (i32, i8*, ...) @fprintf(i32 %94, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0), i32 %71)
br label %96
96: ; preds = %93, %87
%97 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%98 = call i64 @bind(i32 %77, %struct.sockaddr* %97, i32 16)
%99 = icmp eq i64 %98, 0
br i1 %99, label %111, label %100
100: ; preds = %96
%101 = load i32, i32* @errno
%102 = call i32 @close(i32 %77)
%103 = and i32 %2, 1
%104 = icmp eq i32 %103, 0
br i1 %104, label %110, label %105
105: ; preds = %100
%106 = load i32, i32* @stderr
%107 = load i32, i32* @errno
%108 = call i8* @strerror(i32 %107)
%109 = call i32 (i32, i8*, ...) @fprintf(i32 %106, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.4, i64 0, i64 0), i32 %71, i8* %108)
br label %110
110: ; preds = %105, %100
store i32 %101, i32* @errno
br label %129
111: ; preds = %96
%112 = call i64 @listen(i32 %77, i32 3)
%113 = icmp eq i64 %112, 0
br i1 %113, label %125, label %114
114: ; preds = %111
%115 = load i32, i32* @errno
%116 = call i32 @close(i32 %77)
%117 = and i32 %2, 1
%118 = icmp eq i32 %117, 0
br i1 %118, label %124, label %119
119: ; preds = %114
%120 = load i32, i32* @stderr
%121 = load i32, i32* @errno
%122 = call i8* @strerror(i32 %121)
%123 = call i32 (i32, i8*, ...) @fprintf(i32 %120, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.5, i64 0, i64 0), i32 %71, i8* %122)
br label %124
124: ; preds = %119, %114
store i32 %115, i32* @errno
br label %129
125: ; preds = %111
br i1 %92, label %129, label %126
126: ; preds = %125
%127 = load i32, i32* @stderr
%128 = call i32 (i32, i8*, ...) @fprintf(i32 %127, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.6, i64 0, i64 0))
br label %129
129: ; preds = %125, %126, %79, %82, %124, %110, %50, %18
%130 = phi i32 [ -1, %18 ], [ -1, %50 ], [ -1, %110 ], [ -1, %124 ], [ -1, %82 ], [ -1, %79 ], [ %77, %126 ], [ %77, %125 ]
ret i32 %130
}