#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*/ u_short;
typedef int /*<<< orphan*/ optval;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_BROADCAST;
int /*<<< orphan*/ htons(int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *);
int setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, int *, int);
int socket(int /*<<< orphan*/, int, int /*<<< orphan*/);
int openRawSocket(int *s, u_short type) {
  int optval = 1;
  if ((*s = socket(AF_INET, 10, htons(type))) == -1) {
    do {
      printf("Could not open raw socket");
      printf("\n");
    } while (0);
    return -1;
  }
  if (setsockopt(*s, SOL_SOCKET, SO_BROADCAST, &optval, sizeof(optval)) == -1) {
    do {
      printf("Could not setsocketopt on raw socket");
      printf("\n");
    } while (0);
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<openRawSocket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
movl   $0x1,-0x18(%rbp)
mov    0x2e83(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x14(%rbp),%edi
call   1040 <htons@plt>
mov    -0x1c(%rbp),%edi
mov    %eax,%edx
mov    $0xa,%esi
call   1060 <socket@plt>
mov    -0x10(%rbp),%rcx
mov    %eax,(%rcx)
cmp    $0xffffffff,%eax
jne    11b1 <openRawSocket+0x71>
jmp    118d <openRawSocket+0x4d>
lea    0xe6c(%rip),%rdi
call   1050 <printf@plt>
lea    0xe7a(%rip),%rdi
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1211 <openRawSocket+0xd1>
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
mov    0x2e2a(%rip),%rax
mov    (%rax),%esi
mov    0x2e11(%rip),%rax
mov    (%rax),%edx
lea    -0x18(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0xffffffff,%eax
jne    120a <openRawSocket+0xca>
jmp    11e6 <openRawSocket+0xa6>
lea    0xe2f(%rip),%rdi
call   1050 <printf@plt>
lea    0xe21(%rip),%rdi
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1211 <openRawSocket+0xd1>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<openRawSocket>:
push   %rbp
push   %rbx
push   %rax
mov    %rdi,%rbx
movl   $0x1,0x4(%rsp)
mov    0x2e7b(%rip),%rax
mov    (%rax),%ebp
mov    %esi,%edi
call   1050 <htons@plt>
mov    %ebp,%edi
mov    $0xa,%esi
mov    %eax,%edx
call   1070 <socket@plt>
mov    %eax,(%rbx)
cmp    $0xffffffff,%eax
je     11b9 <openRawSocket+0x69>
mov    0x2e5e(%rip),%rcx
mov    (%rcx),%esi
mov    0x2e45(%rip),%rcx
mov    (%rcx),%edx
lea    0x4(%rsp),%rcx
mov    %eax,%edi
mov    $0x4,%r8d
call   1040 <setsockopt@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    $0xffffffff,%ecx
jne    11d4 <openRawSocket+0x84>
lea    0xe63(%rip),%rdi
jmp    11c0 <openRawSocket+0x70>
lea    0xe40(%rip),%rdi
call   1060 <printf@plt>
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@AF_INET = global i32 0
@.str = [26 x i8] c"Could not open raw socket\00"
@.str.1 = [2 x i8] c"\0A\00"
@SOL_SOCKET = global i32 0
@SO_BROADCAST = global i32 0
@.str.2 = [37 x i8] c"Could not setsocketopt on raw socket\00"
define i32 @openRawSocket(i32* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i32
%6 = alloca i32
store i32* %0, i32** %4
store i32 %1, i32* %5
store i32 1, i32* %6
%7 = load i32, i32* @AF_INET
%8 = load i32, i32* %5
%9 = call i32 @htons(i32 %8)
%10 = call i32 @socket(i32 %7, i32 10, i32 %9)
%11 = load i32*, i32** %4
store i32 %10, i32* %11
%12 = icmp eq i32 %10, -1
br i1 %12, label %13, label %18
13: ; preds = %2
br label %14
14: ; preds = %13
%15 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
%16 = call i32 @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
br label %17
17: ; preds = %14
store i32 -1, i32* %3
br label %31
18: ; preds = %2
%19 = load i32*, i32** %4
%20 = load i32, i32* %19
%21 = load i32, i32* @SOL_SOCKET
%22 = load i32, i32* @SO_BROADCAST
%23 = call i32 @setsockopt(i32 %20, i32 %21, i32 %22, i32* %6, i32 4)
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %30
25: ; preds = %18
br label %26
26: ; preds = %25
%27 = call i32 @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.2, i64 0, i64 0))
%28 = call i32 @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
br label %29
29: ; preds = %26
store i32 -1, i32* %3
br label %31
30: ; preds = %18
store i32 0, i32* %3
br label %31
31: ; preds = %30, %29, %17
%32 = load i32, i32* %3
ret i32 %32
}

LLVM IR (-O2):
@AF_INET = global i32 0
@.str = [26 x i8] c"Could not open raw socket\00"
@SOL_SOCKET = global i32 0
@SO_BROADCAST = global i32 0
@.str.2 = [37 x i8] c"Could not setsocketopt on raw socket\00"
define i32 @openRawSocket(i32* nocapture writeonly %0, i32 %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
store i32 1, i32* %3
%5 = load i32, i32* @AF_INET
%6 = tail call i32 @htons(i32 %1)
%7 = tail call i32 @socket(i32 %5, i32 10, i32 %6)
store i32 %7, i32* %0
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %12
9: ; preds = %2
%10 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
%11 = tail call i32 @putchar(i32 10)
br label %20
12: ; preds = %2
%13 = load i32, i32* @SOL_SOCKET
%14 = load i32, i32* @SO_BROADCAST
%15 = call i32 @setsockopt(i32 %7, i32 %13, i32 %14, i32* %3, i32 4)
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %20
17: ; preds = %12
%18 = call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @.str.2, i64 0, i64 0))
%19 = call i32 @putchar(i32 10)
br label %20
20: ; preds = %12, %17, %9
%21 = phi i32 [ -1, %9 ], [ -1, %17 ], [ 0, %12 ]
ret i32 %21
}