#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 */

/* Variables and functions */
char *append_byte(char *, int);
char *append_bytes(char *, unsigned char *, int);
char *append_short(char *, unsigned short);
unsigned short in_cksum(unsigned short *, int);
int /*<<< orphan*/ memcpy(char *, unsigned short *, int);
char *write_ipv4(char *bufferptr, unsigned short identifier,
                 unsigned char *local_ip, unsigned char *dest_ip) {
  char *start = bufferptr;
  bufferptr = append_byte(bufferptr, 0x45);
  ;
  bufferptr = append_byte(bufferptr, 0);
  ;
  bufferptr = append_short(bufferptr, (64 - (2 * 6 + 2)));
  ;
  bufferptr = append_short(bufferptr, identifier);
  ;
  bufferptr = append_byte(bufferptr, 0x40);
  ;
  bufferptr = append_byte(bufferptr, 0);
  ;
  bufferptr = append_byte(bufferptr, 64);
  ;
  bufferptr = append_byte(bufferptr, 1);
  ;
  char *checksumstart = bufferptr;
  bufferptr = append_short(bufferptr, 0);
  ;
  bufferptr = append_bytes(bufferptr, local_ip, 4);
  ;
  bufferptr = append_bytes(bufferptr, dest_ip, 4);
  ;
  unsigned short checksum = in_cksum((short unsigned int *)start, 20);
  memcpy(checksumstart, &checksum, 2);
  return bufferptr;
}


x86 assembly (clang, -O0):
<write_ipv4>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %si,%ax
mov    %rdi,-0x8(%rbp)
mov    %ax,-0xa(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x8(%rbp),%rdi
mov    $0x45,%esi
call   1060 <append_byte@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
xor    %esi,%esi
call   1060 <append_byte@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
mov    $0x32,%esi
call   1070 <append_short@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
movzwl -0xa(%rbp),%esi
call   1070 <append_short@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
mov    $0x40,%esi
call   1060 <append_byte@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
xor    %esi,%esi
call   1060 <append_byte@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
mov    $0x40,%esi
call   1060 <append_byte@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
mov    $0x1,%esi
call   1060 <append_byte@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x8(%rbp),%rdi
xor    %esi,%esi
call   1070 <append_short@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    $0x4,%edx
call   1040 <append_bytes@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    $0x4,%edx
call   1040 <append_bytes@plt>
mov    %rax,-0x8(%rbp)
mov    -0x28(%rbp),%rdi
mov    $0x14,%esi
call   1030 <in_cksum@plt>
mov    %ax,-0x32(%rbp)
mov    -0x30(%rbp),%rdi
lea    -0x32(%rbp),%rsi
mov    $0x2,%edx
call   1050 <memcpy@plt>
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_ipv4>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rcx,%r14
mov    %rdx,%r15
mov    %esi,%ebx
mov    %rdi,%r12
mov    $0x45,%esi
call   1060 <append_byte@plt>
mov    %rax,%rdi
xor    %esi,%esi
call   1060 <append_byte@plt>
mov    %rax,%rdi
mov    $0x32,%esi
call   1070 <append_short@plt>
mov    %rax,%rdi
mov    %ebx,%esi
call   1070 <append_short@plt>
mov    %rax,%rdi
mov    $0x40,%esi
call   1060 <append_byte@plt>
mov    %rax,%rdi
xor    %esi,%esi
call   1060 <append_byte@plt>
mov    %rax,%rdi
mov    $0x40,%esi
call   1060 <append_byte@plt>
mov    %rax,%rdi
mov    $0x1,%esi
call   1060 <append_byte@plt>
mov    %rax,%rbx
mov    %rax,%rdi
xor    %esi,%esi
call   1070 <append_short@plt>
mov    %rax,%rdi
mov    %r15,%rsi
mov    $0x4,%edx
call   1040 <append_bytes@plt>
mov    %rax,%rdi
mov    %r14,%rsi
mov    $0x4,%edx
call   1040 <append_bytes@plt>
mov    %rax,%r14
mov    %r12,%rdi
mov    $0x14,%esi
call   1030 <in_cksum@plt>
mov    %ax,0x6(%rsp)
lea    0x6(%rsp),%rsi
mov    %rbx,%rdi
mov    $0x2,%edx
call   1050 <memcpy@plt>
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i8* @write_ipv4(i8* %0, i16 zeroext %1, i8* %2, i8* %3) {
%5 = alloca i8*
%6 = alloca i16
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i16
store i8* %0, i8** %5
store i16 %1, i16* %6
store i8* %2, i8** %7
store i8* %3, i8** %8
%12 = load i8*, i8** %5
store i8* %12, i8** %9
%13 = load i8*, i8** %5
%14 = call i8* @append_byte(i8* %13, i32 69)
store i8* %14, i8** %5
%15 = load i8*, i8** %5
%16 = call i8* @append_byte(i8* %15, i32 0)
store i8* %16, i8** %5
%17 = load i8*, i8** %5
%18 = call i8* @append_short(i8* %17, i16 zeroext 50)
store i8* %18, i8** %5
%19 = load i8*, i8** %5
%20 = load i16, i16* %6
%21 = call i8* @append_short(i8* %19, i16 zeroext %20)
store i8* %21, i8** %5
%22 = load i8*, i8** %5
%23 = call i8* @append_byte(i8* %22, i32 64)
store i8* %23, i8** %5
%24 = load i8*, i8** %5
%25 = call i8* @append_byte(i8* %24, i32 0)
store i8* %25, i8** %5
%26 = load i8*, i8** %5
%27 = call i8* @append_byte(i8* %26, i32 64)
store i8* %27, i8** %5
%28 = load i8*, i8** %5
%29 = call i8* @append_byte(i8* %28, i32 1)
store i8* %29, i8** %5
%30 = load i8*, i8** %5
store i8* %30, i8** %10
%31 = load i8*, i8** %5
%32 = call i8* @append_short(i8* %31, i16 zeroext 0)
store i8* %32, i8** %5
%33 = load i8*, i8** %5
%34 = load i8*, i8** %7
%35 = call i8* @append_bytes(i8* %33, i8* %34, i32 4)
store i8* %35, i8** %5
%36 = load i8*, i8** %5
%37 = load i8*, i8** %8
%38 = call i8* @append_bytes(i8* %36, i8* %37, i32 4)
store i8* %38, i8** %5
%39 = load i8*, i8** %9
%40 = bitcast i8* %39 to i16*
%41 = call zeroext i16 @in_cksum(i16* %40, i32 20)
store i16 %41, i16* %11
%42 = load i8*, i8** %10
%43 = call i32 @memcpy(i8* %42, i16* %11, i32 2)
%44 = load i8*, i8** %5
ret i8* %44
}

LLVM IR (-O2):
define i8* @write_ipv4(i8* %0, i16 zeroext %1, i8* %2, i8* %3) {
%5 = alloca i16
%6 = tail call i8* @append_byte(i8* %0, i32 69)
%7 = tail call i8* @append_byte(i8* %6, i32 0)
%8 = tail call i8* @append_short(i8* %7, i16 zeroext 50)
%9 = tail call i8* @append_short(i8* %8, i16 zeroext %1)
%10 = tail call i8* @append_byte(i8* %9, i32 64)
%11 = tail call i8* @append_byte(i8* %10, i32 0)
%12 = tail call i8* @append_byte(i8* %11, i32 64)
%13 = tail call i8* @append_byte(i8* %12, i32 1)
%14 = tail call i8* @append_short(i8* %13, i16 zeroext 0)
%15 = tail call i8* @append_bytes(i8* %14, i8* %2, i32 4)
%16 = tail call i8* @append_bytes(i8* %15, i8* %3, i32 4)
%17 = bitcast i16* %5 to i8*
%18 = bitcast i8* %0 to i16*
%19 = tail call zeroext i16 @in_cksum(i16* %18, i32 20)
store i16 %19, i16* %5
%20 = call i32 @memcpy(i8* %13, i16* %5, i32 2)
ret i8* %16
}