#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 iphdr {
  int version;
  int ihl;
  int tos;
  int tot_len;
  int frag_off;
  int ttl;
  int protocol;
  int check;
  int /*<<< orphan*/ daddr;
  int /*<<< orphan*/ saddr;
  int /*<<< orphan*/ id;
};

/* Variables and functions */
int ntohs(int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
void PrintIpv4packet(char *datagram, char *data) {
  struct iphdr *iphd = (struct iphdr *)datagram;
  printf("\n\n---------- PACKET START ----------");
  printf("\nVersion: %d\n", iphd->version);
  printf("Ihl: %d\n", iphd->ihl);
  printf("tos: %d\n", iphd->tos);
  printf("tot len: %d\n", iphd->tot_len);
  printf("id: %d\n", ntohs(iphd->id));
  printf("frag off: %d\n", iphd->frag_off);
  printf("ttl: %d\n", iphd->ttl);
  unsigned char *addr = (unsigned char *)&(iphd->saddr);
  printf("saddr: %d.%d.%d.%d\n", addr[0], addr[1], addr[2], addr[3]);
  addr = (unsigned char *)&(iphd->daddr);
  printf("daddr: %d.%d.%d.%d\n", addr[0], addr[1], addr[2], addr[3]);
  printf("proto (ipv4): %d\n", iphd->protocol);
  printf("checksum: %x\n", iphd->check);
  printf("data: %s", data);
  printf("\n---------- PACKET END ----------\n");
}


x86 assembly (clang, -O0):
<PrintIpv4packet>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x18(%rbp)
lea    0xec1(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x18(%rbp),%rax
mov    (%rax),%esi
lea    0xed2(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x18(%rbp),%rax
mov    0x4(%rax),%esi
lea    0xecb(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x18(%rbp),%rax
mov    0x8(%rax),%esi
lea    0xebf(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x18(%rbp),%rax
mov    0xc(%rax),%esi
lea    0xeb3(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x18(%rbp),%rax
mov    0x28(%rax),%edi
call   1040 <ntohs@plt>
mov    %eax,%esi
lea    0xea4(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x18(%rbp),%rax
mov    0x10(%rax),%esi
lea    0xe97(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x18(%rbp),%rax
mov    0x14(%rax),%esi
lea    0xe90(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x18(%rbp),%rax
add    $0x24,%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
movzbl (%rax),%esi
mov    -0x20(%rbp),%rax
movzbl 0x1(%rax),%edx
mov    -0x20(%rbp),%rax
movzbl 0x2(%rax),%ecx
mov    -0x20(%rbp),%rax
movzbl 0x3(%rax),%r8d
lea    0xe5f(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x18(%rbp),%rax
add    $0x20,%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
movzbl (%rax),%esi
mov    -0x20(%rbp),%rax
movzbl 0x1(%rax),%edx
mov    -0x20(%rbp),%rax
movzbl 0x2(%rax),%ecx
mov    -0x20(%rbp),%rax
movzbl 0x3(%rax),%r8d
lea    0xe39(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x18(%rbp),%rax
mov    0x18(%rax),%esi
lea    0xe38(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x18(%rbp),%rax
mov    0x1c(%rax),%esi
lea    0xe35(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x10(%rbp),%rsi
lea    0xe31(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xe2c(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<PrintIpv4packet>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbx
lea    0xebf(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    (%rbx),%esi
lea    0xed4(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x4(%rbx),%esi
lea    0xed1(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x8(%rbx),%esi
lea    0xec9(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0xc(%rbx),%esi
lea    0xec1(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x28(%rbx),%edi
call   1050 <ntohs@plt>
lea    0xeb8(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x10(%rbx),%esi
lea    0xead(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x14(%rbx),%esi
lea    0xeaa(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
movzbl 0x24(%rbx),%esi
movzbl 0x25(%rbx),%edx
movzbl 0x26(%rbx),%ecx
movzbl 0x27(%rbx),%r8d
lea    0xe94(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
movzbl 0x20(%rbx),%esi
movzbl 0x21(%rbx),%edx
movzbl 0x22(%rbx),%ecx
movzbl 0x23(%rbx),%r8d
lea    0xe89(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x18(%rbx),%esi
lea    0xe8c(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x1c(%rbx),%esi
lea    0xe8d(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xe8d(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xe85(%rip),%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1030 <puts@plt>

LLVM IR (-O0):
%struct.iphdr = type { i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32 }
@.str = [37 x i8] c"\0A\0A---------- PACKET START ----------\00"
@.str.1 = [14 x i8] c"\0AVersion: %d\0A\00"
@.str.2 = [9 x i8] c"Ihl: %d\0A\00"
@.str.3 = [9 x i8] c"tos: %d\0A\00"
@.str.4 = [13 x i8] c"tot len: %d\0A\00"
@.str.5 = [8 x i8] c"id: %d\0A\00"
@.str.6 = [14 x i8] c"frag off: %d\0A\00"
@.str.7 = [9 x i8] c"ttl: %d\0A\00"
@.str.8 = [20 x i8] c"saddr: %d.%d.%d.%d\0A\00"
@.str.9 = [20 x i8] c"daddr: %d.%d.%d.%d\0A\00"
@.str.10 = [18 x i8] c"proto (ipv4): %d\0A\00"
@.str.11 = [14 x i8] c"checksum: %x\0A\00"
@.str.12 = [9 x i8] c"data: %s\00"
@.str.13 = [35 x i8] c"\0A---------- PACKET END ----------\0A\00"
define void @PrintIpv4packet(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca %struct.iphdr*
%6 = alloca i8*
store i8* %0, i8** %3
store i8* %1, i8** %4
%7 = load i8*, i8** %3
%8 = bitcast i8* %7 to %struct.iphdr*
store %struct.iphdr* %8, %struct.iphdr** %5
%9 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0))
%10 = load %struct.iphdr*, %struct.iphdr** %5
%11 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %10, i32 0, i32 0
%12 = load i32, i32* %11
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i32 %12)
%14 = load %struct.iphdr*, %struct.iphdr** %5
%15 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %14, i32 0, i32 1
%16 = load i32, i32* %15
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i32 %16)
%18 = load %struct.iphdr*, %struct.iphdr** %5
%19 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %18, i32 0, i32 2
%20 = load i32, i32* %19
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i32 %20)
%22 = load %struct.iphdr*, %struct.iphdr** %5
%23 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %22, i32 0, i32 3
%24 = load i32, i32* %23
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0), i32 %24)
%26 = load %struct.iphdr*, %struct.iphdr** %5
%27 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %26, i32 0, i32 10
%28 = load i32, i32* %27
%29 = call i32 @ntohs(i32 %28)
%30 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.5, i64 0, i64 0), i32 %29)
%31 = load %struct.iphdr*, %struct.iphdr** %5
%32 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %31, i32 0, i32 4
%33 = load i32, i32* %32
%34 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.6, i64 0, i64 0), i32 %33)
%35 = load %struct.iphdr*, %struct.iphdr** %5
%36 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %35, i32 0, i32 5
%37 = load i32, i32* %36
%38 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.7, i64 0, i64 0), i32 %37)
%39 = load %struct.iphdr*, %struct.iphdr** %5
%40 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %39, i32 0, i32 9
%41 = bitcast i32* %40 to i8*
store i8* %41, i8** %6
%42 = load i8*, i8** %6
%43 = getelementptr inbounds i8, i8* %42, i64 0
%44 = load i8, i8* %43
%45 = zext i8 %44 to i32
%46 = load i8*, i8** %6
%47 = getelementptr inbounds i8, i8* %46, i64 1
%48 = load i8, i8* %47
%49 = zext i8 %48 to i32
%50 = load i8*, i8** %6
%51 = getelementptr inbounds i8, i8* %50, i64 2
%52 = load i8, i8* %51
%53 = zext i8 %52 to i32
%54 = load i8*, i8** %6
%55 = getelementptr inbounds i8, i8* %54, i64 3
%56 = load i8, i8* %55
%57 = zext i8 %56 to i32
%58 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.8, i64 0, i64 0), i32 %45, i32 %49, i32 %53, i32 %57)
%59 = load %struct.iphdr*, %struct.iphdr** %5
%60 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %59, i32 0, i32 8
%61 = bitcast i32* %60 to i8*
store i8* %61, i8** %6
%62 = load i8*, i8** %6
%63 = getelementptr inbounds i8, i8* %62, i64 0
%64 = load i8, i8* %63
%65 = zext i8 %64 to i32
%66 = load i8*, i8** %6
%67 = getelementptr inbounds i8, i8* %66, i64 1
%68 = load i8, i8* %67
%69 = zext i8 %68 to i32
%70 = load i8*, i8** %6
%71 = getelementptr inbounds i8, i8* %70, i64 2
%72 = load i8, i8* %71
%73 = zext i8 %72 to i32
%74 = load i8*, i8** %6
%75 = getelementptr inbounds i8, i8* %74, i64 3
%76 = load i8, i8* %75
%77 = zext i8 %76 to i32
%78 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.9, i64 0, i64 0), i32 %65, i32 %69, i32 %73, i32 %77)
%79 = load %struct.iphdr*, %struct.iphdr** %5
%80 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %79, i32 0, i32 6
%81 = load i32, i32* %80
%82 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.10, i64 0, i64 0), i32 %81)
%83 = load %struct.iphdr*, %struct.iphdr** %5
%84 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %83, i32 0, i32 7
%85 = load i32, i32* %84
%86 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.11, i64 0, i64 0), i32 %85)
%87 = load i8*, i8** %4
%88 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.12, i64 0, i64 0), i8* %87)
%89 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.13, i64 0, i64 0))
ret void
}

LLVM IR (-O2):
@.str = [37 x i8] c"\0A\0A---------- PACKET START ----------\00"
@.str.1 = [14 x i8] c"\0AVersion: %d\0A\00"
@.str.2 = [9 x i8] c"Ihl: %d\0A\00"
@.str.3 = [9 x i8] c"tos: %d\0A\00"
@.str.4 = [13 x i8] c"tot len: %d\0A\00"
@.str.5 = [8 x i8] c"id: %d\0A\00"
@.str.6 = [14 x i8] c"frag off: %d\0A\00"
@.str.7 = [9 x i8] c"ttl: %d\0A\00"
@.str.8 = [20 x i8] c"saddr: %d.%d.%d.%d\0A\00"
@.str.9 = [20 x i8] c"daddr: %d.%d.%d.%d\0A\00"
@.str.10 = [18 x i8] c"proto (ipv4): %d\0A\00"
@.str.11 = [14 x i8] c"checksum: %x\0A\00"
@.str.12 = [9 x i8] c"data: %s\00"
@str = [34 x i8] c"\0A---------- PACKET END ----------\00"
define void @PrintIpv4packet(i8* nocapture %0, i8* %1) {
%3 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0))
%4 = bitcast i8* %0 to i32*
%5 = load i32, i32* %4
%6 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i32 %5)
%7 = getelementptr inbounds i8, i8* %0, i64 4
%8 = bitcast i8* %7 to i32*
%9 = load i32, i32* %8
%10 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i32 %9)
%11 = getelementptr inbounds i8, i8* %0, i64 8
%12 = bitcast i8* %11 to i32*
%13 = load i32, i32* %12
%14 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i32 %13)
%15 = getelementptr inbounds i8, i8* %0, i64 12
%16 = bitcast i8* %15 to i32*
%17 = load i32, i32* %16
%18 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0), i32 %17)
%19 = getelementptr inbounds i8, i8* %0, i64 40
%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 ([8 x i8], [8 x i8]* @.str.5, i64 0, i64 0), i32 %22)
%24 = getelementptr inbounds i8, i8* %0, i64 16
%25 = bitcast i8* %24 to i32*
%26 = load i32, i32* %25
%27 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.6, i64 0, i64 0), i32 %26)
%28 = getelementptr inbounds i8, i8* %0, i64 20
%29 = bitcast i8* %28 to i32*
%30 = load i32, i32* %29
%31 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.7, i64 0, i64 0), i32 %30)
%32 = getelementptr inbounds i8, i8* %0, i64 36
%33 = load i8, i8* %32
%34 = zext i8 %33 to i32
%35 = getelementptr inbounds i8, i8* %0, i64 37
%36 = load i8, i8* %35
%37 = zext i8 %36 to i32
%38 = getelementptr inbounds i8, i8* %0, i64 38
%39 = load i8, i8* %38
%40 = zext i8 %39 to i32
%41 = getelementptr inbounds i8, i8* %0, i64 39
%42 = load i8, i8* %41
%43 = zext i8 %42 to i32
%44 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.8, i64 0, i64 0), i32 %34, i32 %37, i32 %40, i32 %43)
%45 = getelementptr inbounds i8, i8* %0, i64 32
%46 = load i8, i8* %45
%47 = zext i8 %46 to i32
%48 = getelementptr inbounds i8, i8* %0, i64 33
%49 = load i8, i8* %48
%50 = zext i8 %49 to i32
%51 = getelementptr inbounds i8, i8* %0, i64 34
%52 = load i8, i8* %51
%53 = zext i8 %52 to i32
%54 = getelementptr inbounds i8, i8* %0, i64 35
%55 = load i8, i8* %54
%56 = zext i8 %55 to i32
%57 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.9, i64 0, i64 0), i32 %47, i32 %50, i32 %53, i32 %56)
%58 = getelementptr inbounds i8, i8* %0, i64 24
%59 = bitcast i8* %58 to i32*
%60 = load i32, i32* %59
%61 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.10, i64 0, i64 0), i32 %60)
%62 = getelementptr inbounds i8, i8* %0, i64 28
%63 = bitcast i8* %62 to i32*
%64 = load i32, i32* %63
%65 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.11, i64 0, i64 0), i32 %64)
%66 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.12, i64 0, i64 0), i8* %1)
%67 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @str, i64 0, i64 0))
ret void
}