#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 uint8_t;
typedef int uint32_t;

/* Variables and functions */
int /*<<< orphan*/ parse_icmp(int *, int);
int /*<<< orphan*/ parse_tcp(int *, int);
int /*<<< orphan*/ parse_udp(int *, int);
int /*<<< orphan*/ printf(char *, ...);
void parse_ipv4(uint8_t *packet, int len) {
  int i;
  uint8_t proto = packet[9];
  uint32_t hdrlen = (packet[0] & 0x0F) << 2;
  printf("ipv4 header len %u bytes, ttl %u, proto %u, ", hdrlen, packet[8],
         proto);
  printf("%d.%d.%d.%d -> %d.%d.%d.%d\n", packet[12], packet[13], packet[14],
         packet[15], packet[16], packet[17], packet[18], packet[19]);
  packet += hdrlen;
  len -= hdrlen;
  if (len < 0) {
    printf("ip packet too small\n");
    return;
  }
  switch (proto) {
  case 1:
    parse_icmp(packet, len);
    break;
  case 6:
    parse_tcp(packet, len);
    break;
  case 17:
    parse_udp(packet, len);
    break;
  default:
    printf("unknown IP protocol: %d, data:\n", proto);
    for (i = 0; i < len; i++)
      printf("%02X ", packet[i]);
    printf("\n");
  }
}


x86 assembly (clang, -O0):
<parse_ipv4>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%rax
mov    0x24(%rax),%eax
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%eax
and    $0xf,%eax
shl    $0x2,%eax
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%esi
mov    -0x8(%rbp),%rax
mov    0x20(%rax),%edx
mov    -0x14(%rbp),%ecx
lea    0xe84(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x8(%rbp),%rax
mov    0x30(%rax),%esi
mov    -0x8(%rbp),%rax
mov    0x34(%rax),%edx
mov    -0x8(%rbp),%rax
mov    0x38(%rax),%ecx
mov    -0x8(%rbp),%rax
mov    0x3c(%rax),%r8d
mov    -0x8(%rbp),%rax
mov    0x40(%rax),%r9d
mov    -0x8(%rbp),%rax
mov    0x44(%rax),%r11d
mov    -0x8(%rbp),%rax
mov    0x48(%rax),%r10d
mov    -0x8(%rbp),%rax
mov    0x4c(%rax),%eax
lea    0xe67(%rip),%rdi
mov    %r11d,(%rsp)
mov    %r10d,0x8(%rsp)
mov    %eax,0x10(%rsp)
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x18(%rbp),%ecx
mov    -0x8(%rbp),%rax
movslq %ecx,%rcx
shl    $0x2,%rcx
add    %rcx,%rax
mov    %rax,-0x8(%rbp)
mov    -0x18(%rbp),%ecx
mov    -0xc(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    1217 <parse_ipv4+0xd7>
lea    0xe3e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    12d9 <parse_ipv4+0x199>
mov    -0x14(%rbp),%eax
mov    %eax,-0x1c(%rbp)
sub    $0x1,%eax
je     124d <parse_ipv4+0x10d>
jmp    122b <parse_ipv4+0xeb>
mov    -0x1c(%rbp),%eax
sub    $0x6,%eax
je     125e <parse_ipv4+0x11e>
jmp    123c <parse_ipv4+0xfc>
mov    -0x1c(%rbp),%eax
sub    $0x11,%eax
je     126f <parse_ipv4+0x12f>
jmp    1280 <parse_ipv4+0x140>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
call   1060 <parse_icmp@plt>
jmp    12d9 <parse_ipv4+0x199>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
call   1030 <parse_tcp@plt>
jmp    12d9 <parse_ipv4+0x199>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
call   1050 <parse_udp@plt>
jmp    12d9 <parse_ipv4+0x199>
mov    -0x14(%rbp),%esi
lea    0xdd4(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x10(%rbp)
mov    -0x10(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    12cb <parse_ipv4+0x18b>
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
mov    (%rax,%rcx,4),%esi
lea    0xdc8(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    1298 <parse_ipv4+0x158>
lea    0xdaa(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<parse_ipv4>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %esi,%r14d
mov    %rdi,%rbx
mov    0x24(%rdi),%r15d
mov    (%rdi),%ebp
mov    0x20(%rdi),%edx
shl    $0x2,%ebp
and    $0x3c,%ebp
lea    0xe7c(%rip),%rdi
mov    %ebp,%esi
mov    %r15d,%ecx
xor    %eax,%eax
call   1060 <printf@plt>
mov    0x30(%rbx),%esi
mov    0x34(%rbx),%edx
mov    0x38(%rbx),%ecx
mov    0x3c(%rbx),%r8d
mov    0x40(%rbx),%r9d
mov    0x44(%rbx),%r10d
mov    0x48(%rbx),%r11d
mov    0x4c(%rbx),%r12d
sub    $0x8,%rsp
lea    0xe75(%rip),%rdi
xor    %eax,%eax
push   %r12
push   %r11
push   %r10
call   1060 <printf@plt>
add    $0x20,%rsp
sub    %ebp,%r14d
js     11f7 <parse_ipv4+0x97>
lea    (%rbx,%rbp,4),%r12
cmp    $0x11,%r15d
je     120b <parse_ipv4+0xab>
cmp    $0x6,%r15d
je     121e <parse_ipv4+0xbe>
cmp    $0x1,%r15d
jne    1231 <parse_ipv4+0xd1>
mov    %r12,%rdi
mov    %r14d,%esi
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1080 <parse_icmp@plt>
lea    0xe71(%rip),%rdi
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <puts@plt>
mov    %r12,%rdi
mov    %r14d,%esi
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1070 <parse_udp@plt>
mov    %r12,%rdi
mov    %r14d,%esi
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1050 <parse_tcp@plt>
lea    0xe11(%rip),%rdi
mov    %r15d,%esi
xor    %eax,%eax
call   1060 <printf@plt>
test   %r14d,%r14d
je     1277 <parse_ipv4+0x117>
mov    %r14d,%ebp
lea    0xe18(%rip),%r14
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    (%r12,%rbx,4),%esi
mov    %r14,%rdi
xor    %eax,%eax
call   1060 <printf@plt>
add    $0x1,%rbx
cmp    %rbx,%rbp
jne    1260 <parse_ipv4+0x100>
mov    $0xa,%edi
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <putchar@plt>

LLVM IR (-O0):
@.str = [45 x i8] c"ipv4 header len %u bytes, ttl %u, proto %u, \00"
@.str.1 = [28 x i8] c"%d.%d.%d.%d -> %d.%d.%d.%d\0A\00"
@.str.2 = [21 x i8] c"ip packet too small\0A\00"
@.str.3 = [32 x i8] c"unknown IP protocol: %d, data:\0A\00"
@.str.4 = [6 x i8] c"%02X \00"
@.str.5 = [2 x i8] c"\0A\00"
define void @parse_ipv4(i32* %0, i32 %1) {
%3 = alloca i32*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i32* %0, i32** %3
store i32 %1, i32* %4
%8 = load i32*, i32** %3
%9 = getelementptr inbounds i32, i32* %8, i64 9
%10 = load i32, i32* %9
store i32 %10, i32* %6
%11 = load i32*, i32** %3
%12 = getelementptr inbounds i32, i32* %11, i64 0
%13 = load i32, i32* %12
%14 = and i32 %13, 15
%15 = shl i32 %14, 2
store i32 %15, i32* %7
%16 = load i32, i32* %7
%17 = load i32*, i32** %3
%18 = getelementptr inbounds i32, i32* %17, i64 8
%19 = load i32, i32* %18
%20 = load i32, i32* %6
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str, i64 0, i64 0), i32 %16, i32 %19, i32 %20)
%22 = load i32*, i32** %3
%23 = getelementptr inbounds i32, i32* %22, i64 12
%24 = load i32, i32* %23
%25 = load i32*, i32** %3
%26 = getelementptr inbounds i32, i32* %25, i64 13
%27 = load i32, i32* %26
%28 = load i32*, i32** %3
%29 = getelementptr inbounds i32, i32* %28, i64 14
%30 = load i32, i32* %29
%31 = load i32*, i32** %3
%32 = getelementptr inbounds i32, i32* %31, i64 15
%33 = load i32, i32* %32
%34 = load i32*, i32** %3
%35 = getelementptr inbounds i32, i32* %34, i64 16
%36 = load i32, i32* %35
%37 = load i32*, i32** %3
%38 = getelementptr inbounds i32, i32* %37, i64 17
%39 = load i32, i32* %38
%40 = load i32*, i32** %3
%41 = getelementptr inbounds i32, i32* %40, i64 18
%42 = load i32, i32* %41
%43 = load i32*, i32** %3
%44 = getelementptr inbounds i32, i32* %43, i64 19
%45 = load i32, i32* %44
%46 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i32 %24, i32 %27, i32 %30, i32 %33, i32 %36, i32 %39, i32 %42, i32 %45)
%47 = load i32, i32* %7
%48 = load i32*, i32** %3
%49 = sext i32 %47 to i64
%50 = getelementptr inbounds i32, i32* %48, i64 %49
store i32* %50, i32** %3
%51 = load i32, i32* %7
%52 = load i32, i32* %4
%53 = sub nsw i32 %52, %51
store i32 %53, i32* %4
%54 = load i32, i32* %4
%55 = icmp slt i32 %54, 0
br i1 %55, label %56, label %58
56: ; preds = %2
%57 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
br label %91
58: ; preds = %2
%59 = load i32, i32* %6
switch i32 %59, label %72 [
i32 1, label %60
i32 6, label %64
i32 17, label %68
]
60: ; preds = %58
%61 = load i32*, i32** %3
%62 = load i32, i32* %4
%63 = call i32 @parse_icmp(i32* %61, i32 %62)
br label %91
64: ; preds = %58
%65 = load i32*, i32** %3
%66 = load i32, i32* %4
%67 = call i32 @parse_tcp(i32* %65, i32 %66)
br label %91
68: ; preds = %58
%69 = load i32*, i32** %3
%70 = load i32, i32* %4
%71 = call i32 @parse_udp(i32* %69, i32 %70)
br label %91
72: ; preds = %58
%73 = load i32, i32* %6
%74 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0), i32 %73)
store i32 0, i32* %5
br label %75
75: ; preds = %86, %72
%76 = load i32, i32* %5
%77 = load i32, i32* %4
%78 = icmp slt i32 %76, %77
br i1 %78, label %79, label %89
79: ; preds = %75
%80 = load i32*, i32** %3
%81 = load i32, i32* %5
%82 = sext i32 %81 to i64
%83 = getelementptr inbounds i32, i32* %80, i64 %82
%84 = load i32, i32* %83
%85 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), i32 %84)
br label %86
86: ; preds = %79
%87 = load i32, i32* %5
%88 = add nsw i32 %87, 1
store i32 %88, i32* %5
br label %75
89: ; preds = %75
%90 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
br label %91
91: ; preds = %56, %89, %68, %64, %60
ret void
}

LLVM IR (-O2):
@.str = [45 x i8] c"ipv4 header len %u bytes, ttl %u, proto %u, \00"
@.str.1 = [28 x i8] c"%d.%d.%d.%d -> %d.%d.%d.%d\0A\00"
@.str.3 = [32 x i8] c"unknown IP protocol: %d, data:\0A\00"
@.str.4 = [6 x i8] c"%02X \00"
@str = [20 x i8] c"ip packet too small\00"
define void @parse_ipv4(i32* %0, i32 %1) {
%3 = getelementptr inbounds i32, i32* %0, i64 9
%4 = load i32, i32* %3
%5 = load i32, i32* %0
%6 = shl i32 %5, 2
%7 = and i32 %6, 60
%8 = getelementptr inbounds i32, i32* %0, i64 8
%9 = load i32, i32* %8
%10 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([45 x i8], [45 x i8]* @.str, i64 0, i64 0), i32 %7, i32 %9, i32 %4)
%11 = getelementptr inbounds i32, i32* %0, i64 12
%12 = load i32, i32* %11
%13 = getelementptr inbounds i32, i32* %0, i64 13
%14 = load i32, i32* %13
%15 = getelementptr inbounds i32, i32* %0, i64 14
%16 = load i32, i32* %15
%17 = getelementptr inbounds i32, i32* %0, i64 15
%18 = load i32, i32* %17
%19 = getelementptr inbounds i32, i32* %0, i64 16
%20 = load i32, i32* %19
%21 = getelementptr inbounds i32, i32* %0, i64 17
%22 = load i32, i32* %21
%23 = getelementptr inbounds i32, i32* %0, i64 18
%24 = load i32, i32* %23
%25 = getelementptr inbounds i32, i32* %0, i64 19
%26 = load i32, i32* %25
%27 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i32 %12, i32 %14, i32 %16, i32 %18, i32 %20, i32 %22, i32 %24, i32 %26)
%28 = zext i32 %7 to i64
%29 = getelementptr inbounds i32, i32* %0, i64 %28
%30 = sub i32 %1, %7
%31 = icmp slt i32 %30, 0
br i1 %31, label %32, label %34
32: ; preds = %2
%33 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str, i64 0, i64 0))
br label %55
34: ; preds = %2
switch i32 %4, label %41 [
i32 1, label %35
i32 6, label %37
i32 17, label %39
]
35: ; preds = %34
%36 = tail call i32 @parse_icmp(i32* %29, i32 %30)
br label %55
37: ; preds = %34
%38 = tail call i32 @parse_tcp(i32* %29, i32 %30)
br label %55
39: ; preds = %34
%40 = tail call i32 @parse_udp(i32* %29, i32 %30)
br label %55
41: ; preds = %34
%42 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0), i32 %4)
%43 = icmp eq i32 %30, 0
br i1 %43, label %53, label %44
44: ; preds = %41
%45 = zext i32 %30 to i64
br label %46
46: ; preds = %44, %46
%47 = phi i64 [ 0, %44 ], [ %51, %46 ]
%48 = getelementptr inbounds i32, i32* %29, i64 %47
%49 = load i32, i32* %48
%50 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), i32 %49)
%51 = add nuw nsw i64 %47, 1
%52 = icmp eq i64 %51, %45
br i1 %52, label %53, label %46
53: ; preds = %46, %41
%54 = tail call i32 @putchar(i32 10)
br label %55
55: ; preds = %35, %37, %39, %53, %32
ret void
}