#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 tcphdr {
  int doff;
  void *window;
  void *dest;
  void *source;
};

/* Variables and functions */
void *htons(int);
int /*<<< orphan*/ memset(int /*<<< orphan*/ *, int /*<<< orphan*/, size_t);
int /*<<< orphan*/ tcp_make_mask(int /*<<< orphan*/, int /*<<< orphan*/,
                                 int /*<<< orphan*/, int /*<<< orphan*/,
                                 int /*<<< orphan*/, int /*<<< orphan*/,
                                 int /*<<< orphan*/, int /*<<< orphan*/);
size_t tcp_write_default_header(uint8_t *tcp_segment) {
  struct tcphdr *tcp_header = (struct tcphdr *)tcp_segment;
  size_t size = 5 << 2;
  if (tcp_segment) {
    memset(tcp_segment, 0, size);
    tcp_header->doff = 5;
    tcp_header->source = htons(2222);
    tcp_header->dest = htons(3333);
    tcp_header->window = htons(5840);
    ((uint8_t *)tcp_header)[13] = tcp_make_mask(0, 0, 0, 0, 0, 0, 0, 0);
  }
  return size;
}


x86 assembly (clang, -O0):
<tcp_write_default_header>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x10(%rbp)
movq   $0x14,-0x18(%rbp)
cmpq   $0x0,-0x8(%rbp)
je     11de <tcp_write_default_header+0xae>
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%rdx
xor    %esi,%esi
call   1050 <memset@plt>
mov    -0x10(%rbp),%rax
movl   $0x5,(%rax)
mov    $0x8ae,%edi
call   1040 <htons@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,0x18(%rax)
mov    $0xd05,%edi
call   1040 <htons@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,0x10(%rax)
mov    $0x16d0,%edi
call   1040 <htons@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,0x8(%rax)
xor    %r9d,%r9d
mov    %r9d,%edi
mov    %r9d,%esi
mov    %r9d,%edx
mov    %r9d,%ecx
mov    %r9d,%r8d
movl   $0x0,(%rsp)
movl   $0x0,0x8(%rsp)
call   1030 <tcp_make_mask@plt>
mov    %eax,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,0x34(%rax)
mov    -0x18(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tcp_write_default_header>:
test   %rdi,%rdi
je     1194 <tcp_write_default_header+0x64>
push   %rbx
mov    %rdi,%rbx
mov    $0x14,%edx
xor    %esi,%esi
call   1050 <memset@plt>
movl   $0x5,(%rbx)
mov    $0x8ae,%edi
call   1040 <htons@plt>
mov    %rax,0x18(%rbx)
mov    $0xd05,%edi
call   1040 <htons@plt>
mov    %rax,0x10(%rbx)
mov    $0x16d0,%edi
call   1040 <htons@plt>
mov    %rax,0x8(%rbx)
xor    %edi,%edi
xor    %esi,%esi
xor    %edx,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
xor    %r9d,%r9d
push   $0x0
push   $0x0
call   1030 <tcp_make_mask@plt>
add    $0x10,%rsp
mov    %eax,0x34(%rbx)
pop    %rbx
mov    $0x14,%eax
ret

LLVM IR (-O0):
%struct.tcphdr = type { i32, i8*, i8*, i8* }
define i64 @tcp_write_default_header(i32* %0) {
%2 = alloca i32*
%3 = alloca %struct.tcphdr*
%4 = alloca i64
store i32* %0, i32** %2
%5 = load i32*, i32** %2
%6 = bitcast i32* %5 to %struct.tcphdr*
store %struct.tcphdr* %6, %struct.tcphdr** %3
store i64 20, i64* %4
%7 = load i32*, i32** %2
%8 = icmp ne i32* %7, null
br i1 %8, label %9, label %28
9: ; preds = %1
%10 = load i32*, i32** %2
%11 = load i64, i64* %4
%12 = call i32 @memset(i32* %10, i32 0, i64 %11)
%13 = load %struct.tcphdr*, %struct.tcphdr** %3
%14 = getelementptr inbounds %struct.tcphdr, %struct.tcphdr* %13, i32 0, i32 0
store i32 5, i32* %14
%15 = call i8* @htons(i32 2222)
%16 = load %struct.tcphdr*, %struct.tcphdr** %3
%17 = getelementptr inbounds %struct.tcphdr, %struct.tcphdr* %16, i32 0, i32 3
store i8* %15, i8** %17
%18 = call i8* @htons(i32 3333)
%19 = load %struct.tcphdr*, %struct.tcphdr** %3
%20 = getelementptr inbounds %struct.tcphdr, %struct.tcphdr* %19, i32 0, i32 2
store i8* %18, i8** %20
%21 = call i8* @htons(i32 5840)
%22 = load %struct.tcphdr*, %struct.tcphdr** %3
%23 = getelementptr inbounds %struct.tcphdr, %struct.tcphdr* %22, i32 0, i32 1
store i8* %21, i8** %23
%24 = call i32 @tcp_make_mask(i32 0, i32 0, i32 0, i32 0, i32 0, i32 0, i32 0, i32 0)
%25 = load %struct.tcphdr*, %struct.tcphdr** %3
%26 = bitcast %struct.tcphdr* %25 to i32*
%27 = getelementptr inbounds i32, i32* %26, i64 13
store i32 %24, i32* %27
br label %28
28: ; preds = %9, %1
%29 = load i64, i64* %4
ret i64 %29
}

LLVM IR (-O2):
define i64 @tcp_write_default_header(i32* %0) {
%2 = icmp eq i32* %0, null
br i1 %2, label %16, label %3
3: ; preds = %1
%4 = tail call i32 @memset(i32* %0, i32 0, i64 20)
store i32 5, i32* %0
%5 = tail call i8* @htons(i32 2222)
%6 = getelementptr inbounds i32, i32* %0, i64 6
%7 = bitcast i32* %6 to i8**
store i8* %5, i8** %7
%8 = tail call i8* @htons(i32 3333)
%9 = getelementptr inbounds i32, i32* %0, i64 4
%10 = bitcast i32* %9 to i8**
store i8* %8, i8** %10
%11 = tail call i8* @htons(i32 5840)
%12 = getelementptr inbounds i32, i32* %0, i64 2
%13 = bitcast i32* %12 to i8**
store i8* %11, i8** %13
%14 = tail call i32 @tcp_make_mask(i32 0, i32 0, i32 0, i32 0, i32 0, i32 0, i32 0, i32 0)
%15 = getelementptr inbounds i32, i32* %0, i64 13
store i32 %14, i32* %15
br label %16
16: ; preds = %3, %1
ret i64 20
}