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

/* Variables and functions */
int /*<<< orphan*/ AF_INET6;
int AI_ALL;
int AI_PASSIVE;
int AI_V4MAPPED;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEADDR;
scalar_t__ bind(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
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 /*<<< orphan*/ listen(int, int);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
int /*<<< orphan*/ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/,
                              int *, int);
int socket(int /*<<< orphan*/, int, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int j_socket_bind(int type, char *port) {
  struct addrinfo hints, *res, *ressave;
  int n, sockfd;
  int sz = 1;
  memset(&hints, 0, sizeof(struct addrinfo));
  hints.ai_flags = AI_PASSIVE | AI_V4MAPPED | AI_ALL;
  hints.ai_family = AF_INET6;
  hints.ai_socktype = type;
  n = getaddrinfo(NULL, port, &hints, &res);
  if (n < 0) {
    if (0)
      fprintf(stderr, "getaddrinfo error:: [%s]\n", gai_strerror(n));
    return -1;
  }
  ressave = res;
  sockfd = -1;
  while (res) {
    sockfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
    if (!(sockfd < 0)) {
      setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &sz, sizeof sz);
      if (bind(sockfd, res->ai_addr, res->ai_addrlen) == 0)
        break;
      close(sockfd);
      sockfd = -1;
    }
    res = res->ai_next;
  }
  if (sockfd < 0) {
    freeaddrinfo(ressave);
    if (0)
      fprintf(stderr, "socket error:: could not open socket\n");
    return -1;
  }
  listen(sockfd, 5);
  freeaddrinfo(ressave);
  return sockfd;
}


x86 assembly (clang, -O0):
<j_socket_bind>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x1,-0x4c(%rbp)
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1040 <memset@plt>
mov    0x2e2b(%rip),%rax
mov    (%rax),%eax
mov    0x2e2a(%rip),%rcx
or     (%rcx),%eax
mov    0x2e01(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x30(%rbp)
mov    0x2e05(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x10(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edi
lea    -0x30(%rbp),%rdx
lea    -0x38(%rbp),%rcx
call   1080 <getaddrinfo@plt>
mov    %eax,-0x44(%rbp)
cmpl   $0x0,-0x44(%rbp)
jge    1204 <j_socket_bind+0x84>
movl   $0xffffffff,-0x4(%rbp)
jmp    12e9 <j_socket_bind+0x169>
mov    -0x38(%rbp),%rax
mov    %rax,-0x40(%rbp)
movl   $0xffffffff,-0x48(%rbp)
cmpq   $0x0,-0x38(%rbp)
je     12ae <j_socket_bind+0x12e>
mov    -0x38(%rbp),%rax
mov    0x1c(%rax),%edi
mov    -0x38(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x38(%rbp),%rax
mov    0x18(%rax),%edx
call   10a0 <socket@plt>
mov    %eax,-0x48(%rbp)
cmpl   $0x0,-0x48(%rbp)
jl     129d <j_socket_bind+0x11d>
mov    -0x48(%rbp),%edi
mov    0x2d99(%rip),%rax
mov    (%rax),%esi
mov    0x2d60(%rip),%rax
mov    (%rax),%edx
lea    -0x4c(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    -0x48(%rbp),%edi
mov    -0x38(%rbp),%rax
mov    0x14(%rax),%esi
mov    -0x38(%rbp),%rax
mov    0x10(%rax),%edx
call   1070 <bind@plt>
cmp    $0x0,%rax
jne    128e <j_socket_bind+0x10e>
jmp    12ae <j_socket_bind+0x12e>
mov    -0x48(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x48(%rbp)
mov    -0x38(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x38(%rbp)
jmp    1213 <j_socket_bind+0x93>
cmpl   $0x0,-0x48(%rbp)
jge    12cd <j_socket_bind+0x14d>
mov    -0x40(%rbp),%rdi
call   1090 <freeaddrinfo@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12e9 <j_socket_bind+0x169>
mov    -0x48(%rbp),%edi
mov    $0x5,%esi
call   1060 <listen@plt>
mov    -0x40(%rbp),%rdi
call   1090 <freeaddrinfo@plt>
mov    -0x48(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<j_socket_bind>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    %rsi,%rbx
mov    %edi,%ebp
movl   $0x1,0x14(%rsp)
lea    0x18(%rsp),%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1040 <memset@plt>
mov    0x2e22(%rip),%rax
mov    0x2e23(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    0x2df8(%rip),%rax
or     (%rax),%ecx
mov    %ecx,0x18(%rsp)
mov    0x2dfb(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x34(%rsp)
mov    %ebp,0x1c(%rsp)
lea    0x8(%rsp),%rcx
xor    %edi,%edi
mov    %rbx,%rsi
mov    %r14,%rdx
call   1080 <getaddrinfo@plt>
mov    $0xffffffff,%r14d
test   %eax,%eax
js     12b0 <j_socket_bind+0x130>
mov    0x8(%rsp),%r15
test   %r15,%r15
je     128f <j_socket_bind+0x10f>
mov    0x2dd4(%rip),%r12
mov    0x2d9d(%rip),%r13
lea    0x14(%rsp),%rbp
mov    %r15,%rax
jmp    1243 <j_socket_bind+0xc3>
cs nopw 0x0(%rax,%rax,1)
nop
mov    0x8(%rsp),%rax
mov    0x8(%rax),%rax
mov    %rax,0x8(%rsp)
test   %rax,%rax
je     128f <j_socket_bind+0x10f>
mov    0x1c(%rax),%edi
mov    0x4(%rax),%esi
mov    0x18(%rax),%edx
call   10a0 <socket@plt>
test   %eax,%eax
js     1230 <j_socket_bind+0xb0>
mov    %eax,%ebx
mov    (%r12),%esi
mov    0x0(%r13),%edx
mov    %eax,%edi
mov    %rbp,%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    0x8(%rsp),%rax
mov    0x10(%rax),%edx
mov    0x14(%rax),%esi
mov    %ebx,%edi
call   1070 <bind@plt>
test   %rax,%rax
je     1299 <j_socket_bind+0x119>
mov    %ebx,%edi
call   1050 <close@plt>
jmp    1230 <j_socket_bind+0xb0>
mov    %r15,%rdi
call   1090 <freeaddrinfo@plt>
jmp    12b0 <j_socket_bind+0x130>
mov    %ebx,%edi
mov    $0x5,%esi
call   1060 <listen@plt>
mov    %r15,%rdi
call   1090 <freeaddrinfo@plt>
mov    %ebx,%r14d
mov    %r14d,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.addrinfo = type { i32, i32, %struct.addrinfo*, i32, i32, i32, i32 }
@AI_PASSIVE = global i32 0
@AI_V4MAPPED = global i32 0
@AI_ALL = global i32 0
@AF_INET6 = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@stderr = global i32 0
define i32 @j_socket_bind(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca %struct.addrinfo
%7 = alloca %struct.addrinfo*
%8 = alloca %struct.addrinfo*
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
store i32 %0, i32* %4
store i8* %1, i8** %5
store i32 1, i32* %11
%12 = call i32 @memset(%struct.addrinfo* %6, i32 0, i32 32)
%13 = load i32, i32* @AI_PASSIVE
%14 = load i32, i32* @AI_V4MAPPED
%15 = or i32 %13, %14
%16 = load i32, i32* @AI_ALL
%17 = or i32 %15, %16
%18 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %6, i32 0, i32 0
store i32 %17, i32* %18
%19 = load i32, i32* @AF_INET6
%20 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %6, i32 0, i32 6
store i32 %19, i32* %20
%21 = load i32, i32* %4
%22 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %6, i32 0, i32 1
store i32 %21, i32* %22
%23 = load i8*, i8** %5
%24 = call i32 @getaddrinfo(i32* null, i8* %23, %struct.addrinfo* %6, %struct.addrinfo** %7)
store i32 %24, i32* %9
%25 = load i32, i32* %9
%26 = icmp slt i32 %25, 0
br i1 %26, label %27, label %28
27: ; preds = %2
store i32 -1, i32* %3
br label %80
28: ; preds = %2
%29 = load %struct.addrinfo*, %struct.addrinfo** %7
store %struct.addrinfo* %29, %struct.addrinfo** %8
store i32 -1, i32* %10
br label %30
30: ; preds = %64, %28
%31 = load %struct.addrinfo*, %struct.addrinfo** %7
%32 = icmp ne %struct.addrinfo* %31, null
br i1 %32, label %33, label %68
33: ; preds = %30
%34 = load %struct.addrinfo*, %struct.addrinfo** %7
%35 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %34, i32 0, i32 6
%36 = load i32, i32* %35
%37 = load %struct.addrinfo*, %struct.addrinfo** %7
%38 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %37, i32 0, i32 1
%39 = load i32, i32* %38
%40 = load %struct.addrinfo*, %struct.addrinfo** %7
%41 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %40, i32 0, i32 5
%42 = load i32, i32* %41
%43 = call i32 @socket(i32 %36, i32 %39, i32 %42)
store i32 %43, i32* %10
%44 = load i32, i32* %10
%45 = icmp slt i32 %44, 0
br i1 %45, label %64, label %46
46: ; preds = %33
%47 = load i32, i32* %10
%48 = load i32, i32* @SOL_SOCKET
%49 = load i32, i32* @SO_REUSEADDR
%50 = call i32 @setsockopt(i32 %47, i32 %48, i32 %49, i32* %11, i32 4)
%51 = load i32, i32* %10
%52 = load %struct.addrinfo*, %struct.addrinfo** %7
%53 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %52, i32 0, i32 4
%54 = load i32, i32* %53
%55 = load %struct.addrinfo*, %struct.addrinfo** %7
%56 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %55, i32 0, i32 3
%57 = load i32, i32* %56
%58 = call i64 @bind(i32 %51, i32 %54, i32 %57)
%59 = icmp eq i64 %58, 0
br i1 %59, label %60, label %61
60: ; preds = %46
br label %68
61: ; preds = %46
%62 = load i32, i32* %10
%63 = call i32 @close(i32 %62)
store i32 -1, i32* %10
br label %64
64: ; preds = %61, %33
%65 = load %struct.addrinfo*, %struct.addrinfo** %7
%66 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %65, i32 0, i32 2
%67 = load %struct.addrinfo*, %struct.addrinfo** %66
store %struct.addrinfo* %67, %struct.addrinfo** %7
br label %30
68: ; preds = %60, %30
%69 = load i32, i32* %10
%70 = icmp slt i32 %69, 0
br i1 %70, label %71, label %74
71: ; preds = %68
%72 = load %struct.addrinfo*, %struct.addrinfo** %8
%73 = call i32 @freeaddrinfo(%struct.addrinfo* %72)
store i32 -1, i32* %3
br label %80
74: ; preds = %68
%75 = load i32, i32* %10
%76 = call i32 @listen(i32 %75, i32 5)
%77 = load %struct.addrinfo*, %struct.addrinfo** %8
%78 = call i32 @freeaddrinfo(%struct.addrinfo* %77)
%79 = load i32, i32* %10
store i32 %79, i32* %3
br label %80
80: ; preds = %74, %71, %27
%81 = load i32, i32* %3
ret i32 %81
}

LLVM IR (-O2):
%struct.addrinfo = type { i32, i32, %struct.addrinfo*, i32, i32, i32, i32 }
@AI_PASSIVE = global i32 0
@AI_V4MAPPED = global i32 0
@AI_ALL = global i32 0
@AF_INET6 = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@stderr = global i32 0
define i32 @j_socket_bind(i32 %0, i8* %1) {
%3 = alloca %struct.addrinfo
%4 = alloca %struct.addrinfo*
%5 = alloca i32
%6 = bitcast %struct.addrinfo* %3 to i8*
%7 = bitcast %struct.addrinfo** %4 to i8*
%8 = bitcast i32* %5 to i8*
store i32 1, i32* %5
%9 = call i32 @memset(%struct.addrinfo* %3, i32 0, i32 32)
%10 = load i32, i32* @AI_PASSIVE
%11 = load i32, i32* @AI_V4MAPPED
%12 = or i32 %11, %10
%13 = load i32, i32* @AI_ALL
%14 = or i32 %12, %13
%15 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 0
store i32 %14, i32* %15
%16 = load i32, i32* @AF_INET6
%17 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 6
store i32 %16, i32* %17
%18 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 1
store i32 %0, i32* %18
%19 = call i32 @getaddrinfo(i32* null, i8* %1, %struct.addrinfo* %3, %struct.addrinfo** %4)
%20 = icmp slt i32 %19, 0
br i1 %20, label %57, label %21
21: ; preds = %2
%22 = load %struct.addrinfo*, %struct.addrinfo** %4
%23 = icmp eq %struct.addrinfo* %22, null
br i1 %23, label %52, label %24
24: ; preds = %21, %47
%25 = phi %struct.addrinfo* [ %50, %47 ], [ %22, %21 ]
%26 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %25, i64 0, i32 6
%27 = load i32, i32* %26
%28 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %25, i64 0, i32 1
%29 = load i32, i32* %28
%30 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %25, i64 0, i32 5
%31 = load i32, i32* %30
%32 = call i32 @socket(i32 %27, i32 %29, i32 %31)
%33 = icmp slt i32 %32, 0
br i1 %33, label %47, label %34
34: ; preds = %24
%35 = load i32, i32* @SOL_SOCKET
%36 = load i32, i32* @SO_REUSEADDR
%37 = call i32 @setsockopt(i32 %32, i32 %35, i32 %36, i32* %5, i32 4)
%38 = load %struct.addrinfo*, %struct.addrinfo** %4
%39 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %38, i64 0, i32 4
%40 = load i32, i32* %39
%41 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %38, i64 0, i32 3
%42 = load i32, i32* %41
%43 = call i64 @bind(i32 %32, i32 %40, i32 %42)
%44 = icmp eq i64 %43, 0
br i1 %44, label %54, label %45
45: ; preds = %34
%46 = call i32 @close(i32 %32)
br label %47
47: ; preds = %45, %24
%48 = load %struct.addrinfo*, %struct.addrinfo** %4
%49 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %48, i64 0, i32 2
%50 = load %struct.addrinfo*, %struct.addrinfo** %49
store %struct.addrinfo* %50, %struct.addrinfo** %4
%51 = icmp eq %struct.addrinfo* %50, null
br i1 %51, label %52, label %24
52: ; preds = %47, %21
%53 = call i32 @freeaddrinfo(%struct.addrinfo* %22)
br label %57
54: ; preds = %34
%55 = call i32 @listen(i32 %32, i32 5)
%56 = call i32 @freeaddrinfo(%struct.addrinfo* %22)
br label %57
57: ; preds = %2, %54, %52
%58 = phi i32 [ -1, %52 ], [ %32, %54 ], [ -1, %2 ]
ret i32 %58
}