#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 */
typedef int /*<<< orphan*/ target_si;
struct sockaddr_in {
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ ai_addrlen;
  int /*<<< orphan*/ ai_addr;
  struct sockaddr_in *ai_next;
  int /*<<< orphan*/ ai_family;
};
struct sockaddr {
  int dummy;
};
struct addrinfo {
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ ai_addrlen;
  int /*<<< orphan*/ ai_addr;
  struct addrinfo *ai_next;
  int /*<<< orphan*/ ai_family;
};
typedef int /*<<< orphan*/ hints;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ freeaddrinfo(struct sockaddr_in *);
int getaddrinfo(char *, int /*<<< orphan*/ *, struct sockaddr_in *,
                struct sockaddr_in **);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ memcpy(struct sockaddr_in *, int /*<<< orphan*/,
                          int /*<<< orphan*/);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int sendto(int, char const *, int, int /*<<< orphan*/, struct sockaddr *, int);
void send_udp_datagram(void *buf, int len, int socket, char *target_host,
                       int target_port) {
  struct sockaddr_in target_si;
  struct addrinfo hints, *res = NULL, *p;
  int errcode;
  memset(&target_si, 0, sizeof(target_si));
  memset(&hints, 0, sizeof(hints));
  hints.ai_family = AF_INET;
  if ((errcode = getaddrinfo(target_host, NULL, &hints, &res)) || !res)
    return;
  for (p = res; p != NULL; p = p->ai_next) {
    memcpy(&target_si, res->ai_addr, res->ai_addrlen);
    break;
  }
  freeaddrinfo(res);
  target_si.sin_port = htons(target_port);
  if (sendto(socket, (const char *)buf, len, 0, (struct sockaddr *)&target_si,
             sizeof(target_si)) == -1)
    return;
}


x86 assembly (clang, -O0):
<send_udp_datagram>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    %rcx,-0x18(%rbp)
mov    %r8d,-0x1c(%rbp)
movq   $0x0,-0x68(%rbp)
lea    -0x40(%rbp),%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1040 <memset@plt>
lea    -0x60(%rbp),%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1040 <memset@plt>
mov    0x2e3c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x48(%rbp)
mov    -0x18(%rbp),%rdi
lea    -0x60(%rbp),%rdx
lea    -0x68(%rbp),%rcx
xor    %eax,%eax
mov    %eax,%esi
call   1070 <getaddrinfo@plt>
mov    %eax,-0x74(%rbp)
cmp    $0x0,%eax
jne    11dd <send_udp_datagram+0x7d>
cmpq   $0x0,-0x68(%rbp)
jne    11e2 <send_udp_datagram+0x82>
jmp    1249 <send_udp_datagram+0xe9>
mov    -0x68(%rbp),%rax
mov    %rax,-0x70(%rbp)
cmpq   $0x0,-0x70(%rbp)
je     120c <send_udp_datagram+0xac>
mov    -0x68(%rbp),%rax
mov    0x8(%rax),%esi
mov    -0x68(%rbp),%rax
mov    0x4(%rax),%edx
lea    -0x40(%rbp),%rdi
call   1060 <memcpy@plt>
mov    -0x68(%rbp),%rdi
call   1080 <freeaddrinfo@plt>
mov    -0x1c(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0x40(%rbp)
mov    -0x10(%rbp),%edi
mov    -0x8(%rbp),%rsi
mov    -0xc(%rbp),%edx
lea    -0x40(%rbp),%r8
xor    %ecx,%ecx
mov    $0x20,%r9d
call   1050 <sendto@plt>
cmp    $0xffffffff,%eax
jne    1249 <send_udp_datagram+0xe9>
jmp    1249 <send_udp_datagram+0xe9>
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<send_udp_datagram>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x48,%rsp
mov    %r8d,%r13d
mov    %rcx,%rbx
mov    %edx,%r15d
mov    %esi,%r14d
mov    %rdi,%r12
movq   $0x0,(%rsp)
lea    0x8(%rsp),%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1040 <memset@plt>
lea    0x28(%rsp),%rbp
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1040 <memset@plt>
mov    0x2e37(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x40(%rsp)
mov    %rsp,%rcx
mov    %rbx,%rdi
xor    %esi,%esi
mov    %rbp,%rdx
call   1070 <getaddrinfo@plt>
test   %eax,%eax
jne    1215 <send_udp_datagram+0xb5>
mov    (%rsp),%rax
test   %rax,%rax
je     1215 <send_udp_datagram+0xb5>
mov    0x4(%rax),%edx
mov    0x8(%rax),%esi
lea    0x8(%rsp),%rbx
mov    %rbx,%rdi
call   1060 <memcpy@plt>
mov    (%rsp),%rdi
call   1080 <freeaddrinfo@plt>
mov    %r13d,%edi
call   1030 <htons@plt>
mov    %eax,0x8(%rsp)
mov    %r15d,%edi
mov    %r12,%rsi
mov    %r14d,%edx
xor    %ecx,%ecx
mov    %rbx,%r8
mov    $0x20,%r9d
call   1050 <sendto@plt>
add    $0x48,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32, i32, %struct.sockaddr_in*, i32 }
%struct.addrinfo = type { i32, i32, i32, %struct.addrinfo*, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
define void @send_udp_datagram(i8* %0, i32 %1, i32 %2, i8* %3, i32 %4) {
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i32
%11 = alloca %struct.sockaddr_in
%12 = alloca %struct.addrinfo
%13 = alloca %struct.addrinfo*
%14 = alloca %struct.addrinfo*
%15 = alloca i32
store i8* %0, i8** %6
store i32 %1, i32* %7
store i32 %2, i32* %8
store i8* %3, i8** %9
store i32 %4, i32* %10
store %struct.addrinfo* null, %struct.addrinfo** %13
%16 = call i32 @memset(%struct.sockaddr_in* %11, i32 0, i32 32)
%17 = bitcast %struct.addrinfo* %12 to %struct.sockaddr_in*
%18 = call i32 @memset(%struct.sockaddr_in* %17, i32 0, i32 32)
%19 = load i32, i32* @AF_INET
%20 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %12, i32 0, i32 4
store i32 %19, i32* %20
%21 = load i8*, i8** %9
%22 = bitcast %struct.addrinfo* %12 to %struct.sockaddr_in*
%23 = bitcast %struct.addrinfo** %13 to %struct.sockaddr_in**
%24 = call i32 @getaddrinfo(i8* %21, i32* null, %struct.sockaddr_in* %22, %struct.sockaddr_in** %23)
store i32 %24, i32* %15
%25 = icmp ne i32 %24, 0
br i1 %25, label %29, label %26
26: ; preds = %5
%27 = load %struct.addrinfo*, %struct.addrinfo** %13
%28 = icmp ne %struct.addrinfo* %27, null
br i1 %28, label %30, label %29
29: ; preds = %26, %5
br label %61
30: ; preds = %26
%31 = load %struct.addrinfo*, %struct.addrinfo** %13
store %struct.addrinfo* %31, %struct.addrinfo** %14
br label %32
32: ; preds = %43, %30
%33 = load %struct.addrinfo*, %struct.addrinfo** %14
%34 = icmp ne %struct.addrinfo* %33, null
br i1 %34, label %35, label %47
35: ; preds = %32
%36 = load %struct.addrinfo*, %struct.addrinfo** %13
%37 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %36, i32 0, i32 2
%38 = load i32, i32* %37
%39 = load %struct.addrinfo*, %struct.addrinfo** %13
%40 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %39, i32 0, i32 1
%41 = load i32, i32* %40
%42 = call i32 @memcpy(%struct.sockaddr_in* %11, i32 %38, i32 %41)
br label %47
43: ; No predecessors!
%44 = load %struct.addrinfo*, %struct.addrinfo** %14
%45 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %44, i32 0, i32 3
%46 = load %struct.addrinfo*, %struct.addrinfo** %45
store %struct.addrinfo* %46, %struct.addrinfo** %14
br label %32
47: ; preds = %35, %32
%48 = load %struct.addrinfo*, %struct.addrinfo** %13
%49 = bitcast %struct.addrinfo* %48 to %struct.sockaddr_in*
%50 = call i32 @freeaddrinfo(%struct.sockaddr_in* %49)
%51 = load i32, i32* %10
%52 = call i32 @htons(i32 %51)
%53 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %11, i32 0, i32 0
store i32 %52, i32* %53
%54 = load i32, i32* %8
%55 = load i8*, i8** %6
%56 = load i32, i32* %7
%57 = bitcast %struct.sockaddr_in* %11 to %struct.sockaddr*
%58 = call i32 @sendto(i32 %54, i8* %55, i32 %56, i32 0, %struct.sockaddr* %57, i32 32)
%59 = icmp eq i32 %58, -1
br i1 %59, label %60, label %61
60: ; preds = %47
br label %61
61: ; preds = %29, %60, %47
ret void
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, i32, %struct.sockaddr_in*, i32 }
%struct.addrinfo = type { i32, i32, i32, %struct.addrinfo*, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
define void @send_udp_datagram(i8* %0, i32 %1, i32 %2, i8* %3, i32 %4) {
%6 = alloca %struct.sockaddr_in
%7 = alloca %struct.addrinfo
%8 = alloca %struct.addrinfo*
%9 = bitcast %struct.sockaddr_in* %6 to i8*
%10 = bitcast %struct.addrinfo* %7 to i8*
%11 = bitcast %struct.addrinfo** %8 to i8*
store %struct.addrinfo* null, %struct.addrinfo** %8
%12 = call i32 @memset(%struct.sockaddr_in* %6, i32 0, i32 32)
%13 = bitcast %struct.addrinfo* %7 to %struct.sockaddr_in*
%14 = call i32 @memset(%struct.sockaddr_in* %13, i32 0, i32 32)
%15 = load i32, i32* @AF_INET
%16 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %7, i64 0, i32 4
store i32 %15, i32* %16
%17 = bitcast %struct.addrinfo** %8 to %struct.sockaddr_in**
%18 = call i32 @getaddrinfo(i8* %3, i32* null, %struct.sockaddr_in* %13, %struct.sockaddr_in** %17)
%19 = icmp eq i32 %18, 0
%20 = load %struct.addrinfo*, %struct.addrinfo** %8
%21 = icmp ne %struct.addrinfo* %20, null
%22 = select i1 %19, i1 %21, i1 false
br i1 %22, label %23, label %35
23: ; preds = %5
%24 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %20, i64 0, i32 2
%25 = load i32, i32* %24
%26 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %20, i64 0, i32 1
%27 = load i32, i32* %26
%28 = call i32 @memcpy(%struct.sockaddr_in* %6, i32 %25, i32 %27)
%29 = load %struct.sockaddr_in*, %struct.sockaddr_in** %17
%30 = call i32 @freeaddrinfo(%struct.sockaddr_in* %29)
%31 = call i32 @htons(i32 %4)
%32 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i64 0, i32 0
store i32 %31, i32* %32
%33 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%34 = call i32 @sendto(i32 %2, i8* %0, i32 %1, i32 0, %struct.sockaddr* %33, i32 32)
br label %35
35: ; preds = %23, %5
ret void
}