#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_in6 {
  int /*<<< orphan*/ sin6_addr;
};
struct sockaddr_in {
  int /*<<< orphan*/ sin_addr;
};

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ AF_INET6;
scalar_t__ inet_pton(int /*<<< orphan*/, char *, int /*<<< orphan*/ *);
int is_valid_ip(char *s) {
  struct sockaddr_in sa;
  struct sockaddr_in6 sa6;
  int ret = inet_pton(AF_INET, s, &(sa.sin_addr)) ||
            inet_pton(AF_INET6, s, &(sa6.sin6_addr));
  return ret == 1;
}


x86 assembly (clang, -O0):
<is_valid_ip>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    0x2ec5(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rsi
lea    -0x10(%rbp),%rdx
call   1030 <inet_pton@plt>
mov    %rax,%rcx
mov    $0x1,%al
cmp    $0x0,%rcx
mov    %al,-0x1d(%rbp)
jne    1164 <is_valid_ip+0x54>
mov    0x2e95(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rsi
lea    -0x18(%rbp),%rdx
call   1030 <inet_pton@plt>
cmp    $0x0,%rax
setne  %al
mov    %al,-0x1d(%rbp)
mov    -0x1d(%rbp),%al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x1c(%rbp)
cmpl   $0x1,-0x1c(%rbp)
sete   %al
and    $0x1,%al
movzbl %al,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<is_valid_ip>:
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%rbx
mov    0x2ec9(%rip),%rax
mov    (%rax),%edi
lea    0x8(%rsp),%rdx
mov    %rbx,%rsi
call   1030 <inet_pton@plt>
mov    %rax,%rcx
mov    $0x1,%eax
test   %rcx,%rcx
jne    115a <is_valid_ip+0x4a>
mov    0x2e9e(%rip),%rax
mov    (%rax),%edi
mov    %rsp,%rdx
mov    %rbx,%rsi
call   1030 <inet_pton@plt>
mov    %rax,%rcx
xor    %eax,%eax
test   %rcx,%rcx
setne  %al
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32 }
%struct.sockaddr_in6 = type { i32 }
@AF_INET = global i32 0
@AF_INET6 = global i32 0
define i32 @is_valid_ip(i8* %0) {
%2 = alloca i8*
%3 = alloca %struct.sockaddr_in
%4 = alloca %struct.sockaddr_in6
%5 = alloca i32
store i8* %0, i8** %2
%6 = load i32, i32* @AF_INET
%7 = load i8*, i8** %2
%8 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 0
%9 = call i64 @inet_pton(i32 %6, i8* %7, i32* %8)
%10 = icmp ne i64 %9, 0
br i1 %10, label %17, label %11
11: ; preds = %1
%12 = load i32, i32* @AF_INET6
%13 = load i8*, i8** %2
%14 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %4, i32 0, i32 0
%15 = call i64 @inet_pton(i32 %12, i8* %13, i32* %14)
%16 = icmp ne i64 %15, 0
br label %17
17: ; preds = %11, %1
%18 = phi i1 [ true, %1 ], [ %16, %11 ]
%19 = zext i1 %18 to i32
store i32 %19, i32* %5
%20 = load i32, i32* %5
%21 = icmp eq i32 %20, 1
%22 = zext i1 %21 to i32
ret i32 %22
}

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