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

/* Variables and functions */
void *AF_INET;
int /*<<< orphan*/ IPPROTO_UDP;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_BROADCAST;
scalar_t__ bind(int, struct sockaddr const *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
void *htonl(int /*<<< orphan*/);
void *htons(int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ sendto(int, int /*<<< orphan*/ *, int, int /*<<< orphan*/,
                          struct sockaddr const *, int);
int /*<<< orphan*/ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/,
                              int *, int);
int socket(void *, int /*<<< orphan*/, int /*<<< orphan*/);
int sendudp(uint32_t s_ip, uint32_t d_ip, uint16_t sport, uint16_t dport,
            uint8_t *buf, int buflen) {
  int sock;
  struct sockaddr_in daddr;
  struct sockaddr_in saddr;
  int one = 1;
  sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
  if (sock <= 0) {
    perror("Can't open socket");
    exit(1);
  }
  daddr.sin_family = AF_INET;
  daddr.sin_addr.s_addr = htonl(d_ip);
  daddr.sin_port = htons(dport);
  saddr.sin_family = AF_INET;
  saddr.sin_addr.s_addr = htonl(s_ip);
  saddr.sin_port = htons(sport);
  if (bind(sock, (const struct sockaddr *)&saddr, sizeof(saddr)) < 0) {
    perror("can't bind socket");
    exit(1);
  }
  setsockopt(sock, SOL_SOCKET, SO_BROADCAST, &one, sizeof(one));
  sendto(sock, buf, buflen, 0, (const struct sockaddr *)&daddr,
         sizeof(struct sockaddr_in));
  close(sock);
  return (0);
}


x86 assembly (clang, -O0):
<sendudp>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %edx,-0xc(%rbp)
mov    %ecx,-0x10(%rbp)
mov    %r8,-0x18(%rbp)
mov    %r9d,-0x1c(%rbp)
movl   $0x1,-0x54(%rbp)
mov    0x2e26(%rip),%rax
mov    (%rax),%rdi
mov    0x2e14(%rip),%rax
mov    (%rax),%esi
mov    0x2deb(%rip),%rax
mov    (%rax),%edx
call   10b0 <socket@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jg     11f7 <sendudp+0x67>
lea    0xe18(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    0x2de2(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x28(%rbp)
mov    -0x8(%rbp),%edi
call   1050 <htonl@plt>
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%edi
call   1040 <htons@plt>
mov    %rax,-0x38(%rbp)
mov    0x2dbc(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x40(%rbp)
mov    -0x4(%rbp),%edi
call   1050 <htonl@plt>
mov    %rax,-0x48(%rbp)
mov    -0xc(%rbp),%edi
call   1040 <htons@plt>
mov    %rax,-0x50(%rbp)
mov    -0x20(%rbp),%edi
lea    -0x50(%rbp),%rsi
mov    $0x18,%edx
call   1080 <bind@plt>
cmp    $0x0,%rax
jge    1274 <sendudp+0xe4>
lea    0xdad(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x20(%rbp),%edi
mov    0x2d6a(%rip),%rax
mov    (%rax),%esi
mov    0x2d49(%rip),%rax
mov    (%rax),%edx
lea    -0x54(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    -0x20(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
lea    -0x38(%rbp),%r8
xor    %ecx,%ecx
mov    $0x18,%r9d
call   1060 <sendto@plt>
mov    -0x20(%rbp),%edi
call   1070 <close@plt>
xor    %eax,%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sendudp>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x48,%rsp
mov    %r9d,0x8(%rsp)
mov    %r8,0x10(%rsp)
mov    %ecx,%ebx
mov    %edx,%r12d
mov    %esi,%r14d
mov    %edi,%r13d
movl   $0x1,0xc(%rsp)
mov    0x2e1e(%rip),%r15
mov    (%r15),%rdi
mov    0x2e0c(%rip),%rax
mov    (%rax),%esi
mov    0x2de3(%rip),%rax
mov    (%rax),%edx
call   10b0 <socket@plt>
test   %eax,%eax
jle    1298 <sendudp+0x108>
mov    %eax,%ebp
mov    (%r15),%rax
mov    %rax,0x40(%rsp)
mov    %r14d,%edi
call   1050 <htonl@plt>
mov    %rax,0x38(%rsp)
mov    %ebx,%edi
call   1040 <htons@plt>
mov    %rax,0x30(%rsp)
mov    (%r15),%rax
mov    %rax,0x28(%rsp)
mov    %r13d,%edi
call   1050 <htonl@plt>
mov    %rax,0x20(%rsp)
mov    %r12d,%edi
call   1040 <htons@plt>
mov    %rax,0x18(%rsp)
lea    0x18(%rsp),%rsi
mov    %ebp,%edi
mov    $0x18,%edx
call   1080 <bind@plt>
test   %rax,%rax
js     12a1 <sendudp+0x111>
mov    0x2da2(%rip),%rax
mov    (%rax),%esi
mov    0x2d81(%rip),%rax
mov    (%rax),%edx
lea    0xc(%rsp),%rcx
mov    %ebp,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
lea    0x30(%rsp),%r8
mov    %ebp,%edi
mov    0x10(%rsp),%rsi
mov    0x8(%rsp),%edx
xor    %ecx,%ecx
mov    $0x18,%r9d
call   1060 <sendto@plt>
mov    %ebp,%edi
call   1070 <close@plt>
xor    %eax,%eax
add    $0x48,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd61(%rip),%rdi
jmp    12a8 <sendudp+0x118>
lea    0xd6a(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { i8*, %struct.TYPE_2__, i8* }
%struct.TYPE_2__ = type { i8* }
%struct.sockaddr = type { i32 }
@AF_INET = global i8* null
@SOCK_DGRAM = global i32 0
@IPPROTO_UDP = global i32 0
@.str = [18 x i8] c"Can't open socket\00"
@.str.1 = [18 x i8] c"can't bind socket\00"
@SOL_SOCKET = global i32 0
@SO_BROADCAST = global i32 0
define i32 @sendudp(i32 %0, i32 %1, i32 %2, i32 %3, i32* %4, i32 %5) {
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32*
%12 = alloca i32
%13 = alloca i32
%14 = alloca %struct.sockaddr_in
%15 = alloca %struct.sockaddr_in
%16 = alloca i32
store i32 %0, i32* %7
store i32 %1, i32* %8
store i32 %2, i32* %9
store i32 %3, i32* %10
store i32* %4, i32** %11
store i32 %5, i32* %12
store i32 1, i32* %16
%17 = load i8*, i8** @AF_INET
%18 = load i32, i32* @SOCK_DGRAM
%19 = load i32, i32* @IPPROTO_UDP
%20 = call i32 @socket(i8* %17, i32 %18, i32 %19)
store i32 %20, i32* %13
%21 = load i32, i32* %13
%22 = icmp sle i32 %21, 0
br i1 %22, label %23, label %26
23: ; preds = %6
%24 = call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
%25 = call i32 @exit(i32 1)
unreachable
26: ; preds = %6
%27 = load i8*, i8** @AF_INET
%28 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %14, i32 0, i32 2
store i8* %27, i8** %28
%29 = load i32, i32* %8
%30 = call i8* @htonl(i32 %29)
%31 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %14, i32 0, i32 1
%32 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %31, i32 0, i32 0
store i8* %30, i8** %32
%33 = load i32, i32* %10
%34 = call i8* @htons(i32 %33)
%35 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %14, i32 0, i32 0
store i8* %34, i8** %35
%36 = load i8*, i8** @AF_INET
%37 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %15, i32 0, i32 2
store i8* %36, i8** %37
%38 = load i32, i32* %7
%39 = call i8* @htonl(i32 %38)
%40 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %15, i32 0, i32 1
%41 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %40, i32 0, i32 0
store i8* %39, i8** %41
%42 = load i32, i32* %9
%43 = call i8* @htons(i32 %42)
%44 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %15, i32 0, i32 0
store i8* %43, i8** %44
%45 = load i32, i32* %13
%46 = bitcast %struct.sockaddr_in* %15 to %struct.sockaddr*
%47 = call i64 @bind(i32 %45, %struct.sockaddr* %46, i32 24)
%48 = icmp slt i64 %47, 0
br i1 %48, label %49, label %52
49: ; preds = %26
%50 = call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
%51 = call i32 @exit(i32 1)
unreachable
52: ; preds = %26
%53 = load i32, i32* %13
%54 = load i32, i32* @SOL_SOCKET
%55 = load i32, i32* @SO_BROADCAST
%56 = call i32 @setsockopt(i32 %53, i32 %54, i32 %55, i32* %16, i32 4)
%57 = load i32, i32* %13
%58 = load i32*, i32** %11
%59 = load i32, i32* %12
%60 = bitcast %struct.sockaddr_in* %14 to %struct.sockaddr*
%61 = call i32 @sendto(i32 %57, i32* %58, i32 %59, i32 0, %struct.sockaddr* %60, i32 24)
%62 = load i32, i32* %13
%63 = call i32 @close(i32 %62)
ret i32 0
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i8*, %struct.TYPE_2__, i8* }
%struct.TYPE_2__ = type { i8* }
%struct.sockaddr = type { i32 }
@AF_INET = global i8* null
@SOCK_DGRAM = global i32 0
@IPPROTO_UDP = global i32 0
@.str = [18 x i8] c"Can't open socket\00"
@.str.1 = [18 x i8] c"can't bind socket\00"
@SOL_SOCKET = global i32 0
@SO_BROADCAST = global i32 0
define i32 @sendudp(i32 %0, i32 %1, i32 %2, i32 %3, i32* %4, i32 %5) {
%7 = alloca %struct.sockaddr_in
%8 = alloca %struct.sockaddr_in
%9 = alloca i32
%10 = bitcast %struct.sockaddr_in* %7 to i8*
%11 = bitcast %struct.sockaddr_in* %8 to i8*
%12 = bitcast i32* %9 to i8*
store i32 1, i32* %9
%13 = load i8*, i8** @AF_INET
%14 = load i32, i32* @SOCK_DGRAM
%15 = load i32, i32* @IPPROTO_UDP
%16 = tail call i32 @socket(i8* %13, i32 %14, i32 %15)
%17 = icmp slt i32 %16, 1
br i1 %17, label %18, label %21
18: ; preds = %6
%19 = tail call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
%20 = tail call i32 @exit(i32 1)
unreachable
21: ; preds = %6
%22 = load i8*, i8** @AF_INET
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i64 0, i32 2
store i8* %22, i8** %23
%24 = tail call i8* @htonl(i32 %1)
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i64 0, i32 1, i32 0
store i8* %24, i8** %25
%26 = tail call i8* @htons(i32 %3)
%27 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i64 0, i32 0
store i8* %26, i8** %27
%28 = load i8*, i8** @AF_INET
%29 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i64 0, i32 2
store i8* %28, i8** %29
%30 = tail call i8* @htonl(i32 %0)
%31 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i64 0, i32 1, i32 0
store i8* %30, i8** %31
%32 = tail call i8* @htons(i32 %2)
%33 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i64 0, i32 0
store i8* %32, i8** %33
%34 = bitcast %struct.sockaddr_in* %8 to %struct.sockaddr*
%35 = call i64 @bind(i32 %16, %struct.sockaddr* %34, i32 24)
%36 = icmp slt i64 %35, 0
br i1 %36, label %37, label %40
37: ; preds = %21
%38 = call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
%39 = call i32 @exit(i32 1)
unreachable
40: ; preds = %21
%41 = load i32, i32* @SOL_SOCKET
%42 = load i32, i32* @SO_BROADCAST
%43 = call i32 @setsockopt(i32 %16, i32 %41, i32 %42, i32* %9, i32 4)
%44 = bitcast %struct.sockaddr_in* %7 to %struct.sockaddr*
%45 = call i32 @sendto(i32 %16, i32* %4, i32 %5, i32 0, %struct.sockaddr* %44, i32 24)
%46 = call i32 @close(i32 %16)
ret i32 0
}