#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_int;
struct iphdr {
  int dummy;
};
struct ip6_hdr {
  int dummy;
};
struct ether_header {
  int dummy;
};

/* Variables and functions */
#define ETHERTYPE_IP 129
#define ETHERTYPE_IPV6 128
int /*<<< orphan*/ disp_icmp4(int);
int /*<<< orphan*/ disp_icmp6(int);
int /*<<< orphan*/ disp_igmp(int);
int /*<<< orphan*/ disp_tcp(int);
int /*<<< orphan*/ disp_udp(int);
int /*<<< orphan*/ putchar(char);
void layer4(u_int type, u_int8_t protocol) {
  int size = sizeof(struct ether_header);
  switch (type) {
  case ETHERTYPE_IP:
    size += sizeof(struct iphdr);
    break;
  case ETHERTYPE_IPV6:
    size += sizeof(struct ip6_hdr);
    break;
  }
  switch (protocol) {
  case 1:
    disp_icmp4(size);
    break;
  case 2:
    disp_igmp(size);
    break;
  case 6:
    disp_tcp(size);
    break;
  case 17:
    disp_udp(size);
    break;
  case 58:
    disp_icmp6(size);
  default:
    break;
  }
  putchar('\n');
}


x86 assembly (clang, -O0):
<layer4>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
movl   $0x4,-0xc(%rbp)
mov    -0x4(%rbp),%eax
mov    %eax,-0x10(%rbp)
sub    $0x80,%eax
je     11ae <layer4+0x4e>
jmp    118b <layer4+0x2b>
mov    -0x10(%rbp),%eax
sub    $0x81,%eax
jne    11b9 <layer4+0x59>
jmp    119e <layer4+0x3e>
movslq -0xc(%rbp),%rax
add    $0x4,%rax
mov    %eax,-0xc(%rbp)
jmp    11b9 <layer4+0x59>
movslq -0xc(%rbp),%rax
add    $0x4,%rax
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x14(%rbp)
sub    $0x1,%eax
je     1211 <layer4+0xb1>
jmp    11cd <layer4+0x6d>
mov    -0x14(%rbp),%eax
sub    $0x2,%eax
je     121e <layer4+0xbe>
jmp    11de <layer4+0x7e>
mov    -0x14(%rbp),%eax
sub    $0x6,%eax
je     122b <layer4+0xcb>
jmp    11ef <layer4+0x8f>
mov    -0x14(%rbp),%eax
sub    $0x11,%eax
je     1238 <layer4+0xd8>
jmp    1200 <layer4+0xa0>
mov    -0x14(%rbp),%eax
sub    $0x3a,%eax
je     1245 <layer4+0xe5>
jmp    124d <layer4+0xed>
mov    -0xc(%rbp),%edi
call   1060 <disp_icmp4@plt>
jmp    1252 <layer4+0xf2>
mov    -0xc(%rbp),%edi
call   1080 <disp_igmp@plt>
jmp    1252 <layer4+0xf2>
mov    -0xc(%rbp),%edi
call   1040 <disp_tcp@plt>
jmp    1252 <layer4+0xf2>
mov    -0xc(%rbp),%edi
call   1070 <disp_udp@plt>
jmp    1252 <layer4+0xf2>
mov    -0xc(%rbp),%edi
call   1050 <disp_icmp6@plt>
jmp    1252 <layer4+0xf2>
mov    $0xa,%edi
call   1030 <putchar@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<layer4>:
push   %rax
and    $0xfffffffe,%edi
xor    %eax,%eax
cmp    $0x80,%edi
sete   %al
lea    0x4(,%rax,4),%edi
lea    -0x1(%rsi),%eax
cmp    $0x10,%eax
ja     1195 <layer4+0x35>
lea    0xe7b(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
call   1060 <disp_icmp4@plt>
jmp    11b4 <layer4+0x54>
cmp    $0x3a,%esi
jne    11b4 <layer4+0x54>
call   1050 <disp_icmp6@plt>
jmp    11b4 <layer4+0x54>
call   1080 <disp_igmp@plt>
jmp    11b4 <layer4+0x54>
call   1040 <disp_tcp@plt>
jmp    11b4 <layer4+0x54>
call   1070 <disp_udp@plt>
mov    $0xa,%edi
pop    %rax
jmp    1030 <putchar@plt>

LLVM IR (-O0):
define void @layer4(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
store i32 %0, i32* %3
store i32 %1, i32* %4
store i32 4, i32* %5
%6 = load i32, i32* %3
switch i32 %6, label %17 [
i32 129, label %7
i32 128, label %12
]
7: ; preds = %2
%8 = load i32, i32* %5
%9 = sext i32 %8 to i64
%10 = add i64 %9, 4
%11 = trunc i64 %10 to i32
store i32 %11, i32* %5
br label %17
12: ; preds = %2
%13 = load i32, i32* %5
%14 = sext i32 %13 to i64
%15 = add i64 %14, 4
%16 = trunc i64 %15 to i32
store i32 %16, i32* %5
br label %17
17: ; preds = %2, %12, %7
%18 = load i32, i32* %4
switch i32 %18, label %34 [
i32 1, label %19
i32 2, label %22
i32 6, label %25
i32 17, label %28
i32 58, label %31
]
19: ; preds = %17
%20 = load i32, i32* %5
%21 = call i32 @disp_icmp4(i32 %20)
br label %35
22: ; preds = %17
%23 = load i32, i32* %5
%24 = call i32 @disp_igmp(i32 %23)
br label %35
25: ; preds = %17
%26 = load i32, i32* %5
%27 = call i32 @disp_tcp(i32 %26)
br label %35
28: ; preds = %17
%29 = load i32, i32* %5
%30 = call i32 @disp_udp(i32 %29)
br label %35
31: ; preds = %17
%32 = load i32, i32* %5
%33 = call i32 @disp_icmp6(i32 %32)
br label %34
34: ; preds = %17, %31
br label %35
35: ; preds = %34, %28, %25, %22, %19
%36 = call i32 @putchar(i8 signext 10)
ret void
}

LLVM IR (-O2):
define void @layer4(i32 %0, i32 %1) {
%3 = and i32 %0, -2
%4 = icmp eq i32 %3, 128
%5 = select i1 %4, i32 8, i32 4
switch i32 %1, label %16 [
i32 1, label %6
i32 2, label %8
i32 6, label %10
i32 17, label %12
i32 58, label %14
]
6: ; preds = %2
%7 = tail call i32 @disp_icmp4(i32 %5)
br label %16
8: ; preds = %2
%9 = tail call i32 @disp_igmp(i32 %5)
br label %16
10: ; preds = %2
%11 = tail call i32 @disp_tcp(i32 %5)
br label %16
12: ; preds = %2
%13 = tail call i32 @disp_udp(i32 %5)
br label %16
14: ; preds = %2
%15 = tail call i32 @disp_icmp6(i32 %5)
br label %16
16: ; preds = %14, %2, %12, %10, %8, %6
%17 = tail call i32 @putchar(i8 signext 10)
ret void
}