#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*/ to;
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 /*<<< orphan*/ socklen_t;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
scalar_t__ EAGAIN;
scalar_t__ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, scalar_t__);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ inet_addr(char *);
int sendto(int, void const *, int /*<<< orphan*/, int /*<<< orphan*/,
           struct sockaddr *, int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ udp_socket_error();
int udp_socket_send(int s, int dstport, const void *data, socklen_t len) {
  int rc;
  struct sockaddr_in to;
  to.sin_family = AF_INET;
  to.sin_port = htons(dstport);
  to.sin_addr.s_addr = inet_addr("127.0.0.1");
  rc = sendto(s, data, len, 0, (struct sockaddr *)&to, sizeof(to));
  if ((rc < 0) && (errno != EAGAIN)) {
    fprintf(stderr, "could not send, errno=%d\n", errno);
    udp_socket_error();
  }
  return rc;
}


x86 assembly (clang, -O0):
<udp_socket_send>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %rdx,-0x10(%rbp)
mov    %ecx,-0x14(%rbp)
mov    0x2e7c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x8(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0x24(%rbp)
lea    0xe7d(%rip),%rdi
call   1050 <inet_addr@plt>
mov    %eax,-0x28(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
lea    -0x28(%rbp),%r8
xor    %ecx,%ecx
mov    $0xc,%r9d
call   1040 <sendto@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    11f3 <udp_socket_send+0xa3>
mov    0x2e16(%rip),%rax
mov    (%rax),%rax
mov    0x2dfc(%rip),%rcx
cmp    (%rcx),%rax
je     11f3 <udp_socket_send+0xa3>
mov    0x2e04(%rip),%rax
mov    (%rax),%edi
mov    0x2df3(%rip),%rax
mov    (%rax),%rdx
lea    0xe23(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x0,%al
call   1070 <udp_socket_error@plt>
mov    -0x18(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<udp_socket_send>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %ecx,%r14d
mov    %rdx,%rbx
mov    %edi,%ebp
mov    0x2e81(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
mov    %esi,%edi
call   1030 <htons@plt>
mov    %eax,0x4(%rsp)
lea    0xe81(%rip),%rdi
call   1050 <inet_addr@plt>
mov    %eax,(%rsp)
mov    %rsp,%r8
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r14d,%edx
xor    %ecx,%ecx
mov    $0xc,%r9d
call   1040 <sendto@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    11d7 <udp_socket_send+0x87>
mov    0x2e24(%rip),%rax
mov    (%rax),%rdx
mov    0x2e0a(%rip),%rax
cmp    (%rax),%rdx
je     11d7 <udp_socket_send+0x87>
mov    0x2e16(%rip),%rax
mov    (%rax),%edi
lea    0xe3f(%rip),%rsi
call   1060 <fprintf@plt>
xor    %eax,%eax
call   1070 <udp_socket_error@plt>
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

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 = [10 x i8] c"127.0.0.1\00"
@errno = global i64 0
@EAGAIN = global i64 0
@stderr = global i32 0
@.str.1 = [26 x i8] c"could not send, errno=%d\0A\00"
define i32 @udp_socket_send(i32 %0, i32 %1, i8* %2, i32 %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca %struct.sockaddr_in
store i32 %0, i32* %5
store i32 %1, i32* %6
store i8* %2, i8** %7
store i32 %3, i32* %8
%11 = load i32, i32* @AF_INET
%12 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 2
store i32 %11, i32* %12
%13 = load i32, i32* %6
%14 = call i32 @htons(i32 %13)
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 1
store i32 %14, i32* %15
%16 = call i32 @inet_addr(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 0
%18 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %17, i32 0, i32 0
store i32 %16, i32* %18
%19 = load i32, i32* %5
%20 = load i8*, i8** %7
%21 = load i32, i32* %8
%22 = bitcast %struct.sockaddr_in* %10 to %struct.sockaddr*
%23 = call i32 @sendto(i32 %19, i8* %20, i32 %21, i32 0, %struct.sockaddr* %22, i32 12)
store i32 %23, i32* %9
%24 = load i32, i32* %9
%25 = icmp slt i32 %24, 0
br i1 %25, label %26, label %35
26: ; preds = %4
%27 = load i64, i64* @errno
%28 = load i64, i64* @EAGAIN
%29 = icmp ne i64 %27, %28
br i1 %29, label %30, label %35
30: ; preds = %26
%31 = load i32, i32* @stderr
%32 = load i64, i64* @errno
%33 = call i32 @fprintf(i32 %31, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i64 %32)
%34 = call i32 (...) @udp_socket_error()
br label %35
35: ; preds = %30, %26, %4
%36 = load i32, i32* %9
ret i32 %36
}

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 = [10 x i8] c"127.0.0.1\00"
@errno = global i64 0
@EAGAIN = global i64 0
@stderr = global i32 0
@.str.1 = [26 x i8] c"could not send, errno=%d\0A\00"
define i32 @udp_socket_send(i32 %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 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 2
store i32 %7, i32* %8
%9 = tail call i32 @htons(i32 %1)
%10 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 1
store i32 %9, i32* %10
%11 = tail call i32 @inet_addr(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%12 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 0, i32 0
store i32 %11, i32* %12
%13 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%14 = call i32 @sendto(i32 %0, i8* %2, i32 %3, i32 0, %struct.sockaddr* %13, i32 12)
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %24
16: ; preds = %4
%17 = load i64, i64* @errno
%18 = load i64, i64* @EAGAIN
%19 = icmp eq i64 %17, %18
br i1 %19, label %24, label %20
20: ; preds = %16
%21 = load i32, i32* @stderr
%22 = call i32 @fprintf(i32 %21, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i64 %17)
%23 = call i32 (...) @udp_socket_error()
br label %24
24: ; preds = %20, %16, %4
ret i32 %14
}