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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ htons(int /*<<< orphan*/);
int /*<<< orphan*/ inet_addr(char const *);
int /*<<< orphan*/ perror(char *);
int sendto(int, void const *, size_t, int /*<<< orphan*/,
           struct sockaddr const *, int);
ssize_t sock_sendto(int sockfd, const void *buf, size_t len,
                    const char *dest_addr_str, uint32_t dest_port) {
  struct sockaddr_in addr;
  addr.sin_family = AF_INET;
  addr.sin_port = htons(dest_port);
  addr.sin_addr.s_addr = inet_addr(dest_addr_str);
  socklen_t addrlen = sizeof(addr);
  ssize_t sbytes;
  sbytes = sendto(sockfd, buf, len, 0, (const struct sockaddr *)&addr, addrlen);
  if (sbytes == -1) {
    perror("sendto");
    return -1;
  }
  return sbytes;
}


x86 assembly (clang, -O0):
<sock_sendto>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8d,-0x24(%rbp)
mov    0x2e86(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    -0x24(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x20(%rbp),%rdi
call   1050 <inet_addr@plt>
mov    %eax,-0x30(%rbp)
movl   $0xc,-0x34(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
lea    -0x30(%rbp),%r8
mov    -0x34(%rbp),%r9d
xor    %ecx,%ecx
call   1040 <sendto@plt>
mov    %eax,-0x38(%rbp)
cmpl   $0xffffffff,-0x38(%rbp)
jne    11c4 <sock_sendto+0x84>
lea    0xe4d(%rip),%rdi
call   1060 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ca <sock_sendto+0x8a>
mov    -0x38(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sock_sendto>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rcx,%rbx
mov    %rdx,%r14
mov    %rsi,%r15
mov    %edi,%ebp
mov    0x2e8c(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
mov    %r8d,%edi
call   1030 <htons@plt>
mov    %eax,0xc(%rsp)
mov    %rbx,%rdi
call   1050 <inet_addr@plt>
mov    %eax,0x8(%rsp)
lea    0x8(%rsp),%r8
mov    %ebp,%edi
mov    %r15,%rsi
mov    %r14,%rdx
xor    %ecx,%ecx
mov    $0xc,%r9d
call   1040 <sendto@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
je     11a8 <sock_sendto+0x68>
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe51(%rip),%rdi
call   1060 <perror@plt>
jmp    119b <sock_sendto+0x5b>

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

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@.str = [7 x i8] c"sendto\00"
define i32 @sock_sendto(i32 %0, i8* %1, i64 %2, i8* %3, i32 %4) {
%6 = alloca %struct.sockaddr_in
%7 = bitcast %struct.sockaddr_in* %6 to i8*
%8 = load i32, i32* @AF_INET
%9 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i64 0, i32 2
store i32 %8, i32* %9
%10 = tail call i32 @htons(i32 %4)
%11 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i64 0, i32 1
store i32 %10, i32* %11
%12 = tail call i32 @inet_addr(i8* %3)
%13 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i64 0, i32 0, i32 0
store i32 %12, i32* %13
%14 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%15 = call i32 @sendto(i32 %0, i8* %1, i64 %2, i32 0, %struct.sockaddr* %14, i32 12)
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %19
17: ; preds = %5
%18 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %19
19: ; preds = %5, %17
ret i32 %15
}