#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 tot_len;
  int /*<<< orphan*/ protocol;
};
struct icmp {
  scalar_t__ icmp_cksum;
  scalar_t__ icmp_code;
  scalar_t__ icmp_type;
};

/* Variables and functions */
scalar_t__ Checksum(unsigned short *, int);
int /*<<< orphan*/ IPPROTO_ICMP;
scalar_t__ atoi(char *);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ memset(unsigned char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ stdin;
void CreateIcmpPacket(unsigned char *datagram) {
  struct icmp *icmp = (struct icmp *)(datagram + sizeof(struct iphdr));
  struct iphdr *iph = (struct iphdr *)datagram;
  char input[32];
  iph->protocol = IPPROTO_ICMP;
  memset(datagram + sizeof(struct iphdr), 0, iph->tot_len);
  iph->tot_len = sizeof(struct iphdr) + 8;
  printf("\nICMP type (0): ");
  fgets(input, 32, stdin);
  if (atoi(input) == 0)
    icmp->icmp_type = 0;
  else
    icmp->icmp_type = atoi(input);
  printf("\nICMP code (0): ");
  fgets(input, 32, stdin);
  if (atoi(input) == 0)
    icmp->icmp_code = 0;
  else
    icmp->icmp_code = atoi(input);
  printf("\nICMP checksum will be calcuated... \n");
  icmp->icmp_cksum = 0;
  icmp->icmp_cksum =
      Checksum((unsigned short *)(datagram + sizeof(struct iphdr)), 4);
}


x86 assembly (clang, -O0):
<CreateIcmpPacket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x8,%rax
mov    %rax,-0x10(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    0x2e69(%rip),%rax
mov    (%rax),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,0x4(%rax)
mov    -0x8(%rbp),%rdi
add    $0x8,%rdi
mov    -0x18(%rbp),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1050 <memset@plt>
mov    -0x18(%rbp),%rax
movl   $0x10,(%rax)
lea    0xe5a(%rip),%rdi
call   1040 <printf@plt>
lea    -0x40(%rbp),%rdi
mov    0x2e32(%rip),%rax
mov    (%rax),%edx
mov    $0x20,%esi
call   1060 <fgets@plt>
lea    -0x40(%rbp),%rdi
call   1070 <atoi@plt>
cmp    $0x0,%rax
jne    11e6 <CreateIcmpPacket+0x96>
mov    -0x10(%rbp),%rax
movq   $0x0,0x10(%rax)
jmp    11fa <CreateIcmpPacket+0xaa>
lea    -0x40(%rbp),%rdi
call   1070 <atoi@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,0x10(%rax)
lea    0xe10(%rip),%rdi
call   1040 <printf@plt>
lea    -0x40(%rbp),%rdi
mov    0x2dd7(%rip),%rax
mov    (%rax),%edx
mov    $0x20,%esi
call   1060 <fgets@plt>
lea    -0x40(%rbp),%rdi
call   1070 <atoi@plt>
cmp    $0x0,%rax
jne    1241 <CreateIcmpPacket+0xf1>
mov    -0x10(%rbp),%rax
movq   $0x0,0x8(%rax)
jmp    1255 <CreateIcmpPacket+0x105>
lea    -0x40(%rbp),%rdi
call   1070 <atoi@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,0x8(%rax)
lea    0xdc6(%rip),%rdi
call   1040 <printf@plt>
mov    -0x10(%rbp),%rax
movq   $0x0,(%rax)
mov    -0x8(%rbp),%rdi
add    $0x8,%rdi
mov    $0x4,%esi
call   1030 <Checksum@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<CreateIcmpPacket>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rdi,%rbx
lea    0x8(%rdi),%r14
mov    0x2e67(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x4(%rdi)
mov    (%rdi),%edx
mov    %r14,%rdi
xor    %esi,%esi
call   1060 <memset@plt>
movl   $0x10,(%rbx)
lea    0xe69(%rip),%rdi
call   1050 <printf@plt>
mov    0x2e45(%rip),%r12
mov    (%r12),%edx
mov    %rsp,%r15
mov    %r15,%rdi
mov    $0x20,%esi
call   1070 <fgets@plt>
mov    %r15,%rdi
call   1080 <atoi@plt>
mov    %rax,0x18(%rbx)
lea    0xe47(%rip),%rdi
call   1050 <printf@plt>
mov    (%r12),%edx
mov    %r15,%rdi
mov    $0x20,%esi
call   1070 <fgets@plt>
mov    %r15,%rdi
call   1080 <atoi@plt>
mov    %rax,0x10(%rbx)
lea    0xe2f(%rip),%rdi
call   1040 <puts@plt>
movq   $0x0,0x8(%rbx)
mov    %r14,%rdi
mov    $0x4,%esi
call   1030 <Checksum@plt>
mov    %rax,0x8(%rbx)
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.icmp = type { i64, i64, i64 }
%struct.iphdr = type { i32, i32 }
@IPPROTO_ICMP = global i32 0
@.str = [17 x i8] c"\0AICMP type (0): \00"
@stdin = global i32 0
@.str.1 = [17 x i8] c"\0AICMP code (0): \00"
@.str.2 = [38 x i8] c"\0AICMP checksum will be calcuated... \0A\00"
define void @CreateIcmpPacket(i8* %0) {
%2 = alloca i8*
%3 = alloca %struct.icmp*
%4 = alloca %struct.iphdr*
%5 = alloca [32 x i8]
store i8* %0, i8** %2
%6 = load i8*, i8** %2
%7 = getelementptr inbounds i8, i8* %6, i64 8
%8 = bitcast i8* %7 to %struct.icmp*
store %struct.icmp* %8, %struct.icmp** %3
%9 = load i8*, i8** %2
%10 = bitcast i8* %9 to %struct.iphdr*
store %struct.iphdr* %10, %struct.iphdr** %4
%11 = load i32, i32* @IPPROTO_ICMP
%12 = load %struct.iphdr*, %struct.iphdr** %4
%13 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %12, i32 0, i32 1
store i32 %11, i32* %13
%14 = load i8*, i8** %2
%15 = getelementptr inbounds i8, i8* %14, i64 8
%16 = load %struct.iphdr*, %struct.iphdr** %4
%17 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %16, i32 0, i32 0
%18 = load i32, i32* %17
%19 = call i32 @memset(i8* %15, i32 0, i32 %18)
%20 = load %struct.iphdr*, %struct.iphdr** %4
%21 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %20, i32 0, i32 0
store i32 16, i32* %21
%22 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
%23 = getelementptr inbounds [32 x i8], [32 x i8]* %5, i64 0, i64 0
%24 = load i32, i32* @stdin
%25 = call i32 @fgets(i8* %23, i32 32, i32 %24)
%26 = getelementptr inbounds [32 x i8], [32 x i8]* %5, i64 0, i64 0
%27 = call i64 @atoi(i8* %26)
%28 = icmp eq i64 %27, 0
br i1 %28, label %29, label %32
29: ; preds = %1
%30 = load %struct.icmp*, %struct.icmp** %3
%31 = getelementptr inbounds %struct.icmp, %struct.icmp* %30, i32 0, i32 2
store i64 0, i64* %31
br label %37
32: ; preds = %1
%33 = getelementptr inbounds [32 x i8], [32 x i8]* %5, i64 0, i64 0
%34 = call i64 @atoi(i8* %33)
%35 = load %struct.icmp*, %struct.icmp** %3
%36 = getelementptr inbounds %struct.icmp, %struct.icmp* %35, i32 0, i32 2
store i64 %34, i64* %36
br label %37
37: ; preds = %32, %29
%38 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
%39 = getelementptr inbounds [32 x i8], [32 x i8]* %5, i64 0, i64 0
%40 = load i32, i32* @stdin
%41 = call i32 @fgets(i8* %39, i32 32, i32 %40)
%42 = getelementptr inbounds [32 x i8], [32 x i8]* %5, i64 0, i64 0
%43 = call i64 @atoi(i8* %42)
%44 = icmp eq i64 %43, 0
br i1 %44, label %45, label %48
45: ; preds = %37
%46 = load %struct.icmp*, %struct.icmp** %3
%47 = getelementptr inbounds %struct.icmp, %struct.icmp* %46, i32 0, i32 1
store i64 0, i64* %47
br label %53
48: ; preds = %37
%49 = getelementptr inbounds [32 x i8], [32 x i8]* %5, i64 0, i64 0
%50 = call i64 @atoi(i8* %49)
%51 = load %struct.icmp*, %struct.icmp** %3
%52 = getelementptr inbounds %struct.icmp, %struct.icmp* %51, i32 0, i32 1
store i64 %50, i64* %52
br label %53
53: ; preds = %48, %45
%54 = call i32 @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.2, i64 0, i64 0))
%55 = load %struct.icmp*, %struct.icmp** %3
%56 = getelementptr inbounds %struct.icmp, %struct.icmp* %55, i32 0, i32 0
store i64 0, i64* %56
%57 = load i8*, i8** %2
%58 = getelementptr inbounds i8, i8* %57, i64 8
%59 = bitcast i8* %58 to i16*
%60 = call i64 @Checksum(i16* %59, i32 4)
%61 = load %struct.icmp*, %struct.icmp** %3
%62 = getelementptr inbounds %struct.icmp, %struct.icmp* %61, i32 0, i32 0
store i64 %60, i64* %62
ret void
}

LLVM IR (-O2):
@IPPROTO_ICMP = global i32 0
@.str = [17 x i8] c"\0AICMP type (0): \00"
@stdin = global i32 0
@.str.1 = [17 x i8] c"\0AICMP code (0): \00"
@str = [37 x i8] c"\0AICMP checksum will be calcuated... \00"
define void @CreateIcmpPacket(i8* %0) {
%2 = alloca [32 x i8]
%3 = getelementptr inbounds i8, i8* %0, i64 8
%4 = getelementptr inbounds [32 x i8], [32 x i8]* %2, i64 0, i64 0
%5 = load i32, i32* @IPPROTO_ICMP
%6 = getelementptr inbounds i8, i8* %0, i64 4
%7 = bitcast i8* %6 to i32*
store i32 %5, i32* %7
%8 = bitcast i8* %0 to i32*
%9 = load i32, i32* %8
%10 = tail call i32 @memset(i8* %3, i32 0, i32 %9)
store i32 16, i32* %8
%11 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
%12 = load i32, i32* @stdin
%13 = call i32 @fgets(i8* %4, i32 32, i32 %12)
%14 = call i64 @atoi(i8* %4)
%15 = getelementptr inbounds i8, i8* %0, i64 24
%16 = bitcast i8* %15 to i64*
store i64 %14, i64* %16
%17 = call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
%18 = load i32, i32* @stdin
%19 = call i32 @fgets(i8* %4, i32 32, i32 %18)
%20 = call i64 @atoi(i8* %4)
%21 = getelementptr inbounds i8, i8* %0, i64 16
%22 = bitcast i8* %21 to i64*
store i64 %20, i64* %22
%23 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @str, i64 0, i64 0))
%24 = bitcast i8* %3 to i64*
store i64 0, i64* %24
%25 = bitcast i8* %3 to i16*
%26 = call i64 @Checksum(i16* %25, i32 4)
store i64 %26, i64* %24
ret void
}