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

/* Variables and functions */
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_BROADCAST;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ sendto(int, char *, int /*<<< orphan*/, int /*<<< orphan*/,
                          struct sockaddr const *, int /*<<< orphan*/);
scalar_t__ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, int const *,
                      int);
int /*<<< orphan*/ strcpy(char *, char *);
int /*<<< orphan*/ strlen(char *);
void send_udp(int sockfd, const struct sockaddr *pservaddr, socklen_t servlen,
              int numPkts) {
  int ii;
  char senddata[4096];
  const int set_on = 1;
  if (0 >
      setsockopt(sockfd, SOL_SOCKET, SO_BROADCAST, &set_on, sizeof(set_on))) {
    perror("setsockopt(): ");
    exit(1);
  }
  strcpy(senddata, "A simple message to send in UDP.");
  for (ii = 0; ii < numPkts; ++ii) {
    sendto(sockfd, senddata, strlen(senddata), 0, pservaddr, servlen);
  }
}


x86 assembly (clang, -O0):
<send_udp>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1040,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
movl   $0x1,-0x1024(%rbp)
mov    -0x4(%rbp),%edi
mov    0x2e5c(%rip),%rax
mov    (%rax),%esi
mov    0x2e4b(%rip),%rax
mov    (%rax),%edx
lea    -0x1024(%rbp),%rcx
mov    $0x4,%r8d
call   1040 <setsockopt@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
jle    11cd <send_udp+0x6d>
lea    0xe42(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
lea    -0x1020(%rbp),%rdi
lea    0xe34(%rip),%rsi
call   1030 <strcpy@plt>
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x18(%rbp),%eax
jge    1242 <send_udp+0xe2>
mov    -0x4(%rbp),%eax
mov    %eax,-0x1034(%rbp)
lea    -0x1020(%rbp),%rax
mov    %rax,-0x1030(%rbp)
lea    -0x1020(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x1034(%rbp),%edi
mov    -0x1030(%rbp),%rsi
mov    %eax,%edx
mov    -0x10(%rbp),%r8
mov    -0x14(%rbp),%r9d
xor    %ecx,%ecx
call   1060 <sendto@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    11e7 <send_udp+0x87>
add    $0x1040,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<send_udp>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x1010,%rsp
mov    %ecx,%ebx
mov    %edx,%r14d
mov    %rsi,%r15
mov    %edi,%r12d
movl   $0x1,0xc(%rsp)
mov    0x2e5f(%rip),%rax
mov    (%rax),%esi
mov    0x2e4e(%rip),%rax
mov    (%rax),%edx
lea    0xc(%rsp),%rcx
mov    $0x4,%r8d
call   1040 <setsockopt@plt>
test   %rax,%rax
js     1202 <send_udp+0xa2>
lea    0xe5f(%rip),%rsi
lea    0x10(%rsp),%rdi
call   1030 <strcpy@plt>
test   %ebx,%ebx
jle    11f2 <send_udp+0x92>
lea    0x10(%rsp),%rbp
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %rbp,%rdi
call   1050 <strlen@plt>
mov    %r12d,%edi
mov    %rbp,%rsi
mov    %eax,%edx
xor    %ecx,%ecx
mov    %r15,%r8
mov    %r14d,%r9d
call   1060 <sendto@plt>
add    $0xffffffff,%ebx
jne    11d0 <send_udp+0x70>
add    $0x1010,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdf7(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr = type { i32 }
@SOL_SOCKET = global i32 0
@SO_BROADCAST = global i32 0
@.str = [15 x i8] c"setsockopt(): \00"
@.str.1 = [33 x i8] c"A simple message to send in UDP.\00"
define void @send_udp(i32 %0, %struct.sockaddr* %1, i32 %2, i32 %3) {
%5 = alloca i32
%6 = alloca %struct.sockaddr*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca [4096 x i8]
%11 = alloca i32
store i32 %0, i32* %5
store %struct.sockaddr* %1, %struct.sockaddr** %6
store i32 %2, i32* %7
store i32 %3, i32* %8
store i32 1, i32* %11
%12 = load i32, i32* %5
%13 = load i32, i32* @SOL_SOCKET
%14 = load i32, i32* @SO_BROADCAST
%15 = call i64 @setsockopt(i32 %12, i32 %13, i32 %14, i32* %11, i32 4)
%16 = icmp sgt i64 0, %15
br i1 %16, label %17, label %20
17: ; preds = %4
%18 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%19 = call i32 @exit(i32 1)
unreachable
20: ; preds = %4
%21 = getelementptr inbounds [4096 x i8], [4096 x i8]* %10, i64 0, i64 0
%22 = call i32 @strcpy(i8* %21, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0))
store i32 0, i32* %9
br label %23
23: ; preds = %35, %20
%24 = load i32, i32* %9
%25 = load i32, i32* %8
%26 = icmp slt i32 %24, %25
br i1 %26, label %27, label %38
27: ; preds = %23
%28 = load i32, i32* %5
%29 = getelementptr inbounds [4096 x i8], [4096 x i8]* %10, i64 0, i64 0
%30 = getelementptr inbounds [4096 x i8], [4096 x i8]* %10, i64 0, i64 0
%31 = call i32 @strlen(i8* %30)
%32 = load %struct.sockaddr*, %struct.sockaddr** %6
%33 = load i32, i32* %7
%34 = call i32 @sendto(i32 %28, i8* %29, i32 %31, i32 0, %struct.sockaddr* %32, i32 %33)
br label %35
35: ; preds = %27
%36 = load i32, i32* %9
%37 = add nsw i32 %36, 1
store i32 %37, i32* %9
br label %23
38: ; preds = %23
ret void
}

LLVM IR (-O2):
%struct.sockaddr = type { i32 }
@SOL_SOCKET = global i32 0
@SO_BROADCAST = global i32 0
@.str = [15 x i8] c"setsockopt(): \00"
@.str.1 = [33 x i8] c"A simple message to send in UDP.\00"
define void @send_udp(i32 %0, %struct.sockaddr* %1, i32 %2, i32 %3) {
%5 = alloca [4096 x i8]
%6 = alloca i32
%7 = getelementptr inbounds [4096 x i8], [4096 x i8]* %5, i64 0, i64 0
%8 = bitcast i32* %6 to i8*
store i32 1, i32* %6
%9 = load i32, i32* @SOL_SOCKET
%10 = load i32, i32* @SO_BROADCAST
%11 = call i64 @setsockopt(i32 %0, i32 %9, i32 %10, i32* %6, i32 4)
%12 = icmp slt i64 %11, 0
br i1 %12, label %13, label %16
13: ; preds = %4
%14 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%15 = call i32 @exit(i32 1)
unreachable
16: ; preds = %4
%17 = call i32 @strcpy(i8* %7, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0))
%18 = icmp sgt i32 %3, 0
br i1 %18, label %19, label %25
19: ; preds = %16, %19
%20 = phi i32 [ %23, %19 ], [ 0, %16 ]
%21 = call i32 @strlen(i8* %7)
%22 = call i32 @sendto(i32 %0, i8* %7, i32 %21, i32 0, %struct.sockaddr* %1, i32 %2)
%23 = add nuw nsw i32 %20, 1
%24 = icmp eq i32 %23, %3
br i1 %24, label %25, label %19
25: ; preds = %19, %16
ret void
}