#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 */
typedef struct TYPE_2__ TYPE_1__;

/* Type definitions */
typedef int /*<<< orphan*/ target_si;
struct TYPE_2__ {
  int /*<<< orphan*/ s_addr;
};
struct sockaddr_in {
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
struct hostent {
  int /*<<< orphan*/ h_length;
  int /*<<< orphan*/ h_addr;
};

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ diep(char *);
struct hostent *gethostbyname2(char *, int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ memmove(int /*<<< orphan*/ *, int /*<<< orphan*/,
                           int /*<<< orphan*/);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int sendto(int, void *, 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 hostent *he;
  memset((char *)&target_si, 0, sizeof(target_si));
  target_si.sin_family = AF_INET;
  target_si.sin_port = htons(target_port);
  if (!(he = gethostbyname2(target_host, AF_INET)))
    diep("gethostbyname2");
  memmove(&target_si.sin_addr.s_addr, he->h_addr, he->h_length);
  if (sendto(socket, buf, len, 0, (struct sockaddr *)&target_si,
             sizeof(target_si)) == -1)
    diep("sendto");
}


x86 assembly (clang, -O0):
<send_udp_datagram>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    %rcx,-0x18(%rbp)
mov    %r8d,-0x1c(%rbp)
lea    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1040 <memset@plt>
mov    0x2e57(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x1c(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0x24(%rbp)
mov    -0x18(%rbp),%rdi
mov    0x2e3c(%rip),%rax
mov    (%rax),%esi
call   1060 <gethostbyname2@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
jne    11cd <send_udp_datagram+0x6d>
lea    0xe38(%rip),%rdi
call   1070 <diep@plt>
lea    -0x28(%rbp),%rdi
mov    -0x30(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x30(%rbp),%rax
mov    (%rax),%edx
call   1080 <memmove@plt>
mov    -0x10(%rbp),%edi
mov    -0x8(%rbp),%rsi
mov    -0xc(%rbp),%edx
lea    -0x28(%rbp),%r8
xor    %ecx,%ecx
mov    $0xc,%r9d
call   1050 <sendto@plt>
cmp    $0xffffffff,%eax
jne    1213 <send_udp_datagram+0xb3>
lea    0xe01(%rip),%rdi
call   1070 <diep@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<send_udp_datagram>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %r8d,%ebp
mov    %rcx,%r13
mov    %edx,%r15d
mov    %esi,%r14d
mov    %rdi,%r12
lea    0x8(%rsp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1040 <memset@plt>
mov    0x2e53(%rip),%rbx
mov    (%rbx),%eax
mov    %eax,0x10(%rsp)
mov    %ebp,%edi
call   1030 <htons@plt>
mov    %eax,0xc(%rsp)
mov    (%rbx),%esi
mov    %r13,%rdi
call   1060 <gethostbyname2@plt>
mov    %rax,%rbp
test   %rax,%rax
jne    11c4 <send_udp_datagram+0x64>
lea    0xe41(%rip),%rdi
call   1070 <diep@plt>
mov    0x0(%rbp),%edx
mov    0x4(%rbp),%esi
lea    0x8(%rsp),%rbx
mov    %rbx,%rdi
call   1080 <memmove@plt>
mov    %r15d,%edi
mov    %r12,%rsi
mov    %r14d,%edx
xor    %ecx,%ecx
mov    %rbx,%r8
mov    $0xc,%r9d
call   1050 <sendto@plt>
cmp    $0xffffffff,%eax
jne    1201 <send_udp_datagram+0xa1>
lea    0xe13(%rip),%rdi
call   1070 <diep@plt>
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.hostent = type { i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@.str = [15 x i8] c"gethostbyname2\00"
@.str.1 = [7 x i8] c"sendto\00"
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.hostent*
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
%13 = bitcast %struct.sockaddr_in* %11 to i8*
%14 = call i32 @memset(i8* %13, i32 0, i32 12)
%15 = load i32, i32* @AF_INET
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %11, i32 0, i32 2
store i32 %15, i32* %16
%17 = load i32, i32* %10
%18 = call i32 @htons(i32 %17)
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %11, i32 0, i32 1
store i32 %18, i32* %19
%20 = load i8*, i8** %9
%21 = load i32, i32* @AF_INET
%22 = call %struct.hostent* @gethostbyname2(i8* %20, i32 %21)
store %struct.hostent* %22, %struct.hostent** %12
%23 = icmp ne %struct.hostent* %22, null
br i1 %23, label %26, label %24
24: ; preds = %5
%25 = call i32 @diep(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
br label %26
26: ; preds = %24, %5
%27 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %11, i32 0, i32 0
%28 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %27, i32 0, i32 0
%29 = load %struct.hostent*, %struct.hostent** %12
%30 = getelementptr inbounds %struct.hostent, %struct.hostent* %29, i32 0, i32 1
%31 = load i32, i32* %30
%32 = load %struct.hostent*, %struct.hostent** %12
%33 = getelementptr inbounds %struct.hostent, %struct.hostent* %32, i32 0, i32 0
%34 = load i32, i32* %33
%35 = call i32 @memmove(i32* %28, i32 %31, i32 %34)
%36 = load i32, i32* %8
%37 = load i8*, i8** %6
%38 = load i32, i32* %7
%39 = bitcast %struct.sockaddr_in* %11 to %struct.sockaddr*
%40 = call i32 @sendto(i32 %36, i8* %37, i32 %38, i32 0, %struct.sockaddr* %39, i32 12)
%41 = icmp eq i32 %40, -1
br i1 %41, label %42, label %44
42: ; preds = %26
%43 = call i32 @diep(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
br label %44
44: ; preds = %42, %26
ret void
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.hostent = type { i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@.str = [15 x i8] c"gethostbyname2\00"
@.str.1 = [7 x i8] c"sendto\00"
define void @send_udp_datagram(i8* %0, i32 %1, i32 %2, i8* %3, i32 %4) {
%6 = alloca %struct.sockaddr_in
%7 = bitcast %struct.sockaddr_in* %6 to i8*
%8 = call i32 @memset(i8* %7, i32 0, i32 12)
%9 = load i32, i32* @AF_INET
%10 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i64 0, i32 2
store i32 %9, i32* %10
%11 = call i32 @htons(i32 %4)
%12 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i64 0, i32 1
store i32 %11, i32* %12
%13 = load i32, i32* @AF_INET
%14 = call %struct.hostent* @gethostbyname2(i8* %3, i32 %13)
%15 = icmp eq %struct.hostent* %14, null
br i1 %15, label %16, label %18
16: ; preds = %5
%17 = call i32 @diep(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
br label %18
18: ; preds = %16, %5
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i64 0, i32 0, i32 0
%20 = getelementptr inbounds %struct.hostent, %struct.hostent* %14, i64 0, i32 1
%21 = load i32, i32* %20
%22 = getelementptr inbounds %struct.hostent, %struct.hostent* %14, i64 0, i32 0
%23 = load i32, i32* %22
%24 = call i32 @memmove(i32* %19, i32 %21, i32 %23)
%25 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%26 = call i32 @sendto(i32 %2, i8* %0, i32 %1, i32 0, %struct.sockaddr* %25, i32 12)
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %30
28: ; preds = %18
%29 = call i32 @diep(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
br label %30
30: ; preds = %28, %18
ret void
}