#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 */
typedef struct TYPE_2__ TYPE_1__;

/* Type definitions */
struct TYPE_2__ {
  int /*<<< orphan*/ s_addr;
};
struct sockaddr_in {
  TYPE_1__ sin_addr;
};
struct iphdr {
  int tot_len;
  int /*<<< orphan*/ daddr;
  int /*<<< orphan*/ saddr;
};
struct ethhdr {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ inet_ntoa(TYPE_1__);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
scalar_t__ strcmp(int /*<<< orphan*/, char *);
int /*<<< orphan*/ strcpy(char *, int /*<<< orphan*/);
int sniff_datagram_ipv4(char *buffer, char *local_address, char *dst_address,
                        unsigned int *packet_size) {
  struct iphdr *iph = (struct iphdr *)(buffer + sizeof(struct ethhdr));
  struct sockaddr_in dst_addr, src_addr;
  memset(&src_addr, 0, sizeof(struct sockaddr_in));
  src_addr.sin_addr.s_addr = iph->saddr;
  if (strcmp(inet_ntoa(src_addr.sin_addr), local_address) == 0) {
    return 0;
  }
  memset(&dst_addr, 0, sizeof(struct sockaddr_in));
  dst_addr.sin_addr.s_addr = iph->daddr;
  strcpy(dst_address, inet_ntoa(dst_addr.sin_addr));
  *packet_size = iph->tot_len >> 8;
  return 1;
}


x86 assembly (clang, -O0):
<sniff_datagram_ipv4>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    -0x10(%rbp),%rax
add    $0x4,%rax
mov    %rax,-0x30(%rbp)
lea    -0x40(%rbp),%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1050 <memset@plt>
mov    -0x30(%rbp),%rax
mov    0x8(%rax),%eax
mov    %eax,-0x40(%rbp)
mov    -0x40(%rbp),%edi
call   1040 <inet_ntoa@plt>
mov    %eax,%edi
mov    -0x18(%rbp),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
jne    11a7 <sniff_datagram_ipv4+0x67>
movl   $0x0,-0x4(%rbp)
jmp    11f2 <sniff_datagram_ipv4+0xb2>
lea    -0x38(%rbp),%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1050 <memset@plt>
mov    -0x30(%rbp),%rax
mov    0x4(%rax),%eax
mov    %eax,-0x38(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x38(%rbp),%edi
call   1040 <inet_ntoa@plt>
mov    -0x48(%rbp),%rdi
mov    %eax,%esi
call   1030 <strcpy@plt>
mov    -0x30(%rbp),%rax
mov    (%rax),%ecx
sar    $0x8,%ecx
mov    -0x28(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sniff_datagram_ipv4>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rcx,%r14
mov    %rdx,%r15
mov    %rsi,%r12
mov    %rdi,%rbx
mov    %rsp,%rdi
xor    %ebp,%ebp
xor    %esi,%esi
mov    $0x4,%edx
call   1050 <memset@plt>
mov    0xc(%rbx),%edi
mov    %edi,(%rsp)
call   1040 <inet_ntoa@plt>
mov    %eax,%edi
mov    %r12,%rsi
call   1060 <strcmp@plt>
test   %rax,%rax
je     11b8 <sniff_datagram_ipv4+0x78>
lea    0x8(%rsp),%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1050 <memset@plt>
mov    0x8(%rbx),%edi
mov    %edi,0x8(%rsp)
call   1040 <inet_ntoa@plt>
mov    %r15,%rdi
mov    %eax,%esi
call   1030 <strcpy@plt>
mov    0x4(%rbx),%eax
sar    $0x8,%eax
mov    %eax,(%r14)
mov    $0x1,%ebp
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.iphdr = type { i32, i32, i32 }
%struct.sockaddr_in = type { %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i32 }
define i32 @sniff_datagram_ipv4(i8* %0, i8* %1, i8* %2, i32* %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i32*
%10 = alloca %struct.iphdr*
%11 = alloca %struct.sockaddr_in
%12 = alloca %struct.sockaddr_in
store i8* %0, i8** %6
store i8* %1, i8** %7
store i8* %2, i8** %8
store i32* %3, i32** %9
%13 = load i8*, i8** %6
%14 = getelementptr inbounds i8, i8* %13, i64 4
%15 = bitcast i8* %14 to %struct.iphdr*
store %struct.iphdr* %15, %struct.iphdr** %10
%16 = call i32 @memset(%struct.sockaddr_in* %12, i32 0, i32 4)
%17 = load %struct.iphdr*, %struct.iphdr** %10
%18 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %17, i32 0, i32 2
%19 = load i32, i32* %18
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %12, i32 0, i32 0
%21 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %20, i32 0, i32 0
store i32 %19, i32* %21
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %12, i32 0, i32 0
%23 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %22, i32 0, i32 0
%24 = load i32, i32* %23
%25 = call i32 @inet_ntoa(i32 %24)
%26 = load i8*, i8** %7
%27 = call i64 @strcmp(i32 %25, i8* %26)
%28 = icmp eq i64 %27, 0
br i1 %28, label %29, label %30
29: ; preds = %4
store i32 0, i32* %5
br label %48
30: ; preds = %4
%31 = call i32 @memset(%struct.sockaddr_in* %11, i32 0, i32 4)
%32 = load %struct.iphdr*, %struct.iphdr** %10
%33 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %32, i32 0, i32 1
%34 = load i32, i32* %33
%35 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %11, i32 0, i32 0
%36 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %35, i32 0, i32 0
store i32 %34, i32* %36
%37 = load i8*, i8** %8
%38 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %11, i32 0, i32 0
%39 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %38, i32 0, i32 0
%40 = load i32, i32* %39
%41 = call i32 @inet_ntoa(i32 %40)
%42 = call i32 @strcpy(i8* %37, i32 %41)
%43 = load %struct.iphdr*, %struct.iphdr** %10
%44 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %43, i32 0, i32 0
%45 = load i32, i32* %44
%46 = ashr i32 %45, 8
%47 = load i32*, i32** %9
store i32 %46, i32* %47
store i32 1, i32* %5
br label %48
48: ; preds = %30, %29
%49 = load i32, i32* %5
ret i32 %49
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i32 }
define i32 @sniff_datagram_ipv4(i8* nocapture %0, i8* %1, i8* %2, i32* nocapture writeonly %3) {
%5 = alloca %struct.sockaddr_in
%6 = alloca %struct.sockaddr_in
%7 = bitcast %struct.sockaddr_in* %5 to i8*
%8 = bitcast %struct.sockaddr_in* %6 to i8*
%9 = call i32 @memset(%struct.sockaddr_in* %6, i32 0, i32 4)
%10 = getelementptr inbounds i8, i8* %0, i64 12
%11 = bitcast i8* %10 to i32*
%12 = load i32, i32* %11
%13 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i64 0, i32 0, i32 0
store i32 %12, i32* %13
%14 = call i32 @inet_ntoa(i32 %12)
%15 = call i64 @strcmp(i32 %14, i8* %1)
%16 = icmp eq i64 %15, 0
br i1 %16, label %29, label %17
17: ; preds = %4
%18 = getelementptr inbounds i8, i8* %0, i64 4
%19 = call i32 @memset(%struct.sockaddr_in* %5, i32 0, i32 4)
%20 = getelementptr inbounds i8, i8* %0, i64 8
%21 = bitcast i8* %20 to i32*
%22 = load i32, i32* %21
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 0, i32 0
store i32 %22, i32* %23
%24 = call i32 @inet_ntoa(i32 %22)
%25 = call i32 @strcpy(i8* %2, i32 %24)
%26 = bitcast i8* %18 to i32*
%27 = load i32, i32* %26
%28 = ashr i32 %27, 8
store i32 %28, i32* %3
br label %29
29: ; preds = %4, %17
%30 = phi i32 [ 1, %17 ], [ 0, %4 ]
ret i32 %30
}