#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;
};

/* Variables and functions */
int AF_INET;
int AF_INET6;
int inet_pton(int, char *, int /*<<< orphan*/ *);
int isValidIpv6Address(char *);
int isValidIpv4Address(char *ipAddress) {
  struct sockaddr_in sa;
  int result = inet_pton(AF_INET, ipAddress, &(sa.sin_addr));
  if (result == 0) {
    if (isValidIpv6Address(ipAddress) == 1)
      return AF_INET6;
    else
      return 0;
  }
  if (result == 1)
    return AF_INET;
  else
    return result;
}


x86 assembly (clang, -O0):
<isValidIpv4Address>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2eb5(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rsi
lea    -0x18(%rbp),%rdx
call   1040 <inet_pton@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jne    117e <isValidIpv4Address+0x5e>
mov    -0x10(%rbp),%rdi
call   1030 <isValidIpv6Address@plt>
cmp    $0x1,%eax
jne    1172 <isValidIpv4Address+0x52>
mov    0x2e78(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    119f <isValidIpv4Address+0x7f>
movl   $0x0,-0x4(%rbp)
jmp    119f <isValidIpv4Address+0x7f>
cmpl   $0x1,-0x1c(%rbp)
jne    1199 <isValidIpv4Address+0x79>
mov    0x2e59(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    119f <isValidIpv4Address+0x7f>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<isValidIpv4Address>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbx
mov    0x2eba(%rip),%r14
mov    (%r14),%edi
mov    %rsp,%rdx
mov    %rbx,%rsi
call   1040 <inet_pton@plt>
cmp    $0x1,%eax
je     1161 <isValidIpv4Address+0x41>
test   %eax,%eax
jne    1164 <isValidIpv4Address+0x44>
mov    %rbx,%rdi
call   1030 <isValidIpv6Address@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    $0x1,%ecx
jne    1164 <isValidIpv4Address+0x44>
mov    0x2e83(%rip),%rax
mov    (%rax),%eax
jmp    1164 <isValidIpv4Address+0x44>
mov    (%r14),%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32 }
@AF_INET = global i32 0
@AF_INET6 = global i32 0
define i32 @isValidIpv4Address(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca %struct.sockaddr_in
%5 = alloca i32
store i8* %0, i8** %3
%6 = load i32, i32* @AF_INET
%7 = load i8*, i8** %3
%8 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 0
%9 = call i32 @inet_pton(i32 %6, i8* %7, i32* %8)
store i32 %9, i32* %5
%10 = load i32, i32* %5
%11 = icmp eq i32 %10, 0
br i1 %11, label %12, label %19
12: ; preds = %1
%13 = load i8*, i8** %3
%14 = call i32 @isValidIpv6Address(i8* %13)
%15 = icmp eq i32 %14, 1
br i1 %15, label %16, label %18
16: ; preds = %12
%17 = load i32, i32* @AF_INET6
store i32 %17, i32* %2
br label %26
18: ; preds = %12
store i32 0, i32* %2
br label %26
19: ; preds = %1
%20 = load i32, i32* %5
%21 = icmp eq i32 %20, 1
br i1 %21, label %22, label %24
22: ; preds = %19
%23 = load i32, i32* @AF_INET
store i32 %23, i32* %2
br label %26
24: ; preds = %19
%25 = load i32, i32* %5
store i32 %25, i32* %2
br label %26
26: ; preds = %24, %22, %18, %16
%27 = load i32, i32* %2
ret i32 %27
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32 }
@AF_INET = global i32 0
@AF_INET6 = global i32 0
define i32 @isValidIpv4Address(i8* %0) {
%2 = alloca %struct.sockaddr_in
%3 = bitcast %struct.sockaddr_in* %2 to i8*
%4 = load i32, i32* @AF_INET
%5 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 0
%6 = call i32 @inet_pton(i32 %4, i8* %0, i32* %5)
switch i32 %6, label %14 [
i32 0, label %7
i32 1, label %12
]
7: ; preds = %1
%8 = call i32 @isValidIpv6Address(i8* %0)
%9 = icmp eq i32 %8, 1
%10 = load i32, i32* @AF_INET6
%11 = select i1 %9, i32 %10, i32 0
br label %14
12: ; preds = %1
%13 = load i32, i32* @AF_INET
br label %14
14: ; preds = %7, %1, %12
%15 = phi i32 [ %13, %12 ], [ %6, %1 ], [ %11, %7 ]
ret i32 %15
}