#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_3__ TYPE_1__;

/* Type definitions */
typedef int /*<<< orphan*/ yes;
struct TYPE_3__ {
  int /*<<< orphan*/ s_addr;
};
struct sockaddr_in {
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ sa;
typedef int /*<<< orphan*/ broadcast;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_BROADCAST;
int INET_ADDRSTRLEN;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_BROADCAST;
int /*<<< orphan*/ SO_REUSEADDR;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ htons(int);
char *inet_ntoa(TYPE_1__);
int /*<<< orphan*/ inet_ntop(int /*<<< orphan*/, TYPE_1__ *, char *, int);
int /*<<< orphan*/ printf(char *, int, ...);
int sendto(int, char *, int /*<<< orphan*/, int /*<<< orphan*/,
           struct sockaddr *, int);
int setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, int *, int);
int /*<<< orphan*/ sleep(int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ sprintf(char *, char *, int /*<<< orphan*/);
int /*<<< orphan*/ strlen(char *);
int /*<<< orphan*/ time(int /*<<< orphan*/ *);
int broadcast_time(void) {
  struct sockaddr_in sa;
  sa.sin_family = AF_INET;
  sa.sin_port = htons(9004);
  sa.sin_addr.s_addr = INADDR_BROADCAST;
  int sockfd = socket(PF_INET, SOCK_DGRAM, 0);
  int broadcast = 1;
  int setsockopt_broadcast_result = setsockopt(sockfd, SOL_SOCKET, SO_BROADCAST,
                                               &broadcast, sizeof(broadcast));
  int yes = 1;
  int setsockopt_reuse_result =
      setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes));
  char ip4[INET_ADDRSTRLEN];
  inet_ntop(AF_INET, &(sa.sin_addr), ip4, INET_ADDRSTRLEN);
  printf(
      "sockfd=%d, setsockopt_broadcast_result=%d, setsockopt_reuse_result=%d\n",
      sockfd, setsockopt_broadcast_result, setsockopt_reuse_result);
  for (;;) {
    char payload[100];
    sprintf(payload, "hello world, it's %ld\n", time(NULL));
    int send_result = sendto(sockfd, payload, strlen(payload), 0,
                             (struct sockaddr *)&sa, sizeof(sa));
    printf("sent %d bytes to %s\n", send_result, inet_ntoa(sa.sin_addr));
    sleep(1);
  }
  printf("closing sockfd: %d", sockfd);
  close(sockfd);
  return 0;
}


x86 assembly (clang, -O0):
<broadcast_time>:
push   %rbp
mov    %rsp,%rbp
sub    $0xd0,%rsp
mov    0x2e1e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x8(%rbp)
mov    $0x232c,%edi
call   1070 <htons@plt>
mov    %eax,-0xc(%rbp)
mov    0x2dfd(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    0x2dc9(%rip),%rax
mov    (%rax),%edi
mov    0x2de0(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
mov    %eax,-0x14(%rbp)
movl   $0x1,-0x18(%rbp)
mov    -0x14(%rbp),%edi
mov    0x2ddb(%rip),%rax
mov    %rax,-0xb0(%rbp)
mov    (%rax),%esi
mov    0x2dab(%rip),%rax
mov    (%rax),%edx
lea    -0x18(%rbp),%rcx
mov    $0x4,%r8d
mov    %r8d,-0xb4(%rbp)
call   1040 <setsockopt@plt>
mov    -0xb4(%rbp),%r8d
mov    %eax,%ecx
mov    -0xb0(%rbp),%rax
mov    %ecx,-0x1c(%rbp)
movl   $0x1,-0x20(%rbp)
mov    -0x14(%rbp),%edi
mov    (%rax),%esi
mov    0x2d4d(%rip),%rax
mov    (%rax),%edx
lea    -0x20(%rbp),%rcx
call   1040 <setsockopt@plt>
mov    %eax,-0x24(%rbp)
mov    0x2d48(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x30(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdx
sub    %rcx,%rdx
mov    %rdx,%rsp
mov    %rax,-0x38(%rbp)
mov    0x2d48(%rip),%rax
mov    (%rax),%edi
lea    -0x10(%rbp),%rsi
mov    0x2d13(%rip),%rax
mov    (%rax),%ecx
call   1030 <inet_ntop@plt>
mov    -0x14(%rbp),%esi
mov    -0x1c(%rbp),%edx
mov    -0x24(%rbp),%ecx
lea    0xd44(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
lea    -0xa0(%rbp),%rax
mov    %rax,-0xd0(%rbp)
xor    %eax,%eax
mov    %eax,%edi
call   10a0 <time@plt>
mov    -0xd0(%rbp),%rdi
mov    %eax,%edx
lea    0xd5d(%rip),%rsi
call   10b0 <sprintf@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0xc4(%rbp)
lea    -0xa0(%rbp),%rax
mov    %rax,-0xc0(%rbp)
lea    -0xa0(%rbp),%rdi
call   1060 <strlen@plt>
mov    -0xc4(%rbp),%edi
mov    -0xc0(%rbp),%rsi
mov    %eax,%edx
lea    -0x10(%rbp),%r8
xor    %ecx,%ecx
mov    $0xc,%r9d
call   1090 <sendto@plt>
mov    %eax,-0xa4(%rbp)
mov    -0xa4(%rbp),%eax
mov    %eax,-0xb8(%rbp)
mov    -0x10(%rbp),%edi
call   1050 <inet_ntoa@plt>
mov    -0xb8(%rbp),%esi
mov    %rax,%rdx
lea    0xd02(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
mov    $0x1,%edi
call   10c0 <sleep@plt>
jmp    12c3 <broadcast_time+0x113>

x86 assembly (clang, -O2):
<broadcast_time>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x88,%rsp
mov    0x2e15(%rip),%r14
mov    (%r14),%eax
mov    %eax,-0x30(%rbp)
mov    $0x232c,%edi
call   1070 <htons@plt>
mov    %eax,-0x34(%rbp)
mov    0x2df3(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x38(%rbp)
mov    0x2dbf(%rip),%rax
mov    (%rax),%edi
mov    0x2dd6(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
mov    %eax,%r12d
movl   $0x1,-0x40(%rbp)
mov    0x2dd4(%rip),%rbx
mov    (%rbx),%esi
mov    0x2dab(%rip),%rax
mov    (%rax),%edx
lea    -0x40(%rbp),%rcx
mov    %r12d,%edi
mov    $0x4,%r8d
call   1040 <setsockopt@plt>
mov    %eax,%r15d
movl   $0x1,-0x3c(%rbp)
mov    (%rbx),%esi
mov    0x2d64(%rip),%rax
mov    (%rax),%edx
lea    -0x3c(%rbp),%rcx
mov    %r12d,%edi
mov    $0x4,%r8d
call   1040 <setsockopt@plt>
mov    %eax,%ebx
mov    0x2d57(%rip),%rax
mov    (%rax),%ecx
mov    %rsp,%rdx
mov    %rcx,%rax
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rdx
mov    %rdx,%rsp
mov    (%r14),%edi
lea    -0x38(%rbp),%rsi
call   1030 <inet_ntop@plt>
lea    0xd76(%rip),%rdi
mov    %r12d,%esi
mov    %r15d,%edx
mov    %ebx,%ecx
xor    %eax,%eax
call   1080 <printf@plt>
lea    0xda7(%rip),%r15
lea    -0xb0(%rbp),%rbx
lea    0xdb0(%rip),%r13
xchg   %ax,%ax
xor    %edi,%edi
call   10a0 <time@plt>
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %eax,%edx
call   10b0 <sprintf@plt>
mov    %rbx,%rdi
call   1060 <strlen@plt>
mov    %r12d,%edi
mov    %rbx,%rsi
mov    %eax,%edx
xor    %ecx,%ecx
lea    -0x38(%rbp),%r8
mov    $0xc,%r9d
call   1090 <sendto@plt>
mov    %eax,%r14d
mov    -0x38(%rbp),%edi
call   1050 <inet_ntoa@plt>
mov    %r13,%rdi
mov    %r14d,%esi
mov    %rax,%rdx
xor    %eax,%eax
call   1080 <printf@plt>
mov    $0x1,%edi
call   10c0 <sleep@plt>
jmp    12b0 <broadcast_time+0x100>

LLVM IR (-O0):
%struct.sockaddr_in = type { %struct.TYPE_3__, i32, i32 }
%struct.TYPE_3__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@INADDR_BROADCAST = global i32 0
@PF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@SOL_SOCKET = global i32 0
@SO_BROADCAST = global i32 0
@SO_REUSEADDR = global i32 0
@INET_ADDRSTRLEN = global i32 0
@.str = [71 x i8] c"sockfd=%d, setsockopt_broadcast_result=%d, setsockopt_reuse_result=%d\0A\00"
@.str.1 = [23 x i8] c"hello world, it's %ld\0A\00"
@.str.2 = [21 x i8] c"sent %d bytes to %s\0A\00"
define i32 @broadcast_time() {
%1 = alloca %struct.sockaddr_in
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i64
%9 = alloca [100 x i8]
%10 = alloca i32
%11 = load i32, i32* @AF_INET
%12 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i32 0, i32 2
store i32 %11, i32* %12
%13 = call i32 @htons(i32 9004)
%14 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i32 0, i32 1
store i32 %13, i32* %14
%15 = load i32, i32* @INADDR_BROADCAST
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i32 0, i32 0
%17 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %16, i32 0, i32 0
store i32 %15, i32* %17
%18 = load i32, i32* @PF_INET
%19 = load i32, i32* @SOCK_DGRAM
%20 = call i32 @socket(i32 %18, i32 %19, i32 0)
store i32 %20, i32* %2
store i32 1, i32* %3
%21 = load i32, i32* %2
%22 = load i32, i32* @SOL_SOCKET
%23 = load i32, i32* @SO_BROADCAST
%24 = call i32 @setsockopt(i32 %21, i32 %22, i32 %23, i32* %3, i32 4)
store i32 %24, i32* %4
store i32 1, i32* %5
%25 = load i32, i32* %2
%26 = load i32, i32* @SOL_SOCKET
%27 = load i32, i32* @SO_REUSEADDR
%28 = call i32 @setsockopt(i32 %25, i32 %26, i32 %27, i32* %5, i32 4)
store i32 %28, i32* %6
%29 = load i32, i32* @INET_ADDRSTRLEN
%30 = zext i32 %29 to i64
%31 = call i8* @llvm.stacksave()
store i8* %31, i8** %7
%32 = alloca i8, i64 %30
store i64 %30, i64* %8
%33 = load i32, i32* @AF_INET
%34 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i32 0, i32 0
%35 = load i32, i32* @INET_ADDRSTRLEN
%36 = call i32 @inet_ntop(i32 %33, %struct.TYPE_3__* %34, i8* %32, i32 %35)
%37 = load i32, i32* %2
%38 = load i32, i32* %4
%39 = load i32, i32* %6
%40 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([71 x i8], [71 x i8]* @.str, i64 0, i64 0), i32 %37, i32 %38, i32 %39)
br label %41
41: ; preds = %41, %0
%42 = getelementptr inbounds [100 x i8], [100 x i8]* %9, i64 0, i64 0
%43 = call i32 @time(i32* null)
%44 = call i32 @sprintf(i8* %42, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i32 %43)
%45 = load i32, i32* %2
%46 = getelementptr inbounds [100 x i8], [100 x i8]* %9, i64 0, i64 0
%47 = getelementptr inbounds [100 x i8], [100 x i8]* %9, i64 0, i64 0
%48 = call i32 @strlen(i8* %47)
%49 = bitcast %struct.sockaddr_in* %1 to %struct.sockaddr*
%50 = call i32 @sendto(i32 %45, i8* %46, i32 %48, i32 0, %struct.sockaddr* %49, i32 12)
store i32 %50, i32* %10
%51 = load i32, i32* %10
%52 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i32 0, i32 0
%53 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %52, i32 0, i32 0
%54 = load i32, i32* %53
%55 = call i8* @inet_ntoa(i32 %54)
%56 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i32 %51, i8* %55)
%57 = call i32 @sleep(i32 1)
br label %41
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_3__, i32, i32 }
%struct.TYPE_3__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@INADDR_BROADCAST = global i32 0
@PF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@SOL_SOCKET = global i32 0
@SO_BROADCAST = global i32 0
@SO_REUSEADDR = global i32 0
@INET_ADDRSTRLEN = global i32 0
@.str = [71 x i8] c"sockfd=%d, setsockopt_broadcast_result=%d, setsockopt_reuse_result=%d\0A\00"
@.str.1 = [23 x i8] c"hello world, it's %ld\0A\00"
@.str.2 = [21 x i8] c"sent %d bytes to %s\0A\00"
define i32 @broadcast_time() {
%1 = alloca %struct.sockaddr_in
%2 = alloca i32
%3 = alloca i32
%4 = alloca [100 x i8]
%5 = bitcast %struct.sockaddr_in* %1 to i8*
%6 = load i32, i32* @AF_INET
%7 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 2
store i32 %6, i32* %7
%8 = tail call i32 @htons(i32 9004)
%9 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 1
store i32 %8, i32* %9
%10 = load i32, i32* @INADDR_BROADCAST
%11 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 0
%12 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 0, i32 0
store i32 %10, i32* %12
%13 = load i32, i32* @PF_INET
%14 = load i32, i32* @SOCK_DGRAM
%15 = tail call i32 @socket(i32 %13, i32 %14, i32 0)
%16 = bitcast i32* %2 to i8*
store i32 1, i32* %2
%17 = load i32, i32* @SOL_SOCKET
%18 = load i32, i32* @SO_BROADCAST
%19 = call i32 @setsockopt(i32 %15, i32 %17, i32 %18, i32* %2, i32 4)
%20 = bitcast i32* %3 to i8*
store i32 1, i32* %3
%21 = load i32, i32* @SOL_SOCKET
%22 = load i32, i32* @SO_REUSEADDR
%23 = call i32 @setsockopt(i32 %15, i32 %21, i32 %22, i32* %3, i32 4)
%24 = load i32, i32* @INET_ADDRSTRLEN
%25 = zext i32 %24 to i64
%26 = alloca i8, i64 %25
%27 = load i32, i32* @AF_INET
%28 = call i32 @inet_ntop(i32 %27, %struct.TYPE_3__* %11, i8* %26, i32 %24)
%29 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([71 x i8], [71 x i8]* @.str, i64 0, i64 0), i32 %15, i32 %19, i32 %23)
%30 = getelementptr inbounds [100 x i8], [100 x i8]* %4, i64 0, i64 0
%31 = bitcast %struct.sockaddr_in* %1 to %struct.sockaddr*
br label %32
32: ; preds = %32, %0
%33 = call i32 @time(i32* null)
%34 = call i32 @sprintf(i8* %30, i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i32 %33)
%35 = call i32 @strlen(i8* %30)
%36 = call i32 @sendto(i32 %15, i8* %30, i32 %35, i32 0, %struct.sockaddr* %31, i32 12)
%37 = load i32, i32* %12
%38 = call i8* @inet_ntoa(i32 %37)
%39 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i32 %36, i8* %38)
%40 = call i32 @sleep(i32 1)
br label %32
}