#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 iphdr {
  int protocol;
  int /*<<< orphan*/ tot_len;
  int /*<<< orphan*/ daddr;
  int /*<<< orphan*/ saddr;
};
typedef int /*<<< orphan*/ saddr;
typedef int /*<<< orphan*/ daddr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int INET_ADDRSTRLEN;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ error(int, int /*<<< orphan*/, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, char *, int,
                           int);
int /*<<< orphan*/ inet_ntop(int /*<<< orphan*/, int /*<<< orphan*/ *, char *,
                             int);
int ntohs(int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
void parse_ipv4(struct iphdr *iph) {
  char saddr[INET_ADDRSTRLEN], daddr[INET_ADDRSTRLEN];
  if (!inet_ntop(AF_INET, &iph->saddr, saddr, sizeof(saddr)))
    error(1, errno, "inet_ntop saddr");
  if (!inet_ntop(AF_INET, &iph->daddr, daddr, sizeof(daddr)))
    error(1, errno, "inet_ntop daddr");
  fprintf(stderr, "ip: src=%s dst=%s proto=%u len=%u\n", saddr, daddr,
          iph->protocol, ntohs(iph->tot_len));
}


x86 assembly (clang, -O0):
<parse_ipv4>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    0x2e75(%rip),%rcx
mov    (%rcx),%eax
mov    %rsp,%rdx
mov    %rdx,-0x10(%rbp)
mov    %rax,%rsi
add    $0xf,%rsi
and    $0xfffffffffffffff0,%rsi
mov    %rsp,%rdx
sub    %rsi,%rdx
mov    %rdx,-0x38(%rbp)
mov    %rdx,%rsp
mov    %rax,-0x18(%rbp)
mov    (%rcx),%ecx
mov    %rcx,-0x30(%rbp)
mov    %rcx,%rdi
add    $0xf,%rdi
and    $0xfffffffffffffff0,%rdi
mov    %rsp,%rsi
sub    %rdi,%rsi
mov    %rsi,-0x28(%rbp)
mov    %rsi,%rsp
mov    %rcx,-0x20(%rbp)
mov    0x2e47(%rip),%rcx
mov    (%rcx),%edi
mov    -0x8(%rbp),%rsi
add    $0xc,%rsi
mov    %eax,%ecx
call   1030 <inet_ntop@plt>
cmp    $0x0,%eax
jne    11d5 <parse_ipv4+0x95>
mov    0x2e0e(%rip),%rax
mov    (%rax),%esi
mov    $0x1,%edi
lea    0xe30(%rip),%rdx
call   1060 <error@plt>
mov    -0x28(%rbp),%rdx
mov    -0x30(%rbp),%rax
mov    0x2e04(%rip),%rcx
mov    (%rcx),%edi
mov    -0x8(%rbp),%rsi
add    $0x8,%rsi
mov    %eax,%ecx
call   1030 <inet_ntop@plt>
cmp    $0x0,%eax
jne    1218 <parse_ipv4+0xd8>
mov    0x2dcb(%rip),%rax
mov    (%rax),%esi
mov    $0x1,%edi
lea    0xdfd(%rip),%rdx
call   1060 <error@plt>
mov    0x2db9(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x40(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x8(%rbp),%rax
mov    0x4(%rax),%edi
call   1050 <ntohs@plt>
mov    -0x40(%rbp),%edi
mov    -0x38(%rbp),%rdx
mov    -0x28(%rbp),%rcx
mov    -0x3c(%rbp),%r8d
mov    %eax,%r9d
lea    0xdce(%rip),%rsi
call   1040 <fprintf@plt>
mov    -0x10(%rbp),%rax
mov    %rax,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<parse_ipv4>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r14
mov    0x2e70(%rip),%rax
mov    (%rax),%r12d
mov    %rsp,%r15
mov    %r12,%rax
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r15
mov    %r15,%rsp
mov    %rsp,%rbx
sub    %rax,%rbx
mov    %rbx,%rsp
mov    0x2e69(%rip),%r13
mov    0x0(%r13),%edi
lea    0xc(%r14),%rsi
mov    %r15,%rdx
mov    %r12d,%ecx
call   1030 <inet_ntop@plt>
test   %eax,%eax
jne    11b0 <parse_ipv4+0x70>
mov    0x2e33(%rip),%rax
mov    (%rax),%esi
lea    0xe5a(%rip),%rdx
mov    $0x1,%edi
call   1060 <error@plt>
mov    0x0(%r13),%edi
lea    0x8(%r14),%rsi
mov    %rbx,%rdx
mov    %r12d,%ecx
call   1030 <inet_ntop@plt>
test   %eax,%eax
jne    11e1 <parse_ipv4+0xa1>
mov    0x2e02(%rip),%rax
mov    (%rax),%esi
lea    0xe39(%rip),%rdx
mov    $0x1,%edi
call   1060 <error@plt>
mov    0x2df0(%rip),%rax
mov    (%rax),%r12d
mov    (%r14),%r13d
mov    0x4(%r14),%edi
call   1050 <ntohs@plt>
lea    0xe22(%rip),%rsi
mov    %r12d,%edi
mov    %r15,%rdx
mov    %rbx,%rcx
mov    %r13d,%r8d
mov    %eax,%r9d
call   1040 <fprintf@plt>
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.iphdr = type { i32, i32, i32, i32 }
@INET_ADDRSTRLEN = global i32 0
@AF_INET = global i32 0
@errno = global i32 0
@.str = [16 x i8] c"inet_ntop saddr\00"
@.str.1 = [16 x i8] c"inet_ntop daddr\00"
@stderr = global i32 0
@.str.2 = [35 x i8] c"ip: src=%s dst=%s proto=%u len=%u\0A\00"
define void @parse_ipv4(%struct.iphdr* %0) {
%2 = alloca %struct.iphdr*
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i64
store %struct.iphdr* %0, %struct.iphdr** %2
%6 = load i32, i32* @INET_ADDRSTRLEN
%7 = zext i32 %6 to i64
%8 = call i8* @llvm.stacksave()
store i8* %8, i8** %3
%9 = alloca i8, i64 %7
store i64 %7, i64* %4
%10 = load i32, i32* @INET_ADDRSTRLEN
%11 = zext i32 %10 to i64
%12 = alloca i8, i64 %11
store i64 %11, i64* %5
%13 = load i32, i32* @AF_INET
%14 = load %struct.iphdr*, %struct.iphdr** %2
%15 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %14, i32 0, i32 3
%16 = trunc i64 %7 to i32
%17 = call i32 @inet_ntop(i32 %13, i32* %15, i8* %9, i32 %16)
%18 = icmp ne i32 %17, 0
br i1 %18, label %22, label %19
19: ; preds = %1
%20 = load i32, i32* @errno
%21 = call i32 @error(i32 1, i32 %20, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
br label %22
22: ; preds = %19, %1
%23 = load i32, i32* @AF_INET
%24 = load %struct.iphdr*, %struct.iphdr** %2
%25 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %24, i32 0, i32 2
%26 = trunc i64 %11 to i32
%27 = call i32 @inet_ntop(i32 %23, i32* %25, i8* %12, i32 %26)
%28 = icmp ne i32 %27, 0
br i1 %28, label %32, label %29
29: ; preds = %22
%30 = load i32, i32* @errno
%31 = call i32 @error(i32 1, i32 %30, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
br label %32
32: ; preds = %29, %22
%33 = load i32, i32* @stderr
%34 = load %struct.iphdr*, %struct.iphdr** %2
%35 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %34, i32 0, i32 0
%36 = load i32, i32* %35
%37 = load %struct.iphdr*, %struct.iphdr** %2
%38 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %37, i32 0, i32 1
%39 = load i32, i32* %38
%40 = call i32 @ntohs(i32 %39)
%41 = call i32 @fprintf(i32 %33, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.2, i64 0, i64 0), i8* %9, i8* %12, i32 %36, i32 %40)
%42 = load i8*, i8** %3
ret void
}

LLVM IR (-O2):
%struct.iphdr = type { i32, i32, i32, i32 }
@INET_ADDRSTRLEN = global i32 0
@AF_INET = global i32 0
@errno = global i32 0
@.str = [16 x i8] c"inet_ntop saddr\00"
@.str.1 = [16 x i8] c"inet_ntop daddr\00"
@stderr = global i32 0
@.str.2 = [35 x i8] c"ip: src=%s dst=%s proto=%u len=%u\0A\00"
define void @parse_ipv4(%struct.iphdr* %0) {
%2 = load i32, i32* @INET_ADDRSTRLEN
%3 = zext i32 %2 to i64
%4 = alloca i8, i64 %3
%5 = alloca i8, i64 %3
%6 = load i32, i32* @AF_INET
%7 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %0, i64 0, i32 3
%8 = call i32 @inet_ntop(i32 %6, i32* %7, i8* %4, i32 %2)
%9 = icmp eq i32 %8, 0
br i1 %9, label %10, label %13
10: ; preds = %1
%11 = load i32, i32* @errno
%12 = call i32 @error(i32 1, i32 %11, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
br label %13
13: ; preds = %10, %1
%14 = load i32, i32* @AF_INET
%15 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %0, i64 0, i32 2
%16 = call i32 @inet_ntop(i32 %14, i32* %15, i8* %5, i32 %2)
%17 = icmp eq i32 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %13
%19 = load i32, i32* @errno
%20 = call i32 @error(i32 1, i32 %19, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
br label %21
21: ; preds = %18, %13
%22 = load i32, i32* @stderr
%23 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %0, i64 0, i32 0
%24 = load i32, i32* %23
%25 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %0, i64 0, i32 1
%26 = load i32, i32* %25
%27 = call i32 @ntohs(i32 %26)
%28 = call i32 @fprintf(i32 %22, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.2, i64 0, i64 0), i8* %4, i8* %5, i32 %24, i32 %27)
ret void
}