#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 sockaddr_storage {
  int dummy;
};
struct addrinfo {
  int ai_addrlen;
  int /*<<< orphan*/ ai_addr;
  int /*<<< orphan*/ ai_protocol;
  int /*<<< orphan*/ ai_socktype;
  int /*<<< orphan*/ ai_family;
  struct addrinfo *ai_next;
};

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_DGRAM;
int errno;
int /*<<< orphan*/ free(struct addrinfo *);
int getaddrinfo(char *, char *, struct addrinfo *, struct addrinfo **);
int /*<<< orphan*/ memcpy(struct sockaddr_storage *, int /*<<< orphan*/, int);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int get_udp_socket(char *host, char *port, int *fd,
                   struct sockaddr_storage *addr, int *addrlen) {
  int sockfd, rc = 0;
  struct addrinfo hints, *servinfo = NULL, *p = NULL;
  memset(&hints, 0, sizeof hints);
  hints.ai_family = AF_INET;
  hints.ai_socktype = SOCK_DGRAM;
  rc = getaddrinfo(host, port, &hints, &servinfo);
  if (rc != 0) {
    return errno;
  }
  for (p = servinfo; p != NULL; p = p->ai_next) {
    if ((sockfd = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1) {
      continue;
    }
    memcpy(addr, p->ai_addr, p->ai_addrlen);
    *addrlen = p->ai_addrlen;
    *fd = sockfd;
    break;
  }
  if (p == NULL) {
    rc = errno;
  }
  free(servinfo);
  return rc;
}


x86 assembly (clang, -O0):
<get_udp_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    %r8,-0x30(%rbp)
movl   $0x0,-0x38(%rbp)
movq   $0x0,-0x60(%rbp)
movq   $0x0,-0x68(%rbp)
lea    -0x58(%rbp),%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1040 <memset@plt>
mov    0x2e4e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x48(%rbp)
mov    0x2e3a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4c(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
lea    -0x58(%rbp),%rdx
lea    -0x60(%rbp),%rcx
call   1060 <getaddrinfo@plt>
mov    %eax,-0x38(%rbp)
cmpl   $0x0,-0x38(%rbp)
je     11de <get_udp_socket+0x8e>
mov    0x2dfc(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1283 <get_udp_socket+0x133>
mov    -0x60(%rbp),%rax
mov    %rax,-0x68(%rbp)
cmpq   $0x0,-0x68(%rbp)
je     125d <get_udp_socket+0x10d>
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   1070 <socket@plt>
mov    %eax,-0x34(%rbp)
cmp    $0xffffffff,%eax
jne    121c <get_udp_socket+0xcc>
jmp    124c <get_udp_socket+0xfc>
mov    -0x28(%rbp),%rdi
mov    -0x68(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x68(%rbp),%rax
mov    (%rax),%edx
call   1050 <memcpy@plt>
mov    -0x68(%rbp),%rax
mov    (%rax),%ecx
mov    -0x30(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x34(%rbp),%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
jmp    125d <get_udp_socket+0x10d>
mov    -0x68(%rbp),%rax
mov    0x18(%rax),%rax
mov    %rax,-0x68(%rbp)
jmp    11e6 <get_udp_socket+0x96>
cmpq   $0x0,-0x68(%rbp)
jne    1274 <get_udp_socket+0x124>
mov    0x2d61(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x38(%rbp)
mov    -0x60(%rbp),%rdi
call   1030 <free@plt>
mov    -0x38(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_udp_socket>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %r8,%r14
mov    %rcx,%r12
mov    %rdx,%r15
mov    %rsi,%rbp
mov    %rdi,%rbx
movq   $0x0,(%rsp)
lea    0x8(%rsp),%r13
mov    %r13,%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1040 <memset@plt>
mov    0x2e58(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x18(%rsp)
mov    0x2e43(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x14(%rsp)
mov    %rsp,%rcx
mov    %rbx,%rdi
mov    %rbp,%rsi
mov    %r13,%rdx
call   1060 <getaddrinfo@plt>
test   %eax,%eax
je     11c3 <get_udp_socket+0x73>
mov    0x2e11(%rip),%rax
mov    (%rax),%ebx
jmp    1219 <get_udp_socket+0xc9>
mov    (%rsp),%rbx
test   %rbx,%rbx
je     11ec <get_udp_socket+0x9c>
nopl   0x0(%rax)
mov    0x10(%rbx),%edi
mov    0x8(%rbx),%edx
mov    0xc(%rbx),%esi
call   1070 <socket@plt>
cmp    $0xffffffff,%eax
jne    11f7 <get_udp_socket+0xa7>
mov    0x18(%rbx),%rbx
test   %rbx,%rbx
jne    11d0 <get_udp_socket+0x80>
mov    0x2ddd(%rip),%rax
mov    (%rax),%ebx
jmp    1210 <get_udp_socket+0xc0>
mov    %eax,%ebp
mov    (%rbx),%edx
mov    0x4(%rbx),%esi
mov    %r12,%rdi
call   1050 <memcpy@plt>
mov    (%rbx),%eax
mov    %eax,(%r14)
mov    %ebp,(%r15)
xor    %ebx,%ebx
mov    (%rsp),%rdi
call   1030 <free@plt>
mov    %ebx,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_storage = type { i32 }
%struct.addrinfo = type { i32, i32, i32, i32, i32, %struct.addrinfo* }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@errno = global i32 0
define i32 @get_udp_socket(i8* %0, i8* %1, i32* %2, %struct.sockaddr_storage* %3, i32* %4) {
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i32*
%10 = alloca %struct.sockaddr_storage*
%11 = alloca i32*
%12 = alloca i32
%13 = alloca i32
%14 = alloca %struct.addrinfo
%15 = alloca %struct.addrinfo*
%16 = alloca %struct.addrinfo*
store i8* %0, i8** %7
store i8* %1, i8** %8
store i32* %2, i32** %9
store %struct.sockaddr_storage* %3, %struct.sockaddr_storage** %10
store i32* %4, i32** %11
store i32 0, i32* %13
store %struct.addrinfo* null, %struct.addrinfo** %15
store %struct.addrinfo* null, %struct.addrinfo** %16
%17 = call i32 @memset(%struct.addrinfo* %14, i32 0, i32 32)
%18 = load i32, i32* @AF_INET
%19 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %14, i32 0, i32 4
store i32 %18, i32* %19
%20 = load i32, i32* @SOCK_DGRAM
%21 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %14, i32 0, i32 3
store i32 %20, i32* %21
%22 = load i8*, i8** %7
%23 = load i8*, i8** %8
%24 = call i32 @getaddrinfo(i8* %22, i8* %23, %struct.addrinfo* %14, %struct.addrinfo** %15)
store i32 %24, i32* %13
%25 = load i32, i32* %13
%26 = icmp ne i32 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %5
%28 = load i32, i32* @errno
store i32 %28, i32* %6
br label %75
29: ; preds = %5
%30 = load %struct.addrinfo*, %struct.addrinfo** %15
store %struct.addrinfo* %30, %struct.addrinfo** %16
br label %31
31: ; preds = %62, %29
%32 = load %struct.addrinfo*, %struct.addrinfo** %16
%33 = icmp ne %struct.addrinfo* %32, null
br i1 %33, label %34, label %66
34: ; preds = %31
%35 = load %struct.addrinfo*, %struct.addrinfo** %16
%36 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %35, i32 0, i32 4
%37 = load i32, i32* %36
%38 = load %struct.addrinfo*, %struct.addrinfo** %16
%39 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %38, i32 0, i32 3
%40 = load i32, i32* %39
%41 = load %struct.addrinfo*, %struct.addrinfo** %16
%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* %12
%45 = icmp eq i32 %44, -1
br i1 %45, label %46, label %47
46: ; preds = %34
br label %62
47: ; preds = %34
%48 = load %struct.sockaddr_storage*, %struct.sockaddr_storage** %10
%49 = load %struct.addrinfo*, %struct.addrinfo** %16
%50 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %49, i32 0, i32 1
%51 = load i32, i32* %50
%52 = load %struct.addrinfo*, %struct.addrinfo** %16
%53 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %52, i32 0, i32 0
%54 = load i32, i32* %53
%55 = call i32 @memcpy(%struct.sockaddr_storage* %48, i32 %51, i32 %54)
%56 = load %struct.addrinfo*, %struct.addrinfo** %16
%57 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %56, i32 0, i32 0
%58 = load i32, i32* %57
%59 = load i32*, i32** %11
store i32 %58, i32* %59
%60 = load i32, i32* %12
%61 = load i32*, i32** %9
store i32 %60, i32* %61
br label %66
62: ; preds = %46
%63 = load %struct.addrinfo*, %struct.addrinfo** %16
%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** %16
br label %31
66: ; preds = %47, %31
%67 = load %struct.addrinfo*, %struct.addrinfo** %16
%68 = icmp eq %struct.addrinfo* %67, null
br i1 %68, label %69, label %71
69: ; preds = %66
%70 = load i32, i32* @errno
store i32 %70, i32* %13
br label %71
71: ; preds = %69, %66
%72 = load %struct.addrinfo*, %struct.addrinfo** %15
%73 = call i32 @free(%struct.addrinfo* %72)
%74 = load i32, i32* %13
store i32 %74, i32* %6
br label %75
75: ; preds = %71, %27
%76 = load i32, i32* %6
ret i32 %76
}

LLVM IR (-O2):
%struct.sockaddr_storage = type { i32 }
%struct.addrinfo = type { i32, i32, i32, i32, i32, %struct.addrinfo* }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@errno = global i32 0
define i32 @get_udp_socket(i8* %0, i8* %1, i32* nocapture writeonly %2, %struct.sockaddr_storage* %3, i32* nocapture writeonly %4) {
%6 = alloca %struct.addrinfo
%7 = alloca %struct.addrinfo*
%8 = bitcast %struct.addrinfo* %6 to i8*
%9 = bitcast %struct.addrinfo** %7 to i8*
store %struct.addrinfo* null, %struct.addrinfo** %7
%10 = call i32 @memset(%struct.addrinfo* %6, i32 0, i32 32)
%11 = load i32, i32* @AF_INET
%12 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %6, i64 0, i32 4
store i32 %11, i32* %12
%13 = load i32, i32* @SOCK_DGRAM
%14 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %6, i64 0, i32 3
store i32 %13, i32* %14
%15 = call i32 @getaddrinfo(i8* %0, i8* %1, %struct.addrinfo* %6, %struct.addrinfo** %7)
%16 = icmp eq i32 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %5
%18 = load %struct.addrinfo*, %struct.addrinfo** %7
%19 = icmp eq %struct.addrinfo* %18, null
br i1 %19, label %43, label %22
20: ; preds = %5
%21 = load i32, i32* @errno
br label %49
22: ; preds = %17, %39
%23 = phi %struct.addrinfo* [ %41, %39 ], [ %18, %17 ]
%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 %39, 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 i32 @memcpy(%struct.sockaddr_storage* %3, i32 %34, i32 %36)
%38 = load i32, i32* %35
store i32 %38, i32* %4
store i32 %30, i32* %2
br label %45
39: ; preds = %22
%40 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %23, i64 0, i32 5
%41 = load %struct.addrinfo*, %struct.addrinfo** %40
%42 = icmp eq %struct.addrinfo* %41, null
br i1 %42, label %43, label %22
43: ; preds = %39, %17
%44 = load i32, i32* @errno
br label %45
45: ; preds = %32, %43
%46 = phi i32 [ %44, %43 ], [ 0, %32 ]
%47 = load %struct.addrinfo*, %struct.addrinfo** %7
%48 = call i32 @free(%struct.addrinfo* %47)
br label %49
49: ; preds = %45, %20
%50 = phi i32 [ %21, %20 ], [ %46, %45 ]
ret i32 %50
}