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

/* Forward declarations */

/* Type definitions */
struct addrinfo {
  int ai_family;
  int /*<<< orphan*/ ai_addrlen;
  int /*<<< orphan*/ ai_addr;
  int /*<<< orphan*/ ai_protocol;
  int /*<<< orphan*/ ai_socktype;
  struct addrinfo *ai_next;
  int /*<<< orphan*/ ai_flags;
};
typedef int /*<<< orphan*/ hints;

/* Variables and functions */
int AF_INET6;
int /*<<< orphan*/ AI_PASSIVE;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
int /*<<< orphan*/ gai_strerror(int);
int getaddrinfo(char *, char *, struct addrinfo *, struct addrinfo **);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
int /*<<< orphan*/ rr_net_error(char *, char *, ...);
int rr_net_listen(char *, int, int /*<<< orphan*/, int /*<<< orphan*/, int);
int rr_net_only_inet6(char *, int);
int rr_net_reuseaddr(char *, int);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
int socket(int, int /*<<< orphan*/, int /*<<< orphan*/);
int rr_net_tcpserver(char *err, int port, char *bindaddr, int af, int backlog) {
  int s, error;
  char _port[6];
  struct addrinfo hints, *servinfo, *p;
  snprintf(_port, 6, "%d", port);
  memset(&hints, 0, sizeof(hints));
  hints.ai_family = af;
  hints.ai_socktype = SOCK_STREAM;
  hints.ai_flags = AI_PASSIVE;
  if ((error = getaddrinfo(bindaddr, _port, &hints, &servinfo)) != 0) {
    rr_net_error(err, "%s", gai_strerror(error));
    return -1;
  }
  for (p = servinfo; p != NULL; p = p->ai_next) {
    if ((s = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1)
      continue;
    if (af == AF_INET6 && rr_net_only_inet6(err, s) == -1)
      goto error;
    if (rr_net_reuseaddr(err, s) == -1)
      goto error;
    if (rr_net_listen(err, s, p->ai_addr, p->ai_addrlen, backlog) == -1)
      goto error;
    goto end;
  }
  if (p == NULL) {
    rr_net_error(err, "failed to bind socket");
    goto error;
  }
error:
  close(s);
  s = -1;
end:
  freeaddrinfo(servinfo);
  return s;
}


x86 assembly (clang, -O0):
<rr_net_tcpserver>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %ecx,-0x24(%rbp)
mov    %r8d,-0x28(%rbp)
lea    -0x36(%rbp),%rdi
mov    -0x14(%rbp),%ecx
mov    $0x6,%esi
lea    0xe20(%rip),%rdx
call   1040 <snprintf@plt>
lea    -0x60(%rbp),%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1060 <memset@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x60(%rbp)
mov    0x2dce(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x50(%rbp)
mov    0x2dda(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x40(%rbp)
mov    -0x20(%rbp),%rdi
lea    -0x36(%rbp),%rsi
lea    -0x60(%rbp),%rdx
lea    -0x68(%rbp),%rcx
call   10b0 <getaddrinfo@plt>
mov    %eax,-0x30(%rbp)
cmp    $0x0,%eax
je     1264 <rr_net_tcpserver+0xb4>
mov    -0x10(%rbp),%rax
mov    %rax,-0x78(%rbp)
mov    -0x30(%rbp),%edi
call   1050 <gai_strerror@plt>
mov    -0x78(%rbp),%rdi
mov    %eax,%edx
lea    0xdb2(%rip),%rsi
mov    $0x0,%al
call   1090 <rr_net_error@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    136e <rr_net_tcpserver+0x1be>
mov    -0x68(%rbp),%rax
mov    %rax,-0x70(%rbp)
cmpq   $0x0,-0x70(%rbp)
je     1329 <rr_net_tcpserver+0x179>
mov    -0x70(%rbp),%rax
mov    (%rax),%edi
mov    -0x70(%rbp),%rax
mov    0x10(%rax),%esi
mov    -0x70(%rbp),%rax
mov    0xc(%rax),%edx
call   10d0 <socket@plt>
mov    %eax,-0x2c(%rbp)
cmp    $0xffffffff,%eax
jne    12a1 <rr_net_tcpserver+0xf1>
jmp    1318 <rr_net_tcpserver+0x168>
mov    -0x24(%rbp),%eax
mov    0x2d35(%rip),%rcx
cmp    (%rcx),%eax
jne    12cd <rr_net_tcpserver+0x11d>
mov    -0x10(%rbp),%rdi
mov    -0x2c(%rbp),%esi
call   10a0 <rr_net_only_inet6@plt>
cmp    $0xffffffff,%eax
jne    12cd <rr_net_tcpserver+0x11d>
jmp    1350 <rr_net_tcpserver+0x1a0>
mov    -0x10(%rbp),%rdi
mov    -0x2c(%rbp),%esi
call   1030 <rr_net_reuseaddr@plt>
cmp    $0xffffffff,%eax
jne    12e7 <rr_net_tcpserver+0x137>
jmp    1350 <rr_net_tcpserver+0x1a0>
mov    -0x10(%rbp),%rdi
mov    -0x2c(%rbp),%esi
mov    -0x70(%rbp),%rax
mov    0x8(%rax),%edx
mov    -0x70(%rbp),%rax
mov    0x4(%rax),%ecx
mov    -0x28(%rbp),%r8d
call   1080 <rr_net_listen@plt>
cmp    $0xffffffff,%eax
jne    1313 <rr_net_tcpserver+0x163>
jmp    1350 <rr_net_tcpserver+0x1a0>
jmp    135f <rr_net_tcpserver+0x1af>
mov    -0x70(%rbp),%rax
mov    0x18(%rax),%rax
mov    %rax,-0x70(%rbp)
jmp    126c <rr_net_tcpserver+0xbc>
cmpq   $0x0,-0x70(%rbp)
jne    134b <rr_net_tcpserver+0x19b>
mov    -0x10(%rbp),%rdi
lea    0xcc7(%rip),%rsi
mov    $0x0,%al
call   1090 <rr_net_error@plt>
jmp    1350 <rr_net_tcpserver+0x1a0>
jmp    1350 <rr_net_tcpserver+0x1a0>
mov    -0x2c(%rbp),%edi
call   1070 <close@plt>
movl   $0xffffffff,-0x2c(%rbp)
mov    -0x68(%rbp),%rdi
call   10c0 <freeaddrinfo@plt>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rr_net_tcpserver>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    %r8d,%r14d
mov    %ecx,%r12d
mov    %rdx,%rbx
mov    %esi,%ecx
mov    %rdi,%r15
lea    0xe2d(%rip),%rdx
lea    0xa(%rsp),%r13
mov    %r13,%rdi
mov    $0x6,%esi
call   1040 <snprintf@plt>
lea    0x10(%rsp),%rbp
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1060 <memset@plt>
mov    %r12d,0x10(%rsp)
mov    0x2dcb(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x20(%rsp)
mov    0x2dd6(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x30(%rsp)
mov    %rsp,%rcx
mov    %rbx,%rdi
mov    %r13,%rsi
mov    %rbp,%rdx
call   10b0 <getaddrinfo@plt>
test   %eax,%eax
je     124e <rr_net_tcpserver+0x9e>
mov    %eax,%edi
call   1050 <gai_strerror@plt>
lea    0xdc8(%rip),%rsi
mov    %r15,%rdi
mov    %eax,%edx
xor    %eax,%eax
call   1090 <rr_net_error@plt>
mov    $0xffffffff,%ebx
jmp    12a7 <rr_net_tcpserver+0xf7>
mov    (%rsp),%rbp
test   %rbp,%rbp
je     127c <rr_net_tcpserver+0xcc>
nopw   0x0(%rax,%rax,1)
mov    0x10(%rbp),%esi
mov    0x0(%rbp),%edi
mov    0xc(%rbp),%edx
call   10d0 <socket@plt>
cmp    $0xffffffff,%eax
jne    12b8 <rr_net_tcpserver+0x108>
mov    0x18(%rbp),%rbp
test   %rbp,%rbp
jne    1260 <rr_net_tcpserver+0xb0>
lea    0xd83(%rip),%rsi
mov    %r15,%rdi
xor    %eax,%eax
call   1090 <rr_net_error@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%edi
call   1070 <close@plt>
mov    $0xffffffff,%ebx
mov    (%rsp),%rdi
call   10c0 <freeaddrinfo@plt>
mov    %ebx,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %eax,%ebx
mov    0x2d1f(%rip),%rax
cmp    %r12d,(%rax)
jne    12d5 <rr_net_tcpserver+0x125>
mov    %r15,%rdi
mov    %ebx,%esi
call   10a0 <rr_net_only_inet6@plt>
cmp    $0xffffffff,%eax
je     1292 <rr_net_tcpserver+0xe2>
mov    %r15,%rdi
mov    %ebx,%esi
call   1030 <rr_net_reuseaddr@plt>
cmp    $0xffffffff,%eax
je     1292 <rr_net_tcpserver+0xe2>
mov    0x4(%rbp),%ecx
mov    0x8(%rbp),%edx
mov    %r15,%rdi
mov    %ebx,%esi
mov    %r14d,%r8d
call   1080 <rr_net_listen@plt>
cmp    $0xffffffff,%eax
jne    129e <rr_net_tcpserver+0xee>
jmp    1292 <rr_net_tcpserver+0xe2>

LLVM IR (-O0):
%struct.addrinfo = type { i32, i32, i32, i32, i32, %struct.addrinfo*, i32 }
@.str = [3 x i8] c"%d\00"
@SOCK_STREAM = global i32 0
@AI_PASSIVE = global i32 0
@.str.1 = [3 x i8] c"%s\00"
@AF_INET6 = global i32 0
@.str.2 = [22 x i8] c"failed to bind socket\00"
define i32 @rr_net_tcpserver(i8* %0, i32 %1, i8* %2, i32 %3, i32 %4) {
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca [6 x i8]
%15 = alloca %struct.addrinfo
%16 = alloca %struct.addrinfo*
%17 = alloca %struct.addrinfo*
store i8* %0, i8** %7
store i32 %1, i32* %8
store i8* %2, i8** %9
store i32 %3, i32* %10
store i32 %4, i32* %11
%18 = getelementptr inbounds [6 x i8], [6 x i8]* %14, i64 0, i64 0
%19 = load i32, i32* %8
%20 = call i32 @snprintf(i8* %18, i32 6, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %19)
%21 = call i32 @memset(%struct.addrinfo* %15, i32 0, i32 40)
%22 = load i32, i32* %10
%23 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %15, i32 0, i32 0
store i32 %22, i32* %23
%24 = load i32, i32* @SOCK_STREAM
%25 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %15, i32 0, i32 4
store i32 %24, i32* %25
%26 = load i32, i32* @AI_PASSIVE
%27 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %15, i32 0, i32 6
store i32 %26, i32* %27
%28 = load i8*, i8** %9
%29 = getelementptr inbounds [6 x i8], [6 x i8]* %14, i64 0, i64 0
%30 = call i32 @getaddrinfo(i8* %28, i8* %29, %struct.addrinfo* %15, %struct.addrinfo** %16)
store i32 %30, i32* %13
%31 = icmp ne i32 %30, 0
br i1 %31, label %32, label %37
32: ; preds = %5
%33 = load i8*, i8** %7
%34 = load i32, i32* %13
%35 = call i32 @gai_strerror(i32 %34)
%36 = call i32 (i8*, i8*, ...) @rr_net_error(i8* %33, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 %35)
store i32 -1, i32* %6
br label %103
37: ; preds = %5
%38 = load %struct.addrinfo*, %struct.addrinfo** %16
store %struct.addrinfo* %38, %struct.addrinfo** %17
br label %39
39: ; preds = %85, %37
%40 = load %struct.addrinfo*, %struct.addrinfo** %17
%41 = icmp ne %struct.addrinfo* %40, null
br i1 %41, label %42, label %89
42: ; preds = %39
%43 = load %struct.addrinfo*, %struct.addrinfo** %17
%44 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %43, i32 0, i32 0
%45 = load i32, i32* %44
%46 = load %struct.addrinfo*, %struct.addrinfo** %17
%47 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %46, i32 0, i32 4
%48 = load i32, i32* %47
%49 = load %struct.addrinfo*, %struct.addrinfo** %17
%50 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %49, i32 0, i32 3
%51 = load i32, i32* %50
%52 = call i32 @socket(i32 %45, i32 %48, i32 %51)
store i32 %52, i32* %12
%53 = icmp eq i32 %52, -1
br i1 %53, label %54, label %55
54: ; preds = %42
br label %85
55: ; preds = %42
%56 = load i32, i32* %10
%57 = load i32, i32* @AF_INET6
%58 = icmp eq i32 %56, %57
br i1 %58, label %59, label %65
59: ; preds = %55
%60 = load i8*, i8** %7
%61 = load i32, i32* %12
%62 = call i32 @rr_net_only_inet6(i8* %60, i32 %61)
%63 = icmp eq i32 %62, -1
br i1 %63, label %64, label %65
64: ; preds = %59
br label %96
65: ; preds = %59, %55
%66 = load i8*, i8** %7
%67 = load i32, i32* %12
%68 = call i32 @rr_net_reuseaddr(i8* %66, i32 %67)
%69 = icmp eq i32 %68, -1
br i1 %69, label %70, label %71
70: ; preds = %65
br label %96
71: ; preds = %65
%72 = load i8*, i8** %7
%73 = load i32, i32* %12
%74 = load %struct.addrinfo*, %struct.addrinfo** %17
%75 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %74, i32 0, i32 2
%76 = load i32, i32* %75
%77 = load %struct.addrinfo*, %struct.addrinfo** %17
%78 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %77, i32 0, i32 1
%79 = load i32, i32* %78
%80 = load i32, i32* %11
%81 = call i32 @rr_net_listen(i8* %72, i32 %73, i32 %76, i32 %79, i32 %80)
%82 = icmp eq i32 %81, -1
br i1 %82, label %83, label %84
83: ; preds = %71
br label %96
84: ; preds = %71
br label %99
85: ; preds = %54
%86 = load %struct.addrinfo*, %struct.addrinfo** %17
%87 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %86, i32 0, i32 5
%88 = load %struct.addrinfo*, %struct.addrinfo** %87
store %struct.addrinfo* %88, %struct.addrinfo** %17
br label %39
89: ; preds = %39
%90 = load %struct.addrinfo*, %struct.addrinfo** %17
%91 = icmp eq %struct.addrinfo* %90, null
br i1 %91, label %92, label %95
92: ; preds = %89
%93 = load i8*, i8** %7
%94 = call i32 (i8*, i8*, ...) @rr_net_error(i8* %93, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0))
br label %96
95: ; preds = %89
br label %96
96: ; preds = %95, %92, %83, %70, %64
%97 = load i32, i32* %12
%98 = call i32 @close(i32 %97)
store i32 -1, i32* %12
br label %99
99: ; preds = %96, %84
%100 = load %struct.addrinfo*, %struct.addrinfo** %16
%101 = call i32 @freeaddrinfo(%struct.addrinfo* %100)
%102 = load i32, i32* %12
store i32 %102, i32* %6
br label %103
103: ; preds = %99, %32
%104 = load i32, i32* %6
ret i32 %104
}

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