#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*/ buf_htonl(void *, int);
int idrnet_getaddrinfo(struct addrinfo **, char *, int, int,
                       int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int sendto(int, void *, int, int /*<<< orphan*/, int /*<<< orphan*/,
           int /*<<< orphan*/);
int idrnet_sendto_buf(int sockfd, void *buf, int buf_len, 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;
  }
  buf_htonl(buf, buf_len);
  int send_res = sendto(sockfd, buf, buf_len, 0, remote_host->ai_addr,
                        remote_host->ai_addrlen);
  if (send_res == -1) {
    perror("lib err: sendto \n");
  }
  return send_res;
}


x86 assembly (clang, -O0):
<idrnet_sendto_buf>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8d,-0x24(%rbp)
mov    %r9d,-0x28(%rbp)
mov    -0x20(%rbp),%rsi
mov    -0x24(%rbp),%edx
mov    -0x28(%rbp),%ecx
mov    0x2e79(%rip),%rax
mov    (%rax),%r8d
lea    -0x30(%rbp),%rdi
call   1060 <idrnet_getaddrinfo@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0xffffffff,-0x34(%rbp)
jne    1194 <idrnet_sendto_buf+0x54>
movl   $0xffffffff,-0x4(%rbp)
jmp    11df <idrnet_sendto_buf+0x9f>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1030 <buf_htonl@plt>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
mov    -0x30(%rbp),%rax
mov    0x4(%rax),%r8d
mov    -0x30(%rbp),%rax
mov    (%rax),%r9d
xor    %ecx,%ecx
call   1040 <sendto@plt>
mov    %eax,-0x38(%rbp)
cmpl   $0xffffffff,-0x38(%rbp)
jne    11d9 <idrnet_sendto_buf+0x99>
lea    0xe2c(%rip),%rdi
call   1050 <perror@plt>
mov    -0x38(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<idrnet_sendto_buf>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %r8d,%eax
mov    %edx,%ebp
mov    %rsi,%rbx
mov    %edi,%r14d
mov    0x2e8e(%rip),%rdx
mov    (%rdx),%r8d
lea    0x8(%rsp),%rdi
mov    %rcx,%rsi
mov    %eax,%edx
mov    %r9d,%ecx
call   1060 <idrnet_getaddrinfo@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
cmp    $0xffffffff,%ecx
je     11a5 <idrnet_sendto_buf+0x65>
mov    %rbx,%rdi
mov    %ebp,%esi
call   1030 <buf_htonl@plt>
mov    0x8(%rsp),%rax
mov    (%rax),%r9d
mov    0x4(%rax),%r8d
mov    %r14d,%edi
mov    %rbx,%rsi
mov    %ebp,%edx
xor    %ecx,%ecx
call   1040 <sendto@plt>
cmp    $0xffffffff,%eax
je     11ae <idrnet_sendto_buf+0x6e>
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xe4b(%rip),%rdi
call   1050 <perror@plt>
mov    $0xffffffff,%eax
jmp    11a5 <idrnet_sendto_buf+0x65>

LLVM IR (-O0):
%struct.addrinfo = type { i32, i32 }
@SOCK_DGRAM = global i32 0
@.str = [18 x i8] c"lib err: sendto \0A\00"
define i32 @idrnet_sendto_buf(i32 %0, i8* %1, i32 %2, i8* %3, i32 %4, i32 %5) {
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i8*
%12 = alloca i32
%13 = alloca i32
%14 = alloca %struct.addrinfo*
%15 = alloca i32
%16 = alloca i32
store i32 %0, i32* %8
store i8* %1, i8** %9
store i32 %2, i32* %10
store i8* %3, i8** %11
store i32 %4, i32* %12
store i32 %5, i32* %13
%17 = load i8*, i8** %11
%18 = load i32, i32* %12
%19 = load i32, i32* %13
%20 = load i32, i32* @SOCK_DGRAM
%21 = call i32 @idrnet_getaddrinfo(%struct.addrinfo** %14, i8* %17, i32 %18, i32 %19, i32 %20)
store i32 %21, i32* %15
%22 = load i32, i32* %15
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %25
24: ; preds = %6
store i32 -1, i32* %7
br label %45
25: ; preds = %6
%26 = load i8*, i8** %9
%27 = load i32, i32* %10
%28 = call i32 @buf_htonl(i8* %26, i32 %27)
%29 = load i32, i32* %8
%30 = load i8*, i8** %9
%31 = load i32, i32* %10
%32 = load %struct.addrinfo*, %struct.addrinfo** %14
%33 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %32, i32 0, i32 1
%34 = load i32, i32* %33
%35 = load %struct.addrinfo*, %struct.addrinfo** %14
%36 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %35, i32 0, i32 0
%37 = load i32, i32* %36
%38 = call i32 @sendto(i32 %29, i8* %30, i32 %31, i32 0, i32 %34, i32 %37)
store i32 %38, i32* %16
%39 = load i32, i32* %16
%40 = icmp eq i32 %39, -1
br i1 %40, label %41, label %43
41: ; preds = %25
%42 = call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
br label %43
43: ; preds = %41, %25
%44 = load i32, i32* %16
store i32 %44, i32* %7
br label %45
45: ; preds = %43, %24
%46 = load i32, i32* %7
ret i32 %46
}

LLVM IR (-O2):
%struct.addrinfo = type { i32, i32 }
@SOCK_DGRAM = global i32 0
@.str = [18 x i8] c"lib err: sendto \0A\00"
define i32 @idrnet_sendto_buf(i32 %0, i8* %1, i32 %2, i8* %3, i32 %4, i32 %5) {
%7 = alloca %struct.addrinfo*
%8 = bitcast %struct.addrinfo** %7 to i8*
%9 = load i32, i32* @SOCK_DGRAM
%10 = call i32 @idrnet_getaddrinfo(%struct.addrinfo** %7, i8* %3, i32 %4, i32 %5, i32 %9)
%11 = icmp eq i32 %10, -1
br i1 %11, label %23, label %12
12: ; preds = %6
%13 = call i32 @buf_htonl(i8* %1, i32 %2)
%14 = load %struct.addrinfo*, %struct.addrinfo** %7
%15 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %14, i64 0, i32 1
%16 = load i32, i32* %15
%17 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %14, i64 0, i32 0
%18 = load i32, i32* %17
%19 = call i32 @sendto(i32 %0, i8* %1, i32 %2, i32 0, i32 %16, i32 %18)
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %23
21: ; preds = %12
%22 = call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
br label %23
23: ; preds = %12, %21, %6
%24 = phi i32 [ -1, %6 ], [ -1, %21 ], [ %19, %12 ]
ret i32 %24
}