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

/* Variables and functions */
int IPVERSION;
int /*<<< orphan*/ htonl(int);
void *htons(int);
struct tcphdr *malloc(int);
int /*<<< orphan*/ perror(char *);
struct tcphdr *tcp_header_init(int seq, int win_size, uint16_t s, uint16_t d) {
  struct tcphdr *header = malloc(sizeof(struct tcphdr));
  if (header == NULL) {
    perror("malloc() failure: ");
    return NULL;
  }
  header->source = htons(s);
  header->dest = htons(d);
  header->seq = htonl(seq);
  header->ack_seq = 0;
  header->window = htons(win_size);
  header->urg_ptr = 0;
  header->res1 = 0;
  header->doff = sizeof(struct tcphdr) / IPVERSION;
  header->syn = 1;
  header->check = 0;
  return header;
}


x86 assembly (clang, -O0):
<tcp_header_init>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0xc(%rbp)
mov    %esi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
mov    $0x48,%edi
call   1050 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1186 <tcp_header_init+0x46>
lea    0xe8c(%rip),%rdi
call   1060 <perror@plt>
movq   $0x0,-0x8(%rbp)
jmp    122f <tcp_header_init+0xef>
mov    -0x14(%rbp),%edi
call   1030 <htons@plt>
mov    %rax,%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,0x40(%rax)
mov    -0x18(%rbp),%edi
call   1030 <htons@plt>
mov    %rax,%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,0x38(%rax)
mov    -0xc(%rbp),%edi
call   1040 <htonl@plt>
mov    %eax,%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,0x30(%rax)
mov    -0x20(%rbp),%rax
movq   $0x0,0x28(%rax)
mov    -0x10(%rbp),%edi
call   1030 <htons@plt>
mov    %rax,%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,0x20(%rax)
mov    -0x20(%rbp),%rax
movq   $0x0,0x18(%rax)
mov    -0x20(%rbp),%rax
movq   $0x0,0x10(%rax)
mov    0x2ded(%rip),%rax
movslq (%rax),%rcx
mov    $0x48,%eax
xor    %edx,%edx
div    %rcx
mov    %eax,%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x20(%rbp),%rax
movl   $0x1,0x4(%rax)
mov    -0x20(%rbp),%rax
movq   $0x0,0x8(%rax)
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tcp_header_init>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %ecx,%r12d
mov    %edx,%ebp
mov    %esi,%r14d
mov    %edi,%r15d
mov    $0x48,%edi
call   1050 <malloc@plt>
mov    %rax,%rbx
test   %rax,%rax
je     11dc <tcp_header_init+0x9c>
mov    %ebp,%edi
call   1030 <htons@plt>
mov    %rax,0x40(%rbx)
mov    %r12d,%edi
call   1030 <htons@plt>
mov    %rax,0x38(%rbx)
mov    %r15d,%edi
call   1040 <htonl@plt>
mov    %eax,0x30(%rbx)
movq   $0x0,0x28(%rbx)
mov    %r14d,%edi
call   1030 <htons@plt>
mov    %rax,0x20(%rbx)
xorps  %xmm0,%xmm0
movups %xmm0,0x10(%rbx)
mov    0x2e3f(%rip),%rax
movslq (%rax),%rcx
cmp    $0x48,%rcx
ja     11bd <tcp_header_init+0x7d>
mov    $0x48,%eax
xor    %edx,%edx
div    %ecx
jmp    11bf <tcp_header_init+0x7f>
xor    %eax,%eax
mov    %eax,(%rbx)
movl   $0x1,0x4(%rbx)
movq   $0x0,0x8(%rbx)
mov    %rbx,%rax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe1d(%rip),%rdi
call   1060 <perror@plt>
jmp    11d0 <tcp_header_init+0x90>

LLVM IR (-O0):
%struct.tcphdr = type { i32, i32, i64, i64, i64, i8*, i64, i32, i8*, i8* }
@.str = [19 x i8] c"malloc() failure: \00"
@IPVERSION = global i32 0
define %struct.tcphdr* @tcp_header_init(i32 %0, i32 %1, i32 %2, i32 %3) {
%5 = alloca %struct.tcphdr*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca %struct.tcphdr*
store i32 %0, i32* %6
store i32 %1, i32* %7
store i32 %2, i32* %8
store i32 %3, i32* %9
%11 = call %struct.tcphdr* @malloc(i32 72)
store %struct.tcphdr* %11, %struct.tcphdr** %10
%12 = load %struct.tcphdr*, %struct.tcphdr** %10
%13 = icmp eq %struct.tcphdr* %12, null
br i1 %13, label %14, label %16
14: ; preds = %4
%15 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
store %struct.tcphdr* null, %struct.tcphdr** %5
br label %50
16: ; preds = %4
%17 = load i32, i32* %8
%18 = call i8* @htons(i32 %17)
%19 = load %struct.tcphdr*, %struct.tcphdr** %10
%20 = getelementptr inbounds %struct.tcphdr, %struct.tcphdr* %19, i32 0, i32 9
store i8* %18, i8** %20
%21 = load i32, i32* %9
%22 = call i8* @htons(i32 %21)
%23 = load %struct.tcphdr*, %struct.tcphdr** %10
%24 = getelementptr inbounds %struct.tcphdr, %struct.tcphdr* %23, i32 0, i32 8
store i8* %22, i8** %24
%25 = load i32, i32* %6
%26 = call i32 @htonl(i32 %25)
%27 = load %struct.tcphdr*, %struct.tcphdr** %10
%28 = getelementptr inbounds %struct.tcphdr, %struct.tcphdr* %27, i32 0, i32 7
store i32 %26, i32* %28
%29 = load %struct.tcphdr*, %struct.tcphdr** %10
%30 = getelementptr inbounds %struct.tcphdr, %struct.tcphdr* %29, i32 0, i32 6
store i64 0, i64* %30
%31 = load i32, i32* %7
%32 = call i8* @htons(i32 %31)
%33 = load %struct.tcphdr*, %struct.tcphdr** %10
%34 = getelementptr inbounds %struct.tcphdr, %struct.tcphdr* %33, i32 0, i32 5
store i8* %32, i8** %34
%35 = load %struct.tcphdr*, %struct.tcphdr** %10
%36 = getelementptr inbounds %struct.tcphdr, %struct.tcphdr* %35, i32 0, i32 4
store i64 0, i64* %36
%37 = load %struct.tcphdr*, %struct.tcphdr** %10
%38 = getelementptr inbounds %struct.tcphdr, %struct.tcphdr* %37, i32 0, i32 3
store i64 0, i64* %38
%39 = load i32, i32* @IPVERSION
%40 = sext i32 %39 to i64
%41 = udiv i64 72, %40
%42 = trunc i64 %41 to i32
%43 = load %struct.tcphdr*, %struct.tcphdr** %10
%44 = getelementptr inbounds %struct.tcphdr, %struct.tcphdr* %43, i32 0, i32 0
store i32 %42, i32* %44
%45 = load %struct.tcphdr*, %struct.tcphdr** %10
%46 = getelementptr inbounds %struct.tcphdr, %struct.tcphdr* %45, i32 0, i32 1
store i32 1, i32* %46
%47 = load %struct.tcphdr*, %struct.tcphdr** %10
%48 = getelementptr inbounds %struct.tcphdr, %struct.tcphdr* %47, i32 0, i32 2
store i64 0, i64* %48
%49 = load %struct.tcphdr*, %struct.tcphdr** %10
store %struct.tcphdr* %49, %struct.tcphdr** %5
br label %50
50: ; preds = %16, %14
%51 = load %struct.tcphdr*, %struct.tcphdr** %5
ret %struct.tcphdr* %51
}

LLVM IR (-O2):
%struct.tcphdr = type { i32, i32, i64, i64, i64, i8*, i64, i32, i8*, i8* }
@.str = [19 x i8] c"malloc() failure: \00"
@IPVERSION = global i32 0
define noalias %struct.tcphdr* @tcp_header_init(i32 %0, i32 %1, i32 %2, i32 %3) {
%5 = tail call %struct.tcphdr* @malloc(i32 72)
%6 = icmp eq %struct.tcphdr* %5, null
br i1 %6, label %7, label %9
7: ; preds = %4
%8 = tail call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
br label %28
9: ; preds = %4
%10 = tail call i8* @htons(i32 %2)
%11 = getelementptr inbounds %struct.tcphdr, %struct.tcphdr* %5, i64 0, i32 9
store i8* %10, i8** %11
%12 = tail call i8* @htons(i32 %3)
%13 = getelementptr inbounds %struct.tcphdr, %struct.tcphdr* %5, i64 0, i32 8
store i8* %12, i8** %13
%14 = tail call i32 @htonl(i32 %0)
%15 = getelementptr inbounds %struct.tcphdr, %struct.tcphdr* %5, i64 0, i32 7
store i32 %14, i32* %15
%16 = getelementptr inbounds %struct.tcphdr, %struct.tcphdr* %5, i64 0, i32 6
store i64 0, i64* %16
%17 = tail call i8* @htons(i32 %1)
%18 = getelementptr inbounds %struct.tcphdr, %struct.tcphdr* %5, i64 0, i32 5
store i8* %17, i8** %18
%19 = getelementptr inbounds %struct.tcphdr, %struct.tcphdr* %5, i64 0, i32 3
%20 = bitcast i64* %19 to i8*
%21 = load i32, i32* @IPVERSION
%22 = sext i32 %21 to i64
%23 = udiv i64 72, %22
%24 = trunc i64 %23 to i32
%25 = getelementptr inbounds %struct.tcphdr, %struct.tcphdr* %5, i64 0, i32 0
store i32 %24, i32* %25
%26 = getelementptr inbounds %struct.tcphdr, %struct.tcphdr* %5, i64 0, i32 1
store i32 1, i32* %26
%27 = getelementptr inbounds %struct.tcphdr, %struct.tcphdr* %5, i64 0, i32 2
store i64 0, i64* %27
br label %28
28: ; preds = %9, %7
ret %struct.tcphdr* %5
}