#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;
};

/* Variables and functions */
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
int idrnet_getaddrinfo(struct addrinfo **, char *, int, int,
                       int /*<<< orphan*/);
int sendto(int, char *, int /*<<< orphan*/, int /*<<< orphan*/,
           int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strlen(char *);
int idrnet_sendto(int sockfd, char *data, char *host, int port, int family) {
  struct addrinfo *remote_host;
  int addr_res =
      idrnet_getaddrinfo(&remote_host, host, port, family, SOCK_DGRAM);
  if (addr_res == -1) {
    return -1;
  }
  int send_res = sendto(sockfd, data, strlen(data), 0, remote_host->ai_addr,
                        remote_host->ai_addrlen);
  freeaddrinfo(remote_host);
  return send_res;
}


x86 assembly (clang, -O0):
<idrnet_sendto>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    %r8d,-0x20(%rbp)
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
mov    -0x20(%rbp),%ecx
mov    0x2e7d(%rip),%rax
mov    (%rax),%r8d
lea    -0x28(%rbp),%rdi
call   1050 <idrnet_getaddrinfo@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0xffffffff,-0x2c(%rbp)
jne    1190 <idrnet_sendto+0x50>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d8 <idrnet_sendto+0x98>
mov    -0x8(%rbp),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x3c(%rbp),%edi
mov    -0x38(%rbp),%rsi
mov    %eax,%edx
mov    -0x28(%rbp),%rax
mov    0x4(%rax),%r8d
mov    -0x28(%rbp),%rax
mov    (%rax),%r9d
xor    %ecx,%ecx
call   1040 <sendto@plt>
mov    %eax,-0x30(%rbp)
mov    -0x28(%rbp),%rdi
call   1060 <freeaddrinfo@plt>
mov    -0x30(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<idrnet_sendto>:
push   %rbp
push   %rbx
push   %rax
mov    %r8d,%eax
mov    %rsi,%rbx
mov    %edi,%ebp
mov    0x2e96(%rip),%rsi
mov    (%rsi),%r8d
mov    %rsp,%rdi
mov    %rdx,%rsi
mov    %ecx,%edx
mov    %eax,%ecx
call   1050 <idrnet_getaddrinfo@plt>
cmp    $0xffffffff,%eax
je     1197 <idrnet_sendto+0x57>
mov    %rbx,%rdi
call   1030 <strlen@plt>
mov    (%rsp),%rcx
mov    (%rcx),%r9d
mov    0x4(%rcx),%r8d
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
xor    %ecx,%ecx
call   1040 <sendto@plt>
mov    %eax,%ebx
mov    (%rsp),%rdi
call   1060 <freeaddrinfo@plt>
jmp    119c <idrnet_sendto+0x5c>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.addrinfo = type { i32, i32 }
@SOCK_DGRAM = global i32 0
define i32 @idrnet_sendto(i32 %0, i8* %1, i8* %2, i32 %3, i32 %4) {
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i32
%12 = alloca %struct.addrinfo*
%13 = alloca i32
%14 = alloca i32
store i32 %0, i32* %7
store i8* %1, i8** %8
store i8* %2, i8** %9
store i32 %3, i32* %10
store i32 %4, i32* %11
%15 = load i8*, i8** %9
%16 = load i32, i32* %10
%17 = load i32, i32* %11
%18 = load i32, i32* @SOCK_DGRAM
%19 = call i32 @idrnet_getaddrinfo(%struct.addrinfo** %12, i8* %15, i32 %16, i32 %17, i32 %18)
store i32 %19, i32* %13
%20 = load i32, i32* %13
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %23
22: ; preds = %5
store i32 -1, i32* %6
br label %38
23: ; preds = %5
%24 = load i32, i32* %7
%25 = load i8*, i8** %8
%26 = load i8*, i8** %8
%27 = call i32 @strlen(i8* %26)
%28 = load %struct.addrinfo*, %struct.addrinfo** %12
%29 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %28, i32 0, i32 1
%30 = load i32, i32* %29
%31 = load %struct.addrinfo*, %struct.addrinfo** %12
%32 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %31, i32 0, i32 0
%33 = load i32, i32* %32
%34 = call i32 @sendto(i32 %24, i8* %25, i32 %27, i32 0, i32 %30, i32 %33)
store i32 %34, i32* %14
%35 = load %struct.addrinfo*, %struct.addrinfo** %12
%36 = call i32 @freeaddrinfo(%struct.addrinfo* %35)
%37 = load i32, i32* %14
store i32 %37, i32* %6
br label %38
38: ; preds = %23, %22
%39 = load i32, i32* %6
ret i32 %39
}

LLVM IR (-O2):
%struct.addrinfo = type { i32, i32 }
@SOCK_DGRAM = global i32 0
define i32 @idrnet_sendto(i32 %0, i8* %1, i8* %2, i32 %3, i32 %4) {
%6 = alloca %struct.addrinfo*
%7 = bitcast %struct.addrinfo** %6 to i8*
%8 = load i32, i32* @SOCK_DGRAM
%9 = call i32 @idrnet_getaddrinfo(%struct.addrinfo** %6, i8* %2, i32 %3, i32 %4, i32 %8)
%10 = icmp eq i32 %9, -1
br i1 %10, label %21, label %11
11: ; preds = %5
%12 = call i32 @strlen(i8* %1)
%13 = load %struct.addrinfo*, %struct.addrinfo** %6
%14 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %13, i64 0, i32 1
%15 = load i32, i32* %14
%16 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %13, i64 0, i32 0
%17 = load i32, i32* %16
%18 = call i32 @sendto(i32 %0, i8* %1, i32 %12, i32 0, i32 %15, i32 %17)
%19 = load %struct.addrinfo*, %struct.addrinfo** %6
%20 = call i32 @freeaddrinfo(%struct.addrinfo* %19)
br label %21
21: ; preds = %5, %11
%22 = phi i32 [ %18, %11 ], [ -1, %5 ]
ret i32 %22
}