#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 */
typedef int /*<<< orphan*/ uint16_t;
struct sockaddr_in6 {
  int /*<<< orphan*/ sin6_addr;
  int /*<<< orphan*/ sin6_port;
};
struct sockaddr_in {
  int /*<<< orphan*/ sin_addr;
  int /*<<< orphan*/ sin_port;
};
struct sockaddr {
  scalar_t__ sa_family;
};

/* Variables and functions */
scalar_t__ AF_INET;
scalar_t__ AF_INET6;
char *evutil_inet_ntop(scalar_t__, void const *, char *, int);
int /*<<< orphan*/ ntohs(int /*<<< orphan*/);
int snprintf(char *, int, char *, char *, int /*<<< orphan*/);
int snip_sockaddr_to_string(char *buffer, struct sockaddr *address) {
  char target_string[54];
  const char *ntop_result;
  uint16_t port;
  if (address->sa_family == AF_INET) {
    struct sockaddr_in *sin = (struct sockaddr_in *)address;
    port = ntohs(sin->sin_port);
    ntop_result = evutil_inet_ntop(
        address->sa_family, (const void *)&(sin->sin_addr), target_string, 54);
  } else if (address->sa_family == AF_INET6) {
    struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)address;
    port = ntohs(((struct sockaddr_in6 *)address)->sin6_port);
    ntop_result =
        evutil_inet_ntop(address->sa_family, (const void *)&(sin6->sin6_addr),
                         target_string, 54);
  } else {
    return -1;
  }
  if (!ntop_result) {
    return -1;
  }
  int string_result = snprintf(
      buffer, 54, address->sa_family == AF_INET6 ? "[%s]:%hu" : "%s:%hu",
      target_string, port);
  if (string_result <= 0 || (string_result + 1) > 54) {
    return -1;
  }
  return string_result;
}


x86 assembly (clang, -O0):
<snip_sockaddr_to_string>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
mov    0x2e97(%rip),%rcx
cmp    (%rcx),%rax
jne    1193 <snip_sockaddr_to_string+0x63>
mov    -0x18(%rbp),%rax
mov    %rax,-0x68(%rbp)
mov    -0x68(%rbp),%rax
mov    0x4(%rax),%edi
call   1050 <ntohs@plt>
mov    %eax,-0x5c(%rbp)
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
mov    -0x68(%rbp),%rsi
lea    -0x50(%rbp),%rdx
mov    $0x36,%ecx
call   1030 <evutil_inet_ntop@plt>
mov    %rax,-0x58(%rbp)
jmp    11f4 <snip_sockaddr_to_string+0xc4>
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
mov    0x2e3f(%rip),%rcx
cmp    (%rcx),%rax
jne    11e3 <snip_sockaddr_to_string+0xb3>
mov    -0x18(%rbp),%rax
mov    %rax,-0x70(%rbp)
mov    -0x18(%rbp),%rax
mov    0x4(%rax),%edi
call   1050 <ntohs@plt>
mov    %eax,-0x5c(%rbp)
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
mov    -0x70(%rbp),%rsi
lea    -0x50(%rbp),%rdx
mov    $0x36,%ecx
call   1030 <evutil_inet_ntop@plt>
mov    %rax,-0x58(%rbp)
jmp    11ef <snip_sockaddr_to_string+0xbf>
movl   $0xffffffff,-0x4(%rbp)
jmp    1275 <snip_sockaddr_to_string+0x145>
jmp    11f4 <snip_sockaddr_to_string+0xc4>
cmpq   $0x0,-0x58(%rbp)
jne    120b <snip_sockaddr_to_string+0xdb>
movl   $0xffffffff,-0x4(%rbp)
jmp    1275 <snip_sockaddr_to_string+0x145>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rax
mov    (%rax),%rcx
mov    0x2dc3(%rip),%rax
mov    (%rax),%rsi
lea    0xde2(%rip),%rdx
lea    0xdd2(%rip),%rax
cmp    %rsi,%rcx
cmove  %rax,%rdx
lea    -0x50(%rbp),%rcx
mov    -0x5c(%rbp),%r8d
mov    $0x36,%esi
call   1040 <snprintf@plt>
mov    %eax,-0x74(%rbp)
cmpl   $0x0,-0x74(%rbp)
jle    1263 <snip_sockaddr_to_string+0x133>
mov    -0x74(%rbp),%eax
add    $0x1,%eax
cmp    $0x36,%eax
jle    126f <snip_sockaddr_to_string+0x13f>
movl   $0xffffffff,-0x4(%rbp)
jmp    1275 <snip_sockaddr_to_string+0x145>
mov    -0x74(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<snip_sockaddr_to_string>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x40,%rsp
mov    %rsi,%rbx
mov    %rdi,%r14
mov    (%rsi),%rdi
mov    0x2e9c(%rip),%rax
mov    0x2e8d(%rip),%r12
mov    $0xffffffff,%ebp
cmp    (%rax),%rdi
je     1163 <snip_sockaddr_to_string+0x33>
cmp    (%r12),%rdi
jne    11c1 <snip_sockaddr_to_string+0x91>
shr    $0x20,%rdi
call   1050 <ntohs@plt>
mov    %eax,%r15d
mov    (%rbx),%rdi
mov    %rsp,%rdx
mov    %rbx,%rsi
mov    $0x36,%ecx
call   1030 <evutil_inet_ntop@plt>
test   %rax,%rax
je     11c1 <snip_sockaddr_to_string+0x91>
mov    (%rbx),%rax
cmp    (%r12),%rax
lea    0xe6b(%rip),%rax
lea    0xe6d(%rip),%rdx
cmove  %rax,%rdx
mov    %rsp,%rcx
mov    %r14,%rdi
mov    $0x36,%esi
mov    %r15d,%r8d
call   1040 <snprintf@plt>
lea    -0x36(%rax),%ecx
cmp    $0xffffffcb,%ecx
mov    $0xffffffff,%ebp
cmovae %eax,%ebp
mov    %ebp,%eax
add    $0x40,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr = type { i64 }
%struct.sockaddr_in = type { i32, i32 }
%struct.sockaddr_in6 = type { i32, i32 }
@AF_INET = global i64 0
@AF_INET6 = global i64 0
@.str = [9 x i8] c"[%s]:%hu\00"
@.str.1 = [7 x i8] c"%s:%hu\00"
define i32 @snip_sockaddr_to_string(i8* %0, %struct.sockaddr* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca %struct.sockaddr*
%6 = alloca [54 x i8]
%7 = alloca i8*
%8 = alloca i32
%9 = alloca %struct.sockaddr_in*
%10 = alloca %struct.sockaddr_in6*
%11 = alloca i32
store i8* %0, i8** %4
store %struct.sockaddr* %1, %struct.sockaddr** %5
%12 = load %struct.sockaddr*, %struct.sockaddr** %5
%13 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %12, i32 0, i32 0
%14 = load i64, i64* %13
%15 = load i64, i64* @AF_INET
%16 = icmp eq i64 %14, %15
br i1 %16, label %17, label %32
17: ; preds = %2
%18 = load %struct.sockaddr*, %struct.sockaddr** %5
%19 = bitcast %struct.sockaddr* %18 to %struct.sockaddr_in*
store %struct.sockaddr_in* %19, %struct.sockaddr_in** %9
%20 = load %struct.sockaddr_in*, %struct.sockaddr_in** %9
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %20, i32 0, i32 1
%22 = load i32, i32* %21
%23 = call i32 @ntohs(i32 %22)
store i32 %23, i32* %8
%24 = load %struct.sockaddr*, %struct.sockaddr** %5
%25 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %24, i32 0, i32 0
%26 = load i64, i64* %25
%27 = load %struct.sockaddr_in*, %struct.sockaddr_in** %9
%28 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %27, i32 0, i32 0
%29 = bitcast i32* %28 to i8*
%30 = getelementptr inbounds [54 x i8], [54 x i8]* %6, i64 0, i64 0
%31 = call i8* @evutil_inet_ntop(i64 %26, i8* %29, i8* %30, i32 54)
store i8* %31, i8** %7
br label %56
32: ; preds = %2
%33 = load %struct.sockaddr*, %struct.sockaddr** %5
%34 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %33, i32 0, i32 0
%35 = load i64, i64* %34
%36 = load i64, i64* @AF_INET6
%37 = icmp eq i64 %35, %36
br i1 %37, label %38, label %54
38: ; preds = %32
%39 = load %struct.sockaddr*, %struct.sockaddr** %5
%40 = bitcast %struct.sockaddr* %39 to %struct.sockaddr_in6*
store %struct.sockaddr_in6* %40, %struct.sockaddr_in6** %10
%41 = load %struct.sockaddr*, %struct.sockaddr** %5
%42 = bitcast %struct.sockaddr* %41 to %struct.sockaddr_in6*
%43 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %42, i32 0, i32 1
%44 = load i32, i32* %43
%45 = call i32 @ntohs(i32 %44)
store i32 %45, i32* %8
%46 = load %struct.sockaddr*, %struct.sockaddr** %5
%47 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %46, i32 0, i32 0
%48 = load i64, i64* %47
%49 = load %struct.sockaddr_in6*, %struct.sockaddr_in6** %10
%50 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %49, i32 0, i32 0
%51 = bitcast i32* %50 to i8*
%52 = getelementptr inbounds [54 x i8], [54 x i8]* %6, i64 0, i64 0
%53 = call i8* @evutil_inet_ntop(i64 %48, i8* %51, i8* %52, i32 54)
store i8* %53, i8** %7
br label %55
54: ; preds = %32
store i32 -1, i32* %3
br label %81
55: ; preds = %38
br label %56
56: ; preds = %55, %17
%57 = load i8*, i8** %7
%58 = icmp ne i8* %57, null
br i1 %58, label %60, label %59
59: ; preds = %56
store i32 -1, i32* %3
br label %81
60: ; preds = %56
%61 = load i8*, i8** %4
%62 = load %struct.sockaddr*, %struct.sockaddr** %5
%63 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %62, i32 0, i32 0
%64 = load i64, i64* %63
%65 = load i64, i64* @AF_INET6
%66 = icmp eq i64 %64, %65
%67 = zext i1 %66 to i64
%68 = select i1 %66, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0)
%69 = getelementptr inbounds [54 x i8], [54 x i8]* %6, i64 0, i64 0
%70 = load i32, i32* %8
%71 = call i32 @snprintf(i8* %61, i32 54, i8* %68, i8* %69, i32 %70)
store i32 %71, i32* %11
%72 = load i32, i32* %11
%73 = icmp sle i32 %72, 0
br i1 %73, label %78, label %74
74: ; preds = %60
%75 = load i32, i32* %11
%76 = add nsw i32 %75, 1
%77 = icmp sgt i32 %76, 54
br i1 %77, label %78, label %79
78: ; preds = %74, %60
store i32 -1, i32* %3
br label %81
79: ; preds = %74
%80 = load i32, i32* %11
store i32 %80, i32* %3
br label %81
81: ; preds = %79, %78, %59, %54
%82 = load i32, i32* %3
ret i32 %82
}

LLVM IR (-O2):
%struct.sockaddr = type { i64 }
@AF_INET = global i64 0
@AF_INET6 = global i64 0
@.str = [9 x i8] c"[%s]:%hu\00"
@.str.1 = [7 x i8] c"%s:%hu\00"
define i32 @snip_sockaddr_to_string(i8* %0, %struct.sockaddr* %1) {
%3 = alloca [54 x i8]
%4 = getelementptr inbounds [54 x i8], [54 x i8]* %3, i64 0, i64 0
%5 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %1, i64 0, i32 0
%6 = load i64, i64* %5
%7 = load i64, i64* @AF_INET
%8 = icmp eq i64 %6, %7
%9 = load i64, i64* @AF_INET6
%10 = icmp eq i64 %6, %9
%11 = select i1 %8, i1 true, i1 %10
br i1 %11, label %12, label %29
12: ; preds = %2
%13 = lshr i64 %6, 32
%14 = trunc i64 %13 to i32
%15 = tail call i32 @ntohs(i32 %14)
%16 = load i64, i64* %5
%17 = bitcast %struct.sockaddr* %1 to i8*
%18 = call i8* @evutil_inet_ntop(i64 %16, i8* %17, i8* %4, i32 54)
%19 = icmp eq i8* %18, null
br i1 %19, label %29, label %20
20: ; preds = %12
%21 = load i64, i64* %5
%22 = load i64, i64* @AF_INET6
%23 = icmp eq i64 %21, %22
%24 = select i1 %23, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0)
%25 = call i32 @snprintf(i8* %0, i32 54, i8* %24, i8* %4, i32 %15)
%26 = add i32 %25, -54
%27 = icmp ult i32 %26, -53
%28 = select i1 %27, i32 -1, i32 %25
br label %29
29: ; preds = %2, %12, %20
%30 = phi i32 [ %28, %20 ], [ -1, %12 ], [ -1, %2 ]
ret i32 %30
}