#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 */
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;
};
typedef int ssize_t;
typedef int /*<<< orphan*/ remote_addr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ close(int);
struct hostent *gethostbyname(char const *);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ memcpy(int /*<<< orphan*/ *, int /*<<< orphan*/,
                          int /*<<< orphan*/);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, char const *);
int sendto(int, void const *, int, int /*<<< orphan*/, struct sockaddr *, int);
int udp_socket_create();
int udp_send(const char *ipaddr, int port, const void *data, int datalen) {
  struct sockaddr_in remote_addr;
  memset(&remote_addr, 0, sizeof(struct sockaddr_in));
  remote_addr.sin_family = AF_INET;
  remote_addr.sin_port = htons(port);
  struct hostent *host = gethostbyname(ipaddr);
  if (host == NULL) {
    printf("Couldn't resolve host %s\n", ipaddr);
    return -1;
  }
  memcpy(&remote_addr.sin_addr.s_addr, host->h_addr, host->h_length);
  int sock = udp_socket_create();
  ssize_t res = sendto(sock, data, datalen, 0, (struct sockaddr *)&remote_addr,
                       sizeof(remote_addr));
  close(sock);
  return res != datalen;
}


x86 assembly (clang, -O0):
<udp_send>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %ecx,-0x24(%rbp)
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
mov    0x2e3b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    -0x14(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x10(%rbp),%rdi
call   1080 <gethostbyname@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    11f1 <udp_send+0x71>
mov    -0x10(%rbp),%rsi
lea    0xe20(%rip),%rdi
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1248 <udp_send+0xc8>
lea    -0x30(%rbp),%rdi
mov    -0x38(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x38(%rbp),%rax
mov    (%rax),%edx
call   1090 <memcpy@plt>
mov    $0x0,%al
call   10a0 <udp_socket_create@plt>
mov    %eax,-0x3c(%rbp)
mov    -0x3c(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    -0x24(%rbp),%edx
lea    -0x30(%rbp),%r8
xor    %ecx,%ecx
mov    $0xc,%r9d
call   1060 <sendto@plt>
mov    %eax,-0x40(%rbp)
mov    -0x3c(%rbp),%edi
call   1070 <close@plt>
mov    -0x40(%rbp),%eax
cmp    -0x24(%rbp),%eax
setne  %al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<udp_send>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %ecx,%r15d
mov    %rdx,%r14
mov    %esi,%ebp
mov    %rdi,%rbx
lea    0x8(%rsp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
mov    0x2e3b(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
mov    %ebp,%edi
call   1030 <htons@plt>
mov    %eax,0xc(%rsp)
mov    %rbx,%rdi
call   1080 <gethostbyname@plt>
test   %rax,%rax
je     1211 <udp_send+0x91>
mov    (%rax),%edx
mov    0x4(%rax),%esi
lea    0x8(%rsp),%rbx
mov    %rbx,%rdi
call   1090 <memcpy@plt>
xor    %eax,%eax
call   10a0 <udp_socket_create@plt>
mov    %eax,%ebp
mov    %eax,%edi
mov    %r14,%rsi
mov    %r15d,%edx
xor    %ecx,%ecx
mov    %rbx,%r8
mov    $0xc,%r9d
call   1060 <sendto@plt>
mov    %eax,%ebx
mov    %ebp,%edi
call   1070 <close@plt>
xor    %eax,%eax
cmp    %r15d,%ebx
setne  %al
jmp    1225 <udp_send+0xa5>
lea    0xde8(%rip),%rdi
mov    %rbx,%rsi
call   1040 <printf@plt>
mov    $0xffffffff,%eax
add    $0x18,%rsp
pop    %rbx
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 = [26 x i8] c"Couldn't resolve host %s\0A\00"
define i32 @udp_send(i8* %0, i32 %1, i8* %2, i32 %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i32
%10 = alloca %struct.sockaddr_in
%11 = alloca %struct.hostent*
%12 = alloca i32
%13 = alloca i32
store i8* %0, i8** %6
store i32 %1, i32* %7
store i8* %2, i8** %8
store i32 %3, i32* %9
%14 = call i32 @memset(%struct.sockaddr_in* %10, i32 0, i32 12)
%15 = load i32, i32* @AF_INET
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 2
store i32 %15, i32* %16
%17 = load i32, i32* %7
%18 = call i32 @htons(i32 %17)
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 1
store i32 %18, i32* %19
%20 = load i8*, i8** %6
%21 = call %struct.hostent* @gethostbyname(i8* %20)
store %struct.hostent* %21, %struct.hostent** %11
%22 = load %struct.hostent*, %struct.hostent** %11
%23 = icmp eq %struct.hostent* %22, null
br i1 %23, label %24, label %27
24: ; preds = %4
%25 = load i8*, i8** %6
%26 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i8* %25)
store i32 -1, i32* %5
br label %49
27: ; preds = %4
%28 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 0
%29 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %28, i32 0, i32 0
%30 = load %struct.hostent*, %struct.hostent** %11
%31 = getelementptr inbounds %struct.hostent, %struct.hostent* %30, i32 0, i32 1
%32 = load i32, i32* %31
%33 = load %struct.hostent*, %struct.hostent** %11
%34 = getelementptr inbounds %struct.hostent, %struct.hostent* %33, i32 0, i32 0
%35 = load i32, i32* %34
%36 = call i32 @memcpy(i32* %29, i32 %32, i32 %35)
%37 = call i32 (...) @udp_socket_create()
store i32 %37, i32* %12
%38 = load i32, i32* %12
%39 = load i8*, i8** %8
%40 = load i32, i32* %9
%41 = bitcast %struct.sockaddr_in* %10 to %struct.sockaddr*
%42 = call i32 @sendto(i32 %38, i8* %39, i32 %40, i32 0, %struct.sockaddr* %41, i32 12)
store i32 %42, i32* %13
%43 = load i32, i32* %12
%44 = call i32 @close(i32 %43)
%45 = load i32, i32* %13
%46 = load i32, i32* %9
%47 = icmp ne i32 %45, %46
%48 = zext i1 %47 to i32
store i32 %48, i32* %5
br label %49
49: ; preds = %27, %24
%50 = load i32, i32* %5
ret i32 %50
}

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 = [26 x i8] c"Couldn't resolve host %s\0A\00"
define i32 @udp_send(i8* %0, i32 %1, i8* %2, i32 %3) {
%5 = alloca %struct.sockaddr_in
%6 = bitcast %struct.sockaddr_in* %5 to i8*
%7 = call i32 @memset(%struct.sockaddr_in* %5, i32 0, i32 12)
%8 = load i32, i32* @AF_INET
%9 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 2
store i32 %8, i32* %9
%10 = call i32 @htons(i32 %1)
%11 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 1
store i32 %10, i32* %11
%12 = call %struct.hostent* @gethostbyname(i8* %0)
%13 = icmp eq %struct.hostent* %12, null
br i1 %13, label %14, label %16
14: ; preds = %4
%15 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %29
16: ; preds = %4
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 0, i32 0
%18 = getelementptr inbounds %struct.hostent, %struct.hostent* %12, i64 0, i32 1
%19 = load i32, i32* %18
%20 = getelementptr inbounds %struct.hostent, %struct.hostent* %12, i64 0, i32 0
%21 = load i32, i32* %20
%22 = call i32 @memcpy(i32* %17, i32 %19, i32 %21)
%23 = call i32 (...) @udp_socket_create()
%24 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%25 = call i32 @sendto(i32 %23, i8* %2, i32 %3, i32 0, %struct.sockaddr* %24, i32 12)
%26 = call i32 @close(i32 %23)
%27 = icmp ne i32 %25, %3
%28 = zext i1 %27 to i32
br label %29
29: ; preds = %16, %14
%30 = phi i32 [ -1, %14 ], [ %28, %16 ]
ret i32 %30
}