#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 ethhdr {
  scalar_t__ h_proto;
};

/* Variables and functions */
int /*<<< orphan*/ ETH_P_IP;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int);
scalar_t__ htons(int /*<<< orphan*/);
int ntohs(scalar_t__);
int /*<<< orphan*/ stderr;
void test_payload(void *pay, size_t len) {
  struct ethhdr *eth = pay;
  if (len < sizeof(struct ethhdr)) {
    fprintf(stderr,
            "test_payload: packet too "
            "small: %zu bytes!\n",
            len);
    exit(1);
  }
  if (eth->h_proto != htons(ETH_P_IP)) {
    fprintf(stderr,
            "test_payload: wrong ethernet "
            "type: 0x%x!\n",
            ntohs(eth->h_proto));
    exit(1);
  }
}


x86 assembly (clang, -O0):
<test_payload>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x18(%rbp)
cmpq   $0x8,-0x10(%rbp)
jae    1188 <test_payload+0x48>
mov    0x2e76(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    %eax,%edx
lea    0xe87(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x20(%rbp)
mov    0x2e3e(%rip),%rax
mov    (%rax),%edi
call   1030 <htons@plt>
mov    %rax,%rcx
mov    -0x20(%rbp),%rax
cmp    %rcx,%rax
je     11e4 <test_payload+0xa4>
mov    0x2e28(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
call   1050 <ntohs@plt>
mov    -0x24(%rbp),%edi
mov    %eax,%edx
lea    0xe57(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_payload>:
push   %rbp
push   %rbx
push   %rax
cmp    $0x7,%rsi
jbe    1169 <test_payload+0x29>
mov    %rdi,%rbx
mov    (%rdi),%rbp
mov    0x2e82(%rip),%rax
mov    (%rax),%edi
call   1030 <htons@plt>
cmp    %rax,%rbp
jne    117e <test_payload+0x3e>
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
mov    %rsi,%rdx
mov    0x2e6d(%rip),%rax
mov    (%rax),%edi
lea    0xe84(%rip),%rsi
jmp    119a <test_payload+0x5a>
mov    0x2e5b(%rip),%rax
mov    (%rax),%ebp
mov    (%rbx),%rdi
call   1050 <ntohs@plt>
lea    0xe96(%rip),%rsi
mov    %ebp,%edi
mov    %eax,%edx
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
%struct.ethhdr = type { i64 }
@stderr = global i32 0
@.str = [44 x i8] c"test_payload: packet too small: %zu bytes!\0A\00"
@ETH_P_IP = global i32 0
@.str.1 = [42 x i8] c"test_payload: wrong ethernet type: 0x%x!\0A\00"
define void @test_payload(i8* %0, i64 %1) {
%3 = alloca i8*
%4 = alloca i64
%5 = alloca %struct.ethhdr*
store i8* %0, i8** %3
store i64 %1, i64* %4
%6 = load i8*, i8** %3
%7 = bitcast i8* %6 to %struct.ethhdr*
store %struct.ethhdr* %7, %struct.ethhdr** %5
%8 = load i64, i64* %4
%9 = icmp ult i64 %8, 8
br i1 %9, label %10, label %16
10: ; preds = %2
%11 = load i32, i32* @stderr
%12 = load i64, i64* %4
%13 = trunc i64 %12 to i32
%14 = call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str, i64 0, i64 0), i32 %13)
%15 = call i32 @exit(i32 1)
unreachable
16: ; preds = %2
%17 = load %struct.ethhdr*, %struct.ethhdr** %5
%18 = getelementptr inbounds %struct.ethhdr, %struct.ethhdr* %17, i32 0, i32 0
%19 = load i64, i64* %18
%20 = load i32, i32* @ETH_P_IP
%21 = call i64 @htons(i32 %20)
%22 = icmp ne i64 %19, %21
br i1 %22, label %23, label %31
23: ; preds = %16
%24 = load i32, i32* @stderr
%25 = load %struct.ethhdr*, %struct.ethhdr** %5
%26 = getelementptr inbounds %struct.ethhdr, %struct.ethhdr* %25, i32 0, i32 0
%27 = load i64, i64* %26
%28 = call i32 @ntohs(i64 %27)
%29 = call i32 @fprintf(i32 %24, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.1, i64 0, i64 0), i32 %28)
%30 = call i32 @exit(i32 1)
unreachable
31: ; preds = %16
ret void
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [44 x i8] c"test_payload: packet too small: %zu bytes!\0A\00"
@ETH_P_IP = global i32 0
@.str.1 = [42 x i8] c"test_payload: wrong ethernet type: 0x%x!\0A\00"
define void @test_payload(i8* nocapture %0, i64 %1) {
%3 = icmp ult i64 %1, 8
br i1 %3, label %4, label %9
4: ; preds = %2
%5 = load i32, i32* @stderr
%6 = trunc i64 %1 to i32
%7 = tail call i32 @fprintf(i32 %5, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str, i64 0, i64 0), i32 %6)
%8 = tail call i32 @exit(i32 1)
unreachable
9: ; preds = %2
%10 = bitcast i8* %0 to i64*
%11 = load i64, i64* %10
%12 = load i32, i32* @ETH_P_IP
%13 = tail call i64 @htons(i32 %12)
%14 = icmp eq i64 %11, %13
br i1 %14, label %21, label %15
15: ; preds = %9
%16 = load i32, i32* @stderr
%17 = load i64, i64* %10
%18 = tail call i32 @ntohs(i64 %17)
%19 = tail call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.1, i64 0, i64 0), i32 %18)
%20 = tail call i32 @exit(i32 1)
unreachable
21: ; preds = %9
ret void
}