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

/* Variables and functions */
void *AF_INET;
void *AF_INET6;
int inet_pton(void *, char const *, int /*<<< orphan*/ *);
int /*<<< orphan*/ memcpy(int /*<<< orphan*/ *, int /*<<< orphan*/ *, int);
int strtosockaddr(const char *src, void *addrptr) {
  int ret;
  struct sockaddr_storage *storage = (struct sockaddr_storage *)addrptr;
  struct sockaddr_in addr4;
  ret = inet_pton(AF_INET, src, &(addr4.sin_addr));
  if (ret > 0) {
    storage->ss_family = AF_INET;
    struct sockaddr_in *addr = (struct sockaddr_in *)addrptr;
    memcpy(&addr->sin_addr, &addr4.sin_addr, sizeof(addr4.sin_addr));
    return ret;
  }
  struct sockaddr_in6 addr6;
  ret = inet_pton(AF_INET6, src, &(addr6.sin6_addr));
  if (ret > 0) {
    storage->ss_family = AF_INET6;
    struct sockaddr_in6 *addr = (struct sockaddr_in6 *)addrptr;
    memcpy(&addr->sin6_addr, &addr6.sin6_addr, sizeof(addr6.sin6_addr));
    return ret;
  }
  return -1;
}


x86 assembly (clang, -O0):
<strtosockaddr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    0x2ea9(%rip),%rax
mov    (%rax),%rdi
mov    -0x10(%rbp),%rsi
lea    -0x30(%rbp),%rdx
call   1040 <inet_pton@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jle    1192 <strtosockaddr+0x72>
mov    0x2e85(%rip),%rax
mov    (%rax),%rcx
mov    -0x28(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x18(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rdi
lea    -0x30(%rbp),%rsi
mov    $0x4,%edx
call   1030 <memcpy@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11f3 <strtosockaddr+0xd3>
mov    0x2e47(%rip),%rax
mov    (%rax),%rdi
mov    -0x10(%rbp),%rsi
lea    -0x40(%rbp),%rdx
call   1040 <inet_pton@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jle    11ec <strtosockaddr+0xcc>
mov    0x2e23(%rip),%rax
mov    (%rax),%rcx
mov    -0x28(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x18(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x48(%rbp),%rdi
lea    -0x40(%rbp),%rsi
mov    $0x4,%edx
call   1030 <memcpy@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11f3 <strtosockaddr+0xd3>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strtosockaddr>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%r14
mov    %rdi,%rbx
mov    0x2eb1(%rip),%r15
mov    (%r15),%rdi
lea    0x10(%rsp),%rdx
mov    %rbx,%rsi
call   1040 <inet_pton@plt>
test   %eax,%eax
jle    115a <strtosockaddr+0x3a>
mov    %eax,%ebp
mov    (%r15),%rax
mov    %rax,(%r14)
lea    0x10(%rsp),%rsi
jmp    1182 <strtosockaddr+0x62>
mov    0x2e7f(%rip),%r15
mov    (%r15),%rdi
lea    0x8(%rsp),%rdx
mov    %rbx,%rsi
call   1040 <inet_pton@plt>
test   %eax,%eax
jle    119c <strtosockaddr+0x7c>
mov    %eax,%ebp
mov    (%r15),%rax
mov    %rax,(%r14)
lea    0x8(%rsp),%rsi
mov    %r14,%rdi
mov    $0x4,%edx
call   1030 <memcpy@plt>
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    $0xffffffff,%ebp
jmp    118f <strtosockaddr+0x6f>

LLVM IR (-O0):
%struct.sockaddr_storage = type { i8* }
%struct.sockaddr_in = type { i32 }
%struct.sockaddr_in6 = type { i32 }
@AF_INET = global i8* null
@AF_INET6 = global i8* null
define i32 @strtosockaddr(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca %struct.sockaddr_storage*
%8 = alloca %struct.sockaddr_in
%9 = alloca %struct.sockaddr_in*
%10 = alloca %struct.sockaddr_in6
%11 = alloca %struct.sockaddr_in6*
store i8* %0, i8** %4
store i8* %1, i8** %5
%12 = load i8*, i8** %5
%13 = bitcast i8* %12 to %struct.sockaddr_storage*
store %struct.sockaddr_storage* %13, %struct.sockaddr_storage** %7
%14 = load i8*, i8** @AF_INET
%15 = load i8*, i8** %4
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 0
%17 = call i32 @inet_pton(i8* %14, i8* %15, i32* %16)
store i32 %17, i32* %6
%18 = load i32, i32* %6
%19 = icmp sgt i32 %18, 0
br i1 %19, label %20, label %31
20: ; preds = %2
%21 = load i8*, i8** @AF_INET
%22 = load %struct.sockaddr_storage*, %struct.sockaddr_storage** %7
%23 = getelementptr inbounds %struct.sockaddr_storage, %struct.sockaddr_storage* %22, i32 0, i32 0
store i8* %21, i8** %23
%24 = load i8*, i8** %5
%25 = bitcast i8* %24 to %struct.sockaddr_in*
store %struct.sockaddr_in* %25, %struct.sockaddr_in** %9
%26 = load %struct.sockaddr_in*, %struct.sockaddr_in** %9
%27 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %26, i32 0, i32 0
%28 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 0
%29 = call i32 @memcpy(i32* %27, i32* %28, i32 4)
%30 = load i32, i32* %6
store i32 %30, i32* %3
br label %50
31: ; preds = %2
%32 = load i8*, i8** @AF_INET6
%33 = load i8*, i8** %4
%34 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %10, i32 0, i32 0
%35 = call i32 @inet_pton(i8* %32, i8* %33, i32* %34)
store i32 %35, i32* %6
%36 = load i32, i32* %6
%37 = icmp sgt i32 %36, 0
br i1 %37, label %38, label %49
38: ; preds = %31
%39 = load i8*, i8** @AF_INET6
%40 = load %struct.sockaddr_storage*, %struct.sockaddr_storage** %7
%41 = getelementptr inbounds %struct.sockaddr_storage, %struct.sockaddr_storage* %40, i32 0, i32 0
store i8* %39, i8** %41
%42 = load i8*, i8** %5
%43 = bitcast i8* %42 to %struct.sockaddr_in6*
store %struct.sockaddr_in6* %43, %struct.sockaddr_in6** %11
%44 = load %struct.sockaddr_in6*, %struct.sockaddr_in6** %11
%45 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %44, i32 0, i32 0
%46 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %10, i32 0, i32 0
%47 = call i32 @memcpy(i32* %45, i32* %46, i32 4)
%48 = load i32, i32* %6
store i32 %48, i32* %3
br label %50
49: ; preds = %31
store i32 -1, i32* %3
br label %50
50: ; preds = %49, %38, %20
%51 = load i32, i32* %3
ret i32 %51
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32 }
%struct.sockaddr_in6 = type { i32 }
@AF_INET = global i8* null
@AF_INET6 = global i8* null
define i32 @strtosockaddr(i8* %0, i8* %1) {
%3 = alloca %struct.sockaddr_in
%4 = alloca %struct.sockaddr_in6
%5 = bitcast %struct.sockaddr_in* %3 to i8*
%6 = load i8*, i8** @AF_INET
%7 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
%8 = call i32 @inet_pton(i8* %6, i8* %0, i32* %7)
%9 = icmp sgt i32 %8, 0
br i1 %9, label %10, label %15
10: ; preds = %2
%11 = load i8*, i8** @AF_INET
%12 = bitcast i8* %1 to i8**
store i8* %11, i8** %12
%13 = bitcast i8* %1 to i32*
%14 = call i32 @memcpy(i32* %13, i32* %7, i32 4)
br label %28
15: ; preds = %2
%16 = bitcast %struct.sockaddr_in6* %4 to i8*
%17 = load i8*, i8** @AF_INET6
%18 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %4, i64 0, i32 0
%19 = call i32 @inet_pton(i8* %17, i8* %0, i32* %18)
%20 = icmp sgt i32 %19, 0
br i1 %20, label %21, label %26
21: ; preds = %15
%22 = load i8*, i8** @AF_INET6
%23 = bitcast i8* %1 to i8**
store i8* %22, i8** %23
%24 = bitcast i8* %1 to i32*
%25 = call i32 @memcpy(i32* %24, i32* %18, i32 4)
br label %26
26: ; preds = %15, %21
%27 = phi i32 [ %19, %21 ], [ -1, %15 ]
br label %28
28: ; preds = %26, %10
%29 = phi i32 [ %8, %10 ], [ %27, %26 ]
ret i32 %29
}