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

/* Variables and functions */
int /*<<< orphan*/ AF_UNSPEC;
int /*<<< orphan*/ AI_PASSIVE;
scalar_t__ EPERM;
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ SOCK_STREAM;
int bind(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
scalar_t__ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
char *gai_strerror(int);
int getaddrinfo(int /*<<< orphan*/ *, char *, struct addrinfo *,
                struct addrinfo **);
int listen(int, int);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
int /*<<< orphan*/ net_sys_err(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, scalar_t__);
int /*<<< orphan*/ stderr;
int net_listen(char *service) {
  int sock;
  int ret;
  struct addrinfo *rp;
  struct addrinfo *result = NULL;
  struct addrinfo hints;
  memset(&hints, 0, sizeof(hints));
  hints.ai_family = AF_UNSPEC;
  hints.ai_socktype = SOCK_STREAM;
  hints.ai_flags = AI_PASSIVE;
  hints.ai_protocol = 0;
  ret = getaddrinfo(NULL, service, &hints, &result);
  if (ret) {
    fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(ret));
    exit(EXIT_FAILURE);
  }
  for (rp = result; rp; rp = rp->ai_next) {
    sock = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
    if (sock < 0)
      continue;
    ret = bind(sock, rp->ai_addr, rp->ai_addrlen);
    if (!ret)
      break;
    if (ret < 0 && errno == EPERM)
      net_sys_err("bind");
    close(sock);
  }
  if (!rp) {
    fprintf(stderr, "Could not bind\n");
    exit(EXIT_FAILURE);
  }
  freeaddrinfo(result);
  ret = listen(sock, 128);
  if (ret < 0)
    net_sys_err("listen");
  return sock;
}


x86 assembly (clang, -O0):
<net_listen>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
movq   $0x0,-0x20(%rbp)
lea    -0x48(%rbp),%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1040 <memset@plt>
mov    0x2e05(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    0x2de1(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x38(%rbp)
mov    0x2df5(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
movq   $0x0,-0x40(%rbp)
mov    -0x8(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edi
lea    -0x48(%rbp),%rdx
lea    -0x20(%rbp),%rcx
call   10a0 <getaddrinfo@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
je     1258 <net_listen+0xa8>
mov    0x2da7(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4c(%rbp)
mov    -0x10(%rbp),%edi
call   1030 <gai_strerror@plt>
mov    -0x4c(%rbp),%edi
mov    %rax,%rdx
lea    0xdbd(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    0x2d67(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     1308 <net_listen+0x158>
mov    -0x18(%rbp),%rax
mov    0x14(%rax),%edi
mov    -0x18(%rbp),%rax
mov    0x10(%rax),%esi
mov    -0x18(%rbp),%rax
mov    0x8(%rax),%rdx
call   10d0 <socket@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    1298 <net_listen+0xe8>
jmp    12f7 <net_listen+0x147>
mov    -0xc(%rbp),%edi
mov    -0x18(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x18(%rbp),%rax
mov    (%rax),%edx
call   1080 <bind@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jne    12bf <net_listen+0x10f>
jmp    1308 <net_listen+0x158>
cmpl   $0x0,-0x10(%rbp)
jge    12ef <net_listen+0x13f>
mov    0x2cf0(%rip),%rax
mov    (%rax),%rax
mov    0x2cce(%rip),%rcx
cmp    (%rcx),%rax
jne    12ef <net_listen+0x13f>
lea    0xd27(%rip),%rdi
call   10b0 <net_sys_err@plt>
mov    -0xc(%rbp),%edi
call   1050 <close@plt>
mov    -0x18(%rbp),%rax
mov    0x18(%rax),%rax
mov    %rax,-0x18(%rbp)
jmp    1260 <net_listen+0xb0>
cmpq   $0x0,-0x18(%rbp)
jne    1338 <net_listen+0x188>
mov    0x2cb6(%rip),%rax
mov    (%rax),%edi
lea    0xcf3(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    0x2c87(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    -0x20(%rbp),%rdi
call   10c0 <freeaddrinfo@plt>
mov    -0xc(%rbp),%edi
mov    $0x80,%esi
call   1070 <listen@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jge    1367 <net_listen+0x1b7>
lea    0xcc4(%rip),%rdi
call   10b0 <net_sys_err@plt>
mov    -0xc(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<net_listen>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x30,%rsp
mov    %rdi,%rbx
movq   $0x0,(%rsp)
lea    0x8(%rsp),%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1040 <memset@plt>
mov    0x2dfe(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x1c(%rsp)
mov    0x2dd9(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x18(%rsp)
mov    0x2dec(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x28(%rsp)
movq   $0x0,0x10(%rsp)
mov    %rsp,%rcx
xor    %edi,%edi
mov    %rbx,%rsi
mov    %r14,%rdx
call   10a0 <getaddrinfo@plt>
test   %eax,%eax
jne    12f3 <net_listen+0x143>
mov    (%rsp),%rbx
test   %rbx,%rbx
je     12ce <net_listen+0x11e>
mov    0x2d89(%rip),%r15
mov    0x2d6a(%rip),%r12
lea    0xdcc(%rip),%r14
jmp    1260 <net_listen+0xb0>
nopw   0x0(%rax,%rax,1)
mov    %ebp,%edi
call   1050 <close@plt>
mov    0x18(%rbx),%rbx
test   %rbx,%rbx
je     12ce <net_listen+0x11e>
mov    0x14(%rbx),%edi
mov    0x10(%rbx),%esi
mov    0x8(%rbx),%rdx
call   10d0 <socket@plt>
test   %eax,%eax
js     1257 <net_listen+0xa7>
mov    %eax,%ebp
mov    (%rbx),%edx
mov    0x4(%rbx),%esi
mov    %eax,%edi
call   1080 <bind@plt>
test   %eax,%eax
je     129a <net_listen+0xea>
jns    1250 <net_listen+0xa0>
mov    (%r15),%rax
cmp    (%r12),%rax
jne    1250 <net_listen+0xa0>
mov    %r14,%rdi
call   10b0 <net_sys_err@plt>
jmp    1250 <net_listen+0xa0>
mov    (%rsp),%rdi
call   10c0 <freeaddrinfo@plt>
mov    %ebp,%edi
mov    $0x80,%esi
call   1070 <listen@plt>
test   %eax,%eax
jns    12bf <net_listen+0x10f>
lea    0xd6c(%rip),%rdi
call   10b0 <net_sys_err@plt>
mov    %ebp,%eax
add    $0x30,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2cfb(%rip),%rax
mov    (%rax),%edi
lea    0xd38(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    0x2ccc(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    0x2cd6(%rip),%rcx
mov    (%rcx),%ebx
mov    %eax,%edi
call   1030 <gai_strerror@plt>
lea    0xcf6(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   1060 <fprintf@plt>
jmp    12e5 <net_listen+0x135>

LLVM IR (-O0):
%struct.addrinfo = type { i32, i32, i64, i32, i32, %struct.addrinfo*, i32 }
@AF_UNSPEC = global i32 0
@SOCK_STREAM = global i32 0
@AI_PASSIVE = global i32 0
@stderr = global i32 0
@.str = [17 x i8] c"getaddrinfo: %s\0A\00"
@EXIT_FAILURE = global i32 0
@errno = global i64 0
@EPERM = global i64 0
@.str.1 = [5 x i8] c"bind\00"
@.str.2 = [16 x i8] c"Could not bind\0A\00"
@.str.3 = [7 x i8] c"listen\00"
define i32 @net_listen(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.addrinfo*
%6 = alloca %struct.addrinfo*
%7 = alloca %struct.addrinfo
store i8* %0, i8** %2
store %struct.addrinfo* null, %struct.addrinfo** %6
%8 = call i32 @memset(%struct.addrinfo* %7, i32 0, i32 40)
%9 = load i32, i32* @AF_UNSPEC
%10 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %7, i32 0, i32 4
store i32 %9, i32* %10
%11 = load i32, i32* @SOCK_STREAM
%12 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %7, i32 0, i32 3
store i32 %11, i32* %12
%13 = load i32, i32* @AI_PASSIVE
%14 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %7, i32 0, i32 6
store i32 %13, i32* %14
%15 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %7, i32 0, i32 2
store i64 0, i64* %15
%16 = load i8*, i8** %2
%17 = call i32 @getaddrinfo(i32* null, i8* %16, %struct.addrinfo* %7, %struct.addrinfo** %6)
store i32 %17, i32* %4
%18 = load i32, i32* %4
%19 = icmp ne i32 %18, 0
br i1 %19, label %20, label %27
20: ; preds = %1
%21 = load i32, i32* @stderr
%22 = load i32, i32* %4
%23 = call i8* @gai_strerror(i32 %22)
%24 = call i32 (i32, i8*, ...) @fprintf(i32 %21, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* %23)
%25 = load i32, i32* @EXIT_FAILURE
%26 = call i32 @exit(i32 %25)
unreachable
27: ; preds = %1
%28 = load %struct.addrinfo*, %struct.addrinfo** %6
store %struct.addrinfo* %28, %struct.addrinfo** %5
br label %29
29: ; preds = %70, %27
%30 = load %struct.addrinfo*, %struct.addrinfo** %5
%31 = icmp ne %struct.addrinfo* %30, null
br i1 %31, label %32, label %74
32: ; preds = %29
%33 = load %struct.addrinfo*, %struct.addrinfo** %5
%34 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %33, i32 0, i32 4
%35 = load i32, i32* %34
%36 = load %struct.addrinfo*, %struct.addrinfo** %5
%37 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %36, i32 0, i32 3
%38 = load i32, i32* %37
%39 = load %struct.addrinfo*, %struct.addrinfo** %5
%40 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %39, i32 0, i32 2
%41 = load i64, i64* %40
%42 = call i32 @socket(i32 %35, i32 %38, i64 %41)
store i32 %42, i32* %3
%43 = load i32, i32* %3
%44 = icmp slt i32 %43, 0
br i1 %44, label %45, label %46
45: ; preds = %32
br label %70
46: ; preds = %32
%47 = load i32, i32* %3
%48 = load %struct.addrinfo*, %struct.addrinfo** %5
%49 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %48, i32 0, i32 1
%50 = load i32, i32* %49
%51 = load %struct.addrinfo*, %struct.addrinfo** %5
%52 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %51, i32 0, i32 0
%53 = load i32, i32* %52
%54 = call i32 @bind(i32 %47, i32 %50, i32 %53)
store i32 %54, i32* %4
%55 = load i32, i32* %4
%56 = icmp ne i32 %55, 0
br i1 %56, label %58, label %57
57: ; preds = %46
br label %74
58: ; preds = %46
%59 = load i32, i32* %4
%60 = icmp slt i32 %59, 0
br i1 %60, label %61, label %67
61: ; preds = %58
%62 = load i64, i64* @errno
%63 = load i64, i64* @EPERM
%64 = icmp eq i64 %62, %63
br i1 %64, label %65, label %67
65: ; preds = %61
%66 = call i32 @net_sys_err(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %67
67: ; preds = %65, %61, %58
%68 = load i32, i32* %3
%69 = call i32 @close(i32 %68)
br label %70
70: ; preds = %67, %45
%71 = load %struct.addrinfo*, %struct.addrinfo** %5
%72 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %71, i32 0, i32 5
%73 = load %struct.addrinfo*, %struct.addrinfo** %72
store %struct.addrinfo* %73, %struct.addrinfo** %5
br label %29
74: ; preds = %57, %29
%75 = load %struct.addrinfo*, %struct.addrinfo** %5
%76 = icmp ne %struct.addrinfo* %75, null
br i1 %76, label %82, label %77
77: ; preds = %74
%78 = load i32, i32* @stderr
%79 = call i32 (i32, i8*, ...) @fprintf(i32 %78, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
%80 = load i32, i32* @EXIT_FAILURE
%81 = call i32 @exit(i32 %80)
unreachable
82: ; preds = %74
%83 = load %struct.addrinfo*, %struct.addrinfo** %6
%84 = call i32 @freeaddrinfo(%struct.addrinfo* %83)
%85 = load i32, i32* %3
%86 = call i32 @listen(i32 %85, i32 128)
store i32 %86, i32* %4
%87 = load i32, i32* %4
%88 = icmp slt i32 %87, 0
br i1 %88, label %89, label %91
89: ; preds = %82
%90 = call i32 @net_sys_err(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
br label %91
91: ; preds = %89, %82
%92 = load i32, i32* %3
ret i32 %92
}

LLVM IR (-O2):
%struct.addrinfo = type { i32, i32, i64, i32, i32, %struct.addrinfo*, i32 }
@AF_UNSPEC = global i32 0
@SOCK_STREAM = global i32 0
@AI_PASSIVE = global i32 0
@stderr = global i32 0
@.str = [17 x i8] c"getaddrinfo: %s\0A\00"
@EXIT_FAILURE = global i32 0
@errno = global i64 0
@EPERM = global i64 0
@.str.1 = [5 x i8] c"bind\00"
@.str.2 = [16 x i8] c"Could not bind\0A\00"
@.str.3 = [7 x i8] c"listen\00"
define i32 @net_listen(i8* %0) {
%2 = alloca %struct.addrinfo*
%3 = alloca %struct.addrinfo
%4 = bitcast %struct.addrinfo** %2 to i8*
store %struct.addrinfo* null, %struct.addrinfo** %2
%5 = bitcast %struct.addrinfo* %3 to i8*
%6 = call i32 @memset(%struct.addrinfo* %3, i32 0, i32 40)
%7 = load i32, i32* @AF_UNSPEC
%8 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 4
store i32 %7, i32* %8
%9 = load i32, i32* @SOCK_STREAM
%10 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 3
store i32 %9, i32* %10
%11 = load i32, i32* @AI_PASSIVE
%12 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 6
store i32 %11, i32* %12
%13 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 2
store i64 0, i64* %13
%14 = call i32 @getaddrinfo(i32* null, i8* %0, %struct.addrinfo* %3, %struct.addrinfo** %2)
%15 = icmp eq i32 %14, 0
br i1 %15, label %16, label %19
16: ; preds = %1
%17 = load %struct.addrinfo*, %struct.addrinfo** %2
%18 = icmp eq %struct.addrinfo* %17, null
br i1 %18, label %56, label %25
19: ; preds = %1
%20 = load i32, i32* @stderr
%21 = call i8* @gai_strerror(i32 %14)
%22 = call i32 (i32, i8*, ...) @fprintf(i32 %20, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* %21)
%23 = load i32, i32* @EXIT_FAILURE
%24 = call i32 @exit(i32 %23)
unreachable
25: ; preds = %16, %52
%26 = phi %struct.addrinfo* [ %54, %52 ], [ %17, %16 ]
%27 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %26, i64 0, i32 4
%28 = load i32, i32* %27
%29 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %26, i64 0, i32 3
%30 = load i32, i32* %29
%31 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %26, i64 0, i32 2
%32 = load i64, i64* %31
%33 = call i32 @socket(i32 %28, i32 %30, i64 %32)
%34 = icmp slt i32 %33, 0
br i1 %34, label %52, label %35
35: ; preds = %25
%36 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %26, i64 0, i32 1
%37 = load i32, i32* %36
%38 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %26, i64 0, i32 0
%39 = load i32, i32* %38
%40 = call i32 @bind(i32 %33, i32 %37, i32 %39)
%41 = icmp eq i32 %40, 0
br i1 %41, label %61, label %42
42: ; preds = %35
%43 = icmp slt i32 %40, 0
br i1 %43, label %44, label %50
44: ; preds = %42
%45 = load i64, i64* @errno
%46 = load i64, i64* @EPERM
%47 = icmp eq i64 %45, %46
br i1 %47, label %48, label %50
48: ; preds = %44
%49 = call i32 @net_sys_err(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %50
50: ; preds = %48, %44, %42
%51 = call i32 @close(i32 %33)
br label %52
52: ; preds = %25, %50
%53 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %26, i64 0, i32 5
%54 = load %struct.addrinfo*, %struct.addrinfo** %53
%55 = icmp eq %struct.addrinfo* %54, null
br i1 %55, label %56, label %25
56: ; preds = %52, %16
%57 = load i32, i32* @stderr
%58 = call i32 (i32, i8*, ...) @fprintf(i32 %57, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
%59 = load i32, i32* @EXIT_FAILURE
%60 = call i32 @exit(i32 %59)
unreachable
61: ; preds = %35
%62 = load %struct.addrinfo*, %struct.addrinfo** %2
%63 = call i32 @freeaddrinfo(%struct.addrinfo* %62)
%64 = call i32 @listen(i32 %33, i32 128)
%65 = icmp slt i32 %64, 0
br i1 %65, label %66, label %68
66: ; preds = %61
%67 = call i32 @net_sys_err(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
br label %68
68: ; preds = %66, %61
ret i32 %33
}