#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_UNSPEC;
int /*<<< orphan*/ AI_PASSIVE;
int /*<<< orphan*/ SOCK_DGRAM;
scalar_t__ bind(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ fputs(char *, int /*<<< orphan*/);
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
char *gai_strerror(int);
int getaddrinfo(int /*<<< orphan*/, char const *, struct addrinfo *,
                struct addrinfo **);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int init(const char *port) {
  struct addrinfo *sinfo, *p;
  struct addrinfo hints;
  int r;
  memset(&hints, 0, sizeof hints);
  hints.ai_family = AF_UNSPEC;
  hints.ai_socktype = SOCK_DGRAM;
  hints.ai_flags = AI_PASSIVE;
  if ((r = getaddrinfo(0, port, &hints, &sinfo))) {
    fprintf(stderr, "%s\n", gai_strerror(r));
    return -1;
  }
  for (p = sinfo; p; p = p->ai_next) {
    r = socket(p->ai_family, p->ai_socktype, p->ai_protocol);
    if (r < 0)
      goto err;
    if (bind(r, p->ai_addr, p->ai_addrlen) >= 0) {
      freeaddrinfo(sinfo);
      return r;
    }
    close(r);
  err:
    fprintf(stderr, "%s\n", strerror(errno));
  }
  freeaddrinfo(sinfo);
  fputs("failed to bind socket\n", stderr);
  return -1;
}


x86 assembly (clang, -O0):
<init>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
lea    -0x48(%rbp),%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1050 <memset@plt>
mov    0x2e1d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x38(%rbp)
mov    0x2e09(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
mov    0x2e0d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    -0x10(%rbp),%rsi
xor    %edi,%edi
lea    -0x48(%rbp),%rdx
lea    -0x18(%rbp),%rcx
call   1090 <getaddrinfo@plt>
mov    %eax,-0x4c(%rbp)
cmp    $0x0,%eax
je     1231 <init+0x91>
mov    0x2dc2(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x50(%rbp)
mov    -0x4c(%rbp),%edi
call   1030 <gai_strerror@plt>
mov    -0x50(%rbp),%edi
mov    %rax,%rdx
lea    0xde0(%rip),%rsi
call   1070 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    130d <init+0x16d>
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     12e8 <init+0x148>
mov    -0x20(%rbp),%rax
mov    0x10(%rax),%edi
mov    -0x20(%rbp),%rax
mov    0xc(%rax),%esi
mov    -0x20(%rbp),%rax
mov    0x8(%rax),%edx
call   10c0 <socket@plt>
mov    %eax,-0x4c(%rbp)
cmpl   $0x0,-0x4c(%rbp)
jge    1270 <init+0xd0>
jmp    12ab <init+0x10b>
mov    -0x4c(%rbp),%edi
mov    -0x20(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x20(%rbp),%rax
mov    (%rax),%edx
call   1080 <bind@plt>
cmp    $0x0,%rax
jl     12a3 <init+0x103>
mov    -0x18(%rbp),%rdi
call   10b0 <freeaddrinfo@plt>
mov    -0x4c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    130d <init+0x16d>
mov    -0x4c(%rbp),%edi
call   1060 <close@plt>
mov    0x2d16(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x54(%rbp)
mov    0x2d02(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x54(%rbp),%edi
mov    %rax,%rdx
lea    0xd2e(%rip),%rsi
call   1070 <fprintf@plt>
mov    -0x20(%rbp),%rax
mov    0x18(%rax),%rax
mov    %rax,-0x20(%rbp)
jmp    1239 <init+0x99>
mov    -0x18(%rbp),%rdi
call   10b0 <freeaddrinfo@plt>
mov    0x2cd0(%rip),%rax
mov    (%rax),%esi
lea    0xd03(%rip),%rdi
call   1040 <fputs@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<init>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x30,%rsp
mov    %rdi,%rbx
lea    0x8(%rsp),%rbp
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1050 <memset@plt>
mov    0x2e16(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x18(%rsp)
mov    0x2e01(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x14(%rsp)
mov    0x2e04(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x28(%rsp)
mov    %rsp,%rcx
xor    %edi,%edi
mov    %rbx,%rsi
mov    %rbp,%rdx
call   1090 <getaddrinfo@plt>
test   %eax,%eax
je     1224 <init+0x84>
mov    0x2dc3(%rip),%rcx
mov    (%rcx),%ebx
mov    %eax,%edi
call   1030 <gai_strerror@plt>
lea    0xdeb(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1070 <fprintf@plt>
jmp    12c2 <init+0x122>
mov    (%rsp),%rbx
test   %rbx,%rbx
je     12a0 <init+0x100>
mov    0x2d94(%rip),%r15
mov    0x2d85(%rip),%r12
lea    0xdbe(%rip),%r14
jmp    1272 <init+0xd2>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    (%r15),%ebp
mov    (%r12),%edi
call   10a0 <strerror@plt>
mov    %ebp,%edi
mov    %r14,%rsi
mov    %rax,%rdx
call   1070 <fprintf@plt>
mov    0x18(%rbx),%rbx
test   %rbx,%rbx
je     12a4 <init+0x104>
mov    0x10(%rbx),%edi
mov    0x8(%rbx),%edx
mov    0xc(%rbx),%esi
call   10c0 <socket@plt>
test   %eax,%eax
js     1250 <init+0xb0>
mov    %eax,%ebp
mov    (%rbx),%edx
mov    0x4(%rbx),%esi
mov    %eax,%edi
call   1080 <bind@plt>
test   %rax,%rax
jns    12d6 <init+0x136>
mov    %ebp,%edi
call   1060 <close@plt>
jmp    1250 <init+0xb0>
xor    %edi,%edi
jmp    12a8 <init+0x108>
mov    (%rsp),%rdi
call   10b0 <freeaddrinfo@plt>
mov    0x2d14(%rip),%rax
mov    (%rax),%esi
lea    0xd47(%rip),%rdi
call   1040 <fputs@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x30,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    (%rsp),%rdi
call   10b0 <freeaddrinfo@plt>
jmp    12c7 <init+0x127>

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

LLVM IR (-O2):
%struct.addrinfo = type { i32, i32, i32, i32, i32, %struct.addrinfo*, i32 }
@AF_UNSPEC = global i32 0
@SOCK_DGRAM = global i32 0
@AI_PASSIVE = global i32 0
@stderr = global i32 0
@.str = [4 x i8] c"%s\0A\00"
@errno = global i32 0
@.str.1 = [23 x i8] c"failed to bind socket\0A\00"
define i32 @init(i8* %0) {
%2 = alloca %struct.addrinfo*
%3 = alloca %struct.addrinfo
%4 = bitcast %struct.addrinfo** %2 to i8*
%5 = bitcast %struct.addrinfo* %3 to i8*
%6 = call i32 @memset(%struct.addrinfo* %3, i32 0, i32 40)
%7 = load i32, i32* @AF_UNSPEC
%8 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 4
store i32 %7, i32* %8
%9 = load i32, i32* @SOCK_DGRAM
%10 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 3
store i32 %9, i32* %10
%11 = load i32, i32* @AI_PASSIVE
%12 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 6
store i32 %11, i32* %12
%13 = call i32 @getaddrinfo(i32 0, i8* %0, %struct.addrinfo* %3, %struct.addrinfo** %2)
%14 = icmp eq i32 %13, 0
br i1 %14, label %15, label %18
15: ; preds = %1
%16 = load %struct.addrinfo*, %struct.addrinfo** %2
%17 = icmp eq %struct.addrinfo* %16, null
br i1 %17, label %54, label %22
18: ; preds = %1
%19 = load i32, i32* @stderr
%20 = call i8* @gai_strerror(i32 %13)
%21 = call i32 @fprintf(i32 %19, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* %20)
br label %59
22: ; preds = %15, %44
%23 = phi %struct.addrinfo* [ %50, %44 ], [ %16, %15 ]
%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 slt i32 %30, 0
br i1 %31, label %44, label %32
32: ; preds = %22
%33 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %23, i64 0, i32 1
%34 = load i32, i32* %33
%35 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %23, i64 0, i32 0
%36 = load i32, i32* %35
%37 = call i64 @bind(i32 %30, i32 %34, i32 %36)
%38 = icmp sgt i64 %37, -1
br i1 %38, label %39, label %42
39: ; preds = %32
%40 = load %struct.addrinfo*, %struct.addrinfo** %2
%41 = call i32 @freeaddrinfo(%struct.addrinfo* %40)
br label %59
42: ; preds = %32
%43 = call i32 @close(i32 %30)
br label %44
44: ; preds = %22, %42
%45 = load i32, i32* @stderr
%46 = load i32, i32* @errno
%47 = call i8* @strerror(i32 %46)
%48 = call i32 @fprintf(i32 %45, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* %47)
%49 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %23, i64 0, i32 5
%50 = load %struct.addrinfo*, %struct.addrinfo** %49
%51 = icmp eq %struct.addrinfo* %50, null
br i1 %51, label %52, label %22
52: ; preds = %44
%53 = load %struct.addrinfo*, %struct.addrinfo** %2
br label %54
54: ; preds = %52, %15
%55 = phi %struct.addrinfo* [ %53, %52 ], [ null, %15 ]
%56 = call i32 @freeaddrinfo(%struct.addrinfo* %55)
%57 = load i32, i32* @stderr
%58 = call i32 @fputs(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i32 %57)
br label %59
59: ; preds = %54, %39, %18
%60 = phi i32 [ -1, %18 ], [ %30, %39 ], [ -1, %54 ]
ret i32 %60
}