#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 */
typedef int /*<<< orphan*/ sockopt;
typedef int /*<<< orphan*/ in_addr_t;

/* Variables and functions */
int /*<<< orphan*/ IPPROTO_IP;
int /*<<< orphan*/ IP_MULTICAST_LOOP;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_BROADCAST;
int /*<<< orphan*/ SO_REUSEADDR;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ log_error(char *, int /*<<< orphan*/);
scalar_t__ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, int *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
int udpsocket_open(in_addr_t laddr, in_addr_t raddr, unsigned short port) {
  int sock;
  int sockopt;
  sock = socket(PF_INET, SOCK_DGRAM, 0);
  if (sock < 0) {
    log_error("could not open socket (%s)", strerror(errno));
    return -1;
  }
  sockopt = 1;
  if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &sockopt, sizeof(sockopt)) <
      0) {
    log_error("could not set SO_REUSEADDR socket option (%s)", strerror(errno));
    return -1;
  }
  if (setsockopt(sock, SOL_SOCKET, SO_BROADCAST, &sockopt, sizeof(sockopt)) <
      0) {
    log_error("could not set SO_BROADCAST socket option (%s)", strerror(errno));
    return -1;
  }
  if (setsockopt(sock, IPPROTO_IP, IP_MULTICAST_LOOP, &sockopt,
                 sizeof(sockopt)) < 0) {
    log_error("could not set IP_MULTICAST_LOOP socket option (%s)",
              strerror(errno));
    return -1;
  }
  return sock;
}


x86 assembly (clang, -O0):
<udpsocket_open>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %dx,%ax
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %ax,-0xe(%rbp)
mov    0x2e54(%rip),%rax
mov    (%rax),%edi
mov    0x2e6b(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1060 <socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11a3 <udpsocket_open+0x63>
mov    0x2e36(%rip),%rax
mov    (%rax),%edi
call   1050 <strerror@plt>
mov    %eax,%esi
lea    0xe6e(%rip),%rdi
call   1040 <log_error@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12b2 <udpsocket_open+0x172>
movl   $0x1,-0x18(%rbp)
mov    -0x14(%rbp),%edi
mov    0x2e2c(%rip),%rax
mov    (%rax),%esi
mov    0x2deb(%rip),%rax
mov    (%rax),%edx
lea    -0x18(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0x0,%rax
jge    1200 <udpsocket_open+0xc0>
mov    0x2dd9(%rip),%rax
mov    (%rax),%edi
call   1050 <strerror@plt>
mov    %eax,%esi
lea    0xe2c(%rip),%rdi
call   1040 <log_error@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12b2 <udpsocket_open+0x172>
mov    -0x14(%rbp),%edi
mov    0x2dd6(%rip),%rax
mov    (%rax),%esi
mov    0x2db5(%rip),%rax
mov    (%rax),%edx
lea    -0x18(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0x0,%rax
jge    1256 <udpsocket_open+0x116>
mov    0x2d83(%rip),%rax
mov    (%rax),%edi
call   1050 <strerror@plt>
mov    %eax,%esi
lea    0xe04(%rip),%rdi
call   1040 <log_error@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12b2 <udpsocket_open+0x172>
mov    -0x14(%rbp),%edi
mov    0x2d78(%rip),%rax
mov    (%rax),%esi
mov    0x2d87(%rip),%rax
mov    (%rax),%edx
lea    -0x18(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0x0,%rax
jge    12ac <udpsocket_open+0x16c>
mov    0x2d2d(%rip),%rax
mov    (%rax),%edi
call   1050 <strerror@plt>
mov    %eax,%esi
lea    0xddc(%rip),%rdi
call   1040 <log_error@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12b2 <udpsocket_open+0x172>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<udpsocket_open>:
push   %r14
push   %rbx
push   %rax
mov    0x2e65(%rip),%rax
mov    (%rax),%edi
mov    0x2e7c(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1060 <socket@plt>
test   %eax,%eax
js     11fc <udpsocket_open+0xbc>
mov    %eax,%ebx
movl   $0x1,0x4(%rsp)
mov    0x2e6a(%rip),%r14
mov    (%r14),%esi
mov    0x2e28(%rip),%rax
mov    (%rax),%edx
lea    0x4(%rsp),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
test   %rax,%rax
js     1213 <udpsocket_open+0xd3>
mov    (%r14),%esi
mov    0x2e25(%rip),%rax
mov    (%rax),%edx
lea    0x4(%rsp),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
test   %rax,%rax
js     122a <udpsocket_open+0xea>
mov    0x2e15(%rip),%rax
mov    (%rax),%esi
mov    0x2e24(%rip),%rax
mov    (%rax),%edx
lea    0x4(%rsp),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
test   %rax,%rax
jns    124b <udpsocket_open+0x10b>
mov    0x2dcc(%rip),%rax
mov    (%rax),%edi
call   1050 <strerror@plt>
lea    0xe7d(%rip),%rdi
jmp    123f <udpsocket_open+0xff>
mov    0x2db5(%rip),%rax
mov    (%rax),%edi
call   1050 <strerror@plt>
lea    0xdef(%rip),%rdi
jmp    123f <udpsocket_open+0xff>
mov    0x2d9e(%rip),%rax
mov    (%rax),%edi
call   1050 <strerror@plt>
lea    0xdf3(%rip),%rdi
jmp    123f <udpsocket_open+0xff>
mov    0x2d87(%rip),%rax
mov    (%rax),%edi
call   1050 <strerror@plt>
lea    0xe0a(%rip),%rdi
mov    %eax,%esi
call   1040 <log_error@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@PF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@.str = [27 x i8] c"could not open socket (%s)\00"
@errno = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.1 = [46 x i8] c"could not set SO_REUSEADDR socket option (%s)\00"
@SO_BROADCAST = global i32 0
@.str.2 = [46 x i8] c"could not set SO_BROADCAST socket option (%s)\00"
@IPPROTO_IP = global i32 0
@IP_MULTICAST_LOOP = global i32 0
@.str.3 = [51 x i8] c"could not set IP_MULTICAST_LOOP socket option (%s)\00"
define i32 @udpsocket_open(i32 %0, i32 %1, i16 zeroext %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i16
%8 = alloca i32
%9 = alloca i32
store i32 %0, i32* %5
store i32 %1, i32* %6
store i16 %2, i16* %7
%10 = load i32, i32* @PF_INET
%11 = load i32, i32* @SOCK_DGRAM
%12 = call i32 @socket(i32 %10, i32 %11, i32 0)
store i32 %12, i32* %8
%13 = load i32, i32* %8
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %19
15: ; preds = %3
%16 = load i32, i32* @errno
%17 = call i32 @strerror(i32 %16)
%18 = call i32 @log_error(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i32 %17)
store i32 -1, i32* %4
br label %51
19: ; preds = %3
store i32 1, i32* %9
%20 = load i32, i32* %8
%21 = load i32, i32* @SOL_SOCKET
%22 = load i32, i32* @SO_REUSEADDR
%23 = call i64 @setsockopt(i32 %20, i32 %21, i32 %22, i32* %9, i32 4)
%24 = icmp slt i64 %23, 0
br i1 %24, label %25, label %29
25: ; preds = %19
%26 = load i32, i32* @errno
%27 = call i32 @strerror(i32 %26)
%28 = call i32 @log_error(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.1, i64 0, i64 0), i32 %27)
store i32 -1, i32* %4
br label %51
29: ; preds = %19
%30 = load i32, i32* %8
%31 = load i32, i32* @SOL_SOCKET
%32 = load i32, i32* @SO_BROADCAST
%33 = call i64 @setsockopt(i32 %30, i32 %31, i32 %32, i32* %9, i32 4)
%34 = icmp slt i64 %33, 0
br i1 %34, label %35, label %39
35: ; preds = %29
%36 = load i32, i32* @errno
%37 = call i32 @strerror(i32 %36)
%38 = call i32 @log_error(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.2, i64 0, i64 0), i32 %37)
store i32 -1, i32* %4
br label %51
39: ; preds = %29
%40 = load i32, i32* %8
%41 = load i32, i32* @IPPROTO_IP
%42 = load i32, i32* @IP_MULTICAST_LOOP
%43 = call i64 @setsockopt(i32 %40, i32 %41, i32 %42, i32* %9, i32 4)
%44 = icmp slt i64 %43, 0
br i1 %44, label %45, label %49
45: ; preds = %39
%46 = load i32, i32* @errno
%47 = call i32 @strerror(i32 %46)
%48 = call i32 @log_error(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.3, i64 0, i64 0), i32 %47)
store i32 -1, i32* %4
br label %51
49: ; preds = %39
%50 = load i32, i32* %8
store i32 %50, i32* %4
br label %51
51: ; preds = %49, %45, %35, %25, %15
%52 = load i32, i32* %4
ret i32 %52
}

LLVM IR (-O2):
@PF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@.str = [27 x i8] c"could not open socket (%s)\00"
@errno = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.1 = [46 x i8] c"could not set SO_REUSEADDR socket option (%s)\00"
@SO_BROADCAST = global i32 0
@.str.2 = [46 x i8] c"could not set SO_BROADCAST socket option (%s)\00"
@IPPROTO_IP = global i32 0
@IP_MULTICAST_LOOP = global i32 0
@.str.3 = [51 x i8] c"could not set IP_MULTICAST_LOOP socket option (%s)\00"
define i32 @udpsocket_open(i32 %0, i32 %1, i16 zeroext %2) {
%4 = alloca i32
%5 = bitcast i32* %4 to i8*
%6 = load i32, i32* @PF_INET
%7 = load i32, i32* @SOCK_DGRAM
%8 = tail call i32 @socket(i32 %6, i32 %7, i32 0)
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %14
10: ; preds = %3
%11 = load i32, i32* @errno
%12 = tail call i32 @strerror(i32 %11)
%13 = tail call i32 @log_error(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i32 %12)
br label %41
14: ; preds = %3
store i32 1, i32* %4
%15 = load i32, i32* @SOL_SOCKET
%16 = load i32, i32* @SO_REUSEADDR
%17 = call i64 @setsockopt(i32 %8, i32 %15, i32 %16, i32* %4, i32 4)
%18 = icmp slt i64 %17, 0
br i1 %18, label %19, label %23
19: ; preds = %14
%20 = load i32, i32* @errno
%21 = call i32 @strerror(i32 %20)
%22 = call i32 @log_error(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.1, i64 0, i64 0), i32 %21)
br label %41
23: ; preds = %14
%24 = load i32, i32* @SOL_SOCKET
%25 = load i32, i32* @SO_BROADCAST
%26 = call i64 @setsockopt(i32 %8, i32 %24, i32 %25, i32* %4, i32 4)
%27 = icmp slt i64 %26, 0
br i1 %27, label %28, label %32
28: ; preds = %23
%29 = load i32, i32* @errno
%30 = call i32 @strerror(i32 %29)
%31 = call i32 @log_error(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.2, i64 0, i64 0), i32 %30)
br label %41
32: ; preds = %23
%33 = load i32, i32* @IPPROTO_IP
%34 = load i32, i32* @IP_MULTICAST_LOOP
%35 = call i64 @setsockopt(i32 %8, i32 %33, i32 %34, i32* %4, i32 4)
%36 = icmp slt i64 %35, 0
br i1 %36, label %37, label %41
37: ; preds = %32
%38 = load i32, i32* @errno
%39 = call i32 @strerror(i32 %38)
%40 = call i32 @log_error(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.3, i64 0, i64 0), i32 %39)
br label %41
41: ; preds = %32, %37, %28, %19, %10
%42 = phi i32 [ -1, %10 ], [ -1, %19 ], [ -1, %28 ], [ -1, %37 ], [ %8, %32 ]
ret i32 %42
}