#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;
};

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ IPPROTO_IP;
int /*<<< orphan*/ IP_MULTICAST_LOOP;
int /*<<< orphan*/ IP_MULTICAST_TTL;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEADDR;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ htons(unsigned short);
int /*<<< orphan*/ inet_addr(char *);
int setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, ...);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int makesocket(char *szAddr, unsigned short port, int TTL,
               struct sockaddr_in *sSockAddr) {
  int iRet, iLoop = 1;
  struct sockaddr_in sin;
  char cTtl = (char)TTL;
  char cLoop = 0;
  int iSocket = socket(AF_INET, SOCK_DGRAM, 0);
  if (iSocket < 0) {
    fprintf(stderr, "socket() failed.\n");
    exit(1);
  }
  sSockAddr->sin_family = sin.sin_family = AF_INET;
  sSockAddr->sin_port = sin.sin_port = htons(port);
  sSockAddr->sin_addr.s_addr = inet_addr(szAddr);
  iRet = setsockopt(iSocket, SOL_SOCKET, SO_REUSEADDR, &iLoop, sizeof(int));
  if (iRet < 0) {
    fprintf(stderr, "setsockopt SO_REUSEADDR failed\n");
    exit(1);
  }
  iRet = setsockopt(iSocket, IPPROTO_IP, IP_MULTICAST_TTL, &cTtl, sizeof(char));
  if (iRet < 0) {
    fprintf(stderr,
            "setsockopt IP_MULTICAST_TTL failed.  multicast in kernel?\n");
    exit(1);
  }
  cLoop = 1;
  iRet =
      setsockopt(iSocket, IPPROTO_IP, IP_MULTICAST_LOOP, &cLoop, sizeof(char));
  if (iRet < 0) {
    fprintf(stderr,
            "setsockopt IP_MULTICAST_LOOP failed.  multicast in kernel?\n");
    exit(1);
  }
  return iSocket;
}


x86 assembly (clang, -O0):
<makesocket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %si,%ax
mov    %rdi,-0x8(%rbp)
mov    %ax,-0xa(%rbp)
mov    %edx,-0x10(%rbp)
mov    %rcx,-0x18(%rbp)
movl   $0x1,-0x20(%rbp)
mov    -0x10(%rbp),%eax
mov    %al,-0x31(%rbp)
movb   $0x0,-0x32(%rbp)
mov    0x2e46(%rip),%rax
mov    (%rax),%edi
mov    0x2e2d(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,-0x38(%rbp)
cmpl   $0x0,-0x38(%rbp)
jge    11d0 <makesocket+0x70>
mov    0x2e00(%rip),%rax
mov    (%rax),%edi
lea    0xe3f(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    0x2e01(%rip),%rax
mov    (%rax),%ecx
mov    %ecx,-0x28(%rbp)
mov    -0x18(%rbp),%rax
mov    %ecx,0x8(%rax)
movzwl -0xa(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,%ecx
mov    %ecx,-0x2c(%rbp)
mov    -0x18(%rbp),%rax
mov    %ecx,0x4(%rax)
mov    -0x8(%rbp),%rdi
call   1050 <inet_addr@plt>
mov    %eax,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x38(%rbp),%edi
mov    0x2dcd(%rip),%rax
mov    (%rax),%esi
mov    0x2d8c(%rip),%rax
mov    (%rax),%edx
lea    -0x20(%rbp),%rcx
mov    $0x4,%r8d
mov    $0x0,%al
call   1030 <setsockopt@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    125b <makesocket+0xfb>
mov    0x2d75(%rip),%rax
mov    (%rax),%edi
lea    0xdc6(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x38(%rbp),%edi
mov    0x2d6b(%rip),%rax
mov    (%rax),%esi
mov    0x2d42(%rip),%rax
mov    (%rax),%edx
lea    -0x31(%rbp),%rcx
mov    $0x1,%r8d
mov    $0x0,%al
call   1030 <setsockopt@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    12ad <makesocket+0x14d>
mov    0x2d23(%rip),%rax
mov    (%rax),%edi
lea    0xd94(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
movb   $0x1,-0x32(%rbp)
mov    -0x38(%rbp),%edi
mov    0x2d15(%rip),%rax
mov    (%rax),%esi
mov    0x2d2c(%rip),%rax
mov    (%rax),%edx
lea    -0x32(%rbp),%rcx
mov    $0x1,%r8d
mov    $0x0,%al
call   1030 <setsockopt@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    1303 <makesocket+0x1a3>
mov    0x2ccd(%rip),%rax
mov    (%rax),%edi
lea    0xd79(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x38(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<makesocket>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rcx,%rbx
mov    %esi,%r15d
mov    %rdi,%r14
movl   $0x1,0xc(%rsp)
mov    %dl,0xb(%rsp)
mov    0x2e50(%rip),%r12
mov    (%r12),%edi
mov    0x2e35(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
test   %eax,%eax
js     124e <makesocket+0xee>
mov    %eax,%ebp
mov    (%r12),%eax
mov    %eax,0x8(%rbx)
movzwl %r15w,%edi
call   1040 <htons@plt>
mov    %eax,0x4(%rbx)
mov    %r14,%rdi
call   1050 <inet_addr@plt>
mov    %eax,(%rbx)
mov    0x2e16(%rip),%rax
mov    (%rax),%esi
mov    0x2dd5(%rip),%rax
mov    (%rax),%edx
lea    0xc(%rsp),%rcx
mov    $0x4,%r8d
mov    %ebp,%edi
xor    %eax,%eax
call   1030 <setsockopt@plt>
test   %eax,%eax
js     1260 <makesocket+0x100>
mov    0x2ddc(%rip),%rbx
mov    (%rbx),%esi
mov    0x2db3(%rip),%rax
mov    (%rax),%edx
lea    0xb(%rsp),%rcx
mov    $0x1,%r8d
mov    %ebp,%edi
xor    %eax,%eax
call   1030 <setsockopt@plt>
test   %eax,%eax
js     1272 <makesocket+0x112>
movb   $0x1,0xa(%rsp)
mov    (%rbx),%esi
mov    0x2dcb(%rip),%rax
mov    (%rax),%edx
lea    0xa(%rsp),%rcx
mov    $0x1,%r8d
mov    %ebp,%edi
xor    %eax,%eax
call   1030 <setsockopt@plt>
test   %eax,%eax
js     1284 <makesocket+0x124>
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2d63(%rip),%rax
mov    (%rax),%edi
lea    0xda2(%rip),%rsi
jmp    1294 <makesocket+0x134>
mov    0x2d51(%rip),%rax
mov    (%rax),%edi
lea    0xda2(%rip),%rsi
jmp    1294 <makesocket+0x134>
mov    0x2d3f(%rip),%rax
mov    (%rax),%edi
lea    0xdb0(%rip),%rsi
jmp    1294 <makesocket+0x134>
mov    0x2d2d(%rip),%rax
mov    (%rax),%edi
lea    0xdd9(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@stderr = global i32 0
@.str = [18 x i8] c"socket() failed.\0A\00"
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.1 = [32 x i8] c"setsockopt SO_REUSEADDR failed\0A\00"
@IPPROTO_IP = global i32 0
@IP_MULTICAST_TTL = global i32 0
@.str.2 = [59 x i8] c"setsockopt IP_MULTICAST_TTL failed. multicast in kernel?\0A\00"
@IP_MULTICAST_LOOP = global i32 0
@.str.3 = [60 x i8] c"setsockopt IP_MULTICAST_LOOP failed. multicast in kernel?\0A\00"
define i32 @makesocket(i8* %0, i16 zeroext %1, i32 %2, %struct.sockaddr_in* %3) {
%5 = alloca i8*
%6 = alloca i16
%7 = alloca i32
%8 = alloca %struct.sockaddr_in*
%9 = alloca i32
%10 = alloca i32
%11 = alloca %struct.sockaddr_in
%12 = alloca i8
%13 = alloca i8
%14 = alloca i32
store i8* %0, i8** %5
store i16 %1, i16* %6
store i32 %2, i32* %7
store %struct.sockaddr_in* %3, %struct.sockaddr_in** %8
store i32 1, i32* %10
%15 = load i32, i32* %7
%16 = trunc i32 %15 to i8
store i8 %16, i8* %12
store i8 0, i8* %13
%17 = load i32, i32* @AF_INET
%18 = load i32, i32* @SOCK_DGRAM
%19 = call i32 @socket(i32 %17, i32 %18, i32 0)
store i32 %19, i32* %14
%20 = load i32, i32* %14
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %26
22: ; preds = %4
%23 = load i32, i32* @stderr
%24 = call i32 @fprintf(i32 %23, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
%25 = call i32 @exit(i32 1)
unreachable
26: ; preds = %4
%27 = load i32, i32* @AF_INET
%28 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %11, i32 0, i32 2
store i32 %27, i32* %28
%29 = load %struct.sockaddr_in*, %struct.sockaddr_in** %8
%30 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %29, i32 0, i32 2
store i32 %27, i32* %30
%31 = load i16, i16* %6
%32 = call i32 @htons(i16 zeroext %31)
%33 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %11, i32 0, i32 1
store i32 %32, i32* %33
%34 = load %struct.sockaddr_in*, %struct.sockaddr_in** %8
%35 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %34, i32 0, i32 1
store i32 %32, i32* %35
%36 = load i8*, i8** %5
%37 = call i32 @inet_addr(i8* %36)
%38 = load %struct.sockaddr_in*, %struct.sockaddr_in** %8
%39 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %38, i32 0, i32 0
%40 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %39, i32 0, i32 0
store i32 %37, i32* %40
%41 = load i32, i32* %14
%42 = load i32, i32* @SOL_SOCKET
%43 = load i32, i32* @SO_REUSEADDR
%44 = call i32 (i32, i32, i32, ...) @setsockopt(i32 %41, i32 %42, i32 %43, i32* %10, i64 4)
store i32 %44, i32* %9
%45 = load i32, i32* %9
%46 = icmp slt i32 %45, 0
br i1 %46, label %47, label %51
47: ; preds = %26
%48 = load i32, i32* @stderr
%49 = call i32 @fprintf(i32 %48, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0))
%50 = call i32 @exit(i32 1)
unreachable
51: ; preds = %26
%52 = load i32, i32* %14
%53 = load i32, i32* @IPPROTO_IP
%54 = load i32, i32* @IP_MULTICAST_TTL
%55 = call i32 (i32, i32, i32, ...) @setsockopt(i32 %52, i32 %53, i32 %54, i8* %12, i64 1)
store i32 %55, i32* %9
%56 = load i32, i32* %9
%57 = icmp slt i32 %56, 0
br i1 %57, label %58, label %62
58: ; preds = %51
%59 = load i32, i32* @stderr
%60 = call i32 @fprintf(i32 %59, i8* getelementptr inbounds ([59 x i8], [59 x i8]* @.str.2, i64 0, i64 0))
%61 = call i32 @exit(i32 1)
unreachable
62: ; preds = %51
store i8 1, i8* %13
%63 = load i32, i32* %14
%64 = load i32, i32* @IPPROTO_IP
%65 = load i32, i32* @IP_MULTICAST_LOOP
%66 = call i32 (i32, i32, i32, ...) @setsockopt(i32 %63, i32 %64, i32 %65, i8* %13, i64 1)
store i32 %66, i32* %9
%67 = load i32, i32* %9
%68 = icmp slt i32 %67, 0
br i1 %68, label %69, label %73
69: ; preds = %62
%70 = load i32, i32* @stderr
%71 = call i32 @fprintf(i32 %70, i8* getelementptr inbounds ([60 x i8], [60 x i8]* @.str.3, i64 0, i64 0))
%72 = call i32 @exit(i32 1)
unreachable
73: ; preds = %62
%74 = load i32, i32* %14
ret i32 %74
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@stderr = global i32 0
@.str = [18 x i8] c"socket() failed.\0A\00"
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.1 = [32 x i8] c"setsockopt SO_REUSEADDR failed\0A\00"
@IPPROTO_IP = global i32 0
@IP_MULTICAST_TTL = global i32 0
@.str.2 = [59 x i8] c"setsockopt IP_MULTICAST_TTL failed. multicast in kernel?\0A\00"
@IP_MULTICAST_LOOP = global i32 0
@.str.3 = [60 x i8] c"setsockopt IP_MULTICAST_LOOP failed. multicast in kernel?\0A\00"
define i32 @makesocket(i8* %0, i16 zeroext %1, i32 %2, %struct.sockaddr_in* nocapture writeonly %3) {
%5 = alloca i32
%6 = alloca i8
%7 = alloca i8
%8 = bitcast i32* %5 to i8*
store i32 1, i32* %5
%9 = trunc i32 %2 to i8
store i8 %9, i8* %6
%10 = load i32, i32* @AF_INET
%11 = load i32, i32* @SOCK_DGRAM
%12 = tail call i32 @socket(i32 %10, i32 %11, i32 0)
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %18
14: ; preds = %4
%15 = load i32, i32* @stderr
%16 = tail call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
%17 = tail call i32 @exit(i32 1)
unreachable
18: ; preds = %4
%19 = load i32, i32* @AF_INET
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %19, i32* %20
%21 = tail call i32 @htons(i16 zeroext %1)
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
store i32 %21, i32* %22
%23 = tail call i32 @inet_addr(i8* %0)
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0, i32 0
store i32 %23, i32* %24
%25 = load i32, i32* @SOL_SOCKET
%26 = load i32, i32* @SO_REUSEADDR
%27 = call i32 (i32, i32, i32, ...) @setsockopt(i32 %12, i32 %25, i32 %26, i32* %5, i64 4)
%28 = icmp slt i32 %27, 0
br i1 %28, label %29, label %33
29: ; preds = %18
%30 = load i32, i32* @stderr
%31 = call i32 @fprintf(i32 %30, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0))
%32 = call i32 @exit(i32 1)
unreachable
33: ; preds = %18
%34 = load i32, i32* @IPPROTO_IP
%35 = load i32, i32* @IP_MULTICAST_TTL
%36 = call i32 (i32, i32, i32, ...) @setsockopt(i32 %12, i32 %34, i32 %35, i8* %6, i64 1)
%37 = icmp slt i32 %36, 0
br i1 %37, label %38, label %42
38: ; preds = %33
%39 = load i32, i32* @stderr
%40 = call i32 @fprintf(i32 %39, i8* getelementptr inbounds ([59 x i8], [59 x i8]* @.str.2, i64 0, i64 0))
%41 = call i32 @exit(i32 1)
unreachable
42: ; preds = %33
store i8 1, i8* %7
%43 = load i32, i32* @IPPROTO_IP
%44 = load i32, i32* @IP_MULTICAST_LOOP
%45 = call i32 (i32, i32, i32, ...) @setsockopt(i32 %12, i32 %43, i32 %44, i8* %7, i64 1)
%46 = icmp slt i32 %45, 0
br i1 %46, label %47, label %51
47: ; preds = %42
%48 = load i32, i32* @stderr
%49 = call i32 @fprintf(i32 %48, i8* getelementptr inbounds ([60 x i8], [60 x i8]* @.str.3, i64 0, i64 0))
%50 = call i32 @exit(i32 1)
unreachable
51: ; preds = %42
ret i32 %12
}