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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ AI_PASSIVE;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ anetSetError(char *, char *, ...);
int anetSetReuseAddr(char *, int);
int bind(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
int /*<<< orphan*/ gai_strerror(int);
int getaddrinfo(char *, char *, struct addrinfo *, struct addrinfo **);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
int anetUdpServer(char *err, int port, char *bindaddr) {
  int s, rv;
  char _port[6];
  struct addrinfo hints, *servinfo, *p;
  snprintf(_port, 6, "%d", port);
  memset(&hints, 0, sizeof(hints));
  hints.ai_family = AF_INET;
  hints.ai_socktype = SOCK_DGRAM;
  hints.ai_flags = AI_PASSIVE;
  if ((rv = getaddrinfo(bindaddr, _port, &hints, &servinfo)) != 0) {
    anetSetError(err, "%s", gai_strerror(rv));
    return -1;
  }
  for (p = servinfo; p != NULL; p = p->ai_next) {
    if ((s = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1)
      continue;
    if (anetSetReuseAddr(err, s) == -1)
      goto error;
    if (bind(s, p->ai_addr, p->ai_addrlen) == -1) {
      anetSetError(err, "bind: %s", strerror(errno));
      close(s);
      return -1;
    }
    goto end;
  }
  if (p == NULL) {
    anetSetError(err, "unable to bind socket");
    goto error;
  }
error:
  s = -1;
end:
  freeaddrinfo(servinfo);
  return s;
}


x86 assembly (clang, -O0):
<anetUdpServer>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
lea    -0x2e(%rbp),%rdi
mov    -0x14(%rbp),%ecx
mov    $0x6,%esi
lea    0xe27(%rip),%rdx
call   1030 <snprintf@plt>
lea    -0x58(%rbp),%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1050 <memset@plt>
mov    0x2deb(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x48(%rbp)
mov    0x2dd7(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4c(%rbp)
mov    0x2ddb(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x38(%rbp)
mov    -0x20(%rbp),%rdi
lea    -0x2e(%rbp),%rsi
lea    -0x58(%rbp),%rdx
lea    -0x60(%rbp),%rcx
call   10a0 <getaddrinfo@plt>
mov    %eax,-0x28(%rbp)
cmp    $0x0,%eax
je     1263 <anetUdpServer+0xb3>
mov    -0x10(%rbp),%rax
mov    %rax,-0x70(%rbp)
mov    -0x28(%rbp),%edi
call   1040 <gai_strerror@plt>
mov    -0x70(%rbp),%rdi
mov    %eax,%edx
lea    0xdb9(%rip),%rsi
mov    $0x0,%al
call   1070 <anetSetError@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    136a <anetUdpServer+0x1ba>
mov    -0x60(%rbp),%rax
mov    %rax,-0x68(%rbp)
cmpq   $0x0,-0x68(%rbp)
je     132d <anetUdpServer+0x17d>
mov    -0x68(%rbp),%rax
mov    0x10(%rax),%edi
mov    -0x68(%rbp),%rax
mov    0xc(%rax),%esi
mov    -0x68(%rbp),%rax
mov    0x8(%rax),%edx
call   10d0 <socket@plt>
mov    %eax,-0x24(%rbp)
cmp    $0xffffffff,%eax
jne    12a1 <anetUdpServer+0xf1>
jmp    131c <anetUdpServer+0x16c>
mov    -0x10(%rbp),%rdi
mov    -0x24(%rbp),%esi
call   1080 <anetSetReuseAddr@plt>
cmp    $0xffffffff,%eax
jne    12bb <anetUdpServer+0x10b>
jmp    1354 <anetUdpServer+0x1a4>
mov    -0x24(%rbp),%edi
mov    -0x68(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x68(%rbp),%rax
mov    (%rax),%edx
call   1090 <bind@plt>
cmp    $0xffffffff,%eax
jne    1317 <anetUdpServer+0x167>
mov    -0x10(%rbp),%rax
mov    %rax,-0x78(%rbp)
mov    0x2ce0(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    -0x78(%rbp),%rdi
mov    %eax,%edx
lea    0xd07(%rip),%rsi
mov    $0x0,%al
call   1070 <anetSetError@plt>
mov    -0x24(%rbp),%edi
call   1060 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    136a <anetUdpServer+0x1ba>
jmp    135b <anetUdpServer+0x1ab>
mov    -0x68(%rbp),%rax
mov    0x18(%rax),%rax
mov    %rax,-0x68(%rbp)
jmp    126b <anetUdpServer+0xbb>
cmpq   $0x0,-0x68(%rbp)
jne    134f <anetUdpServer+0x19f>
mov    -0x10(%rbp),%rdi
lea    0xcc9(%rip),%rsi
mov    $0x0,%al
call   1070 <anetSetError@plt>
jmp    1354 <anetUdpServer+0x1a4>
jmp    1354 <anetUdpServer+0x1a4>
movl   $0xffffffff,-0x24(%rbp)
mov    -0x60(%rbp),%rdi
call   10c0 <freeaddrinfo@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<anetUdpServer>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x38,%rsp
mov    %rdx,%rbp
mov    %esi,%ecx
mov    %rdi,%r14
lea    0xe37(%rip),%rdx
lea    0xa(%rsp),%r15
mov    %r15,%rdi
mov    $0x6,%esi
call   1030 <snprintf@plt>
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1050 <memset@plt>
mov    0x2dea(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x20(%rsp)
mov    0x2dd5(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x1c(%rsp)
mov    0x2dd8(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x30(%rsp)
mov    %rsp,%rcx
mov    %rbp,%rdi
mov    %r15,%rsi
mov    %rbx,%rdx
call   10a0 <getaddrinfo@plt>
test   %eax,%eax
je     124c <anetUdpServer+0x9c>
mov    %eax,%edi
call   1040 <gai_strerror@plt>
lea    0xdd0(%rip),%rsi
mov    %r14,%rdi
mov    %eax,%edx
xor    %eax,%eax
call   1070 <anetSetError@plt>
mov    $0xffffffff,%ebp
jmp    129b <anetUdpServer+0xeb>
mov    (%rsp),%rbx
test   %rbx,%rbx
je     127c <anetUdpServer+0xcc>
cs nopw 0x0(%rax,%rax,1)
nop
mov    0x10(%rbx),%edi
mov    0x8(%rbx),%edx
mov    0xc(%rbx),%esi
call   10d0 <socket@plt>
cmp    $0xffffffff,%eax
jne    12a8 <anetUdpServer+0xf8>
mov    0x18(%rbx),%rbx
test   %rbx,%rbx
jne    1260 <anetUdpServer+0xb0>
lea    0xd89(%rip),%rsi
mov    %r14,%rdi
xor    %eax,%eax
call   1070 <anetSetError@plt>
mov    $0xffffffff,%ebp
mov    (%rsp),%rdi
call   10c0 <freeaddrinfo@plt>
mov    %ebp,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %eax,%ebp
mov    %r14,%rdi
mov    %eax,%esi
call   1080 <anetSetReuseAddr@plt>
cmp    $0xffffffff,%eax
je     128d <anetUdpServer+0xdd>
mov    (%rbx),%edx
mov    0x4(%rbx),%esi
mov    %ebp,%edi
call   1090 <bind@plt>
cmp    $0xffffffff,%eax
jne    1292 <anetUdpServer+0xe2>
mov    0x2cf7(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
lea    0xd24(%rip),%rsi
mov    %r14,%rdi
mov    %eax,%edx
xor    %eax,%eax
call   1070 <anetSetError@plt>
mov    %ebp,%edi
call   1060 <close@plt>
mov    $0xffffffff,%ebp
jmp    129b <anetUdpServer+0xeb>

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

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