#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 sockaddr_in {
  int /*<<< orphan*/ sin_addr;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ saddr;

/* Variables and functions */
int /*<<< orphan*/ assert(int);
int if2addr(char const *, struct sockaddr *, int);
int inet_aton(char const *, int /*<<< orphan*/ *);
char const *inet_ntoa(int /*<<< orphan*/);
int /*<<< orphan*/ strncpy(char *, char const *, size_t);
int get_ipv4_address(const char *s, char *buf, size_t len) {
  struct sockaddr_in saddr;
  int rc = 0;
  assert(s && buf && len);
  if (1 == inet_aton(s, &(saddr.sin_addr))) {
    (void)strncpy(buf, s, len);
  } else {
    rc = if2addr(s, (struct sockaddr *)&saddr, sizeof(saddr));
    if (0 != rc)
      return rc;
    (void)strncpy(buf, inet_ntoa(saddr.sin_addr), len);
  }
  buf[len - 1] = 0;
  return rc;
}


x86 assembly (clang, -O0):
<get_ipv4_address>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movl   $0x0,-0x2c(%rbp)
xor    %eax,%eax
cmpq   $0x0,-0x10(%rbp)
mov    %al,-0x2d(%rbp)
je     1196 <get_ipv4_address+0x46>
xor    %eax,%eax
cmpq   $0x0,-0x18(%rbp)
mov    %al,-0x2d(%rbp)
je     1196 <get_ipv4_address+0x46>
cmpq   $0x0,-0x20(%rbp)
setne  %al
mov    %al,-0x2d(%rbp)
mov    -0x2d(%rbp),%al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
mov    -0x10(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   1070 <inet_aton@plt>
mov    %eax,%ecx
mov    $0x1,%eax
cmp    %ecx,%eax
jne    11d5 <get_ipv4_address+0x85>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1030 <strncpy@plt>
jmp    1220 <get_ipv4_address+0xd0>
mov    -0x10(%rbp),%rdi
lea    -0x28(%rbp),%rsi
mov    $0x4,%edx
call   1060 <if2addr@plt>
mov    %eax,-0x2c(%rbp)
xor    %eax,%eax
cmp    -0x2c(%rbp),%eax
je     1200 <get_ipv4_address+0xb0>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1236 <get_ipv4_address+0xe6>
mov    -0x18(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x28(%rbp),%edi
call   1040 <inet_ntoa@plt>
mov    -0x38(%rbp),%rdi
mov    %rax,%rsi
mov    -0x20(%rbp),%rdx
call   1030 <strncpy@plt>
mov    -0x18(%rbp),%rax
mov    -0x20(%rbp),%rcx
sub    $0x1,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_ipv4_address>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%rbx
test   %rdi,%rdi
setne  %al
test   %rsi,%rsi
setne  %cl
and    %al,%cl
test   %rdx,%rdx
setne  %al
and    %cl,%al
movzbl %al,%edi
call   1050 <assert@plt>
lea    0x8(%rsp),%rsi
mov    %rbx,%rdi
call   1070 <inet_aton@plt>
cmp    $0x1,%eax
je     11b4 <get_ipv4_address+0x64>
lea    0x8(%rsp),%rsi
mov    %rbx,%rdi
mov    $0x4,%edx
call   1060 <if2addr@plt>
test   %eax,%eax
jne    11ca <get_ipv4_address+0x7a>
mov    0x8(%rsp),%edi
call   1040 <inet_ntoa@plt>
mov    %rax,%rbx
mov    %r15,%rdi
mov    %rbx,%rsi
mov    %r14,%rdx
call   1030 <strncpy@plt>
movb   $0x0,-0x1(%r14,%r15,1)
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32 }
%struct.sockaddr = type { i32 }
define i32 @get_ipv4_address(i8* %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca %struct.sockaddr_in
%9 = alloca i32
store i8* %0, i8** %5
store i8* %1, i8** %6
store i64 %2, i64* %7
store i32 0, i32* %9
%10 = load i8*, i8** %5
%11 = icmp ne i8* %10, null
br i1 %11, label %12, label %18
12: ; preds = %3
%13 = load i8*, i8** %6
%14 = icmp ne i8* %13, null
br i1 %14, label %15, label %18
15: ; preds = %12
%16 = load i64, i64* %7
%17 = icmp ne i64 %16, 0
br label %18
18: ; preds = %15, %12, %3
%19 = phi i1 [ false, %12 ], [ false, %3 ], [ %17, %15 ]
%20 = zext i1 %19 to i32
%21 = call i32 @assert(i32 %20)
%22 = load i8*, i8** %5
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 0
%24 = call i32 @inet_aton(i8* %22, i32* %23)
%25 = icmp eq i32 1, %24
br i1 %25, label %26, label %31
26: ; preds = %18
%27 = load i8*, i8** %6
%28 = load i8*, i8** %5
%29 = load i64, i64* %7
%30 = call i32 @strncpy(i8* %27, i8* %28, i64 %29)
br label %46
31: ; preds = %18
%32 = load i8*, i8** %5
%33 = bitcast %struct.sockaddr_in* %8 to %struct.sockaddr*
%34 = call i32 @if2addr(i8* %32, %struct.sockaddr* %33, i32 4)
store i32 %34, i32* %9
%35 = load i32, i32* %9
%36 = icmp ne i32 0, %35
br i1 %36, label %37, label %39
37: ; preds = %31
%38 = load i32, i32* %9
store i32 %38, i32* %4
br label %52
39: ; preds = %31
%40 = load i8*, i8** %6
%41 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 0
%42 = load i32, i32* %41
%43 = call i8* @inet_ntoa(i32 %42)
%44 = load i64, i64* %7
%45 = call i32 @strncpy(i8* %40, i8* %43, i64 %44)
br label %46
46: ; preds = %39, %26
%47 = load i8*, i8** %6
%48 = load i64, i64* %7
%49 = sub i64 %48, 1
%50 = getelementptr inbounds i8, i8* %47, i64 %49
store i8 0, i8* %50
%51 = load i32, i32* %9
store i32 %51, i32* %4
br label %52
52: ; preds = %46, %37
%53 = load i32, i32* %4
ret i32 %53
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32 }
%struct.sockaddr = type { i32 }
define i32 @get_ipv4_address(i8* %0, i8* %1, i64 %2) {
%4 = alloca %struct.sockaddr_in
%5 = bitcast %struct.sockaddr_in* %4 to i8*
%6 = icmp ne i8* %0, null
%7 = icmp ne i8* %1, null
%8 = and i1 %6, %7
%9 = icmp ne i64 %2, 0
%10 = and i1 %8, %9
%11 = zext i1 %10 to i32
%12 = tail call i32 @assert(i32 %11)
%13 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0
%14 = call i32 @inet_aton(i8* %0, i32* %13)
%15 = icmp eq i32 %14, 1
br i1 %15, label %23, label %16
16: ; preds = %3
%17 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%18 = call i32 @if2addr(i8* %0, %struct.sockaddr* %17, i32 4)
%19 = icmp eq i32 %18, 0
br i1 %19, label %20, label %28
20: ; preds = %16
%21 = load i32, i32* %13
%22 = call i8* @inet_ntoa(i32 %21)
br label %23
23: ; preds = %3, %20
%24 = phi i8* [ %22, %20 ], [ %0, %3 ]
%25 = call i32 @strncpy(i8* %1, i8* %24, i64 %2)
%26 = add i64 %2, -1
%27 = getelementptr inbounds i8, i8* %1, i64 %26
store i8 0, i8* %27
br label %28
28: ; preds = %16, %23
%29 = phi i32 [ 0, %23 ], [ %18, %16 ]
ret i32 %29
}