#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */
struct sockaddr_un {
  char *sun_path;
};
struct sockaddr_storage {
  scalar_t__ ss_family;
};
struct sockaddr {
  int dummy;
};
typedef int socklen_t;
typedef int /*<<< orphan*/ portname;
typedef int /*<<< orphan*/ n;

/* Variables and functions */
scalar_t__ AF_UNIX;
int NI_DGRAM;
int NI_MAXSERV;
int NI_NUMERICSERV;
int SOCK_DGRAM;
int SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEADDR;
int /*<<< orphan*/ addrsize(struct sockaddr_storage *);
scalar_t__ bind(int, struct sockaddr const *, int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, ...);
char *gai_strerror(int);
int getnameinfo(struct sockaddr *, int, int /*<<< orphan*/ *,
                int /*<<< orphan*/, char *, int, int);
scalar_t__ getsockname(int, struct sockaddr *, int *);
scalar_t__ listen(int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/,
                              char *, int);
int socket(scalar_t__, int, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int listen_on(int dgram, struct sockaddr_storage *ss) {
  int type = dgram ? SOCK_DGRAM : SOCK_STREAM;
  int s = socket(ss->ss_family, type, 0);
  int n = 1;
  socklen_t len;
  int err;
  char portname[NI_MAXSERV];
  if (s < 0) {
    perror("socket");
    return -1;
  }
  if (!dgram)
    setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char *)&n, sizeof(n));
  if (bind(s, (const struct sockaddr *)ss, addrsize(ss)) < 0) {
    perror("bind");
    close(s);
    return -1;
  }
  if (!dgram && listen(s, 5) < 0) {
    perror("listen");
    close(s);
    return -1;
  }
  if (ss->ss_family == AF_UNIX) {
    fprintf(stderr, "[listening on %s]\n",
            ((struct sockaddr_un *)ss)->sun_path);
    return s;
  }
  len = sizeof(*ss);
  if (getsockname(s, (struct sockaddr *)ss, &len) < 0) {
    perror("getsockname");
    close(s);
    return -1;
  }
  err = getnameinfo((struct sockaddr *)ss, len, NULL, 0, portname,
                    sizeof(portname), (dgram ? NI_DGRAM : 0) | NI_NUMERICSERV);
  if (err) {
    fprintf(stderr, "%s\n", gai_strerror(err));
    close(s);
    return -1;
  }
  fprintf(stderr, "[listening on %s port %s]\n", dgram ? "UDP" : "TCP",
          portname);
  return s;
}


x86 assembly (clang, -O0):
<listen_on>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x0,-0x8(%rbp)
je     11dd <listen_on+0x2d>
mov    0x2df5(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x40(%rbp)
jmp    11e9 <listen_on+0x39>
mov    0x2dcc(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x40(%rbp)
mov    -0x40(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x14(%rbp),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
mov    %eax,-0x18(%rbp)
movl   $0x1,-0x1c(%rbp)
mov    0x2dbf(%rip),%rax
mov    (%rax),%eax
mov    %rax,-0x50(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x30(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x48(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x38(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    1263 <listen_on+0xb3>
lea    0xdb5(%rip),%rdi
call   10b0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x3c(%rbp)
jmp    14b9 <listen_on+0x309>
cmpl   $0x0,-0x8(%rbp)
jne    1291 <listen_on+0xe1>
mov    -0x18(%rbp),%edi
mov    0x2d69(%rip),%rax
mov    (%rax),%esi
mov    0x2d28(%rip),%rax
mov    (%rax),%edx
lea    -0x1c(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x5c(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    -0x10(%rbp),%rdi
call   1080 <addrsize@plt>
mov    -0x5c(%rbp),%edi
mov    -0x58(%rbp),%rsi
mov    %eax,%edx
call   10a0 <bind@plt>
cmp    $0x0,%rax
jge    12e7 <listen_on+0x137>
lea    0xd40(%rip),%rdi
call   10b0 <perror@plt>
mov    -0x18(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x3c(%rbp)
jmp    14b9 <listen_on+0x309>
cmpl   $0x0,-0x8(%rbp)
jne    132f <listen_on+0x17f>
mov    -0x18(%rbp),%edi
mov    $0x5,%esi
call   1090 <listen@plt>
cmp    $0x0,%rax
jge    132f <listen_on+0x17f>
lea    0xcfd(%rip),%rdi
call   10b0 <perror@plt>
mov    -0x18(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x3c(%rbp)
jmp    14b9 <listen_on+0x309>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    0x2c9b(%rip),%rcx
cmp    (%rcx),%rax
jne    1376 <listen_on+0x1c6>
mov    0x2c6b(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xcb6(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x3c(%rbp)
jmp    14b9 <listen_on+0x309>
movl   $0x8,-0x20(%rbp)
mov    -0x18(%rbp),%edi
mov    -0x10(%rbp),%rsi
lea    -0x20(%rbp),%rdx
call   10c0 <getsockname@plt>
cmp    $0x0,%rax
jge    13be <listen_on+0x20e>
lea    0xc88(%rip),%rdi
call   10b0 <perror@plt>
mov    -0x18(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x3c(%rbp)
jmp    14b9 <listen_on+0x309>
mov    -0x50(%rbp),%rax
mov    -0x10(%rbp),%rcx
mov    %rcx,-0x70(%rbp)
mov    -0x20(%rbp),%ecx
mov    %ecx,-0x64(%rbp)
mov    %eax,-0x60(%rbp)
cmpl   $0x0,-0x8(%rbp)
je     13ee <listen_on+0x23e>
mov    0x2bb4(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x74(%rbp)
jmp    13f8 <listen_on+0x248>
xor    %eax,%eax
mov    %eax,-0x74(%rbp)
jmp    13f8 <listen_on+0x248>
mov    -0x60(%rbp),%r9d
mov    -0x48(%rbp),%r8
mov    -0x64(%rbp),%esi
mov    -0x70(%rbp),%rdi
mov    -0x74(%rbp),%eax
mov    0x2bd7(%rip),%rcx
or     (%rcx),%eax
xor    %ecx,%ecx
mov    %ecx,%edx
xor    %ecx,%ecx
sub    $0x10,%rsp
mov    %eax,(%rsp)
call   1060 <getnameinfo@plt>
add    $0x10,%rsp
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
je     1479 <listen_on+0x2c9>
mov    0x2b7b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x78(%rbp)
mov    -0x24(%rbp),%edi
call   1040 <gai_strerror@plt>
mov    -0x78(%rbp),%edi
mov    %rax,%rdx
lea    0xbdb(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    -0x18(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x3c(%rbp)
jmp    14b9 <listen_on+0x309>
mov    -0x48(%rbp),%rcx
mov    0x2b34(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%esi
lea    0xbc5(%rip),%rdx
lea    0xbba(%rip),%rax
cmp    $0x0,%esi
cmovne %rax,%rdx
lea    0xb91(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x3c(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<listen_on>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%rbx
mov    %edi,%r15d
test   %edi,%edi
jne    11d4 <listen_on+0x24>
mov    0x2dde(%rip),%rax
jmp    11db <listen_on+0x2b>
mov    0x2ded(%rip),%rax
mov    (%rax),%esi
mov    (%rbx),%rdi
xor    %edx,%edx
call   10d0 <socket@plt>
mov    %eax,%r14d
mov    %rsp,-0x30(%rbp)
movl   $0x1,-0x38(%rbp)
mov    0x2dd4(%rip),%rax
mov    (%rax),%r13d
mov    %rsp,%r12
lea    0xf(%r13),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r12
mov    %r12,%rsp
test   %r14d,%r14d
js     1350 <listen_on+0x1a0>
test   %r15d,%r15d
jne    1242 <listen_on+0x92>
mov    0x2dbb(%rip),%rax
mov    (%rax),%esi
mov    0x2d7a(%rip),%rax
mov    (%rax),%edx
lea    -0x38(%rbp),%rcx
mov    %r14d,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    %rbx,%rdi
call   1080 <addrsize@plt>
mov    %r14d,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   10a0 <bind@plt>
test   %rax,%rax
js     135e <listen_on+0x1ae>
test   %r15d,%r15d
jne    127b <listen_on+0xcb>
mov    %r14d,%edi
mov    $0x5,%esi
call   1090 <listen@plt>
test   %rax,%rax
js     1370 <listen_on+0x1c0>
mov    (%rbx),%rdx
mov    0x2d53(%rip),%rax
cmp    (%rax),%rdx
jne    12a6 <listen_on+0xf6>
mov    0x2d27(%rip),%rax
mov    (%rax),%edi
lea    0xd79(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
jmp    138a <listen_on+0x1da>
movl   $0x8,-0x34(%rbp)
lea    -0x34(%rbp),%rdx
mov    %r14d,%edi
mov    %rbx,%rsi
call   10c0 <getsockname@plt>
test   %rax,%rax
js     1367 <listen_on+0x1b7>
mov    -0x34(%rbp),%esi
mov    %r15d,%eax
test   %r15d,%r15d
je     12d9 <listen_on+0x129>
mov    0x2cc1(%rip),%rax
mov    (%rax),%eax
mov    0x2d08(%rip),%rcx
or     (%rcx),%eax
sub    $0x8,%rsp
mov    %rbx,%rdi
xor    %edx,%edx
xor    %ecx,%ecx
mov    %r12,%r8
mov    %r13d,%r9d
push   %rax
call   1060 <getnameinfo@plt>
add    $0x10,%rsp
mov    0x2cb4(%rip),%rcx
mov    (%rcx),%ebx
test   %eax,%eax
je     1326 <listen_on+0x176>
mov    %eax,%edi
call   1040 <gai_strerror@plt>
lea    0xd1a(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   1070 <fprintf@plt>
jmp    137c <listen_on+0x1cc>
test   %r15d,%r15d
lea    0xd25(%rip),%rax
lea    0xd1a(%rip),%rdx
cmove  %rax,%rdx
lea    0xcf4(%rip),%rsi
mov    %ebx,%edi
mov    %r12,%rcx
xor    %eax,%eax
call   1070 <fprintf@plt>
jmp    138a <listen_on+0x1da>
lea    0xca9(%rip),%rdi
call   10b0 <perror@plt>
jmp    1384 <listen_on+0x1d4>
lea    0xca2(%rip),%rdi
jmp    1377 <listen_on+0x1c7>
lea    0xcb8(%rip),%rdi
jmp    1377 <listen_on+0x1c7>
lea    0xc95(%rip),%rdi
call   10b0 <perror@plt>
mov    %r14d,%edi
call   1050 <close@plt>
mov    $0xffffffff,%r14d
mov    -0x30(%rbp),%rax
mov    %rax,%rsp
mov    %r14d,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_storage = type { i64 }
%struct.sockaddr = type { i32 }
%struct.sockaddr_un = type { i8* }
@SOCK_DGRAM = global i32 0
@SOCK_STREAM = global i32 0
@NI_MAXSERV = global i32 0
@.str = [7 x i8] c"socket\00"
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.1 = [5 x i8] c"bind\00"
@.str.2 = [7 x i8] c"listen\00"
@AF_UNIX = global i64 0
@stderr = global i32 0
@.str.3 = [19 x i8] c"[listening on %s]\0A\00"
@.str.4 = [12 x i8] c"getsockname\00"
@NI_DGRAM = global i32 0
@NI_NUMERICSERV = global i32 0
@.str.5 = [4 x i8] c"%s\0A\00"
@.str.6 = [27 x i8] c"[listening on %s port %s]\0A\00"
@.str.7 = [4 x i8] c"UDP\00"
@.str.8 = [4 x i8] c"TCP\00"
define i32 @listen_on(i32 %0, %struct.sockaddr_storage* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.sockaddr_storage*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i8*
%12 = alloca i64
%13 = alloca i32
store i32 %0, i32* %4
store %struct.sockaddr_storage* %1, %struct.sockaddr_storage** %5
%14 = load i32, i32* %4
%15 = icmp ne i32 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %2
%17 = load i32, i32* @SOCK_DGRAM
br label %20
18: ; preds = %2
%19 = load i32, i32* @SOCK_STREAM
br label %20
20: ; preds = %18, %16
%21 = phi i32 [ %17, %16 ], [ %19, %18 ]
store i32 %21, i32* %6
%22 = load %struct.sockaddr_storage*, %struct.sockaddr_storage** %5
%23 = getelementptr inbounds %struct.sockaddr_storage, %struct.sockaddr_storage* %22, i32 0, i32 0
%24 = load i64, i64* %23
%25 = load i32, i32* %6
%26 = call i32 @socket(i64 %24, i32 %25, i32 0)
store i32 %26, i32* %7
store i32 1, i32* %8
%27 = load i32, i32* @NI_MAXSERV
%28 = zext i32 %27 to i64
%29 = call i8* @llvm.stacksave()
store i8* %29, i8** %11
%30 = alloca i8, i64 %28
store i64 %28, i64* %12
%31 = load i32, i32* %7
%32 = icmp slt i32 %31, 0
br i1 %32, label %33, label %35
33: ; preds = %20
%34 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
store i32 1, i32* %13
br label %123
35: ; preds = %20
%36 = load i32, i32* %4
%37 = icmp ne i32 %36, 0
br i1 %37, label %44, label %38
38: ; preds = %35
%39 = load i32, i32* %7
%40 = load i32, i32* @SOL_SOCKET
%41 = load i32, i32* @SO_REUSEADDR
%42 = bitcast i32* %8 to i8*
%43 = call i32 @setsockopt(i32 %39, i32 %40, i32 %41, i8* %42, i32 4)
br label %44
44: ; preds = %38, %35
%45 = load i32, i32* %7
%46 = load %struct.sockaddr_storage*, %struct.sockaddr_storage** %5
%47 = bitcast %struct.sockaddr_storage* %46 to %struct.sockaddr*
%48 = load %struct.sockaddr_storage*, %struct.sockaddr_storage** %5
%49 = call i32 @addrsize(%struct.sockaddr_storage* %48)
%50 = call i64 @bind(i32 %45, %struct.sockaddr* %47, i32 %49)
%51 = icmp slt i64 %50, 0
br i1 %51, label %52, label %56
52: ; preds = %44
%53 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%54 = load i32, i32* %7
%55 = call i32 @close(i32 %54)
store i32 -1, i32* %3
store i32 1, i32* %13
br label %123
56: ; preds = %44
%57 = load i32, i32* %4
%58 = icmp ne i32 %57, 0
br i1 %58, label %67, label %59
59: ; preds = %56
%60 = load i32, i32* %7
%61 = call i64 @listen(i32 %60, i32 5)
%62 = icmp slt i64 %61, 0
br i1 %62, label %63, label %67
63: ; preds = %59
%64 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%65 = load i32, i32* %7
%66 = call i32 @close(i32 %65)
store i32 -1, i32* %3
store i32 1, i32* %13
br label %123
67: ; preds = %59, %56
%68 = load %struct.sockaddr_storage*, %struct.sockaddr_storage** %5
%69 = getelementptr inbounds %struct.sockaddr_storage, %struct.sockaddr_storage* %68, i32 0, i32 0
%70 = load i64, i64* %69
%71 = load i64, i64* @AF_UNIX
%72 = icmp eq i64 %70, %71
br i1 %72, label %73, label %81
73: ; preds = %67
%74 = load i32, i32* @stderr
%75 = load %struct.sockaddr_storage*, %struct.sockaddr_storage** %5
%76 = bitcast %struct.sockaddr_storage* %75 to %struct.sockaddr_un*
%77 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %76, i32 0, i32 0
%78 = load i8*, i8** %77
%79 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %74, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0), i8* %78)
%80 = load i32, i32* %7
store i32 %80, i32* %3
store i32 1, i32* %13
br label %123
81: ; preds = %67
store i32 8, i32* %9
%82 = load i32, i32* %7
%83 = load %struct.sockaddr_storage*, %struct.sockaddr_storage** %5
%84 = bitcast %struct.sockaddr_storage* %83 to %struct.sockaddr*
%85 = call i64 @getsockname(i32 %82, %struct.sockaddr* %84, i32* %9)
%86 = icmp slt i64 %85, 0
br i1 %86, label %87, label %91
87: ; preds = %81
%88 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0))
%89 = load i32, i32* %7
%90 = call i32 @close(i32 %89)
store i32 -1, i32* %3
store i32 1, i32* %13
br label %123
91: ; preds = %81
%92 = load %struct.sockaddr_storage*, %struct.sockaddr_storage** %5
%93 = bitcast %struct.sockaddr_storage* %92 to %struct.sockaddr*
%94 = load i32, i32* %9
%95 = trunc i64 %28 to i32
%96 = load i32, i32* %4
%97 = icmp ne i32 %96, 0
br i1 %97, label %98, label %100
98: ; preds = %91
%99 = load i32, i32* @NI_DGRAM
br label %101
100: ; preds = %91
br label %101
101: ; preds = %100, %98
%102 = phi i32 [ %99, %98 ], [ 0, %100 ]
%103 = load i32, i32* @NI_NUMERICSERV
%104 = or i32 %102, %103
%105 = call i32 @getnameinfo(%struct.sockaddr* %93, i32 %94, i32* null, i32 0, i8* %30, i32 %95, i32 %104)
store i32 %105, i32* %10
%106 = load i32, i32* %10
%107 = icmp ne i32 %106, 0
br i1 %107, label %108, label %115
108: ; preds = %101
%109 = load i32, i32* @stderr
%110 = load i32, i32* %10
%111 = call i8* @gai_strerror(i32 %110)
%112 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %109, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0), i8* %111)
%113 = load i32, i32* %7
%114 = call i32 @close(i32 %113)
store i32 -1, i32* %3
store i32 1, i32* %13
br label %123
115: ; preds = %101
%116 = load i32, i32* @stderr
%117 = load i32, i32* %4
%118 = icmp ne i32 %117, 0
%119 = zext i1 %118 to i64
%120 = select i1 %118, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.7, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.8, i64 0, i64 0)
%121 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %116, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.6, i64 0, i64 0), i8* %120, i8* %30)
%122 = load i32, i32* %7
store i32 %122, i32* %3
store i32 1, i32* %13
br label %123
123: ; preds = %115, %108, %87, %73, %63, %52, %33
%124 = load i8*, i8** %11
%125 = load i32, i32* %3
ret i32 %125
}

LLVM IR (-O2):
%struct.sockaddr_storage = type { i64 }
%struct.sockaddr = type { i32 }
@SOCK_DGRAM = global i32 0
@SOCK_STREAM = global i32 0
@NI_MAXSERV = global i32 0
@.str = [7 x i8] c"socket\00"
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.1 = [5 x i8] c"bind\00"
@.str.2 = [7 x i8] c"listen\00"
@AF_UNIX = global i64 0
@stderr = global i32 0
@.str.3 = [19 x i8] c"[listening on %s]\0A\00"
@.str.4 = [12 x i8] c"getsockname\00"
@NI_DGRAM = global i32 0
@NI_NUMERICSERV = global i32 0
@.str.5 = [4 x i8] c"%s\0A\00"
@.str.6 = [27 x i8] c"[listening on %s port %s]\0A\00"
@.str.7 = [4 x i8] c"UDP\00"
@.str.8 = [4 x i8] c"TCP\00"
define i32 @listen_on(i32 %0, %struct.sockaddr_storage* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = icmp eq i32 %0, 0
%6 = load i32, i32* @SOCK_DGRAM
%7 = load i32, i32* @SOCK_STREAM
%8 = select i1 %5, i32 %7, i32 %6
%9 = getelementptr inbounds %struct.sockaddr_storage, %struct.sockaddr_storage* %1, i64 0, i32 0
%10 = load i64, i64* %9
%11 = tail call i32 @socket(i64 %10, i32 %8, i32 0)
%12 = bitcast i32* %3 to i8*
store i32 1, i32* %3
%13 = bitcast i32* %4 to i8*
%14 = load i32, i32* @NI_MAXSERV
%15 = zext i32 %14 to i64
%16 = tail call i8* @llvm.stacksave()
%17 = alloca i8, i64 %15
%18 = icmp slt i32 %11, 0
br i1 %18, label %19, label %21
19: ; preds = %2
%20 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %71
21: ; preds = %2
br i1 %5, label %22, label %26
22: ; preds = %21
%23 = load i32, i32* @SOL_SOCKET
%24 = load i32, i32* @SO_REUSEADDR
%25 = call i32 @setsockopt(i32 %11, i32 %23, i32 %24, i8* %12, i32 4)
br label %26
26: ; preds = %22, %21
%27 = bitcast %struct.sockaddr_storage* %1 to %struct.sockaddr*
%28 = call i32 @addrsize(%struct.sockaddr_storage* %1)
%29 = call i64 @bind(i32 %11, %struct.sockaddr* %27, i32 %28)
%30 = icmp slt i64 %29, 0
br i1 %30, label %31, label %34
31: ; preds = %26
%32 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%33 = call i32 @close(i32 %11)
br label %71
34: ; preds = %26
br i1 %5, label %35, label %41
35: ; preds = %34
%36 = call i64 @listen(i32 %11, i32 5)
%37 = icmp slt i64 %36, 0
br i1 %37, label %38, label %41
38: ; preds = %35
%39 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%40 = call i32 @close(i32 %11)
br label %71
41: ; preds = %35, %34
%42 = load i64, i64* %9
%43 = load i64, i64* @AF_UNIX
%44 = icmp eq i64 %42, %43
br i1 %44, label %45, label %49
45: ; preds = %41
%46 = inttoptr i64 %42 to i8*
%47 = load i32, i32* @stderr
%48 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %47, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0), i8* %46)
br label %71
49: ; preds = %41
store i32 8, i32* %4
%50 = call i64 @getsockname(i32 %11, %struct.sockaddr* %27, i32* %4)
%51 = icmp slt i64 %50, 0
br i1 %51, label %52, label %55
52: ; preds = %49
%53 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0))
%54 = call i32 @close(i32 %11)
br label %71
55: ; preds = %49
%56 = load i32, i32* %4
%57 = load i32, i32* @NI_DGRAM
%58 = select i1 %5, i32 0, i32 %57
%59 = load i32, i32* @NI_NUMERICSERV
%60 = or i32 %58, %59
%61 = call i32 @getnameinfo(%struct.sockaddr* %27, i32 %56, i32* null, i32 0, i8* %17, i32 %14, i32 %60)
%62 = icmp eq i32 %61, 0
%63 = load i32, i32* @stderr
br i1 %62, label %68, label %64
64: ; preds = %55
%65 = call i8* @gai_strerror(i32 %61)
%66 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %63, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0), i8* %65)
%67 = call i32 @close(i32 %11)
br label %71
68: ; preds = %55
%69 = select i1 %5, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.8, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.7, i64 0, i64 0)
%70 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %63, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.6, i64 0, i64 0), i8* %69, i8* %17)
br label %71
71: ; preds = %68, %64, %52, %45, %38, %31, %19
%72 = phi i32 [ -1, %19 ], [ -1, %31 ], [ %11, %45 ], [ -1, %52 ], [ -1, %64 ], [ %11, %68 ], [ -1, %38 ]
ret i32 %72
}