#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 sockaddr_in {
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_addr;
};
struct sockaddr {
  int dummy;
};
typedef size_t ssize_t;

/* Variables and functions */
int /*<<< orphan*/ errno;
int /*<<< orphan*/ inet_ntoa(int /*<<< orphan*/);
int /*<<< orphan*/ log_error(char *, int, int, int /*<<< orphan*/, ...);
int /*<<< orphan*/ ntohs(int /*<<< orphan*/);
size_t sendto(int, char *, size_t, int /*<<< orphan*/, struct sockaddr *, int);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
void sock_send_packet(int sock, char *pkt, size_t pktlen,
                      struct sockaddr_in *dst) {
  ssize_t nsent = sendto(sock, pkt, pktlen, 0, (struct sockaddr *)dst,
                         sizeof(struct sockaddr_in));
  if (nsent != pktlen) {
    if (nsent < 0) {
      log_error("UDP send to %s:%hu failed: %s\n", inet_ntoa(dst->sin_addr),
                ntohs(dst->sin_port), strerror(errno));
    } else {
      log_error("Was only able to send %d out of %d bytes to %s:%hu\n",
                (int)nsent, (int)pktlen, inet_ntoa(dst->sin_addr),
                ntohs(dst->sin_port));
    }
  }
}


x86 assembly (clang, -O0):
<sock_send_packet>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
mov    -0x20(%rbp),%r8
xor    %ecx,%ecx
mov    $0x8,%r9d
call   1050 <sendto@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x18(%rbp),%rax
je     122d <sock_send_packet+0xdd>
cmpq   $0x0,-0x28(%rbp)
jae    11e6 <sock_send_packet+0x96>
mov    -0x20(%rbp),%rax
mov    0x4(%rax),%edi
call   1030 <inet_ntoa@plt>
mov    %eax,-0x30(%rbp)
mov    -0x20(%rbp),%rax
mov    (%rax),%edi
call   1060 <ntohs@plt>
mov    %eax,-0x2c(%rbp)
mov    0x2e1c(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x30(%rbp),%esi
mov    -0x2c(%rbp),%edx
mov    %eax,%ecx
lea    0xe26(%rip),%rdi
mov    $0x0,%al
call   1040 <log_error@plt>
jmp    1228 <sock_send_packet+0xd8>
mov    -0x28(%rbp),%rax
mov    %eax,-0x3c(%rbp)
mov    -0x18(%rbp),%rax
mov    %eax,-0x38(%rbp)
mov    -0x20(%rbp),%rax
mov    0x4(%rax),%edi
call   1030 <inet_ntoa@plt>
mov    %eax,-0x34(%rbp)
mov    -0x20(%rbp),%rax
mov    (%rax),%edi
call   1060 <ntohs@plt>
mov    -0x3c(%rbp),%esi
mov    -0x38(%rbp),%edx
mov    -0x34(%rbp),%ecx
mov    %eax,%r8d
lea    0xdfe(%rip),%rdi
mov    $0x0,%al
call   1040 <log_error@plt>
jmp    122d <sock_send_packet+0xdd>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sock_send_packet>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rcx,%rbx
mov    %rdx,%r14
xor    %ecx,%ecx
mov    %rbx,%r8
mov    $0x8,%r9d
call   1050 <sendto@plt>
cmp    %r14,%rax
jne    116d <sock_send_packet+0x2d>
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %rax,%r15
mov    0x4(%rbx),%edi
call   1030 <inet_ntoa@plt>
mov    %eax,%ebp
mov    (%rbx),%edi
call   1060 <ntohs@plt>
lea    0xe78(%rip),%rdi
mov    %r15d,%esi
mov    %r14d,%edx
mov    %ebp,%ecx
mov    %eax,%r8d
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <log_error@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32 }
%struct.sockaddr = type { i32 }
@.str = [31 x i8] c"UDP send to %s:%hu failed: %s\0A\00"
@errno = global i32 0
@.str.1 = [52 x i8] c"Was only able to send %d out of %d bytes to %s:%hu\0A\00"
define void @sock_send_packet(i32 %0, i8* %1, i64 %2, %struct.sockaddr_in* %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca %struct.sockaddr_in*
%9 = alloca i64
store i32 %0, i32* %5
store i8* %1, i8** %6
store i64 %2, i64* %7
store %struct.sockaddr_in* %3, %struct.sockaddr_in** %8
%10 = load i32, i32* %5
%11 = load i8*, i8** %6
%12 = load i64, i64* %7
%13 = load %struct.sockaddr_in*, %struct.sockaddr_in** %8
%14 = bitcast %struct.sockaddr_in* %13 to %struct.sockaddr*
%15 = call i64 @sendto(i32 %10, i8* %11, i64 %12, i32 0, %struct.sockaddr* %14, i32 8)
store i64 %15, i64* %9
%16 = load i64, i64* %9
%17 = load i64, i64* %7
%18 = icmp ne i64 %16, %17
br i1 %18, label %19, label %49
19: ; preds = %4
%20 = load i64, i64* %9
%21 = icmp ult i64 %20, 0
br i1 %21, label %22, label %34
22: ; preds = %19
%23 = load %struct.sockaddr_in*, %struct.sockaddr_in** %8
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %23, i32 0, i32 1
%25 = load i32, i32* %24
%26 = call i32 @inet_ntoa(i32 %25)
%27 = load %struct.sockaddr_in*, %struct.sockaddr_in** %8
%28 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %27, i32 0, i32 0
%29 = load i32, i32* %28
%30 = call i32 @ntohs(i32 %29)
%31 = load i32, i32* @errno
%32 = call i32 @strerror(i32 %31)
%33 = call i32 (i8*, i32, i32, i32, ...) @log_error(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i32 %26, i32 %30, i32 %32)
br label %48
34: ; preds = %19
%35 = load i64, i64* %9
%36 = trunc i64 %35 to i32
%37 = load i64, i64* %7
%38 = trunc i64 %37 to i32
%39 = load %struct.sockaddr_in*, %struct.sockaddr_in** %8
%40 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %39, i32 0, i32 1
%41 = load i32, i32* %40
%42 = call i32 @inet_ntoa(i32 %41)
%43 = load %struct.sockaddr_in*, %struct.sockaddr_in** %8
%44 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %43, i32 0, i32 0
%45 = load i32, i32* %44
%46 = call i32 @ntohs(i32 %45)
%47 = call i32 (i8*, i32, i32, i32, ...) @log_error(i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str.1, i64 0, i64 0), i32 %36, i32 %38, i32 %42, i32 %46)
br label %48
48: ; preds = %34, %22
br label %49
49: ; preds = %48, %4
ret void
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32 }
%struct.sockaddr = type { i32 }
@errno = global i32 0
@.str.1 = [52 x i8] c"Was only able to send %d out of %d bytes to %s:%hu\0A\00"
define void @sock_send_packet(i32 %0, i8* %1, i64 %2, %struct.sockaddr_in* %3) {
%5 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%6 = tail call i64 @sendto(i32 %0, i8* %1, i64 %2, i32 0, %struct.sockaddr* %5, i32 8)
%7 = icmp eq i64 %6, %2
br i1 %7, label %18, label %8
8: ; preds = %4
%9 = trunc i64 %6 to i32
%10 = trunc i64 %2 to i32
%11 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
%12 = load i32, i32* %11
%13 = tail call i32 @inet_ntoa(i32 %12)
%14 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
%15 = load i32, i32* %14
%16 = tail call i32 @ntohs(i32 %15)
%17 = tail call i32 (i8*, i32, i32, i32, ...) @log_error(i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str.1, i64 0, i64 0), i32 %9, i32 %10, i32 %13, i32 %16)
br label %18
18: ; preds = %8, %4
ret void
}