#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_in {
  int /*<<< orphan*/ sin_addr;
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ broadcastEnable;
typedef int /*<<< orphan*/ addr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ IPPROTO_UDP;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_BROADCAST;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ failed(char *);
int /*<<< orphan*/ htons(int);
scalar_t__ inet_aton(char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int sendto(int, char const *, int /*<<< orphan*/, int /*<<< orphan*/,
           struct sockaddr *, int);
int setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, int *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strlen(char const *);
void udp_bcast(int port, const char *str) {
  struct sockaddr_in addr;
  memset(&addr, 0, sizeof(addr));
  addr.sin_family = AF_INET;
  addr.sin_port = htons(port);
  if (inet_aton("255.255.255.255", &addr.sin_addr) == 0) {
    failed("inet_aton()");
  }
  int sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
  if (sock == -1) {
    failed("socket()");
  }
  int broadcastEnable = 1;
  int ret = setsockopt(sock, SOL_SOCKET, SO_BROADCAST, &broadcastEnable,
                       sizeof(broadcastEnable));
  if (ret == -1) {
    failed("setsockopt()");
  }
  int stat =
      sendto(sock, str, strlen(str), 0, (struct sockaddr *)&addr, sizeof(addr));
  if (!stat) {
    failed("stat()");
  }
  close(sock);
}


x86 assembly (clang, -O0):
<udp_bcast>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    0x2e2a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0x4(%rbp),%edi
call   1050 <htons@plt>
mov    %eax,-0x1c(%rbp)
lea    -0x20(%rbp),%rsi
lea    0xe2f(%rip),%rdi
call   10a0 <inet_aton@plt>
cmp    $0x0,%rax
jne    11ec <udp_bcast+0x5c>
lea    0xe29(%rip),%rdi
call   1090 <failed@plt>
mov    0x2ded(%rip),%rax
mov    (%rax),%edi
mov    0x2ddc(%rip),%rax
mov    (%rax),%esi
mov    0x2db3(%rip),%rax
mov    (%rax),%edx
call   10b0 <socket@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    1225 <udp_bcast+0x95>
lea    0xdfc(%rip),%rdi
call   1090 <failed@plt>
movl   $0x1,-0x28(%rbp)
mov    -0x24(%rbp),%edi
mov    0x2db2(%rip),%rax
mov    (%rax),%esi
mov    0x2d91(%rip),%rax
mov    (%rax),%edx
lea    -0x28(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0xffffffff,-0x2c(%rbp)
jne    1269 <udp_bcast+0xd9>
lea    0xdc1(%rip),%rdi
call   1090 <failed@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x3c(%rbp),%edi
mov    -0x38(%rbp),%rsi
mov    %eax,%edx
lea    -0x20(%rbp),%r8
xor    %ecx,%ecx
mov    $0xc,%r9d
call   1070 <sendto@plt>
mov    %eax,-0x30(%rbp)
cmpl   $0x0,-0x30(%rbp)
jne    12b3 <udp_bcast+0x123>
lea    0xd84(%rip),%rdi
call   1090 <failed@plt>
mov    -0x24(%rbp),%edi
call   1080 <close@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<udp_bcast>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%r14
mov    %edi,%ebp
lea    0x8(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    0x2e26(%rip),%r15
mov    (%r15),%eax
mov    %eax,0x10(%rsp)
mov    %ebp,%edi
call   1050 <htons@plt>
mov    %eax,0xc(%rsp)
lea    0xe2d(%rip),%rdi
mov    %rbx,%rsi
call   10a0 <inet_aton@plt>
test   %rax,%rax
jne    11ec <udp_bcast+0x5c>
lea    0xe29(%rip),%rdi
call   1090 <failed@plt>
mov    (%r15),%edi
mov    0x2de2(%rip),%rax
mov    (%rax),%esi
mov    0x2db9(%rip),%rax
mov    (%rax),%edx
call   10b0 <socket@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
jne    1219 <udp_bcast+0x89>
lea    0xe08(%rip),%rdi
call   1090 <failed@plt>
movl   $0x1,0x4(%rsp)
mov    0x2dc0(%rip),%rax
mov    (%rax),%esi
mov    0x2d9f(%rip),%rax
mov    (%rax),%edx
lea    0x4(%rsp),%rcx
mov    %ebp,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0xffffffff,%eax
jne    1256 <udp_bcast+0xc6>
lea    0xdd4(%rip),%rdi
call   1090 <failed@plt>
mov    %r14,%rdi
call   1040 <strlen@plt>
lea    0x8(%rsp),%r8
mov    %ebp,%edi
mov    %r14,%rsi
mov    %eax,%edx
xor    %ecx,%ecx
mov    $0xc,%r9d
call   1070 <sendto@plt>
test   %eax,%eax
jne    1287 <udp_bcast+0xf7>
lea    0xdb0(%rip),%rdi
call   1090 <failed@plt>
mov    %ebp,%edi
call   1080 <close@plt>
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@.str = [16 x i8] c"255.255.255.255\00"
@.str.1 = [12 x i8] c"inet_aton()\00"
@SOCK_DGRAM = global i32 0
@IPPROTO_UDP = global i32 0
@.str.2 = [9 x i8] c"socket()\00"
@SOL_SOCKET = global i32 0
@SO_BROADCAST = global i32 0
@.str.3 = [13 x i8] c"setsockopt()\00"
@.str.4 = [7 x i8] c"stat()\00"
define void @udp_bcast(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca %struct.sockaddr_in
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i32 %0, i32* %3
store i8* %1, i8** %4
%10 = call i32 @memset(%struct.sockaddr_in* %5, i32 0, i32 12)
%11 = load i32, i32* @AF_INET
%12 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 2
store i32 %11, i32* %12
%13 = load i32, i32* %3
%14 = call i32 @htons(i32 %13)
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 1
store i32 %14, i32* %15
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 0
%17 = call i64 @inet_aton(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32* %16)
%18 = icmp eq i64 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %2
%20 = call i32 @failed(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
br label %21
21: ; preds = %19, %2
%22 = load i32, i32* @AF_INET
%23 = load i32, i32* @SOCK_DGRAM
%24 = load i32, i32* @IPPROTO_UDP
%25 = call i32 @socket(i32 %22, i32 %23, i32 %24)
store i32 %25, i32* %6
%26 = load i32, i32* %6
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %30
28: ; preds = %21
%29 = call i32 @failed(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0))
br label %30
30: ; preds = %28, %21
store i32 1, i32* %7
%31 = load i32, i32* %6
%32 = load i32, i32* @SOL_SOCKET
%33 = load i32, i32* @SO_BROADCAST
%34 = call i32 @setsockopt(i32 %31, i32 %32, i32 %33, i32* %7, i32 4)
store i32 %34, i32* %8
%35 = load i32, i32* %8
%36 = icmp eq i32 %35, -1
br i1 %36, label %37, label %39
37: ; preds = %30
%38 = call i32 @failed(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
br label %39
39: ; preds = %37, %30
%40 = load i32, i32* %6
%41 = load i8*, i8** %4
%42 = load i8*, i8** %4
%43 = call i32 @strlen(i8* %42)
%44 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%45 = call i32 @sendto(i32 %40, i8* %41, i32 %43, i32 0, %struct.sockaddr* %44, i32 12)
store i32 %45, i32* %9
%46 = load i32, i32* %9
%47 = icmp ne i32 %46, 0
br i1 %47, label %50, label %48
48: ; preds = %39
%49 = call i32 @failed(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
br label %50
50: ; preds = %48, %39
%51 = load i32, i32* %6
%52 = call i32 @close(i32 %51)
ret void
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@.str = [16 x i8] c"255.255.255.255\00"
@.str.1 = [12 x i8] c"inet_aton()\00"
@SOCK_DGRAM = global i32 0
@IPPROTO_UDP = global i32 0
@.str.2 = [9 x i8] c"socket()\00"
@SOL_SOCKET = global i32 0
@SO_BROADCAST = global i32 0
@.str.3 = [13 x i8] c"setsockopt()\00"
@.str.4 = [7 x i8] c"stat()\00"
define void @udp_bcast(i32 %0, i8* %1) {
%3 = alloca %struct.sockaddr_in
%4 = alloca i32
%5 = bitcast %struct.sockaddr_in* %3 to i8*
%6 = call i32 @memset(%struct.sockaddr_in* %3, i32 0, i32 12)
%7 = load i32, i32* @AF_INET
%8 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %7, i32* %8
%9 = call i32 @htons(i32 %0)
%10 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
store i32 %9, i32* %10
%11 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
%12 = call i64 @inet_aton(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32* %11)
%13 = icmp eq i64 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %2
%15 = call i32 @failed(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
br label %16
16: ; preds = %14, %2
%17 = load i32, i32* @AF_INET
%18 = load i32, i32* @SOCK_DGRAM
%19 = load i32, i32* @IPPROTO_UDP
%20 = call i32 @socket(i32 %17, i32 %18, i32 %19)
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %24
22: ; preds = %16
%23 = call i32 @failed(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0))
br label %24
24: ; preds = %22, %16
%25 = bitcast i32* %4 to i8*
store i32 1, i32* %4
%26 = load i32, i32* @SOL_SOCKET
%27 = load i32, i32* @SO_BROADCAST
%28 = call i32 @setsockopt(i32 %20, i32 %26, i32 %27, i32* %4, i32 4)
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %32
30: ; preds = %24
%31 = call i32 @failed(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
br label %32
32: ; preds = %30, %24
%33 = call i32 @strlen(i8* %1)
%34 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%35 = call i32 @sendto(i32 %20, i8* %1, i32 %33, i32 0, %struct.sockaddr* %34, i32 12)
%36 = icmp eq i32 %35, 0
br i1 %36, label %37, label %39
37: ; preds = %32
%38 = call i32 @failed(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
br label %39
39: ; preds = %37, %32
%40 = call i32 @close(i32 %20)
ret void
}