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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ IPPROTO_IP;
int /*<<< orphan*/ IPPROTO_UDP;
int /*<<< orphan*/ IP_HDRINCL;
int /*<<< orphan*/ SOCK_RAW;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ perror(char *);
int setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, int *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int create_raw_udp_socket(void) {
  int s = socket(AF_INET, SOCK_RAW, IPPROTO_UDP);
  int rc;
  int on = 1;
  if (s < 0) {
    perror("socket (raw)");
    return s;
  }
  rc = setsockopt(s, IPPROTO_IP, IP_HDRINCL, &on, sizeof(on));
  if (rc < 0) {
    perror("setsockopt(HDRINCL)");
    close(s);
    return -1;
  }
  return s;
}


x86 assembly (clang, -O0):
<create_raw_udp_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    0x2e91(%rip),%rax
mov    (%rax),%edi
mov    0x2e80(%rip),%rax
mov    (%rax),%esi
mov    0x2e57(%rip),%rax
mov    (%rax),%edx
call   1060 <socket@plt>
mov    %eax,-0x8(%rbp)
movl   $0x1,-0x10(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    1193 <create_raw_udp_socket+0x53>
lea    0xe7d(%rip),%rdi
call   1050 <perror@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11ea <create_raw_udp_socket+0xaa>
mov    -0x8(%rbp),%edi
mov    0x2e33(%rip),%rax
mov    (%rax),%esi
mov    0x2e4a(%rip),%rax
mov    (%rax),%edx
lea    -0x10(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    11e4 <create_raw_udp_socket+0xa4>
lea    0xe42(%rip),%rdi
call   1050 <perror@plt>
mov    -0x8(%rbp),%edi
call   1040 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ea <create_raw_udp_socket+0xaa>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<create_raw_udp_socket>:
push   %rbx
sub    $0x10,%rsp
mov    0x2e94(%rip),%rax
mov    (%rax),%edi
mov    0x2e83(%rip),%rax
mov    (%rax),%esi
mov    0x2e5a(%rip),%rax
mov    (%rax),%edx
call   1060 <socket@plt>
mov    %eax,%ebx
movl   $0x1,0xc(%rsp)
test   %eax,%eax
js     11a3 <create_raw_udp_socket+0x63>
mov    0x2e56(%rip),%rax
mov    (%rax),%esi
mov    0x2e6d(%rip),%rax
mov    (%rax),%edx
lea    0xc(%rsp),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
test   %eax,%eax
js     11b1 <create_raw_udp_socket+0x71>
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
ret
lea    0xe56(%rip),%rdi
call   1050 <perror@plt>
jmp    119b <create_raw_udp_socket+0x5b>
lea    0xe55(%rip),%rdi
call   1050 <perror@plt>
mov    %ebx,%edi
call   1040 <close@plt>
mov    $0xffffffff,%ebx
jmp    119b <create_raw_udp_socket+0x5b>

LLVM IR (-O0):
@AF_INET = global i32 0
@SOCK_RAW = global i32 0
@IPPROTO_UDP = global i32 0
@.str = [13 x i8] c"socket (raw)\00"
@IPPROTO_IP = global i32 0
@IP_HDRINCL = global i32 0
@.str.1 = [20 x i8] c"setsockopt(HDRINCL)\00"
define i32 @create_raw_udp_socket() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = load i32, i32* @AF_INET
%6 = load i32, i32* @SOCK_RAW
%7 = load i32, i32* @IPPROTO_UDP
%8 = call i32 @socket(i32 %5, i32 %6, i32 %7)
store i32 %8, i32* %2
store i32 1, i32* %4
%9 = load i32, i32* %2
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %14
11: ; preds = %0
%12 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%13 = load i32, i32* %2
store i32 %13, i32* %1
br label %27
14: ; preds = %0
%15 = load i32, i32* %2
%16 = load i32, i32* @IPPROTO_IP
%17 = load i32, i32* @IP_HDRINCL
%18 = call i32 @setsockopt(i32 %15, i32 %16, i32 %17, i32* %4, i32 4)
store i32 %18, i32* %3
%19 = load i32, i32* %3
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %25
21: ; preds = %14
%22 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
%23 = load i32, i32* %2
%24 = call i32 @close(i32 %23)
store i32 -1, i32* %1
br label %27
25: ; preds = %14
%26 = load i32, i32* %2
store i32 %26, i32* %1
br label %27
27: ; preds = %25, %21, %11
%28 = load i32, i32* %1
ret i32 %28
}

LLVM IR (-O2):
@AF_INET = global i32 0
@SOCK_RAW = global i32 0
@IPPROTO_UDP = global i32 0
@.str = [13 x i8] c"socket (raw)\00"
@IPPROTO_IP = global i32 0
@IP_HDRINCL = global i32 0
@.str.1 = [20 x i8] c"setsockopt(HDRINCL)\00"
define i32 @create_raw_udp_socket() {
%1 = alloca i32
%2 = load i32, i32* @AF_INET
%3 = load i32, i32* @SOCK_RAW
%4 = load i32, i32* @IPPROTO_UDP
%5 = tail call i32 @socket(i32 %2, i32 %3, i32 %4)
%6 = bitcast i32* %1 to i8*
store i32 1, i32* %1
%7 = icmp slt i32 %5, 0
br i1 %7, label %8, label %10
8: ; preds = %0
%9 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
br label %18
10: ; preds = %0
%11 = load i32, i32* @IPPROTO_IP
%12 = load i32, i32* @IP_HDRINCL
%13 = call i32 @setsockopt(i32 %5, i32 %11, i32 %12, i32* %1, i32 4)
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %18
15: ; preds = %10
%16 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
%17 = call i32 @close(i32 %5)
br label %18
18: ; preds = %10, %15, %8
%19 = phi i32 [ %5, %8 ], [ -1, %15 ], [ %5, %10 ]
ret i32 %19
}