#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 */
typedef struct TYPE_2__ TYPE_1__;

/* Type definitions */
typedef int /*<<< orphan*/ u_int16_t;
struct TYPE_2__ {
  int s_addr;
};
struct sockaddr_in {
  int /*<<< orphan*/ sin_family;
  TYPE_1__ sin_addr;
};
struct hostent {
  int /*<<< orphan*/ h_length;
  int /*<<< orphan*/ h_addr;
};

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ IPPROTO_ICMP;
int /*<<< orphan*/ SOCK_RAW;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
struct hostent *gethostbyname(char *);
int /*<<< orphan*/ herror(char *);
int /*<<< orphan*/ icmp_tunnel(int, int, struct sockaddr_in *, int, int,
                               int /*<<< orphan*/);
int inet_addr(char *);
int /*<<< orphan*/ memcpy(int *, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strcmp(char *, char *);
int run_icmp_tunnel(int id, int packetsize, char **argv, int tun_fd) {
  struct sockaddr_in target;
  int s;
  char *daemon = argv[1];
  char *desthost = argv[2];
  if (!desthost) {
    fprintf(stderr, "no destination\n");
    return -1;
  }
  if ((target.sin_addr.s_addr = inet_addr(desthost)) == -1) {
    struct hostent *he;
    if (!(he = gethostbyname(desthost))) {
      herror("gethostbyname");
      return -1;
    }
    memcpy(&target.sin_addr.s_addr, he->h_addr, he->h_length);
  }
  target.sin_family = AF_INET;
  if ((s = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP)) == -1) {
    perror("socket");
    return -1;
  }
  icmp_tunnel(s, !strcmp(daemon, "-d"), &target, tun_fd, packetsize,
              (u_int16_t)id);
  close(s);
  return 0;
}


x86 assembly (clang, -O0):
<run_icmp_tunnel>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    -0x18(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x38(%rbp)
mov    -0x18(%rbp),%rax
mov    0x10(%rax),%rax
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
jne    11f9 <run_icmp_tunnel+0x59>
mov    0x2de9(%rip),%rax
mov    (%rax),%edi
lea    0xe18(%rip),%rsi
call   1080 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12f0 <run_icmp_tunnel+0x150>
mov    -0x40(%rbp),%rdi
call   1040 <inet_addr@plt>
mov    %eax,-0x24(%rbp)
cmp    $0xffffffff,%eax
jne    1257 <run_icmp_tunnel+0xb7>
mov    -0x40(%rbp),%rdi
call   1060 <gethostbyname@plt>
mov    %rax,-0x48(%rbp)
cmp    $0x0,%rax
jne    123d <run_icmp_tunnel+0x9d>
lea    0xde4(%rip),%rdi
call   10a0 <herror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12f0 <run_icmp_tunnel+0x150>
lea    -0x28(%rbp),%rdi
add    $0x4,%rdi
mov    -0x48(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x48(%rbp),%rax
mov    (%rax),%edx
call   1090 <memcpy@plt>
mov    0x2d8a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    0x2d7e(%rip),%rax
mov    (%rax),%edi
mov    0x2d6d(%rip),%rax
mov    (%rax),%esi
mov    0x2d5c(%rip),%rax
mov    (%rax),%edx
call   10c0 <socket@plt>
mov    %eax,-0x2c(%rbp)
cmp    $0xffffffff,%eax
jne    12a7 <run_icmp_tunnel+0x107>
lea    0xd88(%rip),%rdi
call   10b0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12f0 <run_icmp_tunnel+0x150>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4c(%rbp)
mov    -0x38(%rbp),%rdi
lea    0xd6d(%rip),%rsi
call   1050 <strcmp@plt>
mov    -0x4c(%rbp),%edi
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
and    $0x1,%al
movzbl %al,%esi
mov    -0x1c(%rbp),%ecx
mov    -0xc(%rbp),%r8d
mov    -0x8(%rbp),%r9d
lea    -0x28(%rbp),%rdx
call   1070 <icmp_tunnel@plt>
mov    -0x2c(%rbp),%edi
call   1030 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<run_icmp_tunnel>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    0x10(%rdx),%rbx
test   %rbx,%rbx
je     1258 <run_icmp_tunnel+0xb8>
mov    %ecx,%r14d
mov    %esi,%r12d
mov    %edi,%ebp
mov    0x8(%rdx),%r15
mov    %rbx,%rdi
call   1040 <inet_addr@plt>
mov    %eax,0xc(%rsp)
cmp    $0xffffffff,%eax
jne    11f6 <run_icmp_tunnel+0x56>
mov    %rbx,%rdi
call   1060 <gethostbyname@plt>
test   %rax,%rax
je     127f <run_icmp_tunnel+0xdf>
lea    0xc(%rsp),%rdi
mov    (%rax),%edx
mov    0x4(%rax),%esi
call   1090 <memcpy@plt>
mov    0x2deb(%rip),%rax
mov    (%rax),%edi
mov    %edi,0x8(%rsp)
mov    0x2dd6(%rip),%rax
mov    (%rax),%esi
mov    0x2dc5(%rip),%rax
mov    (%rax),%edx
call   10c0 <socket@plt>
cmp    $0xffffffff,%eax
je     128d <run_icmp_tunnel+0xed>
mov    %eax,%ebx
lea    0xdfd(%rip),%rsi
mov    %r15,%rdi
call   1050 <strcmp@plt>
xor    %esi,%esi
test   %eax,%eax
sete   %sil
lea    0x8(%rsp),%rdx
mov    %ebx,%edi
mov    %r14d,%ecx
mov    %r12d,%r8d
mov    %ebp,%r9d
call   1070 <icmp_tunnel@plt>
mov    %ebx,%edi
call   1030 <close@plt>
xor    %eax,%eax
jmp    1272 <run_icmp_tunnel+0xd2>
mov    0x2d69(%rip),%rax
mov    (%rax),%edi
lea    0xd98(%rip),%rsi
call   1080 <fprintf@plt>
mov    $0xffffffff,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd8a(%rip),%rdi
call   10a0 <herror@plt>
jmp    126d <run_icmp_tunnel+0xcd>
lea    0xd8a(%rip),%rdi
call   10b0 <perror@plt>
jmp    126d <run_icmp_tunnel+0xcd>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i32 }
%struct.hostent = type { i32, i32 }
@stderr = global i32 0
@.str = [16 x i8] c"no destination\0A\00"
@.str.1 = [14 x i8] c"gethostbyname\00"
@AF_INET = global i32 0
@SOCK_RAW = global i32 0
@IPPROTO_ICMP = global i32 0
@.str.2 = [7 x i8] c"socket\00"
@.str.3 = [3 x i8] c"-d\00"
define i32 @run_icmp_tunnel(i32 %0, i32 %1, i8** %2, i32 %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8**
%9 = alloca i32
%10 = alloca %struct.sockaddr_in
%11 = alloca i32
%12 = alloca i8*
%13 = alloca i8*
%14 = alloca %struct.hostent*
store i32 %0, i32* %6
store i32 %1, i32* %7
store i8** %2, i8*** %8
store i32 %3, i32* %9
%15 = load i8**, i8*** %8
%16 = getelementptr inbounds i8*, i8** %15, i64 1
%17 = load i8*, i8** %16
store i8* %17, i8** %12
%18 = load i8**, i8*** %8
%19 = getelementptr inbounds i8*, i8** %18, i64 2
%20 = load i8*, i8** %19
store i8* %20, i8** %13
%21 = load i8*, i8** %13
%22 = icmp ne i8* %21, null
br i1 %22, label %26, label %23
23: ; preds = %4
%24 = load i32, i32* @stderr
%25 = call i32 @fprintf(i32 %24, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %5
br label %71
26: ; preds = %4
%27 = load i8*, i8** %13
%28 = call i32 @inet_addr(i8* %27)
%29 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 1
%30 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %29, i32 0, i32 0
store i32 %28, i32* %30
%31 = icmp eq i32 %28, -1
br i1 %31, label %32, label %48
32: ; preds = %26
%33 = load i8*, i8** %13
%34 = call %struct.hostent* @gethostbyname(i8* %33)
store %struct.hostent* %34, %struct.hostent** %14
%35 = icmp ne %struct.hostent* %34, null
br i1 %35, label %38, label %36
36: ; preds = %32
%37 = call i32 @herror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %5
br label %71
38: ; preds = %32
%39 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 1
%40 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %39, i32 0, i32 0
%41 = load %struct.hostent*, %struct.hostent** %14
%42 = getelementptr inbounds %struct.hostent, %struct.hostent* %41, i32 0, i32 1
%43 = load i32, i32* %42
%44 = load %struct.hostent*, %struct.hostent** %14
%45 = getelementptr inbounds %struct.hostent, %struct.hostent* %44, i32 0, i32 0
%46 = load i32, i32* %45
%47 = call i32 @memcpy(i32* %40, i32 %43, i32 %46)
br label %48
48: ; preds = %38, %26
%49 = load i32, i32* @AF_INET
%50 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 0
store i32 %49, i32* %50
%51 = load i32, i32* @AF_INET
%52 = load i32, i32* @SOCK_RAW
%53 = load i32, i32* @IPPROTO_ICMP
%54 = call i32 @socket(i32 %51, i32 %52, i32 %53)
store i32 %54, i32* %11
%55 = icmp eq i32 %54, -1
br i1 %55, label %56, label %58
56: ; preds = %48
%57 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %5
br label %71
58: ; preds = %48
%59 = load i32, i32* %11
%60 = load i8*, i8** %12
%61 = call i32 @strcmp(i8* %60, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
%62 = icmp ne i32 %61, 0
%63 = xor i1 %62, true
%64 = zext i1 %63 to i32
%65 = load i32, i32* %9
%66 = load i32, i32* %7
%67 = load i32, i32* %6
%68 = call i32 @icmp_tunnel(i32 %59, i32 %64, %struct.sockaddr_in* %10, i32 %65, i32 %66, i32 %67)
%69 = load i32, i32* %11
%70 = call i32 @close(i32 %69)
store i32 0, i32* %5
br label %71
71: ; preds = %58, %56, %36, %23
%72 = load i32, i32* %5
ret i32 %72
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i32 }
%struct.hostent = type { i32, i32 }
@stderr = global i32 0
@.str = [16 x i8] c"no destination\0A\00"
@.str.1 = [14 x i8] c"gethostbyname\00"
@AF_INET = global i32 0
@SOCK_RAW = global i32 0
@IPPROTO_ICMP = global i32 0
@.str.2 = [7 x i8] c"socket\00"
@.str.3 = [3 x i8] c"-d\00"
define i32 @run_icmp_tunnel(i32 %0, i32 %1, i8** nocapture %2, i32 %3) {
%5 = alloca %struct.sockaddr_in
%6 = bitcast %struct.sockaddr_in* %5 to i8*
%7 = getelementptr inbounds i8*, i8** %2, i64 1
%8 = load i8*, i8** %7
%9 = getelementptr inbounds i8*, i8** %2, i64 2
%10 = load i8*, i8** %9
%11 = icmp eq i8* %10, null
br i1 %11, label %12, label %15
12: ; preds = %4
%13 = load i32, i32* @stderr
%14 = tail call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
br label %45
15: ; preds = %4
%16 = tail call i32 @inet_addr(i8* %10)
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 1, i32 0
store i32 %16, i32* %17
%18 = icmp eq i32 %16, -1
br i1 %18, label %19, label %30
19: ; preds = %15
%20 = tail call %struct.hostent* @gethostbyname(i8* %10)
%21 = icmp eq %struct.hostent* %20, null
br i1 %21, label %22, label %24
22: ; preds = %19
%23 = tail call i32 @herror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
br label %45
24: ; preds = %19
%25 = getelementptr inbounds %struct.hostent, %struct.hostent* %20, i64 0, i32 1
%26 = load i32, i32* %25
%27 = getelementptr inbounds %struct.hostent, %struct.hostent* %20, i64 0, i32 0
%28 = load i32, i32* %27
%29 = call i32 @memcpy(i32* %17, i32 %26, i32 %28)
br label %30
30: ; preds = %24, %15
%31 = load i32, i32* @AF_INET
%32 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 0
store i32 %31, i32* %32
%33 = load i32, i32* @SOCK_RAW
%34 = load i32, i32* @IPPROTO_ICMP
%35 = call i32 @socket(i32 %31, i32 %33, i32 %34)
%36 = icmp eq i32 %35, -1
br i1 %36, label %37, label %39
37: ; preds = %30
%38 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
br label %45
39: ; preds = %30
%40 = call i32 @strcmp(i8* dereferenceable(1) %8, i8* dereferenceable(3) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
%41 = icmp eq i32 %40, 0
%42 = zext i1 %41 to i32
%43 = call i32 @icmp_tunnel(i32 %35, i32 %42, %struct.sockaddr_in* %5, i32 %3, i32 %1, i32 %0)
%44 = call i32 @close(i32 %35)
br label %45
45: ; preds = %22, %39, %37, %12
%46 = phi i32 [ -1, %37 ], [ 0, %39 ], [ -1, %12 ], [ -1, %22 ]
ret i32 %46
}