#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 tcphdr {
  int ack;
  int fin;
  int doff;
  int /*<<< orphan*/ check;
  scalar_t__ urg_ptr;
  void *window;
  void *ack_seq;
  void *seq;
  void *dest;
  void *source;
};

/* Variables and functions */
int TCP_MAXWIN;
void *htons(int);
int /*<<< orphan*/ memset(struct tcphdr *, int /*<<< orphan*/, int);
void *ntohl(int);
int /*<<< orphan*/ tcp_checksum(struct tcphdr *, int);
void fill_transportlayer(void *buf, int seq_offset, int ack_offset,
                         int payload_len, int fin) {
  struct tcphdr *tcph = buf;
  memset(tcph, 0, sizeof(*tcph));
  tcph->source = htons(1500);
  tcph->dest = htons(8000);
  tcph->seq = ntohl(100 + seq_offset);
  tcph->ack_seq = ntohl(100 + ack_offset);
  tcph->ack = 1;
  tcph->fin = fin;
  tcph->doff = 5;
  tcph->window = htons(TCP_MAXWIN);
  tcph->urg_ptr = 0;
  tcph->check = tcp_checksum(tcph, payload_len);
}


x86 assembly (clang, -O0):
<fill_transportlayer>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    %ecx,-0x14(%rbp)
mov    %r8d,-0x18(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x40,%edx
call   1040 <memset@plt>
mov    $0x5dc,%edi
call   1030 <htons@plt>
mov    %rax,%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,0x38(%rax)
mov    $0x1f40,%edi
call   1030 <htons@plt>
mov    %rax,%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,0x30(%rax)
mov    -0xc(%rbp),%edi
add    $0x64,%edi
call   1060 <ntohl@plt>
mov    %rax,%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,0x28(%rax)
mov    -0x10(%rbp),%edi
add    $0x64,%edi
call   1060 <ntohl@plt>
mov    %rax,%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,0x20(%rax)
mov    -0x20(%rbp),%rax
movl   $0x1,(%rax)
mov    -0x18(%rbp),%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,0x4(%rax)
mov    -0x20(%rbp),%rax
movl   $0x5,0x8(%rax)
mov    0x2dfb(%rip),%rax
mov    (%rax),%edi
call   1030 <htons@plt>
mov    %rax,%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,0x18(%rax)
mov    -0x20(%rbp),%rax
movq   $0x0,0x10(%rax)
mov    -0x20(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1050 <tcp_checksum@plt>
mov    %eax,%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,0xc(%rax)
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fill_transportlayer>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %r8d,%r15d
mov    %ecx,%r14d
mov    %edx,%r12d
mov    %esi,%ebp
mov    %rdi,%rbx
xor    %esi,%esi
mov    $0x40,%edx
call   1040 <memset@plt>
mov    $0x5dc,%edi
call   1030 <htons@plt>
mov    %rax,0x38(%rbx)
mov    $0x1f40,%edi
call   1030 <htons@plt>
mov    %rax,0x30(%rbx)
add    $0x64,%ebp
mov    %ebp,%edi
call   1060 <ntohl@plt>
mov    %rax,0x28(%rbx)
add    $0x64,%r12d
mov    %r12d,%edi
call   1060 <ntohl@plt>
mov    %rax,0x20(%rbx)
movl   $0x1,(%rbx)
mov    %r15d,0x4(%rbx)
movl   $0x5,0x8(%rbx)
mov    0x2e34(%rip),%rax
mov    (%rax),%edi
call   1030 <htons@plt>
mov    %rax,0x18(%rbx)
movq   $0x0,0x10(%rbx)
mov    %rbx,%rdi
mov    %r14d,%esi
call   1050 <tcp_checksum@plt>
mov    %eax,0xc(%rbx)
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.tcphdr = type { i32, i32, i32, i32, i64, i8*, i8*, i8*, i8*, i8* }
@TCP_MAXWIN = global i32 0
define void @fill_transportlayer(i8* %0, i32 %1, i32 %2, i32 %3, i32 %4) {
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca %struct.tcphdr*
store i8* %0, i8** %6
store i32 %1, i32* %7
store i32 %2, i32* %8
store i32 %3, i32* %9
store i32 %4, i32* %10
%12 = load i8*, i8** %6
%13 = bitcast i8* %12 to %struct.tcphdr*
store %struct.tcphdr* %13, %struct.tcphdr** %11
%14 = load %struct.tcphdr*, %struct.tcphdr** %11
%15 = call i32 @memset(%struct.tcphdr* %14, i32 0, i32 64)
%16 = call i8* @htons(i32 1500)
%17 = load %struct.tcphdr*, %struct.tcphdr** %11
%18 = getelementptr inbounds %struct.tcphdr, %struct.tcphdr* %17, i32 0, i32 9
store i8* %16, i8** %18
%19 = call i8* @htons(i32 8000)
%20 = load %struct.tcphdr*, %struct.tcphdr** %11
%21 = getelementptr inbounds %struct.tcphdr, %struct.tcphdr* %20, i32 0, i32 8
store i8* %19, i8** %21
%22 = load i32, i32* %7
%23 = add nsw i32 100, %22
%24 = call i8* @ntohl(i32 %23)
%25 = load %struct.tcphdr*, %struct.tcphdr** %11
%26 = getelementptr inbounds %struct.tcphdr, %struct.tcphdr* %25, i32 0, i32 7
store i8* %24, i8** %26
%27 = load i32, i32* %8
%28 = add nsw i32 100, %27
%29 = call i8* @ntohl(i32 %28)
%30 = load %struct.tcphdr*, %struct.tcphdr** %11
%31 = getelementptr inbounds %struct.tcphdr, %struct.tcphdr* %30, i32 0, i32 6
store i8* %29, i8** %31
%32 = load %struct.tcphdr*, %struct.tcphdr** %11
%33 = getelementptr inbounds %struct.tcphdr, %struct.tcphdr* %32, i32 0, i32 0
store i32 1, i32* %33
%34 = load i32, i32* %10
%35 = load %struct.tcphdr*, %struct.tcphdr** %11
%36 = getelementptr inbounds %struct.tcphdr, %struct.tcphdr* %35, i32 0, i32 1
store i32 %34, i32* %36
%37 = load %struct.tcphdr*, %struct.tcphdr** %11
%38 = getelementptr inbounds %struct.tcphdr, %struct.tcphdr* %37, i32 0, i32 2
store i32 5, i32* %38
%39 = load i32, i32* @TCP_MAXWIN
%40 = call i8* @htons(i32 %39)
%41 = load %struct.tcphdr*, %struct.tcphdr** %11
%42 = getelementptr inbounds %struct.tcphdr, %struct.tcphdr* %41, i32 0, i32 5
store i8* %40, i8** %42
%43 = load %struct.tcphdr*, %struct.tcphdr** %11
%44 = getelementptr inbounds %struct.tcphdr, %struct.tcphdr* %43, i32 0, i32 4
store i64 0, i64* %44
%45 = load %struct.tcphdr*, %struct.tcphdr** %11
%46 = load i32, i32* %9
%47 = call i32 @tcp_checksum(%struct.tcphdr* %45, i32 %46)
%48 = load %struct.tcphdr*, %struct.tcphdr** %11
%49 = getelementptr inbounds %struct.tcphdr, %struct.tcphdr* %48, i32 0, i32 3
store i32 %47, i32* %49
ret void
}

LLVM IR (-O2):
%struct.tcphdr = type { i32, i32, i32, i32, i64, i8*, i8*, i8*, i8*, i8* }
@TCP_MAXWIN = global i32 0
define void @fill_transportlayer(i8* %0, i32 %1, i32 %2, i32 %3, i32 %4) {
%6 = bitcast i8* %0 to %struct.tcphdr*
%7 = tail call i32 @memset(%struct.tcphdr* %6, i32 0, i32 64)
%8 = tail call i8* @htons(i32 1500)
%9 = getelementptr inbounds i8, i8* %0, i64 56
%10 = bitcast i8* %9 to i8**
store i8* %8, i8** %10
%11 = tail call i8* @htons(i32 8000)
%12 = getelementptr inbounds i8, i8* %0, i64 48
%13 = bitcast i8* %12 to i8**
store i8* %11, i8** %13
%14 = add nsw i32 %1, 100
%15 = tail call i8* @ntohl(i32 %14)
%16 = getelementptr inbounds i8, i8* %0, i64 40
%17 = bitcast i8* %16 to i8**
store i8* %15, i8** %17
%18 = add nsw i32 %2, 100
%19 = tail call i8* @ntohl(i32 %18)
%20 = getelementptr inbounds i8, i8* %0, i64 32
%21 = bitcast i8* %20 to i8**
store i8* %19, i8** %21
%22 = bitcast i8* %0 to i32*
store i32 1, i32* %22
%23 = getelementptr inbounds i8, i8* %0, i64 4
%24 = bitcast i8* %23 to i32*
store i32 %4, i32* %24
%25 = getelementptr inbounds i8, i8* %0, i64 8
%26 = bitcast i8* %25 to i32*
store i32 5, i32* %26
%27 = load i32, i32* @TCP_MAXWIN
%28 = tail call i8* @htons(i32 %27)
%29 = getelementptr inbounds i8, i8* %0, i64 24
%30 = bitcast i8* %29 to i8**
store i8* %28, i8** %30
%31 = getelementptr inbounds i8, i8* %0, i64 16
%32 = bitcast i8* %31 to i64*
store i64 0, i64* %32
%33 = tail call i32 @tcp_checksum(%struct.tcphdr* %6, i32 %3)
%34 = getelementptr inbounds i8, i8* %0, i64 12
%35 = bitcast i8* %34 to i32*
store i32 %33, i32* %35
ret void
}