#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__ {
  unsigned int 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);
unsigned int inet_addr(char *);
int ntohl(unsigned int);
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;
  unsigned int tempaddr;
  int iSocket = socket(AF_INET, SOCK_DGRAM, 0);
  if (iSocket < 0) {
    fprintf(stderr, "socket() failed.\n");
    exit(1);
  }
  tempaddr = inet_addr(szAddr);
  sSockAddr->sin_family = sin.sin_family = AF_INET;
  sSockAddr->sin_port = sin.sin_port = htons(port);
  sSockAddr->sin_addr.s_addr = tempaddr;
  iRet = setsockopt(iSocket, SOL_SOCKET, SO_REUSEADDR, &iLoop, sizeof(int));
  if (iRet < 0) {
    fprintf(stderr, "setsockopt SO_REUSEADDR failed\n");
    exit(1);
  }
  if ((ntohl(tempaddr) >> 28) == 0xe) {
    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    0x2e36(%rip),%rax
mov    (%rax),%edi
mov    0x2e1d(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0x0,-0x3c(%rbp)
jge    11e0 <makesocket+0x70>
mov    0x2df0(%rip),%rax
mov    (%rax),%edi
lea    0xe2f(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x8(%rbp),%rdi
call   1050 <inet_addr@plt>
mov    %eax,-0x38(%rbp)
mov    0x2de5(%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    -0x38(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x3c(%rbp),%edi
mov    0x2db9(%rip),%rax
mov    (%rax),%esi
mov    0x2d78(%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    126f <makesocket+0xff>
mov    0x2d61(%rip),%rax
mov    (%rax),%edi
lea    0xdb2(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x38(%rbp),%edi
call   1080 <ntohl@plt>
sar    $0x1c,%eax
cmp    $0xe,%eax
jne    1330 <makesocket+0x1c0>
mov    -0x3c(%rbp),%edi
mov    0x2d43(%rip),%rax
mov    (%rax),%esi
mov    0x2d1a(%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    12d5 <makesocket+0x165>
mov    0x2cfb(%rip),%rax
mov    (%rax),%edi
lea    0xd6c(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
movb   $0x1,-0x32(%rbp)
mov    -0x3c(%rbp),%edi
mov    0x2ced(%rip),%rax
mov    (%rax),%esi
mov    0x2d04(%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    132b <makesocket+0x1bb>
mov    0x2ca5(%rip),%rax
mov    (%rax),%edi
lea    0xd51(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
jmp    1330 <makesocket+0x1c0>
mov    -0x3c(%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    0x2e4c(%rip),%r12
mov    (%r12),%edi
mov    0x2e39(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
test   %eax,%eax
js     1210 <makesocket+0xa0>
mov    %eax,%ebp
mov    %r14,%rdi
call   1050 <inet_addr@plt>
mov    %eax,%r14d
mov    (%r12),%eax
mov    %eax,0x8(%rbx)
movzwl %r15w,%edi
call   1040 <htons@plt>
mov    %eax,0x4(%rbx)
mov    %r14d,(%rbx)
mov    0x2e12(%rip),%rax
mov    (%rax),%esi
mov    0x2de1(%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     1222 <makesocket+0xb2>
mov    %r14d,%edi
call   1080 <ntohl@plt>
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2db1(%rip),%rax
mov    (%rax),%edi
lea    0xde0(%rip),%rsi
jmp    1232 <makesocket+0xc2>
mov    0x2d9f(%rip),%rax
mov    (%rax),%edi
lea    0xde0(%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
%15 = 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
%16 = load i32, i32* %7
%17 = trunc i32 %16 to i8
store i8 %17, i8* %12
store i8 0, i8* %13
%18 = load i32, i32* @AF_INET
%19 = load i32, i32* @SOCK_DGRAM
%20 = call i32 @socket(i32 %18, i32 %19, i32 0)
store i32 %20, i32* %15
%21 = load i32, i32* %15
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %27
23: ; preds = %4
%24 = load i32, i32* @stderr
%25 = call i32 @fprintf(i32 %24, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
%26 = call i32 @exit(i32 1)
unreachable
27: ; preds = %4
%28 = load i8*, i8** %5
%29 = call i32 @inet_addr(i8* %28)
store i32 %29, i32* %14
%30 = load i32, i32* @AF_INET
%31 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %11, i32 0, i32 2
store i32 %30, i32* %31
%32 = load %struct.sockaddr_in*, %struct.sockaddr_in** %8
%33 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %32, i32 0, i32 2
store i32 %30, i32* %33
%34 = load i16, i16* %6
%35 = call i32 @htons(i16 zeroext %34)
%36 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %11, i32 0, i32 1
store i32 %35, i32* %36
%37 = load %struct.sockaddr_in*, %struct.sockaddr_in** %8
%38 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %37, i32 0, i32 1
store i32 %35, i32* %38
%39 = load i32, i32* %14
%40 = load %struct.sockaddr_in*, %struct.sockaddr_in** %8
%41 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %40, i32 0, i32 0
%42 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %41, i32 0, i32 0
store i32 %39, i32* %42
%43 = load i32, i32* %15
%44 = load i32, i32* @SOL_SOCKET
%45 = load i32, i32* @SO_REUSEADDR
%46 = call i32 (i32, i32, i32, ...) @setsockopt(i32 %43, i32 %44, i32 %45, i32* %10, i64 4)
store i32 %46, i32* %9
%47 = load i32, i32* %9
%48 = icmp slt i32 %47, 0
br i1 %48, label %49, label %53
49: ; preds = %27
%50 = load i32, i32* @stderr
%51 = call i32 @fprintf(i32 %50, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0))
%52 = call i32 @exit(i32 1)
unreachable
53: ; preds = %27
%54 = load i32, i32* %14
%55 = call i32 @ntohl(i32 %54)
%56 = ashr i32 %55, 28
%57 = icmp eq i32 %56, 14
br i1 %57, label %58, label %81
58: ; preds = %53
%59 = load i32, i32* %15
%60 = load i32, i32* @IPPROTO_IP
%61 = load i32, i32* @IP_MULTICAST_TTL
%62 = call i32 (i32, i32, i32, ...) @setsockopt(i32 %59, i32 %60, i32 %61, i8* %12, i64 1)
store i32 %62, i32* %9
%63 = load i32, i32* %9
%64 = icmp slt i32 %63, 0
br i1 %64, label %65, label %69
65: ; preds = %58
%66 = load i32, i32* @stderr
%67 = call i32 @fprintf(i32 %66, i8* getelementptr inbounds ([59 x i8], [59 x i8]* @.str.2, i64 0, i64 0))
%68 = call i32 @exit(i32 1)
unreachable
69: ; preds = %58
store i8 1, i8* %13
%70 = load i32, i32* %15
%71 = load i32, i32* @IPPROTO_IP
%72 = load i32, i32* @IP_MULTICAST_LOOP
%73 = call i32 (i32, i32, i32, ...) @setsockopt(i32 %70, i32 %71, i32 %72, i8* %13, i64 1)
store i32 %73, i32* %9
%74 = load i32, i32* %9
%75 = icmp slt i32 %74, 0
br i1 %75, label %76, label %80
76: ; preds = %69
%77 = load i32, i32* @stderr
%78 = call i32 @fprintf(i32 %77, i8* getelementptr inbounds ([60 x i8], [60 x i8]* @.str.3, i64 0, i64 0))
%79 = call i32 @exit(i32 1)
unreachable
80: ; preds = %69
br label %81
81: ; preds = %80, %53
%82 = load i32, i32* %15
ret i32 %82
}

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
@IP_MULTICAST_LOOP = global i32 0
define i32 @makesocket(i8* %0, i16 zeroext %1, i32 %2, %struct.sockaddr_in* nocapture writeonly %3) {
%5 = alloca i32
%6 = bitcast i32* %5 to i8*
store i32 1, i32* %5
%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 = %4
%12 = load i32, i32* @stderr
%13 = tail call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
%14 = tail call i32 @exit(i32 1)
unreachable
15: ; preds = %4
%16 = tail call i32 @inet_addr(i8* %0)
%17 = load i32, i32* @AF_INET
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %17, i32* %18
%19 = tail call i32 @htons(i16 zeroext %1)
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
store i32 %19, i32* %20
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0, i32 0
store i32 %16, i32* %21
%22 = load i32, i32* @SOL_SOCKET
%23 = load i32, i32* @SO_REUSEADDR
%24 = call i32 (i32, i32, i32, ...) @setsockopt(i32 %9, i32 %22, i32 %23, i32* %5, i64 4)
%25 = icmp slt i32 %24, 0
br i1 %25, label %26, label %30
26: ; preds = %15
%27 = load i32, i32* @stderr
%28 = call i32 @fprintf(i32 %27, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0))
%29 = call i32 @exit(i32 1)
unreachable
30: ; preds = %15
%31 = call i32 @ntohl(i32 %16)
ret i32 %9
}