#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;
  struct addrinfo *ai_next;
  int /*<<< orphan*/ ai_addr;
  int /*<<< orphan*/ ai_protocol;
  int /*<<< orphan*/ ai_socktype;
  int /*<<< orphan*/ ai_family;
  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*/ EXIT_FAILURE;
int /*<<< orphan*/ SOCK_DGRAM;
scalar_t__ bind(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ bzero(struct addrinfo *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
char *gai_strerror(int);
int getaddrinfo(char const *, char const *, struct addrinfo *,
                struct addrinfo **);
int /*<<< orphan*/ printf(char *, char const *, char const *, ...);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int udp_server(const char *host, const char *serv, socklen_t *p_len) {
  int sockfd, n;
  struct addrinfo hints, *res, *p_res;
  bzero(&hints, sizeof(hints));
  hints.ai_flags = AI_PASSIVE;
  hints.ai_family = AF_UNSPEC;
  hints.ai_socktype = SOCK_DGRAM;
  if (0 != (n = getaddrinfo(host, serv, &hints, &res))) {
    printf("udp_server error for %s, %s : %s\n", host, serv, gai_strerror(n));
    exit(EXIT_FAILURE);
  }
  p_res = res;
  while (NULL != res) {
    sockfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
    if (sockfd < 0) {
      continue;
    }
    if (0 == bind(sockfd, res->ai_addr, res->ai_addrlen)) {
      break;
    }
    close(sockfd);
    res = res->ai_next;
  }
  if (NULL == res) {
    printf("udp_server error for %s, %s\n", host, serv);
    exit(EXIT_FAILURE);
  }
  if (NULL != p_len) {
    *p_len = res->ai_addrlen;
  }
  freeaddrinfo(p_res);
  return sockfd;
}


x86 assembly (clang, -O0):
<udp_server>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
lea    -0x48(%rbp),%rdi
mov    $0x28,%esi
call   1070 <bzero@plt>
mov    0x2e2f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    0x2e1b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    0x2e07(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
lea    -0x48(%rbp),%rdx
lea    -0x50(%rbp),%rcx
call   1090 <getaddrinfo@plt>
mov    %eax,%ecx
mov    %ecx,-0x20(%rbp)
xor    %eax,%eax
cmp    %ecx,%eax
je     1239 <udp_server+0xa9>
mov    -0x8(%rbp),%rax
mov    %rax,-0x68(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x60(%rbp)
mov    -0x20(%rbp),%edi
call   1040 <gai_strerror@plt>
mov    -0x68(%rbp),%rsi
mov    -0x60(%rbp),%rdx
mov    %rax,%rcx
lea    0xddc(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    0x2d96(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    -0x50(%rbp),%rax
mov    %rax,-0x58(%rbp)
xor    %eax,%eax
cmp    -0x50(%rbp),%rax
je     12ba <udp_server+0x12a>
mov    -0x50(%rbp),%rax
mov    0x1c(%rax),%edi
mov    -0x50(%rbp),%rax
mov    0x18(%rax),%esi
mov    -0x50(%rbp),%rax
mov    0x14(%rax),%edx
call   10b0 <socket@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    1279 <udp_server+0xe9>
jmp    1241 <udp_server+0xb1>
mov    -0x1c(%rbp),%edi
mov    -0x50(%rbp),%rax
mov    0x10(%rax),%esi
mov    -0x50(%rbp),%rax
mov    (%rax),%edx
call   1060 <bind@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
jne    12a1 <udp_server+0x111>
jmp    12ba <udp_server+0x12a>
mov    -0x1c(%rbp),%edi
call   1050 <close@plt>
mov    -0x50(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x50(%rbp)
jmp    1241 <udp_server+0xb1>
xor    %eax,%eax
cmp    -0x50(%rbp),%rax
jne    12ea <udp_server+0x15a>
mov    -0x8(%rbp),%rsi
mov    -0x10(%rbp),%rdx
lea    0xd4d(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    0x2ce5(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
xor    %eax,%eax
cmp    -0x18(%rbp),%rax
je     1302 <udp_server+0x172>
mov    -0x50(%rbp),%rax
mov    (%rax),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x58(%rbp),%rdi
call   10a0 <freeaddrinfo@plt>
mov    -0x1c(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<udp_server>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x30,%rsp
mov    %rdx,%r12
mov    %rsi,%r14
mov    %rdi,%r15
lea    0x8(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x28,%esi
call   1070 <bzero@plt>
mov    0x2e2a(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x28(%rsp)
mov    0x2e15(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x24(%rsp)
mov    0x2e00(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x20(%rsp)
mov    %rsp,%rcx
mov    %r15,%rdi
mov    %r14,%rsi
mov    %rbx,%rdx
call   1090 <getaddrinfo@plt>
test   %eax,%eax
jne    129e <udp_server+0x10e>
mov    (%rsp),%rbx
test   %rbx,%rbx
je     127c <udp_server+0xec>
mov    %rbx,%rax
jmp    1228 <udp_server+0x98>
nopl   0x0(%rax)
mov    %ebp,%edi
call   1050 <close@plt>
mov    (%rsp),%rax
mov    0x8(%rax),%rax
mov    %rax,(%rsp)
test   %rax,%rax
je     127c <udp_server+0xec>
mov    0x1c(%rax),%edi
mov    0x14(%rax),%edx
mov    0x18(%rax),%esi
call   10b0 <socket@plt>
mov    %eax,%ebp
mov    (%rsp),%rax
test   %ebp,%ebp
js     1223 <udp_server+0x93>
mov    (%rax),%edx
mov    0x10(%rax),%esi
mov    %ebp,%edi
call   1060 <bind@plt>
test   %rax,%rax
jne    1210 <udp_server+0x80>
mov    (%rsp),%rax
test   %rax,%rax
je     127c <udp_server+0xec>
test   %r12,%r12
je     1265 <udp_server+0xd5>
mov    (%rax),%eax
mov    %eax,(%r12)
mov    %rbx,%rdi
call   10a0 <freeaddrinfo@plt>
mov    %ebp,%eax
add    $0x30,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd9f(%rip),%rdi
mov    %r15,%rsi
mov    %r14,%rdx
xor    %eax,%eax
call   1030 <printf@plt>
mov    0x2d31(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    %eax,%edi
call   1040 <gai_strerror@plt>
lea    0xd54(%rip),%rdi
mov    %r15,%rsi
mov    %r14,%rdx
mov    %rax,%rcx
xor    %eax,%eax
call   1030 <printf@plt>
jmp    1290 <udp_server+0x100>

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

LLVM IR (-O2):
%struct.addrinfo = type { i32, %struct.addrinfo*, i32, i32, i32, i32, i32 }
@AI_PASSIVE = global i32 0
@AF_UNSPEC = global i32 0
@SOCK_DGRAM = global i32 0
@.str = [34 x i8] c"udp_server error for %s, %s : %s\0A\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [29 x i8] c"udp_server error for %s, %s\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 @bzero(%struct.addrinfo* %4, 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 5
store i32 %11, i32* %12
%13 = load i32, i32* @SOCK_DGRAM
%14 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i64 0, i32 4
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 %22, label %17
17: ; preds = %3
%18 = call i8* @gai_strerror(i32 %15)
%19 = call i32 (i8*, i8*, i8*, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %1, i8* %18)
%20 = load i32, i32* @EXIT_FAILURE
%21 = call i32 @exit(i32 %20)
unreachable
22: ; preds = %3
%23 = load %struct.addrinfo*, %struct.addrinfo** %5
%24 = icmp eq %struct.addrinfo* %23, null
br i1 %24, label %54, label %25
25: ; preds = %22, %48
%26 = phi %struct.addrinfo* [ %49, %48 ], [ %23, %22 ]
%27 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %26, i64 0, i32 5
%28 = load i32, i32* %27
%29 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %26, i64 0, i32 4
%30 = load i32, i32* %29
%31 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %26, i64 0, i32 3
%32 = load i32, i32* %31
%33 = call i32 @socket(i32 %28, i32 %30, i32 %32)
%34 = icmp slt i32 %33, 0
%35 = load %struct.addrinfo*, %struct.addrinfo** %5
br i1 %34, label %48, label %36
36: ; preds = %25
%37 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %35, i64 0, i32 2
%38 = load i32, i32* %37
%39 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %35, i64 0, i32 0
%40 = load i32, i32* %39
%41 = call i64 @bind(i32 %33, i32 %38, i32 %40)
%42 = icmp eq i64 %41, 0
br i1 %42, label %51, label %43
43: ; preds = %36
%44 = call i32 @close(i32 %33)
%45 = load %struct.addrinfo*, %struct.addrinfo** %5
%46 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %45, i64 0, i32 1
%47 = load %struct.addrinfo*, %struct.addrinfo** %46
store %struct.addrinfo* %47, %struct.addrinfo** %5
br label %48
48: ; preds = %25, %43
%49 = phi %struct.addrinfo* [ %47, %43 ], [ %35, %25 ]
%50 = icmp eq %struct.addrinfo* %49, null
br i1 %50, label %54, label %25
51: ; preds = %36
%52 = load %struct.addrinfo*, %struct.addrinfo** %5
%53 = icmp eq %struct.addrinfo* %52, null
br i1 %53, label %54, label %58
54: ; preds = %48, %22, %51
%55 = call i32 (i8*, i8*, i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0), i8* %0, i8* %1)
%56 = load i32, i32* @EXIT_FAILURE
%57 = call i32 @exit(i32 %56)
unreachable
58: ; preds = %51
%59 = icmp eq i32* %2, null
br i1 %59, label %63, label %60
60: ; preds = %58
%61 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %52, i64 0, i32 0
%62 = load i32, i32* %61
store i32 %62, i32* %2
br label %63
63: ; preds = %60, %58
%64 = call i32 @freeaddrinfo(%struct.addrinfo* %23)
ret i32 %33
}