#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 */

/* Type definitions */
struct in_addr {
  int /*<<< orphan*/ s_addr;
};
struct addrinfo {
  int /*<<< orphan*/ ai_addrlen;
  int /*<<< orphan*/ ai_addr;
  int /*<<< orphan*/ ai_socktype;
  int /*<<< orphan*/ ai_family;
};

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ IPPROTO_IP;
int /*<<< orphan*/ IP_MULTICAST_IF;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
char *gai_strerror(int);
int getaddrinfo(char *, char *, struct addrinfo *, struct addrinfo **);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
scalar_t__ sendto(int, char *, int /*<<< orphan*/, int /*<<< orphan*/,
                  int /*<<< orphan*/, int /*<<< orphan*/);
int setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, struct in_addr *,
               int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strlen(char *);
void multicast_send() {
  int sockfd = socket(AF_INET, SOCK_DGRAM, 0);
  if (sockfd == -1) {
    perror("Open socket");
    exit(2);
  }
  printf("Socket opened\n");
  struct addrinfo hints;
  memset(&hints, 0, sizeof(struct addrinfo));
  hints.ai_family = AF_INET;
  hints.ai_socktype = SOCK_DGRAM;
  struct addrinfo *res;
  int wut = getaddrinfo("239.100.100.100", "4321", &hints, &res);
  if (wut != 0) {
    fprintf(stderr, "Can't... %s\n", gai_strerror(wut));
    exit(1);
  }
  printf("Got address info\n");
  struct in_addr localIf;
  localIf.s_addr = INADDR_ANY;
  if (setsockopt(sockfd, IPPROTO_IP, IP_MULTICAST_IF, &localIf,
                 sizeof(struct in_addr)) == -1) {
    perror("Unable to set socket option...");
    close(sockfd);
    exit(1);
  }
  printf("Socket option set!\n");
  char *buf = "Hello there";
  if (sendto(sockfd, buf, strlen(buf), 0, res->ai_addr, res->ai_addrlen) < 0) {
    perror("Could not send shit.");
  }
  close(sockfd);
}


x86 assembly (clang, -O0):
<multicast_send>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    0x2e19(%rip),%rax
mov    (%rax),%edi
mov    0x2df0(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10e0 <socket@plt>
mov    %eax,-0x4(%rbp)
cmpl   $0xffffffff,-0x4(%rbp)
jne    1204 <multicast_send+0x44>
lea    0xe0b(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x2,%edi
call   10c0 <exit@plt>
lea    0xe01(%rip),%rdi
call   1050 <printf@plt>
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1070 <memset@plt>
mov    0x2dc1(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xc(%rbp)
mov    0x2d95(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
lea    0xddc(%rip),%rdi
lea    0xde5(%rip),%rsi
lea    -0x18(%rbp),%rdx
lea    -0x20(%rbp),%rcx
call   10d0 <getaddrinfo@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
je     1290 <multicast_send+0xd0>
mov    0x2d51(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    -0x24(%rbp),%edi
call   1060 <gai_strerror@plt>
mov    -0x34(%rbp),%edi
mov    %rax,%rdx
lea    0xdaf(%rip),%rsi
call   10a0 <fprintf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
lea    0xda6(%rip),%rdi
call   1050 <printf@plt>
mov    0x2d2d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    -0x4(%rbp),%edi
mov    0x2d2e(%rip),%rax
mov    (%rax),%esi
mov    0x2d1d(%rip),%rax
mov    (%rax),%edx
lea    -0x28(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0xffffffff,%eax
jne    12f3 <multicast_send+0x133>
lea    0xd73(%rip),%rdi
call   10b0 <perror@plt>
mov    -0x4(%rbp),%edi
call   1090 <close@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
lea    0xd74(%rip),%rdi
call   1050 <printf@plt>
lea    0xd7c(%rip),%rax
mov    %rax,-0x30(%rbp)
mov    -0x4(%rbp),%eax
mov    %eax,-0x44(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x30(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x44(%rbp),%edi
mov    -0x40(%rbp),%rsi
mov    %eax,%edx
mov    -0x20(%rbp),%rax
mov    0x4(%rax),%r8d
mov    -0x20(%rbp),%rax
mov    (%rax),%r9d
xor    %ecx,%ecx
call   1080 <sendto@plt>
cmp    $0x0,%rax
jge    1356 <multicast_send+0x196>
lea    0xd3d(%rip),%rdi
call   10b0 <perror@plt>
mov    -0x4(%rbp),%edi
call   1090 <close@plt>
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<multicast_send>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x28,%rsp
mov    0x2e17(%rip),%rbx
mov    (%rbx),%edi
mov    0x2dee(%rip),%r15
mov    (%r15),%esi
xor    %edx,%edx
call   10e0 <socket@plt>
cmp    $0xffffffff,%eax
je     12df <multicast_send+0x11f>
mov    %eax,%ebp
lea    0xe78(%rip),%rdi
call   1030 <puts@plt>
lea    0x18(%rsp),%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1070 <memset@plt>
mov    (%rbx),%eax
mov    %eax,0x24(%rsp)
mov    (%r15),%eax
mov    %eax,0x20(%rsp)
lea    0xde9(%rip),%rdi
lea    0xdf2(%rip),%rsi
lea    0x10(%rsp),%rcx
mov    %r14,%rdx
call   10d0 <getaddrinfo@plt>
test   %eax,%eax
jne    12f5 <multicast_send+0x135>
lea    0xe36(%rip),%rdi
call   1030 <puts@plt>
mov    0x2d7e(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
mov    0x2d81(%rip),%rax
mov    (%rax),%esi
mov    0x2d70(%rip),%rax
mov    (%rax),%edx
lea    0x8(%rsp),%rcx
mov    %ebp,%edi
mov    $0x4,%r8d
call   1040 <setsockopt@plt>
cmp    $0xffffffff,%eax
je     1320 <multicast_send+0x160>
lea    0xe01(%rip),%rdi
call   1030 <puts@plt>
lea    0xdb5(%rip),%rbx
mov    %rbx,%rdi
call   1050 <strlen@plt>
mov    0x10(%rsp),%rcx
mov    (%rcx),%r9d
mov    0x4(%rcx),%r8d
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
xor    %ecx,%ecx
call   1080 <sendto@plt>
test   %rax,%rax
js     12d1 <multicast_send+0x111>
mov    %ebp,%edi
call   1090 <close@plt>
add    $0x28,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd81(%rip),%rdi
call   10b0 <perror@plt>
jmp    12bf <multicast_send+0xff>
lea    0xd1a(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x2,%edi
call   10c0 <exit@plt>
mov    0x2cbc(%rip),%rcx
mov    (%rcx),%ebx
mov    %eax,%edi
call   1060 <gai_strerror@plt>
lea    0xd15(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   10a0 <fprintf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
lea    0xd07(%rip),%rdi
call   10b0 <perror@plt>
mov    %ebp,%edi
call   1090 <close@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
%struct.addrinfo = type { i32, i32, i32, i32 }
%struct.in_addr = type { i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@.str = [12 x i8] c"Open socket\00"
@.str.1 = [15 x i8] c"Socket opened\0A\00"
@.str.2 = [16 x i8] c"239.100.100.100\00"
@.str.3 = [5 x i8] c"4321\00"
@stderr = global i32 0
@.str.4 = [13 x i8] c"Can't... %s\0A\00"
@.str.5 = [18 x i8] c"Got address info\0A\00"
@INADDR_ANY = global i32 0
@IPPROTO_IP = global i32 0
@IP_MULTICAST_IF = global i32 0
@.str.6 = [31 x i8] c"Unable to set socket option...\00"
@.str.7 = [20 x i8] c"Socket option set!\0A\00"
@.str.8 = [12 x i8] c"Hello there\00"
@.str.9 = [21 x i8] c"Could not send shit.\00"
define void @multicast_send() {
%1 = alloca i32
%2 = alloca %struct.addrinfo
%3 = alloca %struct.addrinfo*
%4 = alloca i32
%5 = alloca %struct.in_addr
%6 = alloca i8*
%7 = load i32, i32* @AF_INET
%8 = load i32, i32* @SOCK_DGRAM
%9 = call i32 @socket(i32 %7, i32 %8, i32 0)
store i32 %9, i32* %1
%10 = load i32, i32* %1
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %15
12: ; preds = %0
%13 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%14 = call i32 @exit(i32 2)
unreachable
15: ; preds = %0
%16 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
%17 = call i32 @memset(%struct.addrinfo* %2, i32 0, i32 16)
%18 = load i32, i32* @AF_INET
%19 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %2, i32 0, i32 3
store i32 %18, i32* %19
%20 = load i32, i32* @SOCK_DGRAM
%21 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %2, i32 0, i32 2
store i32 %20, i32* %21
%22 = call i32 @getaddrinfo(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), %struct.addrinfo* %2, %struct.addrinfo** %3)
store i32 %22, i32* %4
%23 = load i32, i32* %4
%24 = icmp ne i32 %23, 0
br i1 %24, label %25, label %31
25: ; preds = %15
%26 = load i32, i32* @stderr
%27 = load i32, i32* %4
%28 = call i8* @gai_strerror(i32 %27)
%29 = call i32 @fprintf(i32 %26, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0), i8* %28)
%30 = call i32 @exit(i32 1)
unreachable
31: ; preds = %15
%32 = call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.5, i64 0, i64 0))
%33 = load i32, i32* @INADDR_ANY
%34 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %5, i32 0, i32 0
store i32 %33, i32* %34
%35 = load i32, i32* %1
%36 = load i32, i32* @IPPROTO_IP
%37 = load i32, i32* @IP_MULTICAST_IF
%38 = call i32 @setsockopt(i32 %35, i32 %36, i32 %37, %struct.in_addr* %5, i32 4)
%39 = icmp eq i32 %38, -1
br i1 %39, label %40, label %45
40: ; preds = %31
%41 = call i32 @perror(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.6, i64 0, i64 0))
%42 = load i32, i32* %1
%43 = call i32 @close(i32 %42)
%44 = call i32 @exit(i32 1)
unreachable
45: ; preds = %31
%46 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.7, i64 0, i64 0))
store i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.8, i64 0, i64 0), i8** %6
%47 = load i32, i32* %1
%48 = load i8*, i8** %6
%49 = load i8*, i8** %6
%50 = call i32 @strlen(i8* %49)
%51 = load %struct.addrinfo*, %struct.addrinfo** %3
%52 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %51, i32 0, i32 1
%53 = load i32, i32* %52
%54 = load %struct.addrinfo*, %struct.addrinfo** %3
%55 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %54, i32 0, i32 0
%56 = load i32, i32* %55
%57 = call i64 @sendto(i32 %47, i8* %48, i32 %50, i32 0, i32 %53, i32 %56)
%58 = icmp slt i64 %57, 0
br i1 %58, label %59, label %61
59: ; preds = %45
%60 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.9, i64 0, i64 0))
br label %61
61: ; preds = %59, %45
%62 = load i32, i32* %1
%63 = call i32 @close(i32 %62)
ret void
}

LLVM IR (-O2):
%struct.addrinfo = type { i32, i32, i32, i32 }
%struct.in_addr = type { i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@.str = [12 x i8] c"Open socket\00"
@.str.2 = [16 x i8] c"239.100.100.100\00"
@.str.3 = [5 x i8] c"4321\00"
@stderr = global i32 0
@.str.4 = [13 x i8] c"Can't... %s\0A\00"
@INADDR_ANY = global i32 0
@IPPROTO_IP = global i32 0
@IP_MULTICAST_IF = global i32 0
@.str.6 = [31 x i8] c"Unable to set socket option...\00"
@.str.8 = [12 x i8] c"Hello there\00"
@.str.9 = [21 x i8] c"Could not send shit.\00"
@str = [14 x i8] c"Socket opened\00"
@str.10 = [17 x i8] c"Got address info\00"
@str.11 = [19 x i8] c"Socket option set!\00"
define void @multicast_send() {
%1 = alloca %struct.addrinfo
%2 = alloca %struct.addrinfo*
%3 = alloca %struct.in_addr
%4 = load i32, i32* @AF_INET
%5 = load i32, i32* @SOCK_DGRAM
%6 = tail call i32 @socket(i32 %4, i32 %5, i32 0)
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %11
8: ; preds = %0
%9 = tail call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%10 = tail call i32 @exit(i32 2)
unreachable
11: ; preds = %0
%12 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str, i64 0, i64 0))
%13 = bitcast %struct.addrinfo* %1 to i8*
%14 = call i32 @memset(%struct.addrinfo* %1, i32 0, i32 16)
%15 = load i32, i32* @AF_INET
%16 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %1, i64 0, i32 3
store i32 %15, i32* %16
%17 = load i32, i32* @SOCK_DGRAM
%18 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %1, i64 0, i32 2
store i32 %17, i32* %18
%19 = bitcast %struct.addrinfo** %2 to i8*
%20 = call i32 @getaddrinfo(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), %struct.addrinfo* %1, %struct.addrinfo** %2)
%21 = icmp eq i32 %20, 0
br i1 %21, label %27, label %22
22: ; preds = %11
%23 = load i32, i32* @stderr
%24 = call i8* @gai_strerror(i32 %20)
%25 = call i32 @fprintf(i32 %23, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0), i8* %24)
%26 = call i32 @exit(i32 1)
unreachable
27: ; preds = %11
%28 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @str.10, i64 0, i64 0))
%29 = bitcast %struct.in_addr* %3 to i8*
%30 = load i32, i32* @INADDR_ANY
%31 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %3, i64 0, i32 0
store i32 %30, i32* %31
%32 = load i32, i32* @IPPROTO_IP
%33 = load i32, i32* @IP_MULTICAST_IF
%34 = call i32 @setsockopt(i32 %6, i32 %32, i32 %33, %struct.in_addr* %3, i32 4)
%35 = icmp eq i32 %34, -1
br i1 %35, label %36, label %40
36: ; preds = %27
%37 = call i32 @perror(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.6, i64 0, i64 0))
%38 = call i32 @close(i32 %6)
%39 = call i32 @exit(i32 1)
unreachable
40: ; preds = %27
%41 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str.11, i64 0, i64 0))
%42 = call i32 @strlen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.8, i64 0, i64 0))
%43 = load %struct.addrinfo*, %struct.addrinfo** %2
%44 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %43, i64 0, i32 1
%45 = load i32, i32* %44
%46 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %43, i64 0, i32 0
%47 = load i32, i32* %46
%48 = call i64 @sendto(i32 %6, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.8, i64 0, i64 0), i32 %42, i32 0, i32 %45, i32 %47)
%49 = icmp slt i64 %48, 0
br i1 %49, label %50, label %52
50: ; preds = %40
%51 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.9, i64 0, i64 0))
br label %52
52: ; preds = %50, %40
%53 = call i32 @close(i32 %6)
ret void
}