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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ IPPROTO_IP;
int /*<<< orphan*/ IP_MULTICAST_LOOP;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEADDR;
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 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, int /*<<< orphan*/);
int strlen(char *);
void *SendMsgThread(void *arg) {
  int sfd, ret;
  struct sockaddr_in sender;
  int sender_len = sizeof(sender);
  int on = 1;
  int off = 0;
  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("226.0.0.3");
  sender.sin_port = htons(12345);
  setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &on, (socklen_t)sizeof(on));
  ret = setsockopt(sfd, IPPROTO_IP, IP_MULTICAST_LOOP, &off, sizeof(off));
  if (ret < 0) {
    perror("setsockopt multicast loop off failed");
    exit(EXIT_FAILURE);
  }
  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    $0xa0,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0xc,-0x24(%rbp)
movl   $0x1,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    0x2ddd(%rip),%rax
mov    (%rax),%edi
mov    0x2dc4(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10f0 <socket@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    1234 <SendMsgThread+0x64>
lea    0xddf(%rip),%rdi
call   10b0 <perror@plt>
mov    0x2d8b(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1080 <memset@plt>
mov    0x2d8d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
lea    0xdb7(%rip),%rdi
call   10a0 <inet_addr@plt>
mov    %eax,-0x1c(%rbp)
mov    $0x3039,%edi
call   1060 <htons@plt>
mov    %eax,-0x20(%rbp)
mov    -0xc(%rbp),%edi
mov    0x2d6a(%rip),%rax
mov    (%rax),%esi
mov    0x2d31(%rip),%rax
mov    (%rax),%edx
lea    -0x28(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    -0xc(%rbp),%edi
mov    0x2d36(%rip),%rax
mov    (%rax),%esi
mov    0x2d4d(%rip),%rax
mov    (%rax),%edx
lea    -0x2c(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jge    12db <SendMsgThread+0x10b>
lea    0xd50(%rip),%rdi
call   10b0 <perror@plt>
mov    0x2ce4(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,-0x30(%rbp)
mov    -0x30(%rbp),%esi
lea    0xd4e(%rip),%rdi
call   1070 <printf@plt>
movl   $0x1,-0x34(%rbp)
lea    -0x90(%rbp),%rdi
mov    -0x30(%rbp),%edx
mov    -0x34(%rbp),%ecx
mov    %ecx,%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
lea    0xd55(%rip),%rsi
call   10c0 <sprintf@plt>
lea    -0x90(%rbp),%rdi
call   1050 <strlen@plt>
add    $0x1,%eax
cltq
mov    %rax,-0x98(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x90(%rbp),%rsi
mov    -0x98(%rbp),%rdx
lea    -0x20(%rbp),%r8
mov    -0x24(%rbp),%r9d
xor    %ecx,%ecx
call   1090 <sendto@plt>
mov    %rax,-0xa0(%rbp)
cmpq   $0x0,-0xa0(%rbp)
jge    1375 <SendMsgThread+0x1a5>
lea    0xd12(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x3,%edi
call   10e0 <sleep@plt>
jmp    12fb <SendMsgThread+0x12b>

x86 assembly (clang, -O2):
<SendMsgThread>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x78,%rsp
movl   $0x1,0xc(%rsp)
movl   $0x0,0x8(%rsp)
mov    0x2de3(%rip),%rbx
mov    (%rbx),%edi
mov    0x2dca(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10f0 <socket@plt>
test   %eax,%eax
js     1324 <SendMsgThread+0x154>
mov    %eax,%r14d
lea    0x10(%rsp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1080 <memset@plt>
mov    (%rbx),%eax
mov    %eax,0x18(%rsp)
lea    0xdde(%rip),%rdi
call   10a0 <inet_addr@plt>
mov    %eax,0x14(%rsp)
mov    $0x3039,%edi
call   1060 <htons@plt>
mov    %eax,0x10(%rsp)
mov    0x2d92(%rip),%rax
mov    (%rax),%esi
mov    0x2d59(%rip),%rax
mov    (%rax),%edx
lea    0xc(%rsp),%rcx
mov    %r14d,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    0x2d5d(%rip),%rax
mov    (%rax),%esi
mov    0x2d74(%rip),%rax
mov    (%rax),%edx
lea    0x8(%rsp),%rcx
mov    %r14d,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
test   %eax,%eax
js     132d <SendMsgThread+0x15d>
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %eax,%r12d
lea    0xd93(%rip),%rdi
mov    %eax,%esi
call   1070 <printf@plt>
mov    $0x1,%ebx
lea    0xdaf(%rip),%r15
lea    0x20(%rsp),%rbp
lea    0x10(%rsp),%r13
jmp    12dd <SendMsgThread+0x10d>
nopl   0x0(%rax)
mov    $0x3,%edi
call   10e0 <sleep@plt>
add    $0x1,%ebx
mov    %rbp,%rdi
mov    %r15,%rsi
mov    %r12d,%edx
mov    %ebx,%ecx
call   10c0 <sprintf@plt>
mov    %rbp,%rdi
call   1050 <strlen@plt>
add    $0x1,%eax
movslq %eax,%rdx
mov    %r14d,%edi
mov    %rbp,%rsi
xor    %ecx,%ecx
mov    %r13,%r8
mov    $0xc,%r9d
call   1090 <sendto@plt>
test   %rax,%rax
jns    12d0 <SendMsgThread+0x100>
lea    0xd65(%rip),%rdi
call   10b0 <perror@plt>
jmp    12d0 <SendMsgThread+0x100>
lea    0xcd5(%rip),%rdi
jmp    1334 <SendMsgThread+0x164>
lea    0xce4(%rip),%rdi
call   10b0 <perror@plt>
mov    0x2c78(%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 = [10 x i8] c"226.0.0.3\00"
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@IPPROTO_IP = global i32 0
@IP_MULTICAST_LOOP = global i32 0
@.str.2 = [37 x i8] c"setsockopt multicast loop off failed\00"
@.str.3 = [47 x i8] c"pid %d about to start sending every 3 seconds\0A\00"
@.str.4 = [22 x i8] c"%d sending message %d\00"
@.str.5 = [21 x i8] c"sendto sender failed\00"
define i8* @SendMsgThread(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.sockaddr_in
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca [80 x i8]
%12 = alloca i64
%13 = alloca i64
store i8* %0, i8** %2
store i32 12, i32* %6
store i32 1, i32* %7
store i32 0, i32* %8
%14 = load i32, i32* @AF_INET
%15 = load i32, i32* @SOCK_DGRAM
%16 = call i32 @socket(i32 %14, i32 %15, i32 0)
store i32 %16, i32* %3
%17 = load i32, i32* %3
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %23
19: ; preds = %1
%20 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
%21 = load i32, i32* @EXIT_FAILURE
%22 = call i32 @exit(i32 %21)
unreachable
23: ; preds = %1
%24 = call i32 @memset(%struct.sockaddr_in* %5, i32 0, i32 12)
%25 = load i32, i32* @AF_INET
%26 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 2
store i32 %25, i32* %26
%27 = call i32 @inet_addr(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
%28 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 1
%29 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %28, i32 0, i32 0
store i32 %27, i32* %29
%30 = call i32 @htons(i32 12345)
%31 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 0
store i32 %30, i32* %31
%32 = load i32, i32* %3
%33 = load i32, i32* @SOL_SOCKET
%34 = load i32, i32* @SO_REUSEADDR
%35 = call i32 @setsockopt(i32 %32, i32 %33, i32 %34, i32* %7, i32 4)
%36 = load i32, i32* %3
%37 = load i32, i32* @IPPROTO_IP
%38 = load i32, i32* @IP_MULTICAST_LOOP
%39 = call i32 @setsockopt(i32 %36, i32 %37, i32 %38, i32* %8, i32 4)
store i32 %39, i32* %4
%40 = load i32, i32* %4
%41 = icmp slt i32 %40, 0
br i1 %41, label %42, label %46
42: ; preds = %23
%43 = call i32 @perror(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.2, i64 0, i64 0))
%44 = load i32, i32* @EXIT_FAILURE
%45 = call i32 @exit(i32 %44)
unreachable
46: ; preds = %23
%47 = call i32 (...) @getpid()
store i32 %47, i32* %9
%48 = load i32, i32* %9
%49 = call i32 @printf(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.3, i64 0, i64 0), i32 %48)
store i32 1, i32* %10
br label %50
50: ; preds = %70, %46
%51 = getelementptr inbounds [80 x i8], [80 x i8]* %11, i64 0, i64 0
%52 = load i32, i32* %9
%53 = load i32, i32* %10
%54 = add nsw i32 %53, 1
store i32 %54, i32* %10
%55 = call i32 @sprintf(i8* %51, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0), i32 %52, i32 %53)
%56 = getelementptr inbounds [80 x i8], [80 x i8]* %11, i64 0, i64 0
%57 = call i32 @strlen(i8* %56)
%58 = add nsw i32 %57, 1
%59 = sext i32 %58 to i64
store i64 %59, i64* %12
%60 = load i32, i32* %3
%61 = getelementptr inbounds [80 x i8], [80 x i8]* %11, i64 0, i64 0
%62 = load i64, i64* %12
%63 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%64 = load i32, i32* %6
%65 = call i64 @sendto(i32 %60, i8* %61, i64 %62, i32 0, %struct.sockaddr* %63, i32 %64)
store i64 %65, i64* %13
%66 = load i64, i64* %13
%67 = icmp slt i64 %66, 0
br i1 %67, label %68, label %70
68: ; preds = %50
%69 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.5, i64 0, i64 0))
br label %70
70: ; preds = %68, %50
%71 = call i32 @sleep(i32 3)
br label %50
}

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 = [10 x i8] c"226.0.0.3\00"
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@IPPROTO_IP = global i32 0
@IP_MULTICAST_LOOP = global i32 0
@.str.2 = [37 x i8] c"setsockopt multicast loop off failed\00"
@.str.3 = [47 x i8] c"pid %d about to start sending every 3 seconds\0A\00"
@.str.4 = [22 x i8] c"%d sending message %d\00"
@.str.5 = [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 i32
%5 = alloca [80 x i8]
%6 = bitcast %struct.sockaddr_in* %2 to i8*
%7 = bitcast i32* %3 to i8*
store i32 1, i32* %3
%8 = bitcast i32* %4 to i8*
store i32 0, i32* %4
%9 = load i32, i32* @AF_INET
%10 = load i32, i32* @SOCK_DGRAM
%11 = tail call i32 @socket(i32 %9, i32 %10, i32 0)
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %17
13: ; preds = %1
%14 = tail call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
%15 = load i32, i32* @EXIT_FAILURE
%16 = tail call i32 @exit(i32 %15)
unreachable
17: ; preds = %1
%18 = call i32 @memset(%struct.sockaddr_in* %2, i32 0, i32 12)
%19 = load i32, i32* @AF_INET
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 2
store i32 %19, i32* %20
%21 = call i32 @inet_addr(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 1, i32 0
store i32 %21, i32* %22
%23 = call i32 @htons(i32 12345)
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 0
store i32 %23, i32* %24
%25 = load i32, i32* @SOL_SOCKET
%26 = load i32, i32* @SO_REUSEADDR
%27 = call i32 @setsockopt(i32 %11, i32 %25, i32 %26, i32* %3, i32 4)
%28 = load i32, i32* @IPPROTO_IP
%29 = load i32, i32* @IP_MULTICAST_LOOP
%30 = call i32 @setsockopt(i32 %11, i32 %28, i32 %29, i32* %4, i32 4)
%31 = icmp slt i32 %30, 0
br i1 %31, label %32, label %36
32: ; preds = %17
%33 = call i32 @perror(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.2, i64 0, i64 0))
%34 = load i32, i32* @EXIT_FAILURE
%35 = call i32 @exit(i32 %34)
unreachable
36: ; preds = %17
%37 = call i32 (...) @getpid()
%38 = call i32 @printf(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.3, i64 0, i64 0), i32 %37)
%39 = getelementptr inbounds [80 x i8], [80 x i8]* %5, i64 0, i64 0
%40 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
br label %41
41: ; preds = %52, %36
%42 = phi i32 [ 1, %36 ], [ %43, %52 ]
%43 = add nuw nsw i32 %42, 1
%44 = call i32 @sprintf(i8* %39, i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0), i32 %37, i32 %42)
%45 = call i32 @strlen(i8* %39)
%46 = add nsw i32 %45, 1
%47 = sext i32 %46 to i64
%48 = call i64 @sendto(i32 %11, i8* %39, i64 %47, i32 0, %struct.sockaddr* %40, i32 12)
%49 = icmp slt i64 %48, 0
br i1 %49, label %50, label %52
50: ; preds = %41
%51 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.5, i64 0, i64 0))
br label %52
52: ; preds = %50, %41
%53 = call i32 @sleep(i32 3)
br label %41
}