#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 udphdr {
  int /*<<< orphan*/ check;
  int /*<<< orphan*/ len;
  int /*<<< orphan*/ dest;
  int /*<<< orphan*/ source;
};
struct iphdr {
  int ihl;
};
struct ethhdr {
  int dummy;
};

/* Variables and functions */
int ntohs(int /*<<< orphan*/);
int /*<<< orphan*/ print_data(char *, int);
int /*<<< orphan*/ printf(char *, ...);
void ip_udp_packet_callback(char *packet_content, int size) {
  struct udphdr *udpst;
  int header_size;
  struct iphdr *iph;
  unsigned short iphdrlen;
  iph = (struct iphdr *)(packet_content + sizeof(struct ethhdr));
  iphdrlen = iph->ihl * 4;
  udpst = (struct udphdr *)(packet_content + iphdrlen + sizeof(struct ethhdr));
  header_size = sizeof(struct ethhdr) + iphdrlen + sizeof udpst;
  printf("UDP Header\n");
  printf("   |-Source Port            :%d\n", ntohs(udpst->source));
  printf("   |-Destination Port       :%d\n", ntohs(udpst->dest));
  printf("   |-UDP Length             :%d\n", ntohs(udpst->len));
  printf("   |-UDP Check Sum          :%d\n", ntohs(udpst->check));
  printf("[Ethner Header]\n");
  print_data(packet_content, sizeof(struct ethhdr));
  printf("[IP Header]\n");
  print_data(packet_content + sizeof(struct ethhdr), sizeof(struct iphdr));
  printf("[UDP Header]\n");
  print_data(packet_content + sizeof(struct ethhdr) + sizeof(struct iphdr),
             sizeof udpst);
  printf("[Data Payload]\n");
  print_data(packet_content + header_size, size - header_size);
}


x86 assembly (clang, -O0):
<ip_udp_packet_callback>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%rax
add    $0x4,%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    (%rax),%eax
shl    $0x2,%eax
mov    %ax,-0x2a(%rbp)
mov    -0x8(%rbp),%rax
movzwl -0x2a(%rbp),%ecx
movslq %ecx,%rcx
add    %rcx,%rax
add    $0x4,%rax
mov    %rax,-0x18(%rbp)
movzwl -0x2a(%rbp),%eax
add    $0x4,%rax
add    $0x8,%rax
mov    %eax,-0x1c(%rbp)
lea    0xe7c(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x18(%rbp),%rax
mov    0xc(%rax),%edi
call   1050 <ntohs@plt>
mov    %eax,%esi
lea    0xe6c(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x18(%rbp),%rax
mov    0x8(%rax),%edi
call   1050 <ntohs@plt>
mov    %eax,%esi
lea    0xe71(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x18(%rbp),%rax
mov    0x4(%rax),%edi
call   1050 <ntohs@plt>
mov    %eax,%esi
lea    0xe76(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x18(%rbp),%rax
mov    (%rax),%edi
call   1050 <ntohs@plt>
mov    %eax,%esi
lea    0xe7c(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xe8f(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x8(%rbp),%rdi
mov    $0x4,%esi
call   1040 <print_data@plt>
lea    0xe84(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x8(%rbp),%rdi
add    $0x4,%rdi
mov    $0x4,%esi
call   1040 <print_data@plt>
lea    0xe71(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x8(%rbp),%rdi
add    $0x4,%rdi
add    $0x4,%rdi
mov    $0x8,%esi
call   1040 <print_data@plt>
lea    0xe5b(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x8(%rbp),%rdi
movslq -0x1c(%rbp),%rax
add    %rax,%rdi
mov    -0xc(%rbp),%esi
sub    -0x1c(%rbp),%esi
call   1040 <print_data@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ip_udp_packet_callback>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %esi,%r15d
mov    %rdi,%rbx
lea    0x4(%rdi),%r14
mov    0x4(%rdi),%eax
shl    $0x2,%eax
movzwl %ax,%ebp
movzwl %ax,%r12d
lea    0xf1e(%rip),%rdi
call   1030 <puts@plt>
mov    0x10(%rbx,%rbp,1),%edi
call   1060 <ntohs@plt>
lea    0xe85(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0xc(%rbx,%rbp,1),%edi
call   1060 <ntohs@plt>
lea    0xe8d(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x8(%rbx,%rbp,1),%edi
call   1060 <ntohs@plt>
lea    0xe95(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x4(%rbx,%rbp,1),%edi
call   1060 <ntohs@plt>
lea    0xe9d(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xeb9(%rip),%rdi
call   1030 <puts@plt>
mov    %rbx,%rdi
mov    $0x4,%esi
call   1050 <print_data@plt>
lea    0xeb0(%rip),%rdi
call   1030 <puts@plt>
mov    %r14,%rdi
mov    $0x4,%esi
call   1050 <print_data@plt>
lea    0xea3(%rip),%rdi
call   1030 <puts@plt>
lea    0x8(%rbx),%rdi
mov    $0x8,%esi
call   1050 <print_data@plt>
lea    0xe96(%rip),%rdi
call   1030 <puts@plt>
lea    (%rbx,%rbp,1),%rdi
add    $0xc,%rdi
sub    %r12d,%r15d
add    $0xfffffff4,%r15d
mov    %r15d,%esi
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1050 <print_data@plt>

LLVM IR (-O0):
%struct.udphdr = type { i32, i32, i32, i32 }
%struct.iphdr = type { i32 }
@.str = [12 x i8] c"UDP Header\0A\00"
@.str.1 = [33 x i8] c" |-Source Port :%d\0A\00"
@.str.2 = [33 x i8] c" |-Destination Port :%d\0A\00"
@.str.3 = [33 x i8] c" |-UDP Length :%d\0A\00"
@.str.4 = [33 x i8] c" |-UDP Check Sum :%d\0A\00"
@.str.5 = [17 x i8] c"[Ethner Header]\0A\00"
@.str.6 = [13 x i8] c"[IP Header]\0A\00"
@.str.7 = [14 x i8] c"[UDP Header]\0A\00"
@.str.8 = [16 x i8] c"[Data Payload]\0A\00"
define void @ip_udp_packet_callback(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca %struct.udphdr*
%6 = alloca i32
%7 = alloca %struct.iphdr*
%8 = alloca i16
store i8* %0, i8** %3
store i32 %1, i32* %4
%9 = load i8*, i8** %3
%10 = getelementptr inbounds i8, i8* %9, i64 4
%11 = bitcast i8* %10 to %struct.iphdr*
store %struct.iphdr* %11, %struct.iphdr** %7
%12 = load %struct.iphdr*, %struct.iphdr** %7
%13 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %12, i32 0, i32 0
%14 = load i32, i32* %13
%15 = mul nsw i32 %14, 4
%16 = trunc i32 %15 to i16
store i16 %16, i16* %8
%17 = load i8*, i8** %3
%18 = load i16, i16* %8
%19 = zext i16 %18 to i32
%20 = sext i32 %19 to i64
%21 = getelementptr inbounds i8, i8* %17, i64 %20
%22 = getelementptr inbounds i8, i8* %21, i64 4
%23 = bitcast i8* %22 to %struct.udphdr*
store %struct.udphdr* %23, %struct.udphdr** %5
%24 = load i16, i16* %8
%25 = zext i16 %24 to i64
%26 = add i64 4, %25
%27 = add i64 %26, 8
%28 = trunc i64 %27 to i32
store i32 %28, i32* %6
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%30 = load %struct.udphdr*, %struct.udphdr** %5
%31 = getelementptr inbounds %struct.udphdr, %struct.udphdr* %30, i32 0, i32 3
%32 = load i32, i32* %31
%33 = call i32 @ntohs(i32 %32)
%34 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i32 %33)
%35 = load %struct.udphdr*, %struct.udphdr** %5
%36 = getelementptr inbounds %struct.udphdr, %struct.udphdr* %35, i32 0, i32 2
%37 = load i32, i32* %36
%38 = call i32 @ntohs(i32 %37)
%39 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0), i32 %38)
%40 = load %struct.udphdr*, %struct.udphdr** %5
%41 = getelementptr inbounds %struct.udphdr, %struct.udphdr* %40, i32 0, i32 1
%42 = load i32, i32* %41
%43 = call i32 @ntohs(i32 %42)
%44 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.3, i64 0, i64 0), i32 %43)
%45 = load %struct.udphdr*, %struct.udphdr** %5
%46 = getelementptr inbounds %struct.udphdr, %struct.udphdr* %45, i32 0, i32 0
%47 = load i32, i32* %46
%48 = call i32 @ntohs(i32 %47)
%49 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.4, i64 0, i64 0), i32 %48)
%50 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.5, i64 0, i64 0))
%51 = load i8*, i8** %3
%52 = call i32 @print_data(i8* %51, i32 4)
%53 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.6, i64 0, i64 0))
%54 = load i8*, i8** %3
%55 = getelementptr inbounds i8, i8* %54, i64 4
%56 = call i32 @print_data(i8* %55, i32 4)
%57 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.7, i64 0, i64 0))
%58 = load i8*, i8** %3
%59 = getelementptr inbounds i8, i8* %58, i64 4
%60 = getelementptr inbounds i8, i8* %59, i64 4
%61 = call i32 @print_data(i8* %60, i32 8)
%62 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.8, i64 0, i64 0))
%63 = load i8*, i8** %3
%64 = load i32, i32* %6
%65 = sext i32 %64 to i64
%66 = getelementptr inbounds i8, i8* %63, i64 %65
%67 = load i32, i32* %4
%68 = load i32, i32* %6
%69 = sub nsw i32 %67, %68
%70 = call i32 @print_data(i8* %66, i32 %69)
ret void
}

LLVM IR (-O2):
@.str.1 = [33 x i8] c" |-Source Port :%d\0A\00"
@.str.2 = [33 x i8] c" |-Destination Port :%d\0A\00"
@.str.3 = [33 x i8] c" |-UDP Length :%d\0A\00"
@.str.4 = [33 x i8] c" |-UDP Check Sum :%d\0A\00"
@str = [11 x i8] c"UDP Header\00"
@str.9 = [16 x i8] c"[Ethner Header]\00"
@str.10 = [12 x i8] c"[IP Header]\00"
@str.11 = [13 x i8] c"[UDP Header]\00"
@str.12 = [15 x i8] c"[Data Payload]\00"
define void @ip_udp_packet_callback(i8* %0, i32 %1) {
%3 = getelementptr inbounds i8, i8* %0, i64 4
%4 = bitcast i8* %3 to i32*
%5 = load i32, i32* %4
%6 = trunc i32 %5 to i16
%7 = shl i16 %6, 2
%8 = zext i16 %7 to i64
%9 = getelementptr inbounds i8, i8* %0, i64 %8
%10 = getelementptr inbounds i8, i8* %9, i64 4
%11 = zext i16 %7 to i32
%12 = add nuw nsw i32 %11, 12
%13 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @str, i64 0, i64 0))
%14 = getelementptr inbounds i8, i8* %10, i64 12
%15 = bitcast i8* %14 to i32*
%16 = load i32, i32* %15
%17 = tail call i32 @ntohs(i32 %16)
%18 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i32 %17)
%19 = getelementptr inbounds i8, i8* %10, i64 8
%20 = bitcast i8* %19 to i32*
%21 = load i32, i32* %20
%22 = tail call i32 @ntohs(i32 %21)
%23 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0), i32 %22)
%24 = getelementptr inbounds i8, i8* %10, i64 4
%25 = bitcast i8* %24 to i32*
%26 = load i32, i32* %25
%27 = tail call i32 @ntohs(i32 %26)
%28 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @.str.3, i64 0, i64 0), i32 %27)
%29 = bitcast i8* %10 to i32*
%30 = load i32, i32* %29
%31 = tail call i32 @ntohs(i32 %30)
%32 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @.str.4, i64 0, i64 0), i32 %31)
%33 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str.9, i64 0, i64 0))
%34 = tail call i32 @print_data(i8* %0, i32 4)
%35 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str.10, i64 0, i64 0))
%36 = tail call i32 @print_data(i8* %3, i32 4)
%37 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str.11, i64 0, i64 0))
%38 = getelementptr inbounds i8, i8* %0, i64 8
%39 = tail call i32 @print_data(i8* %38, i32 8)
%40 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str.12, i64 0, i64 0))
%41 = zext i32 %12 to i64
%42 = getelementptr inbounds i8, i8* %0, i64 %41
%43 = sub nsw i32 %1, %12
%44 = tail call i32 @print_data(i8* %42, i32 %43)
ret void
}