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

/* Variables and functions */
int /*<<< orphan*/ AF_UNSPEC;
int /*<<< orphan*/ AI_PASSIVE;
int /*<<< orphan*/ SOCK_DGRAM;
int 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(char const *, 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;
int udp_server(const char *hostname, const char *service, socklen_t *len) {
  int sockfd, n;
  struct addrinfo hints, *res, *rp;
  memset(&hints, 0, sizeof(hints));
  hints.ai_flags = AI_PASSIVE;
  hints.ai_family = AF_UNSPEC;
  hints.ai_socktype = SOCK_DGRAM;
  if ((n = getaddrinfo(hostname, service, &hints, &res)) != 0) {
    fprintf(stderr, "udp_server - getaddrinfo() failed: %s\n", gai_strerror(n));
    return -1;
  }
  for (rp = res; rp != NULL; rp = rp->ai_next) {
    sockfd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
    if (sockfd == -1)
      continue;
    if (bind(sockfd, rp->ai_addr, rp->ai_addrlen) != -1)
      break;
    close(sockfd);
  }
  if (rp == NULL) {
    fprintf(stderr, "udp_server failed\n");
    sockfd = -1;
  }
  if (len != NULL)
    *len = res->ai_addrlen;
  freeaddrinfo(res);
  return sockfd;
}


x86 assembly (clang, -O0):
<udp_server>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
lea    -0x50(%rbp),%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1040 <memset@plt>
mov    0x2e3d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    0x2e29(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x40(%rbp)
mov    0x2e15(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x44(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
lea    -0x50(%rbp),%rdx
lea    -0x58(%rbp),%rcx
call   1080 <getaddrinfo@plt>
mov    %eax,-0x28(%rbp)
cmp    $0x0,%eax
je     121d <udp_server+0x9d>
mov    0x2dd8(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x64(%rbp)
mov    -0x28(%rbp),%edi
call   1030 <gai_strerror@plt>
mov    -0x64(%rbp),%edi
mov    %rax,%rdx
lea    0xdf6(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12e7 <udp_server+0x167>
mov    -0x58(%rbp),%rax
mov    %rax,-0x60(%rbp)
cmpq   $0x0,-0x60(%rbp)
je     1298 <udp_server+0x118>
mov    -0x60(%rbp),%rax
mov    0x10(%rax),%edi
mov    -0x60(%rbp),%rax
mov    0xc(%rax),%esi
mov    -0x60(%rbp),%rax
mov    0x8(%rax),%edx
call   10a0 <socket@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    125c <udp_server+0xdc>
jmp    1287 <udp_server+0x107>
mov    -0x24(%rbp),%edi
mov    -0x60(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x60(%rbp),%rax
mov    (%rax),%edx
call   1070 <bind@plt>
cmp    $0xffffffff,%eax
je     127f <udp_server+0xff>
jmp    1298 <udp_server+0x118>
mov    -0x24(%rbp),%edi
call   1050 <close@plt>
mov    -0x60(%rbp),%rax
mov    0x18(%rax),%rax
mov    %rax,-0x60(%rbp)
jmp    1225 <udp_server+0xa5>
cmpq   $0x0,-0x60(%rbp)
jne    12c1 <udp_server+0x141>
mov    0x2d1e(%rip),%rax
mov    (%rax),%edi
lea    0xd74(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x24(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     12d8 <udp_server+0x158>
mov    -0x58(%rbp),%rax
mov    (%rax),%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x58(%rbp),%rdi
call   1090 <freeaddrinfo@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<udp_server>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x38,%rsp
mov    %rdx,%r14
mov    %rsi,%rbp
mov    %rdi,%rbx
lea    0x10(%rsp),%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1040 <memset@plt>
mov    0x2e3a(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x30(%rsp)
mov    0x2e25(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x20(%rsp)
mov    0x2e10(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x1c(%rsp)
lea    0x8(%rsp),%rcx
mov    %rbx,%rdi
mov    %rbp,%rsi
mov    %r15,%rdx
call   1080 <getaddrinfo@plt>
test   %eax,%eax
je     120f <udp_server+0x8f>
mov    0x2ddc(%rip),%rcx
mov    (%rcx),%ebx
mov    %eax,%edi
call   1030 <gai_strerror@plt>
lea    0xe04(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    $0xffffffff,%ebp
jmp    1288 <udp_server+0x108>
mov    0x8(%rsp),%rbx
jmp    1224 <udp_server+0xa4>
cs nopw 0x0(%rax,%rax,1)
mov    0x18(%rbx),%rbx
test   %rbx,%rbx
je     1258 <udp_server+0xd8>
mov    0x10(%rbx),%edi
mov    0x8(%rbx),%edx
mov    0xc(%rbx),%esi
call   10a0 <socket@plt>
cmp    $0xffffffff,%eax
je     1220 <udp_server+0xa0>
mov    %eax,%ebp
mov    (%rbx),%edx
mov    0x4(%rbx),%esi
mov    %eax,%edi
call   1070 <bind@plt>
cmp    $0xffffffff,%eax
jne    1274 <udp_server+0xf4>
mov    %ebp,%edi
call   1050 <close@plt>
jmp    1220 <udp_server+0xa0>
mov    0x2d69(%rip),%rax
mov    (%rax),%edi
lea    0xdbf(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    $0xffffffff,%ebp
mov    0x8(%rsp),%rdi
test   %r14,%r14
je     1283 <udp_server+0x103>
mov    (%rdi),%eax
mov    %eax,(%r14)
call   1090 <freeaddrinfo@plt>
mov    %ebp,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

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