#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 /*<<< orphan*/ uint8_t;
struct iphdr {
  int ihl;
  int version;
  int ttl;
  int protocol;
  void *daddr;
  void *saddr;
  scalar_t__ frag_off;
  void *id;
  void *tot_len;
  scalar_t__ tos;
};

/* Variables and functions */
void *htons(int);
void *inet_addr(char *);
int /*<<< orphan*/ memcpy(int /*<<< orphan*/ *, struct iphdr *, int);
int rand();
void attach_outter_encapsulation(uint8_t *destBuffer, char *srcIP, char *destIP,
                                 int whichProtocol, int packetLen) {
  struct iphdr iphdr;
  iphdr.ihl = 5;
  iphdr.version = 4;
  iphdr.tos = 0;
  iphdr.tot_len = htons(packetLen);
  iphdr.id = htons(rand() % 65535);
  iphdr.frag_off = 0;
  iphdr.ttl = 64;
  iphdr.protocol = whichProtocol;
  iphdr.saddr = inet_addr(srcIP);
  iphdr.daddr = inet_addr(destIP);
  memcpy(destBuffer, &iphdr, sizeof(struct iphdr));
}


x86 assembly (clang, -O0):
<attach_outter_encapsulation>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    %r8d,-0x20(%rbp)
movl   $0x5,-0x60(%rbp)
movl   $0x4,-0x5c(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x20(%rbp),%edi
call   1030 <htons@plt>
mov    %rax,-0x30(%rbp)
mov    $0x0,%al
call   1060 <rand@plt>
mov    $0xffff,%ecx
cltd
idiv   %ecx
mov    %edx,%edi
call   1030 <htons@plt>
mov    %rax,-0x38(%rbp)
movq   $0x0,-0x40(%rbp)
movl   $0x40,-0x58(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x54(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <inet_addr@plt>
mov    %rax,-0x48(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <inet_addr@plt>
mov    %rax,-0x50(%rbp)
mov    -0x8(%rbp),%rdi
lea    -0x60(%rbp),%rsi
mov    $0x40,%edx
call   1050 <memcpy@plt>
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<attach_outter_encapsulation>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x48,%rsp
mov    %ecx,%ebp
mov    %rdx,%r15
mov    %rsi,%rbx
mov    %rdi,%r14
movabs $0x400000005,%rax
mov    %rax,0x8(%rsp)
movq   $0x0,0x40(%rsp)
mov    %r8d,%edi
call   1030 <htons@plt>
mov    %rax,0x38(%rsp)
xor    %eax,%eax
call   1060 <rand@plt>
cltq
imul   $0xffffffff80008001,%rax,%rdi
shr    $0x20,%rdi
add    %eax,%edi
mov    %edi,%ecx
shr    $0x1f,%ecx
sar    $0xf,%edi
add    %ecx,%edi
mov    %edi,%ecx
shl    $0x10,%ecx
sub    %ecx,%edi
add    %eax,%edi
call   1030 <htons@plt>
mov    %rax,0x30(%rsp)
movq   $0x0,0x28(%rsp)
movl   $0x40,0x10(%rsp)
mov    %ebp,0x14(%rsp)
mov    %rbx,%rdi
call   1040 <inet_addr@plt>
mov    %rax,0x20(%rsp)
mov    %r15,%rdi
call   1040 <inet_addr@plt>
mov    %rax,0x18(%rsp)
lea    0x8(%rsp),%rsi
mov    %r14,%rdi
mov    $0x40,%edx
call   1050 <memcpy@plt>
add    $0x48,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.iphdr = type { i32, i32, i32, i32, i8*, i8*, i64, i8*, i8*, i64 }
define void @attach_outter_encapsulation(i32* %0, i8* %1, i8* %2, i32 %3, i32 %4) {
%6 = alloca i32*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32
%11 = alloca %struct.iphdr
store i32* %0, i32** %6
store i8* %1, i8** %7
store i8* %2, i8** %8
store i32 %3, i32* %9
store i32 %4, i32* %10
%12 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %11, i32 0, i32 0
store i32 5, i32* %12
%13 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %11, i32 0, i32 1
store i32 4, i32* %13
%14 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %11, i32 0, i32 9
store i64 0, i64* %14
%15 = load i32, i32* %10
%16 = call i8* @htons(i32 %15)
%17 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %11, i32 0, i32 8
store i8* %16, i8** %17
%18 = call i32 (...) @rand()
%19 = srem i32 %18, 65535
%20 = call i8* @htons(i32 %19)
%21 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %11, i32 0, i32 7
store i8* %20, i8** %21
%22 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %11, i32 0, i32 6
store i64 0, i64* %22
%23 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %11, i32 0, i32 2
store i32 64, i32* %23
%24 = load i32, i32* %9
%25 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %11, i32 0, i32 3
store i32 %24, i32* %25
%26 = load i8*, i8** %7
%27 = call i8* @inet_addr(i8* %26)
%28 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %11, i32 0, i32 5
store i8* %27, i8** %28
%29 = load i8*, i8** %8
%30 = call i8* @inet_addr(i8* %29)
%31 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %11, i32 0, i32 4
store i8* %30, i8** %31
%32 = load i32*, i32** %6
%33 = call i32 @memcpy(i32* %32, %struct.iphdr* %11, i32 64)
ret void
}

LLVM IR (-O2):
%struct.iphdr = type { i32, i32, i32, i32, i8*, i8*, i64, i8*, i8*, i64 }
define void @attach_outter_encapsulation(i32* %0, i8* %1, i8* %2, i32 %3, i32 %4) {
%6 = alloca %struct.iphdr
%7 = bitcast %struct.iphdr* %6 to i8*
%8 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %6, i64 0, i32 0
store i32 5, i32* %8
%9 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %6, i64 0, i32 1
store i32 4, i32* %9
%10 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %6, i64 0, i32 9
store i64 0, i64* %10
%11 = tail call i8* @htons(i32 %4)
%12 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %6, i64 0, i32 8
store i8* %11, i8** %12
%13 = tail call i32 (...) @rand()
%14 = srem i32 %13, 65535
%15 = tail call i8* @htons(i32 %14)
%16 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %6, i64 0, i32 7
store i8* %15, i8** %16
%17 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %6, i64 0, i32 6
store i64 0, i64* %17
%18 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %6, i64 0, i32 2
store i32 64, i32* %18
%19 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %6, i64 0, i32 3
store i32 %3, i32* %19
%20 = tail call i8* @inet_addr(i8* %1)
%21 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %6, i64 0, i32 5
store i8* %20, i8** %21
%22 = tail call i8* @inet_addr(i8* %2)
%23 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %6, i64 0, i32 4
store i8* %22, i8** %23
%24 = call i32 @memcpy(i32* %0, %struct.iphdr* %6, i32 64)
ret void
}