#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 */
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*/ bcp;
typedef int /*<<< orphan*/ addr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ IPPROTO_UDP;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_BROADCAST;
int /*<<< orphan*/ htons(unsigned short);
int /*<<< orphan*/ inet_addr(char *);
int sendto(int, char *, int, int /*<<< orphan*/, struct sockaddr *, int);
int setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, void *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int strlen(char *);
int sock_broadcast(char *ip, unsigned short port, char *msg) {
  int sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
  if (sock < 0) {
    return 1;
  }
  int bcp = 1;
  int err =
      setsockopt(sock, SOL_SOCKET, SO_BROADCAST, (void *)&bcp, sizeof(bcp));
  if (err < 0) {
    return 2;
  }
  struct sockaddr_in addr = {.sin_family = AF_INET,
                             .sin_addr.s_addr = inet_addr(ip),
                             .sin_port = htons(port)};
  int len = strlen(msg);
  while (1) {
    err = sendto(sock, msg, len, 0, (struct sockaddr *)&addr, sizeof(addr));
    if (err != len) {
      return 3;
    }
  }
}


x86 assembly (clang, -O0):
<sock_broadcast>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %si,%ax
mov    %rdi,-0x10(%rbp)
mov    %ax,-0x12(%rbp)
mov    %rdx,-0x20(%rbp)
mov    0x2e42(%rip),%rax
mov    (%rax),%edi
mov    0x2e51(%rip),%rax
mov    (%rax),%esi
mov    0x2e20(%rip),%rax
mov    (%rax),%edx
call   1080 <socket@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    11b0 <sock_broadcast+0x50>
movl   $0x1,-0x4(%rbp)
jmp    125f <sock_broadcast+0xff>
movl   $0x1,-0x28(%rbp)
mov    -0x24(%rbp),%edi
mov    0x2e27(%rip),%rax
mov    (%rax),%esi
mov    0x2e06(%rip),%rax
mov    (%rax),%edx
lea    -0x28(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jge    11f4 <sock_broadcast+0x94>
movl   $0x2,-0x4(%rbp)
jmp    125f <sock_broadcast+0xff>
movzwl -0x12(%rbp),%edi
call   1050 <htons@plt>
mov    %eax,-0x38(%rbp)
mov    -0x10(%rbp),%rdi
call   1070 <inet_addr@plt>
mov    %eax,-0x34(%rbp)
mov    0x2dcd(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    -0x20(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x3c(%rbp)
mov    -0x24(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    -0x3c(%rbp),%edx
lea    -0x38(%rbp),%r8
xor    %ecx,%ecx
mov    $0xc,%r9d
call   1060 <sendto@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x3c(%rbp),%eax
je     125a <sock_broadcast+0xfa>
movl   $0x3,-0x4(%rbp)
jmp    125f <sock_broadcast+0xff>
jmp    1224 <sock_broadcast+0xc4>
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sock_broadcast>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x20,%rsp
mov    %rdx,%r12
mov    %esi,%r15d
mov    %rdi,%r14
mov    0x2e44(%rip),%rax
mov    (%rax),%edi
mov    0x2e53(%rip),%rax
mov    (%rax),%esi
mov    0x2e22(%rip),%rax
mov    (%rax),%edx
call   1080 <socket@plt>
test   %eax,%eax
js     1232 <sock_broadcast+0xd2>
mov    %eax,%ebp
movl   $0x1,0xc(%rsp)
mov    0x2e3a(%rip),%rax
mov    (%rax),%esi
mov    0x2e19(%rip),%rax
mov    (%rax),%edx
lea    0xc(%rsp),%rcx
mov    %ebp,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
test   %eax,%eax
js     1239 <sock_broadcast+0xd9>
movzwl %r15w,%edi
call   1050 <htons@plt>
mov    %eax,0x10(%rsp)
mov    %r14,%rdi
call   1070 <inet_addr@plt>
mov    %eax,0x14(%rsp)
mov    0x2df1(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x18(%rsp)
mov    %r12,%rdi
call   1040 <strlen@plt>
mov    %eax,%ebx
lea    0x10(%rsp),%r14
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %ebp,%edi
mov    %r12,%rsi
mov    %ebx,%edx
xor    %ecx,%ecx
mov    %r14,%r8
mov    $0xc,%r9d
call   1060 <sendto@plt>
cmp    %ebx,%eax
je     1210 <sock_broadcast+0xb0>
mov    $0x3,%eax
jmp    123e <sock_broadcast+0xde>
mov    $0x1,%eax
jmp    123e <sock_broadcast+0xde>
mov    $0x2,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r12
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 }
@PF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@IPPROTO_UDP = global i32 0
@SOL_SOCKET = global i32 0
@SO_BROADCAST = global i32 0
@AF_INET = global i32 0
define i32 @sock_broadcast(i8* %0, i16 zeroext %1, i8* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i16
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca %struct.sockaddr_in
%12 = alloca i32
store i8* %0, i8** %5
store i16 %1, i16* %6
store i8* %2, i8** %7
%13 = load i32, i32* @PF_INET
%14 = load i32, i32* @SOCK_DGRAM
%15 = load i32, i32* @IPPROTO_UDP
%16 = call i32 @socket(i32 %13, i32 %14, i32 %15)
store i32 %16, i32* %8
%17 = load i32, i32* %8
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %20
19: ; preds = %3
store i32 1, i32* %4
br label %52
20: ; preds = %3
store i32 1, i32* %9
%21 = load i32, i32* %8
%22 = load i32, i32* @SOL_SOCKET
%23 = load i32, i32* @SO_BROADCAST
%24 = bitcast i32* %9 to i8*
%25 = call i32 @setsockopt(i32 %21, i32 %22, i32 %23, i8* %24, i32 4)
store i32 %25, i32* %10
%26 = load i32, i32* %10
%27 = icmp slt i32 %26, 0
br i1 %27, label %28, label %29
28: ; preds = %20
store i32 2, i32* %4
br label %52
29: ; preds = %20
%30 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %11, i32 0, i32 0
%31 = load i16, i16* %6
%32 = call i32 @htons(i16 zeroext %31)
store i32 %32, i32* %30
%33 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %11, i32 0, i32 1
%34 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %33, i32 0, i32 0
%35 = load i8*, i8** %5
%36 = call i32 @inet_addr(i8* %35)
store i32 %36, i32* %34
%37 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %11, i32 0, i32 2
%38 = load i32, i32* @AF_INET
store i32 %38, i32* %37
%39 = load i8*, i8** %7
%40 = call i32 @strlen(i8* %39)
store i32 %40, i32* %12
br label %41
41: ; preds = %29, %51
%42 = load i32, i32* %8
%43 = load i8*, i8** %7
%44 = load i32, i32* %12
%45 = bitcast %struct.sockaddr_in* %11 to %struct.sockaddr*
%46 = call i32 @sendto(i32 %42, i8* %43, i32 %44, i32 0, %struct.sockaddr* %45, i32 12)
store i32 %46, i32* %10
%47 = load i32, i32* %10
%48 = load i32, i32* %12
%49 = icmp ne i32 %47, %48
br i1 %49, label %50, label %51
50: ; preds = %41
store i32 3, i32* %4
br label %52
51: ; preds = %41
br label %41
52: ; preds = %50, %28, %19
%53 = load i32, i32* %4
ret i32 %53
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@PF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@IPPROTO_UDP = global i32 0
@SOL_SOCKET = global i32 0
@SO_BROADCAST = global i32 0
@AF_INET = global i32 0
define i32 @sock_broadcast(i8* %0, i16 zeroext %1, i8* %2) {
%4 = alloca i32
%5 = alloca %struct.sockaddr_in
%6 = load i32, i32* @PF_INET
%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 slt i32 %9, 0
br i1 %10, label %33, label %11
11: ; preds = %3
%12 = bitcast i32* %4 to i8*
store i32 1, i32* %4
%13 = load i32, i32* @SOL_SOCKET
%14 = load i32, i32* @SO_BROADCAST
%15 = call i32 @setsockopt(i32 %9, i32 %13, i32 %14, i8* %12, i32 4)
%16 = icmp slt i32 %15, 0
br i1 %16, label %31, label %17
17: ; preds = %11
%18 = bitcast %struct.sockaddr_in* %5 to i8*
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 0
%20 = call i32 @htons(i16 zeroext %1)
store i32 %20, i32* %19
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 1, i32 0
%22 = call i32 @inet_addr(i8* %0)
store i32 %22, i32* %21
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 2
%24 = load i32, i32* @AF_INET
store i32 %24, i32* %23
%25 = call i32 @strlen(i8* %2)
%26 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
br label %27
27: ; preds = %27, %17
%28 = call i32 @sendto(i32 %9, i8* %2, i32 %25, i32 0, %struct.sockaddr* %26, i32 12)
%29 = icmp eq i32 %28, %25
br i1 %29, label %27, label %30
30: ; preds = %27
br label %31
31: ; preds = %11, %30
%32 = phi i32 [ 3, %30 ], [ 2, %11 ]
br label %33
33: ; preds = %3, %31
%34 = phi i32 [ %32, %31 ], [ 1, %3 ]
ret i32 %34
}