#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 tos;
  int frag_off;
  int ttl;
  int protocol;
  int /*<<< orphan*/ check;
  int /*<<< orphan*/ id;
  int /*<<< orphan*/ tot_len;
};

/* Variables and functions */
int ntohs(int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int PrintIP(struct iphdr *ip) {
  printf("[IP]");
  printf("TOS %1x ", (ip->tos));
  printf("LEN %02x ", ntohs(ip->tot_len));
  printf("ID %02x ", ntohs(ip->id));
  printf("FRAG %02x ", (ip->frag_off));
  printf("TTL %d ", (ip->ttl));
  printf("PROTOCOL %d ", (ip->protocol));
  printf("CHECKSUM %x \n", ntohs(ip->check));
  switch (ip->protocol) {
  case 6:
    printf("TCP ");
    break;
  case 17:
    printf("UDP ");
    break;
  }
}


x86 assembly (clang, -O0):
<PrintIP>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
lea    0xecd(%rip),%rdi
xor    %eax,%eax
mov    %al,-0x15(%rbp)
call   1030 <printf@plt>
mov    -0x15(%rbp),%al
mov    -0x10(%rbp),%rcx
mov    (%rcx),%esi
lea    0xeb8(%rip),%rdi
call   1030 <printf@plt>
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%edi
call   1040 <ntohs@plt>
mov    %eax,%esi
mov    -0x15(%rbp),%al
lea    0xea4(%rip),%rdi
call   1030 <printf@plt>
mov    -0x10(%rbp),%rax
mov    0x14(%rax),%edi
call   1040 <ntohs@plt>
mov    %eax,%esi
mov    -0x15(%rbp),%al
lea    0xe91(%rip),%rdi
call   1030 <printf@plt>
mov    -0x15(%rbp),%al
mov    -0x10(%rbp),%rcx
mov    0x4(%rcx),%esi
lea    0xe84(%rip),%rdi
call   1030 <printf@plt>
mov    -0x15(%rbp),%al
mov    -0x10(%rbp),%rcx
mov    0x8(%rcx),%esi
lea    0xe79(%rip),%rdi
call   1030 <printf@plt>
mov    -0x15(%rbp),%al
mov    -0x10(%rbp),%rcx
mov    0xc(%rcx),%esi
lea    0xe6b(%rip),%rdi
call   1030 <printf@plt>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%edi
call   1040 <ntohs@plt>
mov    %eax,%esi
mov    -0x15(%rbp),%al
lea    0xe5b(%rip),%rdi
call   1030 <printf@plt>
mov    -0x10(%rbp),%rax
mov    0xc(%rax),%eax
mov    %eax,-0x14(%rbp)
sub    $0x6,%eax
je     1214 <PrintIP+0xf4>
jmp    1203 <PrintIP+0xe3>
mov    -0x14(%rbp),%eax
sub    $0x11,%eax
je     1227 <PrintIP+0x107>
jmp    1235 <PrintIP+0x115>
lea    0xe34(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    1235 <PrintIP+0x115>
lea    0xe26(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<PrintIP>:
push   %rbx
mov    %rdi,%rbx
lea    0xed5(%rip),%rdi
xor    %eax,%eax
call   1030 <printf@plt>
mov    (%rbx),%esi
lea    0xeca(%rip),%rdi
xor    %eax,%eax
call   1030 <printf@plt>
mov    0x18(%rbx),%edi
call   1040 <ntohs@plt>
lea    0xebd(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1030 <printf@plt>
mov    0x14(%rbx),%edi
call   1040 <ntohs@plt>
lea    0xeaf(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1030 <printf@plt>
mov    0x4(%rbx),%esi
lea    0xea5(%rip),%rdi
xor    %eax,%eax
call   1030 <printf@plt>
mov    0x8(%rbx),%esi
lea    0xe9f(%rip),%rdi
xor    %eax,%eax
call   1030 <printf@plt>
mov    0xc(%rbx),%esi
lea    0xe96(%rip),%rdi
xor    %eax,%eax
call   1030 <printf@plt>
mov    0x10(%rbx),%edi
call   1040 <ntohs@plt>
lea    0xe8d(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1030 <printf@plt>
mov    0xc(%rbx),%eax
cmp    $0x6,%eax
je     11d3 <PrintIP+0xb3>
cmp    $0x11,%eax
jne    11e1 <PrintIP+0xc1>
lea    0xe83(%rip),%rdi
jmp    11da <PrintIP+0xba>
lea    0xe75(%rip),%rdi
xor    %eax,%eax
call   1030 <printf@plt>
pop    %rbx
ret

LLVM IR (-O0):
%struct.iphdr = type { i32, i32, i32, i32, i32, i32, i32 }
@.str = [5 x i8] c"[IP]\00"
@.str.1 = [9 x i8] c"TOS %1x \00"
@.str.2 = [10 x i8] c"LEN %02x \00"
@.str.3 = [9 x i8] c"ID %02x \00"
@.str.4 = [11 x i8] c"FRAG %02x \00"
@.str.5 = [8 x i8] c"TTL %d \00"
@.str.6 = [13 x i8] c"PROTOCOL %d \00"
@.str.7 = [14 x i8] c"CHECKSUM %x \0A\00"
@.str.8 = [5 x i8] c"TCP \00"
@.str.9 = [5 x i8] c"UDP \00"
define i32 @PrintIP(%struct.iphdr* %0) {
%2 = alloca i32
%3 = alloca %struct.iphdr*
store %struct.iphdr* %0, %struct.iphdr** %3
%4 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%5 = load %struct.iphdr*, %struct.iphdr** %3
%6 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %5, i32 0, i32 0
%7 = load i32, i32* %6
%8 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i32 %7)
%9 = load %struct.iphdr*, %struct.iphdr** %3
%10 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %9, i32 0, i32 6
%11 = load i32, i32* %10
%12 = call i32 @ntohs(i32 %11)
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i32 %12)
%14 = load %struct.iphdr*, %struct.iphdr** %3
%15 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %14, i32 0, i32 5
%16 = load i32, i32* %15
%17 = call i32 @ntohs(i32 %16)
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i32 %17)
%19 = load %struct.iphdr*, %struct.iphdr** %3
%20 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %19, i32 0, i32 1
%21 = load i32, i32* %20
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0), i32 %21)
%23 = load %struct.iphdr*, %struct.iphdr** %3
%24 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %23, i32 0, i32 2
%25 = load i32, i32* %24
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.5, i64 0, i64 0), i32 %25)
%27 = load %struct.iphdr*, %struct.iphdr** %3
%28 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %27, i32 0, i32 3
%29 = load i32, i32* %28
%30 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.6, i64 0, i64 0), i32 %29)
%31 = load %struct.iphdr*, %struct.iphdr** %3
%32 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %31, i32 0, i32 4
%33 = load i32, i32* %32
%34 = call i32 @ntohs(i32 %33)
%35 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.7, i64 0, i64 0), i32 %34)
%36 = load %struct.iphdr*, %struct.iphdr** %3
%37 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %36, i32 0, i32 3
%38 = load i32, i32* %37
switch i32 %38, label %43 [
i32 6, label %39
i32 17, label %41
]
39: ; preds = %1
%40 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.8, i64 0, i64 0))
br label %43
41: ; preds = %1
%42 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.9, i64 0, i64 0))
br label %43
43: ; preds = %1, %41, %39
%44 = load i32, i32* %2
ret i32 %44
}

LLVM IR (-O2):
%struct.iphdr = type { i32, i32, i32, i32, i32, i32, i32 }
@.str = [5 x i8] c"[IP]\00"
@.str.1 = [9 x i8] c"TOS %1x \00"
@.str.2 = [10 x i8] c"LEN %02x \00"
@.str.3 = [9 x i8] c"ID %02x \00"
@.str.4 = [11 x i8] c"FRAG %02x \00"
@.str.5 = [8 x i8] c"TTL %d \00"
@.str.6 = [13 x i8] c"PROTOCOL %d \00"
@.str.7 = [14 x i8] c"CHECKSUM %x \0A\00"
@.str.8 = [5 x i8] c"TCP \00"
@.str.9 = [5 x i8] c"UDP \00"
define i32 @PrintIP(%struct.iphdr* nocapture %0) {
%2 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%3 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %0, i64 0, i32 0
%4 = load i32, i32* %3
%5 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i32 %4)
%6 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %0, i64 0, i32 6
%7 = load i32, i32* %6
%8 = tail call i32 @ntohs(i32 %7)
%9 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i32 %8)
%10 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %0, i64 0, i32 5
%11 = load i32, i32* %10
%12 = tail call i32 @ntohs(i32 %11)
%13 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i32 %12)
%14 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %0, i64 0, i32 1
%15 = load i32, i32* %14
%16 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0), i32 %15)
%17 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %0, i64 0, i32 2
%18 = load i32, i32* %17
%19 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str.5, i64 0, i64 0), i32 %18)
%20 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %0, i64 0, i32 3
%21 = load i32, i32* %20
%22 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.6, i64 0, i64 0), i32 %21)
%23 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %0, i64 0, i32 4
%24 = load i32, i32* %23
%25 = tail call i32 @ntohs(i32 %24)
%26 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.7, i64 0, i64 0), i32 %25)
%27 = load i32, i32* %20
switch i32 %27, label %32 [
i32 6, label %29
i32 17, label %28
]
28: ; preds = %1
br label %29
29: ; preds = %1, %28
%30 = phi i8* [ getelementptr inbounds ([5 x i8], [5 x i8]* @.str.9, i64 0, i64 0), %28 ], [ getelementptr inbounds ([5 x i8], [5 x i8]* @.str.8, i64 0, i64 0), %1 ]
%31 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) %30)
br label %32
32: ; preds = %29, %1
ret i32 undef
}