#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;
};
typedef int /*<<< orphan*/ opt;
typedef int /*<<< orphan*/ hints;

/* Variables and functions */
int /*<<< orphan*/ AF_UNSPEC;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEADDR;
int bind(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
int getaddrinfo(char const *, char const *, struct addrinfo *,
                struct addrinfo **);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int setreuseport(int);
int /*<<< orphan*/ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/,
                              int *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int create_and_bind(const char *addr, const char *port) {
  struct addrinfo hints;
  struct addrinfo *result, *rp;
  int s, listen_sock;
  memset(&hints, 0, sizeof(hints));
  hints.ai_family = AF_UNSPEC;
  hints.ai_socktype = SOCK_STREAM;
  s = getaddrinfo(addr, port, &hints, &result);
  if (s != 0) {
    perror("getaddrinfo");
    return -1;
  }
  for (rp = result; rp != NULL; rp = rp->ai_next) {
    listen_sock = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
    if (listen_sock == -1) {
      continue;
    }
    int opt = 1;
    setsockopt(listen_sock, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
    int err = setreuseport(listen_sock);
    if (err == 0) {
      perror("set reuse port!");
    }
    s = bind(listen_sock, rp->ai_addr, rp->ai_addrlen);
    if (s == 0) {
      break;
    } else {
      perror("bind");
    }
    close(listen_sock);
  }
  if (rp == NULL) {
    perror("can't bind success!");
    return -1;
  }
  freeaddrinfo(result);
  return listen_sock;
}


x86 assembly (clang, -O0):
<create_and_bind>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
lea    -0x38(%rbp),%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1050 <memset@plt>
mov    0x2e29(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    0x2e0d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
lea    -0x38(%rbp),%rdx
lea    -0x40(%rbp),%rcx
call   1090 <getaddrinfo@plt>
mov    %eax,-0x4c(%rbp)
cmpl   $0x0,-0x4c(%rbp)
je     1202 <create_and_bind+0x72>
lea    0xe0f(%rip),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    130b <create_and_bind+0x17b>
mov    -0x40(%rbp),%rax
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x48(%rbp)
je     12d9 <create_and_bind+0x149>
mov    -0x48(%rbp),%rax
mov    0x10(%rax),%edi
mov    -0x48(%rbp),%rax
mov    0xc(%rax),%esi
mov    -0x48(%rbp),%rax
mov    0x8(%rax),%edx
call   10b0 <socket@plt>
mov    %eax,-0x50(%rbp)
cmpl   $0xffffffff,-0x50(%rbp)
jne    1241 <create_and_bind+0xb1>
jmp    12c8 <create_and_bind+0x138>
movl   $0x1,-0x54(%rbp)
mov    -0x50(%rbp),%edi
mov    0x2d96(%rip),%rax
mov    (%rax),%esi
mov    0x2d6d(%rip),%rax
mov    (%rax),%edx
lea    -0x54(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    -0x50(%rbp),%edi
call   1040 <setreuseport@plt>
mov    %eax,-0x58(%rbp)
cmpl   $0x0,-0x58(%rbp)
jne    128d <create_and_bind+0xfd>
lea    0xd84(%rip),%rdi
call   1080 <perror@plt>
mov    -0x50(%rbp),%edi
mov    -0x48(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x48(%rbp),%rax
mov    (%rax),%edx
call   1070 <bind@plt>
mov    %eax,-0x4c(%rbp)
cmpl   $0x0,-0x4c(%rbp)
jne    12b4 <create_and_bind+0x124>
jmp    12d9 <create_and_bind+0x149>
lea    0xd61(%rip),%rdi
call   1080 <perror@plt>
mov    -0x50(%rbp),%edi
call   1060 <close@plt>
mov    -0x48(%rbp),%rax
mov    0x18(%rax),%rax
mov    %rax,-0x48(%rbp)
jmp    120a <create_and_bind+0x7a>
cmpq   $0x0,-0x48(%rbp)
jne    12fc <create_and_bind+0x16c>
lea    0xd36(%rip),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    130b <create_and_bind+0x17b>
mov    -0x40(%rbp),%rdi
call   10a0 <freeaddrinfo@plt>
mov    -0x50(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<create_and_bind>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    %rsi,%rbx
mov    %rdi,%rbp
lea    0x18(%rsp),%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1050 <memset@plt>
mov    0x2e21(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x28(%rsp)
mov    0x2e04(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x24(%rsp)
lea    0x10(%rsp),%rcx
mov    %rbp,%rdi
mov    %rbx,%rsi
mov    %r14,%rdx
call   1090 <getaddrinfo@plt>
test   %eax,%eax
jne    12b6 <create_and_bind+0x126>
mov    0x10(%rsp),%rbx
test   %rbx,%rbx
je     12bf <create_and_bind+0x12f>
mov    0x2de6(%rip),%r13
mov    0x2dbf(%rip),%r14
lea    0xc(%rsp),%r12
lea    0xe07(%rip),%r15
jmp    122d <create_and_bind+0x9d>
nopw   0x0(%rax,%rax,1)
mov    0x18(%rbx),%rbx
test   %rbx,%rbx
je     12bf <create_and_bind+0x12f>
mov    0x10(%rbx),%edi
mov    0x8(%rbx),%edx
mov    0xc(%rbx),%esi
call   10b0 <socket@plt>
cmp    $0xffffffff,%eax
je     1220 <create_and_bind+0x90>
mov    %eax,%ebp
movl   $0x1,0xc(%rsp)
mov    0x0(%r13),%esi
mov    (%r14),%edx
mov    %eax,%edi
mov    %r12,%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    %ebp,%edi
call   1040 <setreuseport@plt>
test   %eax,%eax
je     128d <create_and_bind+0xfd>
mov    (%rbx),%edx
mov    0x4(%rbx),%esi
mov    %ebp,%edi
call   1070 <bind@plt>
test   %eax,%eax
je     129b <create_and_bind+0x10b>
mov    %r15,%rdi
call   1080 <perror@plt>
mov    %ebp,%edi
call   1060 <close@plt>
jmp    1220 <create_and_bind+0x90>
lea    0xd78(%rip),%rdi
call   1080 <perror@plt>
jmp    126c <create_and_bind+0xdc>
mov    0x10(%rsp),%rdi
call   10a0 <freeaddrinfo@plt>
mov    %ebp,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd43(%rip),%rdi
jmp    12c6 <create_and_bind+0x136>
lea    0xd5b(%rip),%rdi
call   1080 <perror@plt>
mov    $0xffffffff,%ebp
jmp    12a5 <create_and_bind+0x115>

LLVM IR (-O0):
%struct.addrinfo = type { i32, i32, i32, i32, i32, %struct.addrinfo* }
@AF_UNSPEC = global i32 0
@SOCK_STREAM = global i32 0
@.str = [12 x i8] c"getaddrinfo\00"
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.1 = [16 x i8] c"set reuse port!\00"
@.str.2 = [5 x i8] c"bind\00"
@.str.3 = [20 x i8] c"can't bind success!\00"
define i32 @create_and_bind(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca %struct.addrinfo
%7 = alloca %struct.addrinfo*
%8 = alloca %struct.addrinfo*
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
%13 = call i32 @memset(%struct.addrinfo* %6, i32 0, i32 32)
%14 = load i32, i32* @AF_UNSPEC
%15 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %6, i32 0, i32 4
store i32 %14, i32* %15
%16 = load i32, i32* @SOCK_STREAM
%17 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %6, i32 0, i32 3
store i32 %16, i32* %17
%18 = load i8*, i8** %4
%19 = load i8*, i8** %5
%20 = call i32 @getaddrinfo(i8* %18, i8* %19, %struct.addrinfo* %6, %struct.addrinfo** %7)
store i32 %20, i32* %9
%21 = load i32, i32* %9
%22 = icmp ne i32 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %2
%24 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %85
25: ; preds = %2
%26 = load %struct.addrinfo*, %struct.addrinfo** %7
store %struct.addrinfo* %26, %struct.addrinfo** %8
br label %27
27: ; preds = %72, %25
%28 = load %struct.addrinfo*, %struct.addrinfo** %8
%29 = icmp ne %struct.addrinfo* %28, null
br i1 %29, label %30, label %76
30: ; preds = %27
%31 = load %struct.addrinfo*, %struct.addrinfo** %8
%32 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %31, i32 0, i32 4
%33 = load i32, i32* %32
%34 = load %struct.addrinfo*, %struct.addrinfo** %8
%35 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %34, i32 0, i32 3
%36 = load i32, i32* %35
%37 = load %struct.addrinfo*, %struct.addrinfo** %8
%38 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %37, i32 0, i32 2
%39 = load i32, i32* %38
%40 = call i32 @socket(i32 %33, i32 %36, i32 %39)
store i32 %40, i32* %10
%41 = load i32, i32* %10
%42 = icmp eq i32 %41, -1
br i1 %42, label %43, label %44
43: ; preds = %30
br label %72
44: ; preds = %30
store i32 1, i32* %11
%45 = load i32, i32* %10
%46 = load i32, i32* @SOL_SOCKET
%47 = load i32, i32* @SO_REUSEADDR
%48 = call i32 @setsockopt(i32 %45, i32 %46, i32 %47, i32* %11, i32 4)
%49 = load i32, i32* %10
%50 = call i32 @setreuseport(i32 %49)
store i32 %50, i32* %12
%51 = load i32, i32* %12
%52 = icmp eq i32 %51, 0
br i1 %52, label %53, label %55
53: ; preds = %44
%54 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
br label %55
55: ; preds = %53, %44
%56 = load i32, i32* %10
%57 = load %struct.addrinfo*, %struct.addrinfo** %8
%58 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %57, i32 0, i32 1
%59 = load i32, i32* %58
%60 = load %struct.addrinfo*, %struct.addrinfo** %8
%61 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %60, i32 0, i32 0
%62 = load i32, i32* %61
%63 = call i32 @bind(i32 %56, i32 %59, i32 %62)
store i32 %63, i32* %9
%64 = load i32, i32* %9
%65 = icmp eq i32 %64, 0
br i1 %65, label %66, label %67
66: ; preds = %55
br label %76
67: ; preds = %55
%68 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
br label %69
69: ; preds = %67
%70 = load i32, i32* %10
%71 = call i32 @close(i32 %70)
br label %72
72: ; preds = %69, %43
%73 = load %struct.addrinfo*, %struct.addrinfo** %8
%74 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %73, i32 0, i32 5
%75 = load %struct.addrinfo*, %struct.addrinfo** %74
store %struct.addrinfo* %75, %struct.addrinfo** %8
br label %27
76: ; preds = %66, %27
%77 = load %struct.addrinfo*, %struct.addrinfo** %8
%78 = icmp eq %struct.addrinfo* %77, null
br i1 %78, label %79, label %81
79: ; preds = %76
%80 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0))
store i32 -1, i32* %3
br label %85
81: ; preds = %76
%82 = load %struct.addrinfo*, %struct.addrinfo** %7
%83 = call i32 @freeaddrinfo(%struct.addrinfo* %82)
%84 = load i32, i32* %10
store i32 %84, i32* %3
br label %85
85: ; preds = %81, %79, %23
%86 = load i32, i32* %3
ret i32 %86
}

LLVM IR (-O2):
%struct.addrinfo = type { i32, i32, i32, i32, i32, %struct.addrinfo* }
@AF_UNSPEC = global i32 0
@SOCK_STREAM = global i32 0
@.str = [12 x i8] c"getaddrinfo\00"
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.1 = [16 x i8] c"set reuse port!\00"
@.str.2 = [5 x i8] c"bind\00"
@.str.3 = [20 x i8] c"can't bind success!\00"
define i32 @create_and_bind(i8* %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 = call i32 @memset(%struct.addrinfo* %3, i32 0, i32 32)
%9 = load i32, i32* @AF_UNSPEC
%10 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 4
store i32 %9, i32* %10
%11 = load i32, i32* @SOCK_STREAM
%12 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 3
store i32 %11, i32* %12
%13 = call i32 @getaddrinfo(i8* %0, i8* %1, %struct.addrinfo* %3, %struct.addrinfo** %4)
%14 = icmp eq i32 %13, 0
br i1 %14, label %15, label %20
15: ; preds = %2
%16 = load %struct.addrinfo*, %struct.addrinfo** %4
%17 = icmp eq %struct.addrinfo* %16, null
br i1 %17, label %54, label %18
18: ; preds = %15
%19 = bitcast i32* %5 to i8*
br label %22
20: ; preds = %2
%21 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
br label %59
22: ; preds = %18, %50
%23 = phi %struct.addrinfo* [ %16, %18 ], [ %52, %50 ]
%24 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %23, i64 0, i32 4
%25 = load i32, i32* %24
%26 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %23, i64 0, i32 3
%27 = load i32, i32* %26
%28 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %23, i64 0, i32 2
%29 = load i32, i32* %28
%30 = call i32 @socket(i32 %25, i32 %27, i32 %29)
%31 = icmp eq i32 %30, -1
br i1 %31, label %50, label %32
32: ; preds = %22
store i32 1, i32* %5
%33 = load i32, i32* @SOL_SOCKET
%34 = load i32, i32* @SO_REUSEADDR
%35 = call i32 @setsockopt(i32 %30, i32 %33, i32 %34, i32* %5, i32 4)
%36 = call i32 @setreuseport(i32 %30)
%37 = icmp eq i32 %36, 0
br i1 %37, label %38, label %40
38: ; preds = %32
%39 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
br label %40
40: ; preds = %38, %32
%41 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %23, i64 0, i32 1
%42 = load i32, i32* %41
%43 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %23, i64 0, i32 0
%44 = load i32, i32* %43
%45 = call i32 @bind(i32 %30, i32 %42, i32 %44)
%46 = icmp eq i32 %45, 0
br i1 %46, label %56, label %47
47: ; preds = %40
%48 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%49 = call i32 @close(i32 %30)
br label %50
50: ; preds = %47, %22
%51 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %23, i64 0, i32 5
%52 = load %struct.addrinfo*, %struct.addrinfo** %51
%53 = icmp eq %struct.addrinfo* %52, null
br i1 %53, label %54, label %22
54: ; preds = %50, %15
%55 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0))
br label %59
56: ; preds = %40
%57 = load %struct.addrinfo*, %struct.addrinfo** %4
%58 = call i32 @freeaddrinfo(%struct.addrinfo* %57)
br label %59
59: ; preds = %56, %54, %20
%60 = phi i32 [ -1, %20 ], [ -1, %54 ], [ %30, %56 ]
ret i32 %60
}