#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 ip {
  int /*<<< orphan*/ ip_dst;
};
struct ether_header {
  int dummy;
};

/* Variables and functions */
scalar_t__ inet_is_ipv4(void *);
char *inet_ntoa(int /*<<< orphan*/);
int /*<<< orphan*/ memcpy(char *, char *, size_t);
size_t strlen(char *);
size_t inet_get_ipv4(void *data, char *ip) {
  struct ip *ip_header;
  char *inet_ip;
  if (inet_is_ipv4(data)) {
    ip_header = (data + sizeof(struct ether_header));
    inet_ip = inet_ntoa(ip_header->ip_dst);
    memcpy(ip, inet_ip, strlen(inet_ip));
    return strlen(inet_ip);
  }
  return -1;
}


x86 assembly (clang, -O0):
<inet_get_ipv4>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <inet_is_ipv4@plt>
cmp    $0x0,%rax
je     11b9 <inet_get_ipv4+0x79>
mov    -0x10(%rbp),%rax
add    $0x4,%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    (%rax),%edi
call   1030 <inet_ntoa@plt>
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x38(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    %rax,%rdx
call   1050 <memcpy@plt>
mov    -0x28(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x8(%rbp)
jmp    11c1 <inet_get_ipv4+0x81>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<inet_get_ipv4>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbx
call   1060 <inet_is_ipv4@plt>
test   %rax,%rax
je     1184 <inet_get_ipv4+0x44>
mov    0x4(%rbx),%edi
call   1030 <inet_ntoa@plt>
mov    %rax,%rbx
mov    %rax,%rdi
call   1040 <strlen@plt>
mov    %r14,%rdi
mov    %rbx,%rsi
mov    %rax,%rdx
call   1050 <memcpy@plt>
mov    %rbx,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1040 <strlen@plt>
mov    $0xffffffffffffffff,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

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

LLVM IR (-O2):
define i64 @inet_get_ipv4(i8* %0, i8* %1) {
%3 = tail call i64 @inet_is_ipv4(i8* %0)
%4 = icmp eq i64 %3, 0
br i1 %4, label %13, label %5
5: ; preds = %2
%6 = getelementptr i8, i8* %0, i64 4
%7 = bitcast i8* %6 to i32*
%8 = load i32, i32* %7
%9 = tail call i8* @inet_ntoa(i32 %8)
%10 = tail call i64 @strlen(i8* dereferenceable(1) %9)
%11 = tail call i32 @memcpy(i8* %1, i8* %9, i64 %10)
%12 = tail call i64 @strlen(i8* dereferenceable(1) %9)
br label %13
13: ; preds = %2, %5
%14 = phi i64 [ %12, %5 ], [ -1, %2 ]
ret i64 %14
}