#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*/ u_short;
struct sockaddr_in {
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
  int /*<<< orphan*/ sin_addr;
};
struct sockaddr {
  int dummy;
};
struct hostent {
  int /*<<< orphan*/ h_length;
  scalar_t__ h_addr;
};

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ bcopy(char *, char *, int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ htons(int /*<<< orphan*/);
int sendto(int, char const *, int, int /*<<< orphan*/, struct sockaddr *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int udp_send(const char *hostname, int port, const char *buf, int lenbuf) {
  int sock, len;
  struct sockaddr_in name;
  struct hostent *hp, *gethostbyname();
  if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
    return -1;
  if ((hp = gethostbyname(hostname)) == (struct hostent *)0)
    return -1;
  bcopy((char *)hp->h_addr, (char *)&name.sin_addr, hp->h_length);
  name.sin_family = AF_INET;
  name.sin_port = htons((u_short)port);
  len = sendto(sock, buf, lenbuf, 0, (struct sockaddr *)&name, sizeof name);
  (void)close(sock);
  return len;
}


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)
mov    0x2e6b(%rip),%rax
mov    (%rax),%edi
mov    0x2e5a(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,-0x28(%rbp)
cmp    $0x0,%eax
jge    11a7 <udp_send+0x47>
movl   $0xffffffff,-0x4(%rbp)
jmp    122a <udp_send+0xca>
mov    -0x10(%rbp),%rdi
mov    $0x0,%al
call   1060 <gethostbyname@plt>
mov    %rax,-0x40(%rbp)
cmp    $0x0,%rax
jne    11cc <udp_send+0x6c>
movl   $0xffffffff,-0x4(%rbp)
jmp    122a <udp_send+0xca>
mov    -0x40(%rbp),%rax
mov    0x8(%rax),%rdi
lea    -0x38(%rbp),%rsi
add    $0x8,%rsi
mov    -0x40(%rbp),%rax
mov    (%rax),%edx
call   1070 <bcopy@plt>
mov    0x2dfa(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    -0x14(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0x38(%rbp)
mov    -0x28(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    -0x24(%rbp),%edx
lea    -0x38(%rbp),%r8
xor    %ecx,%ecx
mov    $0xc,%r9d
call   1040 <sendto@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x28(%rbp),%edi
call   1050 <close@plt>
mov    -0x2c(%rbp),%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   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %ecx,0x4(%rsp)
mov    %rdx,%r15
mov    %esi,%r12d
mov    %rdi,%rbp
mov    0x2e66(%rip),%r14
mov    (%r14),%edi
mov    0x2e54(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    $0xffffffff,%ebx
test   %eax,%eax
js     11f9 <udp_send+0x99>
mov    %eax,%r13d
mov    %rbp,%rdi
xor    %eax,%eax
call   1060 <gethostbyname@plt>
test   %rax,%rax
je     11f9 <udp_send+0x99>
mov    0x8(%rax),%rsi
lea    0x10(%rsp),%rdi
mov    (%rax),%edx
call   1070 <memmove@plt>
mov    (%r14),%eax
mov    %eax,0xc(%rsp)
mov    %r12d,%edi
call   1030 <htons@plt>
mov    %eax,0x8(%rsp)
lea    0x8(%rsp),%r8
mov    %r13d,%edi
mov    %r15,%rsi
mov    0x4(%rsp),%edx
xor    %ecx,%ecx
mov    $0xc,%r9d
call   1040 <sendto@plt>
mov    %eax,%ebx
mov    %r13d,%edi
call   1050 <close@plt>
mov    %ebx,%eax
add    $0x18,%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.hostent = type { i32, i64 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
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 i32
%11 = alloca i32
%12 = alloca %struct.sockaddr_in
%13 = alloca %struct.hostent*
store i8* %0, i8** %6
store i32 %1, i32* %7
store i8* %2, i8** %8
store i32 %3, i32* %9
%14 = load i32, i32* @AF_INET
%15 = load i32, i32* @SOCK_DGRAM
%16 = call i32 @socket(i32 %14, i32 %15, i32 0)
store i32 %16, i32* %10
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %19
18: ; preds = %4
store i32 -1, i32* %5
br label %48
19: ; preds = %4
%20 = load i8*, i8** %6
%21 = call %struct.hostent* (i8*, ...) bitcast (%struct.hostent* (...)* @gethostbyname to %struct.hostent* (i8*, ...)*)(i8* %20)
store %struct.hostent* %21, %struct.hostent** %13
%22 = icmp eq %struct.hostent* %21, null
br i1 %22, label %23, label %24
23: ; preds = %19
store i32 -1, i32* %5
br label %48
24: ; preds = %19
%25 = load %struct.hostent*, %struct.hostent** %13
%26 = getelementptr inbounds %struct.hostent, %struct.hostent* %25, i32 0, i32 1
%27 = load i64, i64* %26
%28 = inttoptr i64 %27 to i8*
%29 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %12, i32 0, i32 2
%30 = bitcast i32* %29 to i8*
%31 = load %struct.hostent*, %struct.hostent** %13
%32 = getelementptr inbounds %struct.hostent, %struct.hostent* %31, i32 0, i32 0
%33 = load i32, i32* %32
%34 = call i32 @bcopy(i8* %28, i8* %30, i32 %33)
%35 = load i32, i32* @AF_INET
%36 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %12, i32 0, i32 1
store i32 %35, i32* %36
%37 = load i32, i32* %7
%38 = call i32 @htons(i32 %37)
%39 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %12, i32 0, i32 0
store i32 %38, i32* %39
%40 = load i32, i32* %10
%41 = load i8*, i8** %8
%42 = load i32, i32* %9
%43 = bitcast %struct.sockaddr_in* %12 to %struct.sockaddr*
%44 = call i32 @sendto(i32 %40, i8* %41, i32 %42, i32 0, %struct.sockaddr* %43, i32 12)
store i32 %44, i32* %11
%45 = load i32, i32* %10
%46 = call i32 @close(i32 %45)
%47 = load i32, i32* %11
store i32 %47, i32* %5
br label %48
48: ; preds = %24, %23, %18
%49 = load i32, i32* %5
ret i32 %49
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.hostent = type { i32, i64 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
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 = load i32, i32* @AF_INET
%8 = load i32, i32* @SOCK_DGRAM
%9 = tail call i32 @socket(i32 %7, i32 %8, i32 0)
%10 = icmp slt i32 %9, 0
br i1 %10, label %29, label %11
11: ; preds = %4
%12 = tail call %struct.hostent* (i8*, ...) bitcast (%struct.hostent* (...)* @gethostbyname to %struct.hostent* (i8*, ...)*)(i8* %0)
%13 = icmp eq %struct.hostent* %12, null
br i1 %13, label %29, label %14
14: ; preds = %11
%15 = getelementptr inbounds %struct.hostent, %struct.hostent* %12, i64 0, i32 1
%16 = load i64, i64* %15
%17 = inttoptr i64 %16 to i8*
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 2
%19 = bitcast i32* %18 to i8*
%20 = getelementptr inbounds %struct.hostent, %struct.hostent* %12, i64 0, i32 0
%21 = load i32, i32* %20
%22 = load i32, i32* @AF_INET
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 1
store i32 %22, i32* %23
%24 = tail call i32 @htons(i32 %1)
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 0
store i32 %24, i32* %25
%26 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%27 = call i32 @sendto(i32 %9, i8* %2, i32 %3, i32 0, %struct.sockaddr* %26, i32 12)
%28 = call i32 @close(i32 %9)
br label %29
29: ; preds = %11, %4, %14
%30 = phi i32 [ %27, %14 ], [ -1, %4 ], [ -1, %11 ]
ret i32 %30
}