#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;
};
struct sockaddr {
  int sa_family;
};
typedef int /*<<< orphan*/ socklen_t;

/* Variables and functions */
#define AF_INET 129
#define AF_INET6 128
int /*<<< orphan*/ INET6_ADDRSTRLEN;
int /*<<< orphan*/ INET_ADDRSTRLEN;
char *inet_ntop(int const, int /*<<< orphan*/ *, char *, int /*<<< orphan*/);
int sockutil_ntop(struct sockaddr *a, char *dst, socklen_t size) {
  const char *in_s = NULL;
  switch (a->sa_family) {
  case AF_INET: {
    struct sockaddr_in *ain = (struct sockaddr_in *)a;
    if (size < INET_ADDRSTRLEN) {
      return -1;
    }
    in_s = inet_ntop(AF_INET, &ain->sin_addr, dst, size);
    break;
  }
  case AF_INET6: {
    struct sockaddr_in6 *ain6 = (struct sockaddr_in6 *)a;
    if (size < INET6_ADDRSTRLEN) {
      return -1;
    }
    in_s = inet_ntop(AF_INET6, &ain6->sin6_addr, dst, size);
    break;
  }
  default:
    return -1;
  }
  return in_s == NULL ? -1 : 0;
}


x86 assembly (clang, -O0):
<sockutil_ntop>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
sub    $0x80,%eax
je     119b <sockutil_ntop+0x8b>
jmp    1144 <sockutil_ntop+0x34>
mov    -0x3c(%rbp),%eax
sub    $0x81,%eax
jne    11df <sockutil_ntop+0xcf>
jmp    1157 <sockutil_ntop+0x47>
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x1c(%rbp),%eax
mov    0x2e6f(%rip),%rcx
cmp    (%rcx),%eax
jge    117d <sockutil_ntop+0x6d>
movl   $0xffffffff,-0x4(%rbp)
jmp    1200 <sockutil_ntop+0xf0>
mov    -0x30(%rbp),%rsi
mov    -0x18(%rbp),%rdx
mov    -0x1c(%rbp),%ecx
mov    $0x81,%edi
call   1030 <inet_ntop@plt>
mov    %rax,-0x28(%rbp)
jmp    11eb <sockutil_ntop+0xdb>
mov    -0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x1c(%rbp),%eax
mov    0x2e3b(%rip),%rcx
cmp    (%rcx),%eax
jge    11c1 <sockutil_ntop+0xb1>
movl   $0xffffffff,-0x4(%rbp)
jmp    1200 <sockutil_ntop+0xf0>
mov    -0x38(%rbp),%rsi
mov    -0x18(%rbp),%rdx
mov    -0x1c(%rbp),%ecx
mov    $0x80,%edi
call   1030 <inet_ntop@plt>
mov    %rax,-0x28(%rbp)
jmp    11eb <sockutil_ntop+0xdb>
movl   $0xffffffff,-0x4(%rbp)
jmp    1200 <sockutil_ntop+0xf0>
mov    -0x28(%rbp),%rdx
xor    %eax,%eax
mov    $0xffffffff,%ecx
cmp    $0x0,%rdx
cmove  %ecx,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sockutil_ntop>:
push   %rax
mov    %edx,%ecx
mov    %rsi,%rdx
mov    %rdi,%rsi
mov    (%rdi),%edi
mov    $0xffffffff,%eax
cmp    $0x80,%edi
je     1143 <sockutil_ntop+0x33>
cmp    $0x81,%edi
jne    115f <sockutil_ntop+0x4f>
mov    0x2ea1(%rip),%r8
mov    $0x81,%edi
cmp    %ecx,(%r8)
jle    1154 <sockutil_ntop+0x44>
jmp    115f <sockutil_ntop+0x4f>
mov    0x2e9e(%rip),%r8
mov    $0x80,%edi
cmp    %ecx,(%r8)
jg     115f <sockutil_ntop+0x4f>
call   1030 <inet_ntop@plt>
cmp    $0x1,%rax
sbb    %eax,%eax
pop    %rcx
ret

LLVM IR (-O0):
%struct.sockaddr = type { i32 }
%struct.sockaddr_in = type { i32 }
%struct.sockaddr_in6 = type { i32 }
@INET_ADDRSTRLEN = global i32 0
@INET6_ADDRSTRLEN = global i32 0
define i32 @sockutil_ntop(%struct.sockaddr* %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca %struct.sockaddr*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i8*
%9 = alloca %struct.sockaddr_in*
%10 = alloca %struct.sockaddr_in6*
store %struct.sockaddr* %0, %struct.sockaddr** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
store i8* null, i8** %8
%11 = load %struct.sockaddr*, %struct.sockaddr** %5
%12 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %11, i32 0, i32 0
%13 = load i32, i32* %12
switch i32 %13, label %40 [
i32 129, label %14
i32 128, label %27
]
14: ; preds = %3
%15 = load %struct.sockaddr*, %struct.sockaddr** %5
%16 = bitcast %struct.sockaddr* %15 to %struct.sockaddr_in*
store %struct.sockaddr_in* %16, %struct.sockaddr_in** %9
%17 = load i32, i32* %7
%18 = load i32, i32* @INET_ADDRSTRLEN
%19 = icmp slt i32 %17, %18
br i1 %19, label %20, label %21
20: ; preds = %14
store i32 -1, i32* %4
br label %46
21: ; preds = %14
%22 = load %struct.sockaddr_in*, %struct.sockaddr_in** %9
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %22, i32 0, i32 0
%24 = load i8*, i8** %6
%25 = load i32, i32* %7
%26 = call i8* @inet_ntop(i32 129, i32* %23, i8* %24, i32 %25)
store i8* %26, i8** %8
br label %41
27: ; preds = %3
%28 = load %struct.sockaddr*, %struct.sockaddr** %5
%29 = bitcast %struct.sockaddr* %28 to %struct.sockaddr_in6*
store %struct.sockaddr_in6* %29, %struct.sockaddr_in6** %10
%30 = load i32, i32* %7
%31 = load i32, i32* @INET6_ADDRSTRLEN
%32 = icmp slt i32 %30, %31
br i1 %32, label %33, label %34
33: ; preds = %27
store i32 -1, i32* %4
br label %46
34: ; preds = %27
%35 = load %struct.sockaddr_in6*, %struct.sockaddr_in6** %10
%36 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %35, i32 0, i32 0
%37 = load i8*, i8** %6
%38 = load i32, i32* %7
%39 = call i8* @inet_ntop(i32 128, i32* %36, i8* %37, i32 %38)
store i8* %39, i8** %8
br label %41
40: ; preds = %3
store i32 -1, i32* %4
br label %46
41: ; preds = %34, %21
%42 = load i8*, i8** %8
%43 = icmp eq i8* %42, null
%44 = zext i1 %43 to i64
%45 = select i1 %43, i32 -1, i32 0
store i32 %45, i32* %4
br label %46
46: ; preds = %41, %40, %33, %20
%47 = load i32, i32* %4
ret i32 %47
}

LLVM IR (-O2):
%struct.sockaddr = type { i32 }
@INET_ADDRSTRLEN = global i32 0
@INET6_ADDRSTRLEN = global i32 0
define i32 @sockutil_ntop(%struct.sockaddr* %0, i8* %1, i32 %2) {
%4 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %0, i64 0, i32 0
%5 = load i32, i32* %4
switch i32 %5, label %17 [
i32 129, label %6
i32 128, label %9
]
6: ; preds = %3
%7 = load i32, i32* @INET_ADDRSTRLEN
%8 = icmp sgt i32 %7, %2
br i1 %8, label %17, label %12
9: ; preds = %3
%10 = load i32, i32* @INET6_ADDRSTRLEN
%11 = icmp sgt i32 %10, %2
br i1 %11, label %17, label %12
12: ; preds = %9, %6
%13 = phi i32 [ 129, %6 ], [ 128, %9 ]
%14 = tail call i8* @inet_ntop(i32 %13, i32* %4, i8* %1, i32 %2)
%15 = icmp eq i8* %14, null
%16 = sext i1 %15 to i32
br label %17
17: ; preds = %9, %6, %3, %12
%18 = phi i32 [ %16, %12 ], [ -1, %3 ], [ -1, %6 ], [ -1, %9 ]
ret i32 %18
}