#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 {
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ gateway;
typedef int /*<<< orphan*/ broadcast;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_BROADCAST;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_BROADCAST;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ htons(int);
char *inet_ntoa(TYPE_1__);
int /*<<< orphan*/ printf(char *, ...);
int sendto(int, char *, scalar_t__, 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*/);
scalar_t__ strlen(char *);
int /*<<< orphan*/ time(int /*<<< orphan*/ *);
int broadcast_loop(void) {
  struct sockaddr_in gateway;
  gateway.sin_family = AF_INET;
  gateway.sin_port = htons(9004);
  gateway.sin_addr.s_addr = INADDR_BROADCAST;
  int sockfd = socket(PF_INET, SOCK_DGRAM, 0);
  int broadcast = 1;
  int setsockopt_err = setsockopt(sockfd, SOL_SOCKET, SO_BROADCAST, &broadcast,
                                  sizeof(broadcast));
  if (setsockopt_err)
    printf("Error in setsockopt: %d\n", setsockopt_err);
  char message[100];
  for (;;) {
    sprintf(message, "The time is currently %ld", time(NULL));
    int bytes_sent = sendto(sockfd, message, strlen(message) + 1, 0,
                            (struct sockaddr *)&gateway, sizeof(gateway));
    printf("sent '%s' (%d bytes) to %s\n", message, bytes_sent,
           inet_ntoa(gateway.sin_addr));
    sleep(1);
  }
  close(sockfd);
  return 0;
}


x86 assembly (clang, -O0):
<broadcast_loop>:
push   %rbp
mov    %rsp,%rbp
sub    $0xb0,%rsp
mov    0x2e2e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x8(%rbp)
mov    $0x232c,%edi
call   1060 <htons@plt>
mov    %eax,-0xc(%rbp)
mov    0x2e0d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    0x2de1(%rip),%rax
mov    (%rax),%edi
mov    0x2df0(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
mov    %eax,-0x14(%rbp)
movl   $0x1,-0x18(%rbp)
mov    -0x14(%rbp),%edi
mov    0x2deb(%rip),%rax
mov    (%rax),%esi
mov    0x2dc2(%rip),%rax
mov    (%rax),%edx
lea    -0x18(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     1235 <broadcast_loop+0x95>
mov    -0x1c(%rbp),%esi
lea    0xdd2(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
jmp    123a <broadcast_loop+0x9a>
lea    -0x80(%rbp),%rax
mov    %rax,-0xb0(%rbp)
xor    %eax,%eax
mov    %eax,%edi
call   1090 <time@plt>
mov    -0xb0(%rbp),%rdi
mov    %eax,%edx
lea    0xdbb(%rip),%rsi
call   10a0 <sprintf@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0xa4(%rbp)
lea    -0x80(%rbp),%rax
mov    %rax,-0xa0(%rbp)
lea    -0x80(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0xa4(%rbp),%edi
mov    -0xa0(%rbp),%rsi
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x98(%rbp)
lea    -0x10(%rbp),%r8
xor    %ecx,%ecx
mov    $0xc,%r9d
call   1080 <sendto@plt>
mov    %eax,-0x84(%rbp)
lea    -0x80(%rbp),%rax
mov    %rax,-0x90(%rbp)
mov    -0x84(%rbp),%eax
mov    %eax,-0x88(%rbp)
mov    -0x10(%rbp),%edi
call   1040 <inet_ntoa@plt>
mov    -0x90(%rbp),%rsi
mov    -0x88(%rbp),%edx
mov    %rax,%rcx
lea    0xd4b(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    $0x1,%edi
call   10b0 <sleep@plt>
jmp    123a <broadcast_loop+0x9a>

x86 assembly (clang, -O2):
<broadcast_loop>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x88,%rsp
mov    0x2e28(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x18(%rsp)
mov    $0x232c,%edi
call   1060 <htons@plt>
mov    %eax,0x14(%rsp)
mov    0x2e05(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
mov    0x2dd8(%rip),%rax
mov    (%rax),%edi
mov    0x2de7(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
mov    %eax,%r13d
movl   $0x1,0xc(%rsp)
mov    0x2de4(%rip),%rax
mov    (%rax),%esi
mov    0x2dbb(%rip),%rax
mov    (%rax),%edx
lea    0xc(%rsp),%rcx
mov    %r13d,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
test   %eax,%eax
je     1236 <broadcast_loop+0x96>
lea    0xdd3(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1070 <printf@plt>
lea    0xddc(%rip),%r14
lea    0x20(%rsp),%rbx
lea    0x10(%rsp),%r15
lea    0xde5(%rip),%r12
xchg   %ax,%ax
xor    %edi,%edi
call   1090 <time@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %eax,%edx
call   10a0 <sprintf@plt>
mov    %rbx,%rdi
call   1050 <strlen@plt>
lea    0x1(%rax),%rdx
mov    %r13d,%edi
mov    %rbx,%rsi
xor    %ecx,%ecx
mov    %r15,%r8
mov    $0xc,%r9d
call   1080 <sendto@plt>
mov    %eax,%ebp
mov    0x10(%rsp),%edi
call   1040 <inet_ntoa@plt>
mov    %r12,%rdi
mov    %rbx,%rsi
mov    %ebp,%edx
mov    %rax,%rcx
xor    %eax,%eax
call   1070 <printf@plt>
mov    $0x1,%edi
call   10b0 <sleep@plt>
jmp    1250 <broadcast_loop+0xb0>

LLVM IR (-O0):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = 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
@.str = [25 x i8] c"Error in setsockopt: %d\0A\00"
@.str.1 = [26 x i8] c"The time is currently %ld\00"
@.str.2 = [28 x i8] c"sent '%s' (%d bytes) to %s\0A\00"
define i32 @broadcast_loop() {
%1 = alloca %struct.sockaddr_in
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca [100 x i8]
%6 = alloca i32
%7 = load i32, i32* @AF_INET
%8 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i32 0, i32 2
store i32 %7, i32* %8
%9 = call i32 @htons(i32 9004)
%10 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i32 0, i32 1
store i32 %9, i32* %10
%11 = load i32, i32* @INADDR_BROADCAST
%12 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i32 0, i32 0
%13 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %12, i32 0, i32 0
store i32 %11, i32* %13
%14 = load i32, i32* @PF_INET
%15 = load i32, i32* @SOCK_DGRAM
%16 = call i32 @socket(i32 %14, i32 %15, i32 0)
store i32 %16, i32* %2
store i32 1, i32* %3
%17 = load i32, i32* %2
%18 = load i32, i32* @SOL_SOCKET
%19 = load i32, i32* @SO_BROADCAST
%20 = call i32 @setsockopt(i32 %17, i32 %18, i32 %19, i32* %3, i32 4)
store i32 %20, i32* %4
%21 = load i32, i32* %4
%22 = icmp ne i32 %21, 0
br i1 %22, label %23, label %26
23: ; preds = %0
%24 = load i32, i32* %4
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i32 %24)
br label %26
26: ; preds = %23, %0
br label %27
27: ; preds = %27, %26
%28 = getelementptr inbounds [100 x i8], [100 x i8]* %5, i64 0, i64 0
%29 = call i32 @time(i32* null)
%30 = call i32 @sprintf(i8* %28, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i32 %29)
%31 = load i32, i32* %2
%32 = getelementptr inbounds [100 x i8], [100 x i8]* %5, i64 0, i64 0
%33 = getelementptr inbounds [100 x i8], [100 x i8]* %5, i64 0, i64 0
%34 = call i64 @strlen(i8* %33)
%35 = add nsw i64 %34, 1
%36 = bitcast %struct.sockaddr_in* %1 to %struct.sockaddr*
%37 = call i32 @sendto(i32 %31, i8* %32, i64 %35, i32 0, %struct.sockaddr* %36, i32 12)
store i32 %37, i32* %6
%38 = getelementptr inbounds [100 x i8], [100 x i8]* %5, i64 0, i64 0
%39 = load i32, i32* %6
%40 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i32 0, i32 0
%41 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %40, i32 0, i32 0
%42 = load i32, i32* %41
%43 = call i8* @inet_ntoa(i32 %42)
%44 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0), i8* %38, i32 %39, i8* %43)
%45 = call i32 @sleep(i32 1)
br label %27
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = 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
@.str = [25 x i8] c"Error in setsockopt: %d\0A\00"
@.str.1 = [26 x i8] c"The time is currently %ld\00"
@.str.2 = [28 x i8] c"sent '%s' (%d bytes) to %s\0A\00"
define i32 @broadcast_loop() {
%1 = alloca %struct.sockaddr_in
%2 = alloca i32
%3 = alloca [100 x i8]
%4 = bitcast %struct.sockaddr_in* %1 to i8*
%5 = load i32, i32* @AF_INET
%6 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 2
store i32 %5, i32* %6
%7 = tail call i32 @htons(i32 9004)
%8 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 1
store i32 %7, i32* %8
%9 = load i32, i32* @INADDR_BROADCAST
%10 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 0, i32 0
store i32 %9, i32* %10
%11 = load i32, i32* @PF_INET
%12 = load i32, i32* @SOCK_DGRAM
%13 = tail call i32 @socket(i32 %11, i32 %12, i32 0)
%14 = bitcast i32* %2 to i8*
store i32 1, i32* %2
%15 = load i32, i32* @SOL_SOCKET
%16 = load i32, i32* @SO_BROADCAST
%17 = call i32 @setsockopt(i32 %13, i32 %15, i32 %16, i32* %2, i32 4)
%18 = icmp eq i32 %17, 0
br i1 %18, label %21, label %19
19: ; preds = %0
%20 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i32 %17)
br label %21
21: ; preds = %19, %0
%22 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 0
%23 = bitcast %struct.sockaddr_in* %1 to %struct.sockaddr*
br label %24
24: ; preds = %24, %21
%25 = call i32 @time(i32* null)
%26 = call i32 @sprintf(i8* %22, i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i32 %25)
%27 = call i64 @strlen(i8* %22)
%28 = add nsw i64 %27, 1
%29 = call i32 @sendto(i32 %13, i8* %22, i64 %28, i32 0, %struct.sockaddr* %23, i32 12)
%30 = load i32, i32* %10
%31 = call i8* @inet_ntoa(i32 %30)
%32 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0), i8* %22, i32 %29, i8* %31)
%33 = call i32 @sleep(i32 1)
br label %24
}