#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 u_int8_t;
typedef int u_int16_t;
struct udphdr {
  int dummy;
};
struct tcphdr {
  int dummy;
};
struct ip {
  int ip_hl;
  int ip_p;
  scalar_t__ ip_tos;
  int /*<<< orphan*/ ip_off;
  int /*<<< orphan*/ ip_sum;
  scalar_t__ ip_ttl;
  int /*<<< orphan*/ ip_id;
  int /*<<< orphan*/ ip_len;
};
struct icmphdr {
  int dummy;
};
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
#define IPPROTO_ICMP 130
#define IPPROTO_TCP 129
#define IPPROTO_UDP 128
int IP_DF;
int IP_MF;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, ...);
int ntohs(int /*<<< orphan*/);
int /*<<< orphan*/ print_icmp(int /*<<< orphan*/ *, struct ip const *,
                              struct icmphdr const *);
int /*<<< orphan*/ print_tcp(int /*<<< orphan*/ *, struct ip const *,
                             struct tcphdr const *);
int /*<<< orphan*/ print_udp(int /*<<< orphan*/ *, struct ip const *,
                             struct udphdr const *);
void print_ipv4(FILE *dst, const struct ip *ip) {
  void *payload = ((char *)ip) + 4 * ip->ip_hl;
  fprintf(dst, "IPv4(HDR[%d])[", 4 * ip->ip_hl);
  fprintf(dst, "Len=%d:", (u_int16_t)ntohs(ip->ip_len));
  fprintf(dst, "ID=%d:", (u_int16_t)ntohs(ip->ip_id));
  fprintf(dst, "TTL=%d:", (u_int8_t)ip->ip_ttl);
  fprintf(dst, "Chk=%d:", (u_int16_t)ntohs(ip->ip_sum));
  if (ntohs(ip->ip_off) & IP_DF) {
    fprintf(dst, "DF");
  }
  if (ntohs(ip->ip_off) & IP_MF) {
    fprintf(dst, "MF");
  }
  fprintf(dst, " Tos:%0x]:\t", (u_int8_t)ip->ip_tos);
  switch (ip->ip_p) {
  case IPPROTO_TCP:
    print_tcp(dst, ip, (const struct tcphdr *)payload);
    break;
  case IPPROTO_UDP:
    print_udp(dst, ip, (const struct udphdr *)payload);
    break;
  case IPPROTO_ICMP:
    print_icmp(dst, ip, (const struct icmphdr *)payload);
    break;
  default:
    fprintf(dst, "Unknown transport protocol: %d \n", ip->ip_p);
    break;
  }
}


x86 assembly (clang, -O0):
<print_ipv4>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    -0x10(%rbp),%rcx
mov    (%rcx),%ecx
shl    $0x2,%ecx
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    (%rax),%edx
shl    $0x2,%edx
lea    0xe75(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    -0x8(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rax
mov    0x24(%rax),%edi
call   1070 <ntohs@plt>
mov    -0x30(%rbp),%rdi
mov    %eax,%edx
lea    0xe5c(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    -0x8(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rax
mov    0x20(%rax),%edi
call   1070 <ntohs@plt>
mov    -0x28(%rbp),%rdi
mov    %eax,%edx
lea    0xe3c(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rax
mov    %eax,%edx
lea    0xe27(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    -0x8(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    0x14(%rax),%edi
call   1070 <ntohs@plt>
mov    -0x20(%rbp),%rdi
mov    %eax,%edx
lea    0xe07(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%edi
call   1070 <ntohs@plt>
mov    0x2daf(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     1256 <print_ipv4+0x106>
mov    -0x8(%rbp),%rdi
lea    0xddf(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%edi
call   1070 <ntohs@plt>
mov    0x2d6f(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     1286 <print_ipv4+0x136>
mov    -0x8(%rbp),%rdi
lea    0xdb2(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%edx
lea    0xd9c(%rip),%rsi
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    -0x10(%rbp),%rax
mov    0x4(%rax),%eax
mov    %eax,-0x34(%rbp)
sub    $0x80,%eax
je     12f5 <print_ipv4+0x1a5>
jmp    12b9 <print_ipv4+0x169>
mov    -0x34(%rbp),%eax
sub    $0x81,%eax
je     12df <print_ipv4+0x18f>
jmp    12cc <print_ipv4+0x17c>
mov    -0x34(%rbp),%eax
sub    $0x82,%eax
je     130b <print_ipv4+0x1bb>
jmp    1321 <print_ipv4+0x1d1>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1030 <print_tcp@plt>
jmp    133a <print_ipv4+0x1ea>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1050 <print_udp@plt>
jmp    133a <print_ipv4+0x1ea>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1060 <print_icmp@plt>
jmp    133a <print_ipv4+0x1ea>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    0x4(%rax),%edx
lea    0xd0d(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<print_ipv4>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%rbx
mov    %rdi,%r14
movslq (%rsi),%r15
lea    0x0(,%r15,4),%rdx
lea    0xe83(%rip),%rsi
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    0x24(%rbx),%edi
call   1070 <ntohs@plt>
lea    0xe7c(%rip),%rsi
mov    %r14,%rdi
mov    %eax,%edx
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    0x20(%rbx),%edi
call   1070 <ntohs@plt>
lea    0xe69(%rip),%rsi
mov    %r14,%rdi
mov    %eax,%edx
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    0x18(%rbx),%edx
lea    0xe5a(%rip),%rsi
mov    %r14,%rdi
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    0x14(%rbx),%edi
call   1070 <ntohs@plt>
lea    0xe49(%rip),%rsi
mov    %r14,%rdi
mov    %eax,%edx
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    0x10(%rbx),%edi
call   1070 <ntohs@plt>
mov    0x2df0(%rip),%rcx
test   %eax,(%rcx)
je     1215 <print_ipv4+0xb5>
lea    0xe2b(%rip),%rdi
mov    $0x2,%esi
mov    $0x1,%edx
mov    %r14,%rcx
call   1080 <fwrite@plt>
mov    0x10(%rbx),%edi
call   1070 <ntohs@plt>
mov    0x2db4(%rip),%rcx
test   %eax,(%rcx)
je     1241 <print_ipv4+0xe1>
lea    0xe02(%rip),%rdi
mov    $0x2,%esi
mov    $0x1,%edx
mov    %r14,%rcx
call   1080 <fwrite@plt>
lea    (%rbx,%r15,4),%r15
mov    0x8(%rbx),%edx
lea    0xde5(%rip),%rsi
mov    %r14,%rdi
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    0x4(%rbx),%edx
cmp    $0x80,%edx
je     1287 <print_ipv4+0x127>
cmp    $0x82,%edx
je     129a <print_ipv4+0x13a>
cmp    $0x81,%edx
jne    12ad <print_ipv4+0x14d>
mov    %r14,%rdi
mov    %rbx,%rsi
mov    %r15,%rdx
pop    %rbx
pop    %r14
pop    %r15
jmp    1030 <print_tcp@plt>
mov    %r14,%rdi
mov    %rbx,%rsi
mov    %r15,%rdx
pop    %rbx
pop    %r14
pop    %r15
jmp    1050 <print_udp@plt>
mov    %r14,%rdi
mov    %rbx,%rsi
mov    %r15,%rdx
pop    %rbx
pop    %r14
pop    %r15
jmp    1060 <print_icmp@plt>
lea    0xd8c(%rip),%rsi
mov    %r14,%rdi
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %r15
jmp    1040 <fprintf@plt>

LLVM IR (-O0):
%struct.ip = type { i32, i32, i64, i32, i32, i64, i32, i32 }
%struct.tcphdr = type { i32 }
%struct.udphdr = type { i32 }
%struct.icmphdr = type { i32 }
@.str = [15 x i8] c"IPv4(HDR[%d])[\00"
@.str.1 = [8 x i8] c"Len=%d:\00"
@.str.2 = [7 x i8] c"ID=%d:\00"
@.str.3 = [8 x i8] c"TTL=%d:\00"
@.str.4 = [8 x i8] c"Chk=%d:\00"
@IP_DF = global i32 0
@.str.5 = [3 x i8] c"DF\00"
@IP_MF = global i32 0
@.str.6 = [3 x i8] c"MF\00"
@.str.7 = [12 x i8] c" Tos:%0x]:\09\00"
@.str.8 = [33 x i8] c"Unknown transport protocol: %d \0A\00"
define void @print_ipv4(i32* %0, %struct.ip* %1) {
%3 = alloca i32*
%4 = alloca %struct.ip*
%5 = alloca i8*
store i32* %0, i32** %3
store %struct.ip* %1, %struct.ip** %4
%6 = load %struct.ip*, %struct.ip** %4
%7 = bitcast %struct.ip* %6 to i8*
%8 = load %struct.ip*, %struct.ip** %4
%9 = getelementptr inbounds %struct.ip, %struct.ip* %8, i32 0, i32 0
%10 = load i32, i32* %9
%11 = mul nsw i32 4, %10
%12 = sext i32 %11 to i64
%13 = getelementptr inbounds i8, i8* %7, i64 %12
store i8* %13, i8** %5
%14 = load i32*, i32** %3
%15 = load %struct.ip*, %struct.ip** %4
%16 = getelementptr inbounds %struct.ip, %struct.ip* %15, i32 0, i32 0
%17 = load i32, i32* %16
%18 = mul nsw i32 4, %17
%19 = call i32 (i32*, i8*, ...) @fprintf(i32* %14, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %18)
%20 = load i32*, i32** %3
%21 = load %struct.ip*, %struct.ip** %4
%22 = getelementptr inbounds %struct.ip, %struct.ip* %21, i32 0, i32 7
%23 = load i32, i32* %22
%24 = call i32 @ntohs(i32 %23)
%25 = call i32 (i32*, i8*, ...) @fprintf(i32* %20, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i32 %24)
%26 = load i32*, i32** %3
%27 = load %struct.ip*, %struct.ip** %4
%28 = getelementptr inbounds %struct.ip, %struct.ip* %27, i32 0, i32 6
%29 = load i32, i32* %28
%30 = call i32 @ntohs(i32 %29)
%31 = call i32 (i32*, i8*, ...) @fprintf(i32* %26, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i32 %30)
%32 = load i32*, i32** %3
%33 = load %struct.ip*, %struct.ip** %4
%34 = getelementptr inbounds %struct.ip, %struct.ip* %33, i32 0, i32 5
%35 = load i64, i64* %34
%36 = trunc i64 %35 to i32
%37 = call i32 (i32*, i8*, ...) @fprintf(i32* %32, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i32 %36)
%38 = load i32*, i32** %3
%39 = load %struct.ip*, %struct.ip** %4
%40 = getelementptr inbounds %struct.ip, %struct.ip* %39, i32 0, i32 4
%41 = load i32, i32* %40
%42 = call i32 @ntohs(i32 %41)
%43 = call i32 (i32*, i8*, ...) @fprintf(i32* %38, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0), i32 %42)
%44 = load %struct.ip*, %struct.ip** %4
%45 = getelementptr inbounds %struct.ip, %struct.ip* %44, i32 0, i32 3
%46 = load i32, i32* %45
%47 = call i32 @ntohs(i32 %46)
%48 = load i32, i32* @IP_DF
%49 = and i32 %47, %48
%50 = icmp ne i32 %49, 0
br i1 %50, label %51, label %54
51: ; preds = %2
%52 = load i32*, i32** %3
%53 = call i32 (i32*, i8*, ...) @fprintf(i32* %52, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0))
br label %54
54: ; preds = %51, %2
%55 = load %struct.ip*, %struct.ip** %4
%56 = getelementptr inbounds %struct.ip, %struct.ip* %55, i32 0, i32 3
%57 = load i32, i32* %56
%58 = call i32 @ntohs(i32 %57)
%59 = load i32, i32* @IP_MF
%60 = and i32 %58, %59
%61 = icmp ne i32 %60, 0
br i1 %61, label %62, label %65
62: ; preds = %54
%63 = load i32*, i32** %3
%64 = call i32 (i32*, i8*, ...) @fprintf(i32* %63, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i64 0, i64 0))
br label %65
65: ; preds = %62, %54
%66 = load i32*, i32** %3
%67 = load %struct.ip*, %struct.ip** %4
%68 = getelementptr inbounds %struct.ip, %struct.ip* %67, i32 0, i32 2
%69 = load i64, i64* %68
%70 = trunc i64 %69 to i32
%71 = call i32 (i32*, i8*, ...) @fprintf(i32* %66, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.7, i64 0, i64 0), i32 %70)
%72 = load %struct.ip*, %struct.ip** %4
%73 = getelementptr inbounds %struct.ip, %struct.ip* %72, i32 0, i32 1
%74 = load i32, i32* %73
switch i32 %74, label %93 [
i32 129, label %75
i32 128, label %81
i32 130, label %87
]
75: ; preds = %65
%76 = load i32*, i32** %3
%77 = load %struct.ip*, %struct.ip** %4
%78 = load i8*, i8** %5
%79 = bitcast i8* %78 to %struct.tcphdr*
%80 = call i32 @print_tcp(i32* %76, %struct.ip* %77, %struct.tcphdr* %79)
br label %99
81: ; preds = %65
%82 = load i32*, i32** %3
%83 = load %struct.ip*, %struct.ip** %4
%84 = load i8*, i8** %5
%85 = bitcast i8* %84 to %struct.udphdr*
%86 = call i32 @print_udp(i32* %82, %struct.ip* %83, %struct.udphdr* %85)
br label %99
87: ; preds = %65
%88 = load i32*, i32** %3
%89 = load %struct.ip*, %struct.ip** %4
%90 = load i8*, i8** %5
%91 = bitcast i8* %90 to %struct.icmphdr*
%92 = call i32 @print_icmp(i32* %88, %struct.ip* %89, %struct.icmphdr* %91)
br label %99
93: ; preds = %65
%94 = load i32*, i32** %3
%95 = load %struct.ip*, %struct.ip** %4
%96 = getelementptr inbounds %struct.ip, %struct.ip* %95, i32 0, i32 1
%97 = load i32, i32* %96
%98 = call i32 (i32*, i8*, ...) @fprintf(i32* %94, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.8, i64 0, i64 0), i32 %97)
br label %99
99: ; preds = %93, %87, %81, %75
ret void
}

LLVM IR (-O2):
%struct.ip = type { i32, i32, i64, i32, i32, i64, i32, i32 }
%struct.tcphdr = type { i32 }
%struct.udphdr = type { i32 }
%struct.icmphdr = type { i32 }
@.str = [15 x i8] c"IPv4(HDR[%d])[\00"
@.str.1 = [8 x i8] c"Len=%d:\00"
@.str.2 = [7 x i8] c"ID=%d:\00"
@.str.3 = [8 x i8] c"TTL=%d:\00"
@.str.4 = [8 x i8] c"Chk=%d:\00"
@IP_DF = global i32 0
@.str.5 = [3 x i8] c"DF\00"
@IP_MF = global i32 0
@.str.6 = [3 x i8] c"MF\00"
@.str.7 = [12 x i8] c" Tos:%0x]:\09\00"
@.str.8 = [33 x i8] c"Unknown transport protocol: %d \0A\00"
define void @print_ipv4(i32* %0, %struct.ip* %1) {
%3 = bitcast %struct.ip* %1 to i8*
%4 = getelementptr inbounds %struct.ip, %struct.ip* %1, i64 0, i32 0
%5 = load i32, i32* %4
%6 = shl nsw i32 %5, 2
%7 = sext i32 %6 to i64
%8 = getelementptr inbounds i8, i8* %3, i64 %7
%9 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %0, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %6)
%10 = getelementptr inbounds %struct.ip, %struct.ip* %1, i64 0, i32 7
%11 = load i32, i32* %10
%12 = tail call i32 @ntohs(i32 %11)
%13 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %0, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i32 %12)
%14 = getelementptr inbounds %struct.ip, %struct.ip* %1, i64 0, i32 6
%15 = load i32, i32* %14
%16 = tail call i32 @ntohs(i32 %15)
%17 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %0, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i32 %16)
%18 = getelementptr inbounds %struct.ip, %struct.ip* %1, i64 0, i32 5
%19 = load i64, i64* %18
%20 = trunc i64 %19 to i32
%21 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %0, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i32 %20)
%22 = getelementptr inbounds %struct.ip, %struct.ip* %1, i64 0, i32 4
%23 = load i32, i32* %22
%24 = tail call i32 @ntohs(i32 %23)
%25 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %0, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0), i32 %24)
%26 = getelementptr inbounds %struct.ip, %struct.ip* %1, i64 0, i32 3
%27 = load i32, i32* %26
%28 = tail call i32 @ntohs(i32 %27)
%29 = load i32, i32* @IP_DF
%30 = and i32 %29, %28
%31 = icmp eq i32 %30, 0
br i1 %31, label %34, label %32
32: ; preds = %2
%33 = tail call i64 @fwrite(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i64 2, i64 1, i32* %0)
br label %34
34: ; preds = %32, %2
%35 = load i32, i32* %26
%36 = tail call i32 @ntohs(i32 %35)
%37 = load i32, i32* @IP_MF
%38 = and i32 %37, %36
%39 = icmp eq i32 %38, 0
br i1 %39, label %42, label %40
40: ; preds = %34
%41 = tail call i64 @fwrite(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i64 0, i64 0), i64 2, i64 1, i32* %0)
br label %42
42: ; preds = %40, %34
%43 = getelementptr inbounds %struct.ip, %struct.ip* %1, i64 0, i32 2
%44 = load i64, i64* %43
%45 = trunc i64 %44 to i32
%46 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %0, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.7, i64 0, i64 0), i32 %45)
%47 = getelementptr inbounds %struct.ip, %struct.ip* %1, i64 0, i32 1
%48 = load i32, i32* %47
switch i32 %48, label %58 [
i32 129, label %49
i32 128, label %52
i32 130, label %55
]
49: ; preds = %42
%50 = bitcast i8* %8 to %struct.tcphdr*
%51 = tail call i32 @print_tcp(i32* %0, %struct.ip* %1, %struct.tcphdr* %50)
br label %60
52: ; preds = %42
%53 = bitcast i8* %8 to %struct.udphdr*
%54 = tail call i32 @print_udp(i32* %0, %struct.ip* %1, %struct.udphdr* %53)
br label %60
55: ; preds = %42
%56 = bitcast i8* %8 to %struct.icmphdr*
%57 = tail call i32 @print_icmp(i32* %0, %struct.ip* %1, %struct.icmphdr* %56)
br label %60
58: ; preds = %42
%59 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %0, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.8, i64 0, i64 0), i32 %48)
br label %60
60: ; preds = %58, %55, %52, %49
ret void
}