#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;
typedef int /*<<< orphan*/ uint16_t;
struct TYPE_2__ {
  int /*<<< orphan*/ s_addr;
};
struct sockaddr_in {
  int /*<<< orphan*/ sin_port;
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ sa;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ htons(int /*<<< orphan*/);
int /*<<< orphan*/ inet_addr(char const *);
int sendto(int, void const *, int /*<<< orphan*/, int /*<<< orphan*/,
           struct sockaddr *, int);
int udp_send(int fd, const char *ip, uint16_t port, const void *buf,
             uint32_t len) {
  int ret = 0;
  struct sockaddr_in sa;
  sa.sin_family = AF_INET;
  sa.sin_addr.s_addr = inet_addr(ip);
  sa.sin_port = htons(port);
  ret = sendto(fd, buf, len, 0, (struct sockaddr *)&sa, sizeof(sa));
  if (ret == -1) {
    close(fd);
  }
  return ret;
}


x86 assembly (clang, -O0):
<udp_send>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8d,-0x24(%rbp)
movl   $0x0,-0x28(%rbp)
mov    0x2e80(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <inet_addr@plt>
mov    %eax,-0x34(%rbp)
mov    -0x14(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0x38(%rbp)
mov    -0x4(%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,-0x28(%rbp)
cmpl   $0xffffffff,-0x28(%rbp)
jne    11b4 <udp_send+0x74>
mov    -0x4(%rbp),%edi
call   1050 <close@plt>
mov    -0x28(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<udp_send>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %r8d,%r14d
mov    %rcx,%rbp
mov    %edx,%ebx
mov    %edi,%r15d
mov    0x2e8c(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
mov    %rsi,%rdi
call   1060 <inet_addr@plt>
mov    %eax,0xc(%rsp)
mov    %ebx,%edi
call   1030 <htons@plt>
mov    %eax,0x8(%rsp)
lea    0x8(%rsp),%r8
mov    %r15d,%edi
mov    %rbp,%rsi
mov    %r14d,%edx
xor    %ecx,%ecx
mov    $0xc,%r9d
call   1040 <sendto@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
jne    11a3 <udp_send+0x63>
mov    %r15d,%edi
call   1050 <close@plt>
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
define i32 @udp_send(i32 %0, i8* %1, i32 %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 @inet_addr(i8* %1)
%11 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i64 0, i32 1, i32 0
store i32 %10, i32* %11
%12 = tail call i32 @htons(i32 %2)
%13 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i64 0, i32 0
store i32 %12, i32* %13
%14 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%15 = call i32 @sendto(i32 %0, i8* %3, i32 %4, 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 @close(i32 %0)
br label %19
19: ; preds = %17, %5
ret i32 %15
}