#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 ip_mreq {
  TYPE_1__ imr_multiaddr;
};
typedef int /*<<< orphan*/ group;

/* Variables and functions */
int /*<<< orphan*/ IPPROTO_IP;
int /*<<< orphan*/ IP_ADD_MEMBERSHIP;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ inet_addr(char const *);
int /*<<< orphan*/ perror(char *);
int rand();
scalar_t__ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, char *, int);
int /*<<< orphan*/ usleep(int);
void join_mcast_group(int sd, const char *g_name) {
  struct ip_mreq group;
  usleep(rand() % (200000 / 10000));
  group.imr_multiaddr.s_addr = inet_addr(g_name);
  if (setsockopt(sd, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char *)&group,
                 sizeof(group)) < 0) {
    perror("adding multicast group");
    exit(1);
  }
}


x86 assembly (clang, -O0):
<join_mcast_group>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0x0,%al
call   1070 <rand@plt>
mov    $0x14,%ecx
cltd
idiv   %ecx
mov    %edx,%edi
call   1080 <usleep@plt>
mov    -0x10(%rbp),%rdi
call   1040 <inet_addr@plt>
mov    %eax,-0x18(%rbp)
mov    -0x4(%rbp),%edi
mov    0x2e45(%rip),%rax
mov    (%rax),%esi
mov    0x2e44(%rip),%rax
mov    (%rax),%edx
lea    -0x18(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0x0,%rax
jge    11d5 <join_mcast_group+0x75>
lea    0xe3a(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<join_mcast_group>:
push   %rbp
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %edi,%ebp
xor    %eax,%eax
call   1070 <rand@plt>
movslq %eax,%rdi
imul   $0x66666667,%rdi,%rax
mov    %rax,%rcx
shr    $0x3f,%rcx
sar    $0x23,%rax
add    %ecx,%eax
shl    $0x2,%eax
lea    (%rax,%rax,4),%eax
sub    %eax,%edi
call   1080 <usleep@plt>
mov    %rbx,%rdi
call   1040 <inet_addr@plt>
mov    %eax,(%rsp)
mov    0x2e3b(%rip),%rax
mov    (%rax),%esi
mov    0x2e3a(%rip),%rax
mov    (%rax),%edx
mov    %rsp,%rcx
mov    %ebp,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
test   %rax,%rax
js     11cc <join_mcast_group+0x6c>
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xe2d(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
%struct.ip_mreq = type { %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i32 }
@IPPROTO_IP = global i32 0
@IP_ADD_MEMBERSHIP = global i32 0
@.str = [23 x i8] c"adding multicast group\00"
define void @join_mcast_group(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca %struct.ip_mreq
store i32 %0, i32* %3
store i8* %1, i8** %4
%6 = call i32 (...) @rand()
%7 = srem i32 %6, 20
%8 = call i32 @usleep(i32 %7)
%9 = load i8*, i8** %4
%10 = call i32 @inet_addr(i8* %9)
%11 = getelementptr inbounds %struct.ip_mreq, %struct.ip_mreq* %5, i32 0, i32 0
%12 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %11, i32 0, i32 0
store i32 %10, i32* %12
%13 = load i32, i32* %3
%14 = load i32, i32* @IPPROTO_IP
%15 = load i32, i32* @IP_ADD_MEMBERSHIP
%16 = bitcast %struct.ip_mreq* %5 to i8*
%17 = call i64 @setsockopt(i32 %13, i32 %14, i32 %15, i8* %16, i32 4)
%18 = icmp slt i64 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %2
%20 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
%21 = call i32 @exit(i32 1)
unreachable
22: ; preds = %2
ret void
}

LLVM IR (-O2):
%struct.ip_mreq = type { %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i32 }
@IPPROTO_IP = global i32 0
@IP_ADD_MEMBERSHIP = global i32 0
@.str = [23 x i8] c"adding multicast group\00"
define void @join_mcast_group(i32 %0, i8* %1) {
%3 = alloca %struct.ip_mreq
%4 = bitcast %struct.ip_mreq* %3 to i8*
%5 = tail call i32 (...) @rand()
%6 = srem i32 %5, 20
%7 = tail call i32 @usleep(i32 %6)
%8 = tail call i32 @inet_addr(i8* %1)
%9 = getelementptr inbounds %struct.ip_mreq, %struct.ip_mreq* %3, i64 0, i32 0, i32 0
store i32 %8, i32* %9
%10 = load i32, i32* @IPPROTO_IP
%11 = load i32, i32* @IP_ADD_MEMBERSHIP
%12 = call i64 @setsockopt(i32 %0, i32 %10, i32 %11, i8* %4, i32 4)
%13 = icmp slt i64 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %2
%15 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
%16 = call i32 @exit(i32 1)
unreachable
17: ; preds = %2
ret void
}