#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 scalar_t__ ssize_t;
typedef int /*<<< orphan*/ socklen_t;
typedef int /*<<< orphan*/ sender;
typedef int pid_t;
typedef int /*<<< orphan*/ on;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_BROADCAST;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int getpid();
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ inet_addr(char *);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int);
scalar_t__ sendto(int, char *, size_t, int /*<<< orphan*/, struct sockaddr *,
                  int);
int /*<<< orphan*/ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/,
                              int *, int /*<<< orphan*/);
int /*<<< orphan*/ sleep(int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ sprintf(char *, char *, int, int /*<<< orphan*/);
int strlen(char *);
void *SendMsgThread(void *arg) {
  int sfd;
  struct sockaddr_in sender;
  int sender_len = sizeof(sender);
  int on = 1;
  sfd = socket(AF_INET, SOCK_DGRAM, 0);
  if (sfd < 0) {
    perror("socket failed");
    exit(EXIT_FAILURE);
  }
  memset(&sender, 0, sizeof(sender));
  sender.sin_family = AF_INET;
  sender.sin_addr.s_addr = inet_addr("192.168.0.255");
  sender.sin_port = htons(11111);
  setsockopt(sfd, SOL_SOCKET, SO_BROADCAST, &on, (socklen_t)sizeof(on));
  pid_t self = getpid();
  printf("pid %d about to start sending every 3 seconds\n", self);
  int i = 1;
  for (;;) {
    char msg[80];
    sprintf(msg, "%d sending message %d", self, i++);
    size_t msg_len = strlen(msg) + 1;
    ssize_t n =
        sendto(sfd, msg, msg_len, 0, (struct sockaddr *)&sender, sender_len);
    if (n < 0) {
      perror("sendto sender failed");
    }
    sleep(3);
  }
  return NULL;
}


x86 assembly (clang, -O0):
<SendMsgThread>:
push   %rbp
mov    %rsp,%rbp
sub    $0x90,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0xc,-0x1c(%rbp)
movl   $0x1,-0x20(%rbp)
mov    0x2dec(%rip),%rax
mov    (%rax),%edi
mov    0x2ddb(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10f0 <socket@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    122d <SendMsgThread+0x5d>
lea    0xde6(%rip),%rdi
call   10b0 <perror@plt>
mov    0x2d9a(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1080 <memset@plt>
mov    0x2d9c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
lea    0xdbe(%rip),%rdi
call   10a0 <inet_addr@plt>
mov    %eax,-0x14(%rbp)
mov    $0x2b67,%edi
call   1060 <htons@plt>
mov    %eax,-0x18(%rbp)
mov    -0xc(%rbp),%edi
mov    0x2d79(%rip),%rax
mov    (%rax),%esi
mov    0x2d58(%rip),%rax
mov    (%rax),%edx
lea    -0x20(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%esi
lea    0xd7f(%rip),%rdi
call   1070 <printf@plt>
movl   $0x1,-0x28(%rbp)
lea    -0x80(%rbp),%rdi
mov    -0x24(%rbp),%edx
mov    -0x28(%rbp),%ecx
mov    %ecx,%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
lea    0xd89(%rip),%rsi
call   10c0 <sprintf@plt>
lea    -0x80(%rbp),%rdi
call   1050 <strlen@plt>
add    $0x1,%eax
cltq
mov    %rax,-0x88(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x80(%rbp),%rsi
mov    -0x88(%rbp),%rdx
lea    -0x18(%rbp),%r8
mov    -0x1c(%rbp),%r9d
xor    %ecx,%ecx
call   1090 <sendto@plt>
mov    %rax,-0x90(%rbp)
cmpq   $0x0,-0x90(%rbp)
jge    131a <SendMsgThread+0x14a>
lea    0xd4c(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x3,%edi
call   10e0 <sleep@plt>
jmp    12a9 <SendMsgThread+0xd9>

x86 assembly (clang, -O2):
<SendMsgThread>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x78,%rsp
movl   $0x1,0xc(%rsp)
mov    0x2df3(%rip),%rbx
mov    (%rbx),%edi
mov    0x2de2(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10f0 <socket@plt>
test   %eax,%eax
js     12f4 <SendMsgThread+0x124>
mov    %eax,%r15d
lea    0x10(%rsp),%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1080 <memset@plt>
mov    (%rbx),%eax
mov    %eax,0x18(%rsp)
lea    0xde3(%rip),%rdi
call   10a0 <inet_addr@plt>
mov    %eax,0x14(%rsp)
mov    $0x2b67,%edi
call   1060 <htons@plt>
mov    %eax,0x10(%rsp)
mov    0x2d9f(%rip),%rax
mov    (%rax),%esi
mov    0x2d7e(%rip),%rax
mov    (%rax),%edx
lea    0xc(%rsp),%rcx
mov    %r15d,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %eax,%r13d
lea    0xda4(%rip),%rdi
mov    %eax,%esi
call   1070 <printf@plt>
mov    $0x1,%ebx
lea    0xdc0(%rip),%r12
lea    0x20(%rsp),%rbp
jmp    12ad <SendMsgThread+0xdd>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    $0x3,%edi
call   10e0 <sleep@plt>
add    $0x1,%ebx
mov    %rbp,%rdi
mov    %r12,%rsi
mov    %r13d,%edx
mov    %ebx,%ecx
call   10c0 <sprintf@plt>
mov    %rbp,%rdi
call   1050 <strlen@plt>
add    $0x1,%eax
movslq %eax,%rdx
mov    %r15d,%edi
mov    %rbp,%rsi
xor    %ecx,%ecx
mov    %r14,%r8
mov    $0xc,%r9d
call   1090 <sendto@plt>
test   %rax,%rax
jns    12a0 <SendMsgThread+0xd0>
lea    0xd74(%rip),%rdi
call   10b0 <perror@plt>
jmp    12a0 <SendMsgThread+0xd0>
lea    0xd05(%rip),%rdi
call   10b0 <perror@plt>
mov    0x2cb9(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@.str = [14 x i8] c"socket failed\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [14 x i8] c"192.168.0.255\00"
@SOL_SOCKET = global i32 0
@SO_BROADCAST = global i32 0
@.str.2 = [47 x i8] c"pid %d about to start sending every 3 seconds\0A\00"
@.str.3 = [22 x i8] c"%d sending message %d\00"
@.str.4 = [21 x i8] c"sendto sender failed\00"
define i8* @SendMsgThread(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca %struct.sockaddr_in
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca [80 x i8]
%10 = alloca i64
%11 = alloca i64
store i8* %0, i8** %2
store i32 12, i32* %5
store i32 1, i32* %6
%12 = load i32, i32* @AF_INET
%13 = load i32, i32* @SOCK_DGRAM
%14 = call i32 @socket(i32 %12, i32 %13, i32 0)
store i32 %14, i32* %3
%15 = load i32, i32* %3
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %21
17: ; preds = %1
%18 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
%19 = load i32, i32* @EXIT_FAILURE
%20 = call i32 @exit(i32 %19)
unreachable
21: ; preds = %1
%22 = call i32 @memset(%struct.sockaddr_in* %4, i32 0, i32 12)
%23 = load i32, i32* @AF_INET
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 2
store i32 %23, i32* %24
%25 = call i32 @inet_addr(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
%26 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 1
%27 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %26, i32 0, i32 0
store i32 %25, i32* %27
%28 = call i32 @htons(i32 11111)
%29 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 0
store i32 %28, i32* %29
%30 = load i32, i32* %3
%31 = load i32, i32* @SOL_SOCKET
%32 = load i32, i32* @SO_BROADCAST
%33 = call i32 @setsockopt(i32 %30, i32 %31, i32 %32, i32* %6, i32 4)
%34 = call i32 (...) @getpid()
store i32 %34, i32* %7
%35 = load i32, i32* %7
%36 = call i32 @printf(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.2, i64 0, i64 0), i32 %35)
store i32 1, i32* %8
br label %37
37: ; preds = %57, %21
%38 = getelementptr inbounds [80 x i8], [80 x i8]* %9, i64 0, i64 0
%39 = load i32, i32* %7
%40 = load i32, i32* %8
%41 = add nsw i32 %40, 1
store i32 %41, i32* %8
%42 = call i32 @sprintf(i8* %38, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0), i32 %39, i32 %40)
%43 = getelementptr inbounds [80 x i8], [80 x i8]* %9, i64 0, i64 0
%44 = call i32 @strlen(i8* %43)
%45 = add nsw i32 %44, 1
%46 = sext i32 %45 to i64
store i64 %46, i64* %10
%47 = load i32, i32* %3
%48 = getelementptr inbounds [80 x i8], [80 x i8]* %9, i64 0, i64 0
%49 = load i64, i64* %10
%50 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%51 = load i32, i32* %5
%52 = call i64 @sendto(i32 %47, i8* %48, i64 %49, i32 0, %struct.sockaddr* %50, i32 %51)
store i64 %52, i64* %11
%53 = load i64, i64* %11
%54 = icmp slt i64 %53, 0
br i1 %54, label %55, label %57
55: ; preds = %37
%56 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0))
br label %57
57: ; preds = %55, %37
%58 = call i32 @sleep(i32 3)
br label %37
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@.str = [14 x i8] c"socket failed\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [14 x i8] c"192.168.0.255\00"
@SOL_SOCKET = global i32 0
@SO_BROADCAST = global i32 0
@.str.2 = [47 x i8] c"pid %d about to start sending every 3 seconds\0A\00"
@.str.3 = [22 x i8] c"%d sending message %d\00"
@.str.4 = [21 x i8] c"sendto sender failed\00"
define noalias i8* @SendMsgThread(i8* nocapture readnone %0) {
%2 = alloca %struct.sockaddr_in
%3 = alloca i32
%4 = alloca [80 x i8]
%5 = bitcast %struct.sockaddr_in* %2 to i8*
%6 = bitcast i32* %3 to i8*
store i32 1, i32* %3
%7 = load i32, i32* @AF_INET
%8 = load i32, i32* @SOCK_DGRAM
%9 = tail call i32 @socket(i32 %7, i32 %8, i32 0)
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %15
11: ; preds = %1
%12 = tail call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
%13 = load i32, i32* @EXIT_FAILURE
%14 = tail call i32 @exit(i32 %13)
unreachable
15: ; preds = %1
%16 = call i32 @memset(%struct.sockaddr_in* %2, i32 0, i32 12)
%17 = load i32, i32* @AF_INET
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 2
store i32 %17, i32* %18
%19 = call i32 @inet_addr(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 1, i32 0
store i32 %19, i32* %20
%21 = call i32 @htons(i32 11111)
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 0
store i32 %21, i32* %22
%23 = load i32, i32* @SOL_SOCKET
%24 = load i32, i32* @SO_BROADCAST
%25 = call i32 @setsockopt(i32 %9, i32 %23, i32 %24, i32* %3, i32 4)
%26 = call i32 (...) @getpid()
%27 = call i32 @printf(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.2, i64 0, i64 0), i32 %26)
%28 = getelementptr inbounds [80 x i8], [80 x i8]* %4, i64 0, i64 0
%29 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
br label %30
30: ; preds = %41, %15
%31 = phi i32 [ 1, %15 ], [ %32, %41 ]
%32 = add nuw nsw i32 %31, 1
%33 = call i32 @sprintf(i8* %28, i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0), i32 %26, i32 %31)
%34 = call i32 @strlen(i8* %28)
%35 = add nsw i32 %34, 1
%36 = sext i32 %35 to i64
%37 = call i64 @sendto(i32 %9, i8* %28, i64 %36, i32 0, %struct.sockaddr* %29, i32 12)
%38 = icmp slt i64 %37, 0
br i1 %38, label %39, label %41
39: ; preds = %30
%40 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0))
br label %41
41: ; preds = %39, %30
%42 = call i32 @sleep(i32 3)
br label %30
}