#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 {
  int /*<<< orphan*/ sa_family;
};
typedef int /*<<< orphan*/ socklen_t;

/* Variables and functions */
int /*<<< orphan*/ IPPROTO_UDP;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ close(int);
int errno;
int /*<<< orphan*/ get_sockaddr_size(struct sockaddr const *);
int sendto(int, char const *, size_t, int /*<<< orphan*/,
           struct sockaddr const *, int /*<<< orphan*/);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int udp_send(const struct sockaddr *addr, size_t len, const char *msg) {
  int sock, rc;
  socklen_t sin_size = get_sockaddr_size(addr);
  sock = socket(addr->sa_family, SOCK_DGRAM, IPPROTO_UDP);
  if (sock == -1)
    return -errno;
  rc = sendto(sock, msg, len, 0, addr, sin_size);
  if (rc == -1)
    rc = -errno;
  close(sock);
  return rc;
}


x86 assembly (clang, -O0):
<udp_send>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <get_sockaddr_size@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
mov    0x2e7b(%rip),%rax
mov    (%rax),%esi
mov    0x2e52(%rip),%rax
mov    (%rax),%edx
call   1060 <socket@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    119d <udp_send+0x5d>
xor    %eax,%eax
mov    0x2e45(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    11e0 <udp_send+0xa0>
mov    -0x24(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    -0x18(%rbp),%rdx
mov    -0x10(%rbp),%r8
mov    -0x2c(%rbp),%r9d
xor    %ecx,%ecx
call   1040 <sendto@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0xffffffff,-0x28(%rbp)
jne    11d2 <udp_send+0x92>
xor    %eax,%eax
mov    0x2e0b(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x28(%rbp)
mov    -0x24(%rbp),%edi
call   1050 <close@plt>
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<udp_send>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r15
mov    %rsi,%r14
mov    %rdi,%rbx
call   1030 <get_sockaddr_size@plt>
mov    %eax,%r12d
mov    (%rbx),%edi
mov    0x2e83(%rip),%rax
mov    (%rax),%esi
mov    0x2e5a(%rip),%rax
mov    (%rax),%edx
call   1060 <socket@plt>
cmp    $0xffffffff,%eax
je     11ad <udp_send+0x6d>
mov    %eax,%r13d
xor    %ebp,%ebp
mov    %eax,%edi
mov    %r15,%rsi
mov    %r14,%rdx
xor    %ecx,%ecx
mov    %rbx,%r8
mov    %r12d,%r9d
call   1040 <sendto@plt>
mov    0x2e3d(%rip),%rcx
sub    (%rcx),%ebp
cmp    $0xffffffff,%eax
cmovne %eax,%ebp
mov    %r13d,%edi
call   1050 <close@plt>
jmp    11b8 <udp_send+0x78>
mov    0x2e24(%rip),%rax
xor    %ebp,%ebp
sub    (%rax),%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr = type { i32 }
@SOCK_DGRAM = global i32 0
@IPPROTO_UDP = global i32 0
@errno = global i32 0
define i32 @udp_send(%struct.sockaddr* %0, i64 %1, i8* %2) {
%4 = alloca i32
%5 = alloca %struct.sockaddr*
%6 = alloca i64
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store %struct.sockaddr* %0, %struct.sockaddr** %5
store i64 %1, i64* %6
store i8* %2, i8** %7
%11 = load %struct.sockaddr*, %struct.sockaddr** %5
%12 = call i32 @get_sockaddr_size(%struct.sockaddr* %11)
store i32 %12, i32* %10
%13 = load %struct.sockaddr*, %struct.sockaddr** %5
%14 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %13, i32 0, i32 0
%15 = load i32, i32* %14
%16 = load i32, i32* @SOCK_DGRAM
%17 = load i32, i32* @IPPROTO_UDP
%18 = call i32 @socket(i32 %15, i32 %16, i32 %17)
store i32 %18, i32* %8
%19 = load i32, i32* %8
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %24
21: ; preds = %3
%22 = load i32, i32* @errno
%23 = sub nsw i32 0, %22
store i32 %23, i32* %4
br label %40
24: ; preds = %3
%25 = load i32, i32* %8
%26 = load i8*, i8** %7
%27 = load i64, i64* %6
%28 = load %struct.sockaddr*, %struct.sockaddr** %5
%29 = load i32, i32* %10
%30 = call i32 @sendto(i32 %25, i8* %26, i64 %27, i32 0, %struct.sockaddr* %28, i32 %29)
store i32 %30, i32* %9
%31 = load i32, i32* %9
%32 = icmp eq i32 %31, -1
br i1 %32, label %33, label %36
33: ; preds = %24
%34 = load i32, i32* @errno
%35 = sub nsw i32 0, %34
store i32 %35, i32* %9
br label %36
36: ; preds = %33, %24
%37 = load i32, i32* %8
%38 = call i32 @close(i32 %37)
%39 = load i32, i32* %9
store i32 %39, i32* %4
br label %40
40: ; preds = %36, %21
%41 = load i32, i32* %4
ret i32 %41
}

LLVM IR (-O2):
%struct.sockaddr = type { i32 }
@SOCK_DGRAM = global i32 0
@IPPROTO_UDP = global i32 0
@errno = global i32 0
define i32 @udp_send(%struct.sockaddr* %0, i64 %1, i8* %2) {
%4 = tail call i32 @get_sockaddr_size(%struct.sockaddr* %0)
%5 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %0, i64 0, i32 0
%6 = load i32, i32* %5
%7 = load i32, i32* @SOCK_DGRAM
%8 = load i32, i32* @IPPROTO_UDP
%9 = tail call i32 @socket(i32 %6, i32 %7, i32 %8)
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %14
11: ; preds = %3
%12 = load i32, i32* @errno
%13 = sub nsw i32 0, %12
br label %21
14: ; preds = %3
%15 = tail call i32 @sendto(i32 %9, i8* %2, i64 %1, i32 0, %struct.sockaddr* %0, i32 %4)
%16 = icmp eq i32 %15, -1
%17 = load i32, i32* @errno
%18 = sub nsw i32 0, %17
%19 = select i1 %16, i32 %18, i32 %15
%20 = tail call i32 @close(i32 %9)
br label %21
21: ; preds = %14, %11
%22 = phi i32 [ %13, %11 ], [ %19, %14 ]
ret i32 %22
}