#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 sockaddr_in {
  int /*<<< orphan*/ sin_port;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ raw_address;
typedef int /*<<< orphan*/ on;

/* Variables and functions */
int /*<<< orphan*/ IPPROTO_IP;
int /*<<< orphan*/ IP_HDRINCL;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SOCK_RAW;
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ htons(unsigned short);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
scalar_t__ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, int *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int);
int socketRaw(int type, unsigned short port) {
  int sockfd = socket(PF_INET, SOCK_RAW, type);
  if (sockfd == -1) {
    perror("socket");
    exit(1);
  }
  struct sockaddr_in raw_address;
  memset(&raw_address, 0, sizeof(raw_address));
  raw_address.sin_port = htons(port);
  if (bind(sockfd, (struct sockaddr *)&raw_address, sizeof(raw_address)) < 0) {
    perror("bind");
    exit(1);
  }
  int on = 1;
  if (setsockopt(sockfd, IPPROTO_IP, IP_HDRINCL, &on, sizeof(on)) < 0) {
    perror("setsockopt");
    exit(1);
  }
  return sockfd;
}


x86 assembly (clang, -O0):
<socketRaw>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %si,%ax
mov    %edi,-0x4(%rbp)
mov    %ax,-0x6(%rbp)
mov    0x2e3f(%rip),%rax
mov    (%rax),%edi
mov    0x2e4e(%rip),%rax
mov    (%rax),%esi
mov    -0x4(%rbp),%edx
call   1090 <socket@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    11bf <socketRaw+0x4f>
lea    0xe50(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
lea    -0x10(%rbp),%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1050 <memset@plt>
movzwl -0x6(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,-0x10(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x10(%rbp),%rsi
mov    $0x4,%edx
call   1060 <bind@plt>
cmp    $0x0,%rax
jge    120c <socketRaw+0x9c>
lea    0xe0a(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
movl   $0x1,-0x14(%rbp)
mov    -0xc(%rbp),%edi
mov    0x2dbb(%rip),%rax
mov    (%rax),%esi
mov    0x2dca(%rip),%rax
mov    (%rax),%edx
lea    -0x14(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0x0,%rax
jge    1257 <socketRaw+0xe7>
lea    0xdc4(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0xc(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<socketRaw>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %esi,%ebp
mov    %edi,%edx
mov    0x2e45(%rip),%rax
mov    (%rax),%edi
mov    0x2e54(%rip),%rax
mov    (%rax),%esi
call   1090 <socket@plt>
cmp    $0xffffffff,%eax
je     120a <socketRaw+0x9a>
mov    %eax,%ebx
lea    0x8(%rsp),%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1050 <memset@plt>
movzwl %bp,%edi
call   1040 <htons@plt>
mov    %eax,0x8(%rsp)
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x4,%edx
call   1060 <bind@plt>
test   %rax,%rax
js     1213 <socketRaw+0xa3>
movl   $0x1,0xc(%rsp)
mov    0x2dfb(%rip),%rax
mov    (%rax),%esi
mov    0x2e0a(%rip),%rax
mov    (%rax),%edx
lea    0xc(%rsp),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
test   %rax,%rax
js     121c <socketRaw+0xac>
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xdef(%rip),%rdi
jmp    1223 <socketRaw+0xb3>
lea    0xded(%rip),%rdi
jmp    1223 <socketRaw+0xb3>
lea    0xde9(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32 }
%struct.sockaddr = type { i32 }
@PF_INET = global i32 0
@SOCK_RAW = global i32 0
@.str = [7 x i8] c"socket\00"
@.str.1 = [5 x i8] c"bind\00"
@IPPROTO_IP = global i32 0
@IP_HDRINCL = global i32 0
@.str.2 = [11 x i8] c"setsockopt\00"
define i32 @socketRaw(i32 %0, i16 zeroext %1) {
%3 = alloca i32
%4 = alloca i16
%5 = alloca i32
%6 = alloca %struct.sockaddr_in
%7 = alloca i32
store i32 %0, i32* %3
store i16 %1, i16* %4
%8 = load i32, i32* @PF_INET
%9 = load i32, i32* @SOCK_RAW
%10 = load i32, i32* %3
%11 = call i32 @socket(i32 %8, i32 %9, i32 %10)
store i32 %11, i32* %5
%12 = load i32, i32* %5
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %17
14: ; preds = %2
%15 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%16 = call i32 @exit(i32 1)
unreachable
17: ; preds = %2
%18 = call i32 @memset(%struct.sockaddr_in* %6, i32 0, i32 4)
%19 = load i16, i16* %4
%20 = call i32 @htons(i16 zeroext %19)
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 0
store i32 %20, i32* %21
%22 = load i32, i32* %5
%23 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%24 = call i64 @bind(i32 %22, %struct.sockaddr* %23, i32 4)
%25 = icmp slt i64 %24, 0
br i1 %25, label %26, label %29
26: ; preds = %17
%27 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%28 = call i32 @exit(i32 1)
unreachable
29: ; preds = %17
store i32 1, i32* %7
%30 = load i32, i32* %5
%31 = load i32, i32* @IPPROTO_IP
%32 = load i32, i32* @IP_HDRINCL
%33 = call i64 @setsockopt(i32 %30, i32 %31, i32 %32, i32* %7, i32 4)
%34 = icmp slt i64 %33, 0
br i1 %34, label %35, label %38
35: ; preds = %29
%36 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
%37 = call i32 @exit(i32 1)
unreachable
38: ; preds = %29
%39 = load i32, i32* %5
ret i32 %39
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32 }
%struct.sockaddr = type { i32 }
@PF_INET = global i32 0
@SOCK_RAW = global i32 0
@.str = [7 x i8] c"socket\00"
@.str.1 = [5 x i8] c"bind\00"
@IPPROTO_IP = global i32 0
@IP_HDRINCL = global i32 0
@.str.2 = [11 x i8] c"setsockopt\00"
define i32 @socketRaw(i32 %0, i16 zeroext %1) {
%3 = alloca %struct.sockaddr_in
%4 = alloca i32
%5 = load i32, i32* @PF_INET
%6 = load i32, i32* @SOCK_RAW
%7 = tail call i32 @socket(i32 %5, i32 %6, i32 %0)
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %12
9: ; preds = %2
%10 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%11 = tail call i32 @exit(i32 1)
unreachable
12: ; preds = %2
%13 = bitcast %struct.sockaddr_in* %3 to i8*
%14 = call i32 @memset(%struct.sockaddr_in* %3, i32 0, i32 4)
%15 = call i32 @htons(i16 zeroext %1)
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
store i32 %15, i32* %16
%17 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%18 = call i64 @bind(i32 %7, %struct.sockaddr* %17, i32 4)
%19 = icmp slt i64 %18, 0
br i1 %19, label %20, label %23
20: ; preds = %12
%21 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%22 = call i32 @exit(i32 1)
unreachable
23: ; preds = %12
%24 = bitcast i32* %4 to i8*
store i32 1, i32* %4
%25 = load i32, i32* @IPPROTO_IP
%26 = load i32, i32* @IP_HDRINCL
%27 = call i64 @setsockopt(i32 %7, i32 %25, i32 %26, i32* %4, i32 4)
%28 = icmp slt i64 %27, 0
br i1 %28, label %29, label %32
29: ; preds = %23
%30 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
%31 = call i32 @exit(i32 1)
unreachable
32: ; preds = %23
ret i32 %7
}