#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 */

/* Variables and functions */
int /*<<< orphan*/ PF_PACKET;
int SOCK_NONBLOCK;
int SOCK_RAW;
int TEMP_FAILURE_RETRY(int /*<<< orphan*/);
int /*<<< orphan*/ exit_on_perror(char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ socket(int /*<<< orphan*/, int, int /*<<< orphan*/);
int get_raw_socket(int protocol) {
  int sd;
  if ((sd = TEMP_FAILURE_RETRY(
           socket(PF_PACKET, SOCK_RAW | SOCK_NONBLOCK, htons(protocol)))) < 0)
    exit_on_perror("RAW socket() failed", NULL);
  return (sd);
}


x86 assembly (clang, -O0):
<get_raw_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
mov    0x2e9e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    0x2e82(%rip),%rax
mov    (%rax),%eax
mov    0x2e69(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0xc(%rbp)
mov    -0x4(%rbp),%edi
call   1040 <htons@plt>
mov    -0x10(%rbp),%edi
mov    -0xc(%rbp),%esi
mov    %eax,%edx
call   1060 <socket@plt>
mov    %eax,%edi
call   1050 <TEMP_FAILURE_RETRY@plt>
mov    %eax,-0x8(%rbp)
cmp    $0x0,%eax
jge    11a4 <get_raw_socket+0x64>
lea    0xe65(%rip),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1030 <exit_on_perror@plt>
mov    -0x8(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_raw_socket>:
push   %rbp
push   %rbx
push   %rax
mov    0x2ea6(%rip),%rax
mov    (%rax),%ebx
mov    0x2e8d(%rip),%rax
mov    0x2e76(%rip),%rcx
mov    (%rcx),%ebp
or     (%rax),%ebp
call   1040 <htons@plt>
mov    %ebx,%edi
mov    %ebp,%esi
mov    %eax,%edx
call   1060 <socket@plt>
mov    %eax,%edi
call   1050 <TEMP_FAILURE_RETRY@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    1189 <get_raw_socket+0x49>
lea    0xe7e(%rip),%rdi
xor    %esi,%esi
call   1030 <exit_on_perror@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@PF_PACKET = global i32 0
@SOCK_RAW = global i32 0
@SOCK_NONBLOCK = global i32 0
@.str = [20 x i8] c"RAW socket() failed\00"
define i32 @get_raw_socket(i32 %0) {
%2 = alloca i32
%3 = alloca i32
store i32 %0, i32* %2
%4 = load i32, i32* @PF_PACKET
%5 = load i32, i32* @SOCK_RAW
%6 = load i32, i32* @SOCK_NONBLOCK
%7 = or i32 %5, %6
%8 = load i32, i32* %2
%9 = call i32 @htons(i32 %8)
%10 = call i32 @socket(i32 %4, i32 %7, i32 %9)
%11 = call i32 @TEMP_FAILURE_RETRY(i32 %10)
store i32 %11, i32* %3
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %1
%14 = call i32 @exit_on_perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i32* null)
br label %15
15: ; preds = %13, %1
%16 = load i32, i32* %3
ret i32 %16
}

LLVM IR (-O2):
@PF_PACKET = global i32 0
@SOCK_RAW = global i32 0
@SOCK_NONBLOCK = global i32 0
@.str = [20 x i8] c"RAW socket() failed\00"
define i32 @get_raw_socket(i32 %0) {
%2 = load i32, i32* @PF_PACKET
%3 = load i32, i32* @SOCK_RAW
%4 = load i32, i32* @SOCK_NONBLOCK
%5 = or i32 %4, %3
%6 = tail call i32 @htons(i32 %0)
%7 = tail call i32 @socket(i32 %2, i32 %5, i32 %6)
%8 = tail call i32 @TEMP_FAILURE_RETRY(i32 %7)
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %1
%11 = tail call i32 @exit_on_perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i32* null)
br label %12
12: ; preds = %10, %1
ret i32 %8
}