#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 */
typedef struct TYPE_2__ TYPE_1__;

/* Type definitions */
typedef int /*<<< orphan*/ uint8_t;
typedef int /*<<< orphan*/ turn_on;
struct TYPE_2__ {
  int /*<<< orphan*/ s_addr;
};
struct sockaddr_in {
  TYPE_1__ sin_addr;
  scalar_t__ sin_port;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
struct iphdr {
  int /*<<< orphan*/ tot_len;
  int /*<<< orphan*/ daddr;
  int /*<<< orphan*/ protocol;
};
typedef int /*<<< orphan*/ din;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ IPPROTO_IP;
int /*<<< orphan*/ IP_HDRINCL;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SOCK_RAW;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ htons(int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ sendto(int, int /*<<< orphan*/ *, int /*<<< orphan*/,
                  int /*<<< orphan*/, struct sockaddr *, int);
scalar_t__ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, int *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
char *strerror(int /*<<< orphan*/);
void sendPacket(uint8_t *packet) {
  struct iphdr *iphdr = (struct iphdr *)packet;
  int sockfd = socket(PF_INET, SOCK_RAW, iphdr->protocol);
  int turn_on = 1;
  if (setsockopt(sockfd, IPPROTO_IP, IP_HDRINCL, &turn_on, sizeof(turn_on)) <
      0) {
    printf("setsockopt() error\n");
    return;
  }
  struct sockaddr_in din;
  din.sin_family = AF_INET;
  din.sin_port = 0;
  din.sin_addr.s_addr = iphdr->daddr;
  if (sendto(sockfd, packet, htons(iphdr->tot_len), 0, (struct sockaddr *)&din,
             sizeof(din)) < 0) {
    printf("sendto() error: %s\n", strerror(errno));
    return;
  }
}


x86 assembly (clang, -O0):
<sendPacket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x10(%rbp)
mov    0x2e3d(%rip),%rax
mov    (%rax),%edi
mov    0x2e54(%rip),%rax
mov    (%rax),%esi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%edx
call   1080 <socket@plt>
mov    %eax,-0x14(%rbp)
movl   $0x1,-0x18(%rbp)
mov    -0x14(%rbp),%edi
mov    0x2e2a(%rip),%rax
mov    (%rax),%esi
mov    0x2e41(%rip),%rax
mov    (%rax),%edx
lea    -0x18(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0x0,%rax
jge    11dd <sendPacket+0x7d>
lea    0xe2f(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    1257 <sendPacket+0xf7>
mov    0x2dfc(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x10(%rbp),%rax
mov    0x4(%rax),%eax
mov    %eax,-0x30(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
call   1040 <htons@plt>
mov    -0x3c(%rbp),%edi
mov    -0x38(%rbp),%rsi
mov    %eax,%edx
lea    -0x30(%rbp),%r8
xor    %ecx,%ecx
mov    $0x18,%r9d
call   1060 <sendto@plt>
cmp    $0x0,%rax
jge    1257 <sendPacket+0xf7>
mov    0x2d81(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    %rax,%rsi
lea    0xdc4(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sendPacket>:
push   %rbp
push   %rbx
sub    $0x28,%rsp
mov    %rdi,%rbx
mov    0x2e38(%rip),%rax
mov    (%rax),%edi
mov    0x2e4f(%rip),%rax
mov    (%rax),%esi
mov    0x8(%rbx),%edx
call   1090 <socket@plt>
mov    %eax,%ebp
movl   $0x1,0xc(%rsp)
mov    0x2e2c(%rip),%rax
mov    (%rax),%esi
mov    0x2e43(%rip),%rax
mov    (%rax),%edx
lea    0xc(%rsp),%rcx
mov    %ebp,%edi
mov    $0x4,%r8d
call   1040 <setsockopt@plt>
test   %rax,%rax
js     1229 <sendPacket+0xb9>
mov    0x2e13(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x20(%rsp)
movq   $0x0,0x18(%rsp)
mov    0x4(%rbx),%eax
mov    %eax,0x10(%rsp)
mov    (%rbx),%edi
call   1050 <htons@plt>
lea    0x10(%rsp),%r8
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
xor    %ecx,%ecx
mov    $0x18,%r9d
call   1070 <sendto@plt>
test   %rax,%rax
jns    1235 <sendPacket+0xc5>
mov    0x2db1(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xde3(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
jmp    1235 <sendPacket+0xc5>
lea    0xde4(%rip),%rdi
call   1030 <puts@plt>
add    $0x28,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.iphdr = type { i32, i32, i32 }
%struct.sockaddr_in = type { %struct.TYPE_2__, i64, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@PF_INET = global i32 0
@SOCK_RAW = global i32 0
@IPPROTO_IP = global i32 0
@IP_HDRINCL = global i32 0
@.str = [20 x i8] c"setsockopt() error\0A\00"
@AF_INET = global i32 0
@.str.1 = [20 x i8] c"sendto() error: %s\0A\00"
@errno = global i32 0
define void @sendPacket(i32* %0) {
%2 = alloca i32*
%3 = alloca %struct.iphdr*
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.sockaddr_in
store i32* %0, i32** %2
%7 = load i32*, i32** %2
%8 = bitcast i32* %7 to %struct.iphdr*
store %struct.iphdr* %8, %struct.iphdr** %3
%9 = load i32, i32* @PF_INET
%10 = load i32, i32* @SOCK_RAW
%11 = load %struct.iphdr*, %struct.iphdr** %3
%12 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %11, i32 0, i32 2
%13 = load i32, i32* %12
%14 = call i32 @socket(i32 %9, i32 %10, i32 %13)
store i32 %14, i32* %4
store i32 1, i32* %5
%15 = load i32, i32* %4
%16 = load i32, i32* @IPPROTO_IP
%17 = load i32, i32* @IP_HDRINCL
%18 = call i64 @setsockopt(i32 %15, i32 %16, i32 %17, i32* %5, i32 4)
%19 = icmp slt i64 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %1
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
br label %44
22: ; preds = %1
%23 = load i32, i32* @AF_INET
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 2
store i32 %23, i32* %24
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 1
store i64 0, i64* %25
%26 = load %struct.iphdr*, %struct.iphdr** %3
%27 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %26, i32 0, i32 1
%28 = load i32, i32* %27
%29 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 0
%30 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %29, i32 0, i32 0
store i32 %28, i32* %30
%31 = load i32, i32* %4
%32 = load i32*, i32** %2
%33 = load %struct.iphdr*, %struct.iphdr** %3
%34 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %33, i32 0, i32 0
%35 = load i32, i32* %34
%36 = call i32 @htons(i32 %35)
%37 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%38 = call i64 @sendto(i32 %31, i32* %32, i32 %36, i32 0, %struct.sockaddr* %37, i32 24)
%39 = icmp slt i64 %38, 0
br i1 %39, label %40, label %44
40: ; preds = %22
%41 = load i32, i32* @errno
%42 = call i8* @strerror(i32 %41)
%43 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i8* %42)
br label %44
44: ; preds = %20, %40, %22
ret void
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_2__, i64, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@PF_INET = global i32 0
@SOCK_RAW = global i32 0
@IPPROTO_IP = global i32 0
@IP_HDRINCL = global i32 0
@AF_INET = global i32 0
@.str.1 = [20 x i8] c"sendto() error: %s\0A\00"
@errno = global i32 0
@str = [19 x i8] c"setsockopt() error\00"
define void @sendPacket(i32* %0) {
%2 = alloca i32
%3 = alloca %struct.sockaddr_in
%4 = load i32, i32* @PF_INET
%5 = load i32, i32* @SOCK_RAW
%6 = getelementptr inbounds i32, i32* %0, i64 2
%7 = load i32, i32* %6
%8 = tail call i32 @socket(i32 %4, i32 %5, i32 %7)
%9 = bitcast i32* %2 to i8*
store i32 1, i32* %2
%10 = load i32, i32* @IPPROTO_IP
%11 = load i32, i32* @IP_HDRINCL
%12 = call i64 @setsockopt(i32 %8, i32 %10, i32 %11, i32* %2, i32 4)
%13 = icmp slt i64 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %1
%15 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str, i64 0, i64 0))
br label %34
16: ; preds = %1
%17 = bitcast %struct.sockaddr_in* %3 to i8*
%18 = load i32, i32* @AF_INET
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %18, i32* %19
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
store i64 0, i64* %20
%21 = getelementptr inbounds i32, i32* %0, i64 1
%22 = load i32, i32* %21
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0, i32 0
store i32 %22, i32* %23
%24 = load i32, i32* %0
%25 = call i32 @htons(i32 %24)
%26 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%27 = call i64 @sendto(i32 %8, i32* %0, i32 %25, i32 0, %struct.sockaddr* %26, i32 24)
%28 = icmp slt i64 %27, 0
br i1 %28, label %29, label %33
29: ; preds = %16
%30 = load i32, i32* @errno
%31 = call i8* @strerror(i32 %30)
%32 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i8* %31)
br label %33
33: ; preds = %16, %29
br label %34
34: ; preds = %33, %14
ret void
}