#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 ars_packet {
  int dummy;
};
typedef int /*<<< orphan*/ one;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ IPPROTO_IP;
int /*<<< orphan*/ IPPROTO_RAW;
int /*<<< orphan*/ IP_HDRINCL;
int /*<<< orphan*/ SOCK_RAW;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_BROADCAST;
int /*<<< orphan*/ ars_set_error(struct ars_packet *, char *);
int /*<<< orphan*/ close(int);
int setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, char *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int ars_open_rawsocket(struct ars_packet *pkt) {
  int s;
  const int one = 1;
  if ((s = socket(AF_INET, SOCK_RAW, IPPROTO_RAW)) == -1) {
    ars_set_error(pkt, "Can't open the raw socket");
    return -1;
  }
  if (setsockopt(s, SOL_SOCKET, SO_BROADCAST, (char *)&one, sizeof(one)) ==
          -1 ||
      setsockopt(s, IPPROTO_IP, IP_HDRINCL, (char *)&one, sizeof(one)) == -1) {
    close(s);
    ars_set_error(pkt, "Can't set socket options");
    return -1;
  }
  return s;
}


x86 assembly (clang, -O0):
<ars_open_rawsocket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x1,-0x18(%rbp)
mov    0x2e7e(%rip),%rax
mov    (%rax),%edi
mov    0x2e6d(%rip),%rax
mov    (%rax),%esi
mov    0x2e44(%rip),%rax
mov    (%rax),%edx
call   1060 <socket@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    119b <ars_open_rawsocket+0x5b>
mov    -0x10(%rbp),%rdi
lea    0xe76(%rip),%rsi
call   1030 <ars_set_error@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    121f <ars_open_rawsocket+0xdf>
mov    -0x14(%rbp),%edi
mov    0x2e3b(%rip),%rax
mov    (%rax),%esi
mov    0x2e12(%rip),%rax
mov    (%rax),%edx
lea    -0x18(%rbp),%rcx
mov    $0x4,%r8d
call   1040 <setsockopt@plt>
cmp    $0xffffffff,%eax
je     11f5 <ars_open_rawsocket+0xb5>
mov    -0x14(%rbp),%edi
mov    0x2df6(%rip),%rax
mov    (%rax),%esi
mov    0x2e15(%rip),%rax
mov    (%rax),%edx
lea    -0x18(%rbp),%rcx
mov    $0x4,%r8d
call   1040 <setsockopt@plt>
cmp    $0xffffffff,%eax
jne    1219 <ars_open_rawsocket+0xd9>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
mov    -0x10(%rbp),%rdi
lea    0xe12(%rip),%rsi
call   1030 <ars_set_error@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    121f <ars_open_rawsocket+0xdf>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ars_open_rawsocket>:
push   %rbp
push   %rbx
push   %rax
mov    %rdi,%rbx
movl   $0x1,0x4(%rsp)
mov    0x2e83(%rip),%rax
mov    (%rax),%edi
mov    0x2e72(%rip),%rax
mov    (%rax),%esi
mov    0x2e49(%rip),%rax
mov    (%rax),%edx
call   1060 <socket@plt>
cmp    $0xffffffff,%eax
je     11d7 <ars_open_rawsocket+0x97>
mov    %eax,%ebp
mov    0x2e64(%rip),%rax
mov    (%rax),%esi
mov    0x2e3b(%rip),%rax
mov    (%rax),%edx
lea    0x4(%rsp),%rcx
mov    %ebp,%edi
mov    $0x4,%r8d
call   1040 <setsockopt@plt>
cmp    $0xffffffff,%eax
je     11c7 <ars_open_rawsocket+0x87>
mov    0x2e23(%rip),%rax
mov    (%rax),%esi
mov    0x2e42(%rip),%rax
mov    (%rax),%edx
lea    0x4(%rsp),%rcx
mov    %ebp,%edi
mov    $0x4,%r8d
call   1040 <setsockopt@plt>
cmp    $0xffffffff,%eax
jne    11eb <ars_open_rawsocket+0xab>
mov    %ebp,%edi
call   1050 <close@plt>
lea    0xe45(%rip),%rsi
jmp    11de <ars_open_rawsocket+0x9e>
lea    0xe22(%rip),%rsi
mov    %rbx,%rdi
call   1030 <ars_set_error@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.ars_packet = type { i32 }
@AF_INET = global i32 0
@SOCK_RAW = global i32 0
@IPPROTO_RAW = global i32 0
@.str = [26 x i8] c"Can't open the raw socket\00"
@SOL_SOCKET = global i32 0
@SO_BROADCAST = global i32 0
@IPPROTO_IP = global i32 0
@IP_HDRINCL = global i32 0
@.str.1 = [25 x i8] c"Can't set socket options\00"
define i32 @ars_open_rawsocket(%struct.ars_packet* %0) {
%2 = alloca i32
%3 = alloca %struct.ars_packet*
%4 = alloca i32
%5 = alloca i32
store %struct.ars_packet* %0, %struct.ars_packet** %3
store i32 1, i32* %5
%6 = load i32, i32* @AF_INET
%7 = load i32, i32* @SOCK_RAW
%8 = load i32, i32* @IPPROTO_RAW
%9 = call i32 @socket(i32 %6, i32 %7, i32 %8)
store i32 %9, i32* %4
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %14
11: ; preds = %1
%12 = load %struct.ars_packet*, %struct.ars_packet** %3
%13 = call i32 @ars_set_error(%struct.ars_packet* %12, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %2
br label %35
14: ; preds = %1
%15 = load i32, i32* %4
%16 = load i32, i32* @SOL_SOCKET
%17 = load i32, i32* @SO_BROADCAST
%18 = bitcast i32* %5 to i8*
%19 = call i32 @setsockopt(i32 %15, i32 %16, i32 %17, i8* %18, i32 4)
%20 = icmp eq i32 %19, -1
br i1 %20, label %28, label %21
21: ; preds = %14
%22 = load i32, i32* %4
%23 = load i32, i32* @IPPROTO_IP
%24 = load i32, i32* @IP_HDRINCL
%25 = bitcast i32* %5 to i8*
%26 = call i32 @setsockopt(i32 %22, i32 %23, i32 %24, i8* %25, i32 4)
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %33
28: ; preds = %21, %14
%29 = load i32, i32* %4
%30 = call i32 @close(i32 %29)
%31 = load %struct.ars_packet*, %struct.ars_packet** %3
%32 = call i32 @ars_set_error(%struct.ars_packet* %31, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %2
br label %35
33: ; preds = %21
%34 = load i32, i32* %4
store i32 %34, i32* %2
br label %35
35: ; preds = %33, %28, %11
%36 = load i32, i32* %2
ret i32 %36
}

LLVM IR (-O2):
%struct.ars_packet = type { i32 }
@AF_INET = global i32 0
@SOCK_RAW = global i32 0
@IPPROTO_RAW = global i32 0
@.str = [26 x i8] c"Can't open the raw socket\00"
@SOL_SOCKET = global i32 0
@SO_BROADCAST = global i32 0
@IPPROTO_IP = global i32 0
@IP_HDRINCL = global i32 0
@.str.1 = [25 x i8] c"Can't set socket options\00"
define i32 @ars_open_rawsocket(%struct.ars_packet* %0) {
%2 = alloca i32
%3 = bitcast i32* %2 to i8*
store i32 1, i32* %2
%4 = load i32, i32* @AF_INET
%5 = load i32, i32* @SOCK_RAW
%6 = load i32, i32* @IPPROTO_RAW
%7 = tail call i32 @socket(i32 %4, i32 %5, i32 %6)
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %11
9: ; preds = %1
%10 = tail call i32 @ars_set_error(%struct.ars_packet* %0, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
br label %24
11: ; preds = %1
%12 = load i32, i32* @SOL_SOCKET
%13 = load i32, i32* @SO_BROADCAST
%14 = call i32 @setsockopt(i32 %7, i32 %12, i32 %13, i8* %3, i32 4)
%15 = icmp eq i32 %14, -1
br i1 %15, label %21, label %16
16: ; preds = %11
%17 = load i32, i32* @IPPROTO_IP
%18 = load i32, i32* @IP_HDRINCL
%19 = call i32 @setsockopt(i32 %7, i32 %17, i32 %18, i8* %3, i32 4)
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %24
21: ; preds = %16, %11
%22 = call i32 @close(i32 %7)
%23 = call i32 @ars_set_error(%struct.ars_packet* %0, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
br label %24
24: ; preds = %16, %21, %9
%25 = phi i32 [ -1, %9 ], [ -1, %21 ], [ %7, %16 ]
ret i32 %25
}