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

/* Variables and functions */
int /*<<< orphan*/ AI_PASSIVE;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEADDR;
scalar_t__ bind(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ error(char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
char *gai_strerror(int);
int getaddrinfo(int /*<<< orphan*/ *, char const *, struct addrinfo *,
                struct addrinfo **);
int /*<<< orphan*/ memset(struct addrinfo *, char, int);
int /*<<< orphan*/ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/,
                              int *, int);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
void bind_forward(int fsock, int af, int tcp, const char *portstr) {
  int ret;
  struct addrinfo hints, *res, *reslist;
  memset(&hints, '\0', sizeof(hints));
  hints.ai_family = af;
  hints.ai_flags = AI_PASSIVE;
  hints.ai_socktype = tcp ? SOCK_STREAM : SOCK_DGRAM;
  if ((ret = getaddrinfo(NULL, portstr, &hints, &reslist))) {
    fprintf(stderr, "getaddrinfo(): %s\n", gai_strerror(ret));
    error("Impossible to resolve listening port.");
  }
  for (res = reslist; res; res = res->ai_next) {
    int one = 1;
    setsockopt(fsock, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
    if (bind(fsock, res->ai_addr, res->ai_addrlen) < 0) {
      fprintf(stderr, "bind(): %s", strerror(errno));
      close(fsock);
      continue;
    }
  }
}


x86 assembly (clang, -O0):
<bind_forward>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %edx,-0xc(%rbp)
mov    %rcx,-0x18(%rbp)
lea    -0x40(%rbp),%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1050 <memset@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x40(%rbp)
mov    0x2e1e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0xc(%rbp)
je     11e2 <bind_forward+0x52>
mov    0x2de8(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x58(%rbp)
jmp    11ee <bind_forward+0x5e>
mov    0x2def(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x58(%rbp)
mov    -0x58(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0x18(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edi
lea    -0x40(%rbp),%rdx
lea    -0x50(%rbp),%rcx
call   10a0 <getaddrinfo@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
je     1247 <bind_forward+0xb7>
mov    0x2dac(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x5c(%rbp)
mov    -0x1c(%rbp),%edi
call   1040 <gai_strerror@plt>
mov    -0x5c(%rbp),%edi
mov    %rax,%rdx
lea    0xdca(%rip),%rsi
call   1070 <fprintf@plt>
lea    0xdd1(%rip),%rdi
call   1090 <error@plt>
mov    -0x50(%rbp),%rax
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x48(%rbp)
je     12f4 <bind_forward+0x164>
movl   $0x1,-0x54(%rbp)
mov    -0x4(%rbp),%edi
mov    0x2d7d(%rip),%rax
mov    (%rax),%esi
mov    0x2d3c(%rip),%rax
mov    (%rax),%edx
lea    -0x54(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    -0x4(%rbp),%edi
mov    -0x48(%rbp),%rax
mov    0x8(%rax),%esi
mov    -0x48(%rbp),%rax
mov    0x4(%rax),%edx
call   1080 <bind@plt>
cmp    $0x0,%rax
jge    12de <bind_forward+0x14e>
mov    0x2d1c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x60(%rbp)
mov    0x2d00(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    -0x60(%rbp),%edi
mov    %rax,%rdx
lea    0xd6d(%rip),%rsi
call   1070 <fprintf@plt>
mov    -0x4(%rbp),%edi
call   1060 <close@plt>
jmp    12e3 <bind_forward+0x153>
jmp    12e3 <bind_forward+0x153>
mov    -0x48(%rbp),%rax
mov    0x10(%rax),%rax
mov    %rax,-0x48(%rbp)
jmp    124f <bind_forward+0xbf>
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bind_forward>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    %rcx,%r14
mov    %edx,%r12d
mov    %esi,%ebp
mov    %edi,%r13d
lea    0x18(%rsp),%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1050 <memset@plt>
mov    %ebp,0x18(%rsp)
mov    0x2e18(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x34(%rsp)
test   %r12d,%r12d
jne    11dc <bind_forward+0x4c>
mov    0x2dfe(%rip),%rax
jmp    11e3 <bind_forward+0x53>
mov    0x2ddd(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x30(%rsp)
lea    0x10(%rsp),%rcx
xor    %edi,%edi
mov    %r14,%rsi
mov    %r15,%rdx
call   10a0 <getaddrinfo@plt>
test   %eax,%eax
je     122c <bind_forward+0x9c>
mov    0x2dc2(%rip),%rcx
mov    (%rcx),%ebp
mov    %eax,%edi
call   1040 <gai_strerror@plt>
lea    0xdea(%rip),%rsi
mov    %ebp,%edi
mov    %rax,%rdx
call   1070 <fprintf@plt>
lea    0xdec(%rip),%rdi
call   1090 <error@plt>
mov    0x10(%rsp),%rbx
test   %rbx,%rbx
je     12be <bind_forward+0x12e>
mov    0x2da7(%rip),%r12
mov    0x2d68(%rip),%r15
lea    0xc(%rsp),%r14
jmp    1259 <bind_forward+0xc9>
nop
mov    0x10(%rbx),%rbx
test   %rbx,%rbx
je     12be <bind_forward+0x12e>
movl   $0x1,0xc(%rsp)
mov    (%r12),%esi
mov    (%r15),%edx
mov    %r13d,%edi
mov    %r14,%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    0x4(%rbx),%edx
mov    0x8(%rbx),%esi
mov    %r13d,%edi
call   1080 <bind@plt>
test   %rax,%rax
jns    1250 <bind_forward+0xc0>
mov    0x2d35(%rip),%rax
mov    (%rax),%ebp
mov    0x2d1c(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    %ebp,%edi
lea    0xd8d(%rip),%rsi
mov    %rax,%rdx
call   1070 <fprintf@plt>
mov    %r13d,%edi
call   1060 <close@plt>
jmp    1250 <bind_forward+0xc0>
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.addrinfo = type { i32, i32, i32, %struct.addrinfo*, i32, i32 }
@AI_PASSIVE = global i32 0
@SOCK_STREAM = global i32 0
@SOCK_DGRAM = global i32 0
@stderr = global i32 0
@.str = [19 x i8] c"getaddrinfo(): %s\0A\00"
@.str.1 = [38 x i8] c"Impossible to resolve listening port.\00"
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.2 = [11 x i8] c"bind(): %s\00"
@errno = global i32 0
define void @bind_forward(i32 %0, i32 %1, i32 %2, i8* %3) {
%5 = alloca %struct.addrinfo
%6 = alloca %struct.addrinfo*
%7 = alloca i32
%8 = bitcast %struct.addrinfo* %5 to i8*
%9 = bitcast %struct.addrinfo** %6 to i8*
%10 = call i32 @memset(%struct.addrinfo* %5, i8 signext 0, i32 32)
%11 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %5, i64 0, i32 0
store i32 %1, i32* %11
%12 = load i32, i32* @AI_PASSIVE
%13 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %5, i64 0, i32 5
store i32 %12, i32* %13
%14 = icmp eq i32 %2, 0
%15 = load i32, i32* @SOCK_STREAM
%16 = load i32, i32* @SOCK_DGRAM
%17 = select i1 %14, i32 %16, i32 %15
%18 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %5, i64 0, i32 4
store i32 %17, i32* %18
%19 = call i32 @getaddrinfo(i32* null, i8* %3, %struct.addrinfo* %5, %struct.addrinfo** %6)
%20 = icmp eq i32 %19, 0
br i1 %20, label %26, label %21
21: ; preds = %4
%22 = load i32, i32* @stderr
%23 = call i8* @gai_strerror(i32 %19)
%24 = call i32 @fprintf(i32 %22, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8* %23)
%25 = call i32 @error(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.1, i64 0, i64 0))
br label %26
26: ; preds = %21, %4
%27 = load %struct.addrinfo*, %struct.addrinfo** %6
%28 = icmp eq %struct.addrinfo* %27, null
br i1 %28, label %52, label %29
29: ; preds = %26
%30 = bitcast i32* %7 to i8*
br label %31
31: ; preds = %29, %48
%32 = phi %struct.addrinfo* [ %27, %29 ], [ %50, %48 ]
store i32 1, i32* %7
%33 = load i32, i32* @SOL_SOCKET
%34 = load i32, i32* @SO_REUSEADDR
%35 = call i32 @setsockopt(i32 %0, i32 %33, i32 %34, i32* %7, i32 4)
%36 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %32, i64 0, i32 2
%37 = load i32, i32* %36
%38 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %32, i64 0, i32 1
%39 = load i32, i32* %38
%40 = call i64 @bind(i32 %0, i32 %37, i32 %39)
%41 = icmp slt i64 %40, 0
br i1 %41, label %42, label %48
42: ; preds = %31
%43 = load i32, i32* @stderr
%44 = load i32, i32* @errno
%45 = call i8* @strerror(i32 %44)
%46 = call i32 @fprintf(i32 %43, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i8* %45)
%47 = call i32 @close(i32 %0)
br label %48
48: ; preds = %31, %42
%49 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %32, i64 0, i32 3
%50 = load %struct.addrinfo*, %struct.addrinfo** %49
%51 = icmp eq %struct.addrinfo* %50, null
br i1 %51, label %52, label %31
52: ; preds = %48, %26
ret void
}