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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ AF_INET6;
int /*<<< orphan*/ AI_PASSIVE;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
int /*<<< orphan*/ gai_strerror(int);
int getaddrinfo(char const *, char *, struct addrinfo *, struct addrinfo **);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
int /*<<< orphan*/ net_err(char *, char *, char const *, ...);
int reuse(char *, int);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int tcp_listen(char *, int, int /*<<< orphan*/, int /*<<< orphan*/);
int tcp_server(char *err, int port, const char *bindaddr, int tcp6) {
  int fd = -1;
  int rv;
  char port_buf[6];
  struct addrinfo hints, *servinfo, *p;
  snprintf(port_buf, 6, "%d", port);
  memset(&hints, 0, sizeof hints);
  hints.ai_family = tcp6 == 1 ? AF_INET6 : AF_INET;
  hints.ai_socktype = SOCK_STREAM;
  hints.ai_flags = AI_PASSIVE;
  if ((rv = getaddrinfo(bindaddr, port_buf, &hints, &servinfo)) != 0) {
    net_err(err, "%s", gai_strerror(rv));
    return -1;
  }
  for (p = servinfo; p != NULL; p = p->ai_next) {
    if ((fd = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1)
      continue;
    if (reuse(err, fd) == -1)
      goto e;
    if (tcp_listen(err, fd, p->ai_addr, p->ai_addrlen) == -1)
      goto e;
    goto end;
  }
  if (p == NULL) {
    net_err(err, "bind to addr error: %s:%d", bindaddr, port);
    goto e;
  }
e:
  fd = -1;
end:
  freeaddrinfo(servinfo);
  return fd;
}


x86 assembly (clang, -O0):
<tcp_server>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %ecx,-0x24(%rbp)
movl   $0xffffffff,-0x28(%rbp)
lea    -0x32(%rbp),%rdi
mov    -0x14(%rbp),%ecx
mov    $0x6,%esi
lea    0xe57(%rip),%rdx
call   1030 <snprintf@plt>
lea    -0x60(%rbp),%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1050 <memset@plt>
cmpl   $0x1,-0x24(%rbp)
jne    11f3 <tcp_server+0x63>
mov    0x2def(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x74(%rbp)
jmp    11ff <tcp_server+0x6f>
mov    0x2de6(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x74(%rbp)
mov    -0x74(%rbp),%eax
mov    %eax,-0x50(%rbp)
mov    0x2dbc(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x54(%rbp)
mov    0x2dd0(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x40(%rbp)
mov    -0x20(%rbp),%rdi
lea    -0x32(%rbp),%rsi
lea    -0x60(%rbp),%rdx
lea    -0x68(%rbp),%rcx
call   1090 <getaddrinfo@plt>
mov    %eax,-0x2c(%rbp)
cmp    $0x0,%eax
je     126f <tcp_server+0xdf>
mov    -0x10(%rbp),%rax
mov    %rax,-0x80(%rbp)
mov    -0x2c(%rbp),%edi
call   1040 <gai_strerror@plt>
mov    -0x80(%rbp),%rdi
movslq %eax,%rdx
lea    0xda4(%rip),%rsi
mov    $0x0,%al
call   1080 <net_err@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1348 <tcp_server+0x1b8>
mov    -0x68(%rbp),%rax
mov    %rax,-0x70(%rbp)
cmpq   $0x0,-0x70(%rbp)
je     1304 <tcp_server+0x174>
mov    -0x70(%rbp),%rax
mov    0x10(%rax),%edi
mov    -0x70(%rbp),%rax
mov    0xc(%rax),%esi
mov    -0x70(%rbp),%rax
mov    0x8(%rax),%edx
call   10b0 <socket@plt>
mov    %eax,-0x28(%rbp)
cmp    $0xffffffff,%eax
jne    12ad <tcp_server+0x11d>
jmp    12f3 <tcp_server+0x163>
mov    -0x10(%rbp),%rdi
mov    -0x28(%rbp),%esi
call   1070 <reuse@plt>
cmp    $0xffffffff,%eax
jne    12c7 <tcp_server+0x137>
jmp    1332 <tcp_server+0x1a2>
mov    -0x10(%rbp),%rdi
mov    -0x28(%rbp),%esi
mov    -0x70(%rbp),%rax
mov    0x4(%rax),%edx
mov    -0x70(%rbp),%rax
mov    (%rax),%ecx
call   1060 <tcp_listen@plt>
cmp    $0xffffffff,%eax
jne    12ee <tcp_server+0x15e>
jmp    1332 <tcp_server+0x1a2>
jmp    1339 <tcp_server+0x1a9>
mov    -0x70(%rbp),%rax
mov    0x18(%rax),%rax
mov    %rax,-0x70(%rbp)
jmp    1277 <tcp_server+0xe7>
cmpq   $0x0,-0x70(%rbp)
jne    132d <tcp_server+0x19d>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    -0x14(%rbp),%ecx
lea    0xce2(%rip),%rsi
mov    $0x0,%al
call   1080 <net_err@plt>
jmp    1332 <tcp_server+0x1a2>
jmp    1332 <tcp_server+0x1a2>
movl   $0xffffffff,-0x28(%rbp)
mov    -0x68(%rbp),%rdi
call   10a0 <freeaddrinfo@plt>
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tcp_server>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    %ecx,%ebx
mov    %rdx,%r15
mov    %esi,%r12d
mov    %rdi,%r14
lea    0xe6a(%rip),%rdx
lea    0xa(%rsp),%r13
mov    %r13,%rdi
mov    $0x6,%esi
mov    %r12d,%ecx
call   1030 <snprintf@plt>
lea    0x10(%rsp),%rbp
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1050 <memset@plt>
cmp    $0x1,%ebx
jne    11e7 <tcp_server+0x57>
mov    0x2df3(%rip),%rax
jmp    11ee <tcp_server+0x5e>
mov    0x2df2(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x20(%rsp)
mov    0x2dcd(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x1c(%rsp)
mov    0x2de0(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x30(%rsp)
mov    %rsp,%rcx
mov    %r15,%rdi
mov    %r13,%rsi
mov    %rbp,%rdx
call   1090 <getaddrinfo@plt>
test   %eax,%eax
je     1245 <tcp_server+0xb5>
mov    %eax,%edi
call   1040 <gai_strerror@plt>
movslq %eax,%rdx
lea    0xdcc(%rip),%rsi
mov    %r14,%rdi
xor    %eax,%eax
call   1080 <net_err@plt>
mov    $0xffffffff,%ebx
jmp    1291 <tcp_server+0x101>
mov    (%rsp),%rbp
test   %rbp,%rbp
je     126c <tcp_server+0xdc>
xchg   %ax,%ax
mov    0x10(%rbp),%edi
mov    0x8(%rbp),%edx
mov    0xc(%rbp),%esi
call   10b0 <socket@plt>
cmp    $0xffffffff,%eax
jne    12a2 <tcp_server+0x112>
mov    0x18(%rbp),%rbp
test   %rbp,%rbp
jne    1250 <tcp_server+0xc0>
lea    0xd90(%rip),%rsi
mov    %r14,%rdi
mov    %r15,%rdx
mov    %r12d,%ecx
xor    %eax,%eax
call   1080 <net_err@plt>
mov    $0xffffffff,%ebx
mov    (%rsp),%rdi
call   10a0 <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    %r14,%rdi
mov    %eax,%esi
call   1070 <reuse@plt>
cmp    $0xffffffff,%eax
je     1283 <tcp_server+0xf3>
mov    0x0(%rbp),%ecx
mov    0x4(%rbp),%edx
mov    %r14,%rdi
mov    %ebx,%esi
call   1060 <tcp_listen@plt>
cmp    $0xffffffff,%eax
jne    1288 <tcp_server+0xf8>
jmp    1283 <tcp_server+0xf3>

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

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