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

/* Variables and functions */
int /*<<< orphan*/ IPPROTO_TCP;
int IPVERSION;
void *htons(int);
void *inet_addr(char const *);
struct iphdr *malloc(int);
int /*<<< orphan*/ perror(char *);
struct iphdr *ip_header_init(int id, int ttl, const char *s, const char *d) {
  struct iphdr *header = malloc(sizeof(struct iphdr));
  if (header == NULL) {
    perror("malloc() failure: ");
    return NULL;
  }
  header->version = IPVERSION;
  header->ihl = sizeof(struct iphdr) / IPVERSION;
  header->tos = 0;
  header->tot_len = htons(sizeof(struct iphdr) + sizeof(struct tcphdr) + 1024);
  header->id = htons(id);
  header->frag_off = 0;
  header->ttl = ttl;
  header->protocol = IPPROTO_TCP;
  header->saddr = inet_addr(s);
  header->daddr = inet_addr(d);
  header->check = 0;
  return header;
}


x86 assembly (clang, -O0):
<ip_header_init>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0xc(%rbp)
mov    %esi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    $0x50,%edi
call   1050 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1188 <ip_header_init+0x48>
lea    0xe8a(%rip),%rdi
call   1060 <perror@plt>
movq   $0x0,-0x8(%rbp)
jmp    124a <ip_header_init+0x10a>
mov    0x2e59(%rip),%rax
mov    (%rax),%ecx
mov    -0x28(%rbp),%rax
mov    %ecx,(%rax)
mov    0x2e4a(%rip),%rax
movslq (%rax),%rcx
mov    $0x50,%eax
xor    %edx,%edx
div    %rcx
mov    %eax,%ecx
mov    -0x28(%rbp),%rax
mov    %ecx,0x4(%rax)
mov    -0x28(%rbp),%rax
movq   $0x0,0x48(%rax)
mov    $0x454,%edi
call   1030 <htons@plt>
mov    %rax,%rcx
mov    -0x28(%rbp),%rax
mov    %rcx,0x40(%rax)
mov    -0xc(%rbp),%edi
call   1030 <htons@plt>
mov    %rax,%rcx
mov    -0x28(%rbp),%rax
mov    %rcx,0x38(%rax)
mov    -0x28(%rbp),%rax
movq   $0x0,0x30(%rax)
mov    -0x10(%rbp),%ecx
mov    -0x28(%rbp),%rax
mov    %ecx,0x8(%rax)
mov    0x2ddb(%rip),%rax
mov    (%rax),%ecx
mov    -0x28(%rbp),%rax
mov    %ecx,0x28(%rax)
mov    -0x18(%rbp),%rdi
call   1040 <inet_addr@plt>
mov    %rax,%rcx
mov    -0x28(%rbp),%rax
mov    %rcx,0x20(%rax)
mov    -0x20(%rbp),%rdi
call   1040 <inet_addr@plt>
mov    %rax,%rcx
mov    -0x28(%rbp),%rax
mov    %rcx,0x18(%rax)
mov    -0x28(%rbp),%rax
movq   $0x0,0x10(%rax)
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ip_header_init>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rcx,%r14
mov    %rdx,%r15
mov    %esi,%r12d
mov    %edi,%ebp
mov    $0x50,%edi
call   1050 <malloc@plt>
mov    %rax,%rbx
test   %rax,%rax
je     11f0 <ip_header_init+0xb0>
mov    0x2e78(%rip),%rax
movslq (%rax),%rcx
mov    %ecx,(%rbx)
cmp    $0x50,%rcx
ja     1186 <ip_header_init+0x46>
mov    $0x50,%eax
xor    %edx,%edx
div    %ecx
jmp    1188 <ip_header_init+0x48>
xor    %eax,%eax
mov    %eax,0x4(%rbx)
movq   $0x0,0x48(%rbx)
mov    $0x454,%edi
call   1030 <htons@plt>
mov    %rax,0x40(%rbx)
mov    %ebp,%edi
call   1030 <htons@plt>
mov    %rax,0x38(%rbx)
movq   $0x0,0x30(%rbx)
mov    %r12d,0x8(%rbx)
mov    0x2e21(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x28(%rbx)
mov    %r15,%rdi
call   1040 <inet_addr@plt>
mov    %rax,0x20(%rbx)
mov    %r14,%rdi
call   1040 <inet_addr@plt>
mov    %rax,0x18(%rbx)
movq   $0x0,0x10(%rbx)
mov    %rbx,%rax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe09(%rip),%rdi
call   1060 <perror@plt>
jmp    11e4 <ip_header_init+0xa4>

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

LLVM IR (-O2):
%struct.iphdr = type { i32, i32, i32, i64, i8*, i8*, i32, i64, i8*, i8*, i64 }
@.str = [19 x i8] c"malloc() failure: \00"
@IPVERSION = global i32 0
@IPPROTO_TCP = global i32 0
define noalias %struct.iphdr* @ip_header_init(i32 %0, i32 %1, i8* %2, i8* %3) {
%5 = tail call %struct.iphdr* @malloc(i32 80)
%6 = icmp eq %struct.iphdr* %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 %30
9: ; preds = %4
%10 = load i32, i32* @IPVERSION
%11 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %5, i64 0, i32 0
store i32 %10, i32* %11
%12 = sext i32 %10 to i64
%13 = udiv i64 80, %12
%14 = trunc i64 %13 to i32
%15 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %5, i64 0, i32 1
store i32 %14, i32* %15
%16 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %5, i64 0, i32 10
store i64 0, i64* %16
%17 = tail call i8* @htons(i32 1108)
%18 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %5, i64 0, i32 9
store i8* %17, i8** %18
%19 = tail call i8* @htons(i32 %0)
%20 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %5, i64 0, i32 8
store i8* %19, i8** %20
%21 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %5, i64 0, i32 7
store i64 0, i64* %21
%22 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %5, i64 0, i32 2
store i32 %1, i32* %22
%23 = load i32, i32* @IPPROTO_TCP
%24 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %5, i64 0, i32 6
store i32 %23, i32* %24
%25 = tail call i8* @inet_addr(i8* %2)
%26 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %5, i64 0, i32 5
store i8* %25, i8** %26
%27 = tail call i8* @inet_addr(i8* %3)
%28 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %5, i64 0, i32 4
store i8* %27, i8** %28
%29 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %5, i64 0, i32 3
store i64 0, i64* %29
br label %30
30: ; preds = %9, %7
ret %struct.iphdr* %5
}