#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_port;
  int /*<<< orphan*/ sin6_addr;
};
struct sockaddr_in {
  scalar_t__ sin_family;
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_addr;
};
typedef int /*<<< orphan*/ placeholder;

/* Variables and functions */
scalar_t__ AF_INET;
scalar_t__ AF_INET6;
int /*<<< orphan*/ assert(int);
char *inet_ntop(scalar_t__, int /*<<< orphan*/ *, char *, size_t);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, size_t);
int ntohs(int /*<<< orphan*/);
int /*<<< orphan*/ snprintf(char *, size_t, char *, int);
int /*<<< orphan*/ strcpy(char *, char const *);
size_t strlen(char const *);
char *red_inet_ntop(const struct sockaddr_in *sa, char *buffer,
                    size_t buffer_size) {
  const char *retval = 0;
  size_t len = 0;
  uint16_t port;
  const char placeholder[] = "???:???";
  assert(buffer_size >= sizeof(placeholder));
  memset(buffer, 0, buffer_size);
  if (sa->sin_family == AF_INET) {
    retval = inet_ntop(AF_INET, &sa->sin_addr, buffer, buffer_size);
    port = ((struct sockaddr_in *)sa)->sin_port;
  } else if (sa->sin_family == AF_INET6) {
    retval = inet_ntop(AF_INET6, &((const struct sockaddr_in6 *)sa)->sin6_addr,
                       buffer, buffer_size);
    port = ((struct sockaddr_in6 *)sa)->sin6_port;
  }
  if (retval) {
    assert(retval == buffer);
    len = strlen(retval);
    snprintf(buffer + len, buffer_size - len, ":%d", ntohs(port));
  } else {
    strcpy(buffer, placeholder);
  }
  return buffer;
}


x86 assembly (clang, -O0):
<red_inet_ntop>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
mov    0xe65(%rip),%rax
mov    %rax,-0x34(%rbp)
cmpq   $0x8,-0x18(%rbp)
setae  %al
and    $0x1,%al
movzbl %al,%edi
call   1080 <assert@plt>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rdx
xor    %esi,%esi
call   1070 <memset@plt>
mov    -0x8(%rbp),%rax
mov    (%rax),%rax
mov    0x2e1a(%rip),%rcx
cmp    (%rcx),%rax
jne    1209 <red_inet_ntop+0x99>
mov    0x2e0a(%rip),%rax
mov    (%rax),%rdi
mov    -0x8(%rbp),%rsi
add    $0xc,%rsi
mov    -0x10(%rbp),%rdx
mov    -0x18(%rbp),%rcx
call   1030 <inet_ntop@plt>
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rax
mov    0x8(%rax),%eax
mov    %eax,-0x2c(%rbp)
jmp    1251 <red_inet_ntop+0xe1>
mov    -0x8(%rbp),%rax
mov    (%rax),%rax
mov    0x2dc9(%rip),%rcx
cmp    (%rcx),%rax
jne    124c <red_inet_ntop+0xdc>
mov    0x2db9(%rip),%rax
mov    (%rax),%rdi
mov    -0x8(%rbp),%rsi
add    $0x4,%rsi
mov    -0x10(%rbp),%rdx
mov    -0x18(%rbp),%rcx
call   1030 <inet_ntop@plt>
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
jmp    1251 <red_inet_ntop+0xe1>
cmpq   $0x0,-0x20(%rbp)
je     12b9 <red_inet_ntop+0x149>
mov    -0x20(%rbp),%rax
cmp    -0x10(%rbp),%rax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1080 <assert@plt>
mov    -0x20(%rbp),%rdi
call   1050 <strlen@plt>
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rax
add    -0x28(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x18(%rbp),%rax
sub    -0x28(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x2c(%rbp),%edi
call   1090 <ntohs@plt>
mov    -0x48(%rbp),%rdi
mov    -0x40(%rbp),%rsi
mov    %eax,%ecx
lea    0xd59(%rip),%rdx
call   1060 <snprintf@plt>
jmp    12c6 <red_inet_ntop+0x156>
mov    -0x10(%rbp),%rdi
lea    -0x34(%rbp),%rsi
call   1040 <strcpy@plt>
mov    -0x10(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<red_inet_ntop>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rdx,%r15
mov    %rsi,%r14
mov    %rdi,%rbx
movabs $0x3f3f3f3a3f3f3f,%rax
mov    %rax,0x8(%rsp)
xor    %edi,%edi
cmp    $0x8,%rdx
setae  %dil
call   1080 <assert@plt>
mov    %r14,%rdi
xor    %esi,%esi
mov    %r15,%rdx
call   1070 <memset@plt>
mov    (%rbx),%rdi
mov    0x2e2e(%rip),%rax
cmp    (%rax),%rdi
jne    11dc <red_inet_ntop+0x6c>
lea    0xc(%rbx),%rsi
mov    %r14,%rdx
mov    %r15,%rcx
call   1030 <inet_ntop@plt>
mov    %rax,%r12
add    $0x8,%rbx
test   %r12,%r12
jne    11ff <red_inet_ntop+0x8f>
jmp    123b <red_inet_ntop+0xcb>
mov    0x2dfd(%rip),%rax
cmp    (%rax),%rdi
jne    123b <red_inet_ntop+0xcb>
lea    0x4(%rbx),%rsi
mov    %r14,%rdx
mov    %r15,%rcx
call   1030 <inet_ntop@plt>
mov    %rax,%r12
test   %r12,%r12
je     123b <red_inet_ntop+0xcb>
mov    (%rbx),%ebp
xor    %edi,%edi
cmp    %r14,%r12
sete   %dil
call   1080 <assert@plt>
mov    %r12,%rdi
call   1050 <strlen@plt>
lea    (%r14,%rax,1),%rbx
sub    %rax,%r15
mov    %ebp,%edi
call   1090 <ntohs@plt>
lea    0xdd4(%rip),%rdx
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %eax,%ecx
call   1060 <snprintf@plt>
jmp    1248 <red_inet_ntop+0xd8>
lea    0x8(%rsp),%rsi
mov    %r14,%rdi
call   1040 <strcpy@plt>
mov    %r14,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.sockaddr_in = type { i64, i32, i32 }
%struct.sockaddr_in6 = type { i32, i32 }
@AF_INET = global i64 0
@AF_INET6 = global i64 0
@.str = [4 x i8] c":%d\00"
define i8* @red_inet_ntop(%struct.sockaddr_in* %0, i8* returned %1, i64 %2) {
%4 = alloca i64
%5 = bitcast i64* %4 to i8*
store i64 17802464325484351, i64* %4
%6 = icmp ugt i64 %2, 7
%7 = zext i1 %6 to i32
%8 = tail call i32 @assert(i32 %7)
%9 = tail call i32 @memset(i8* %1, i32 0, i64 %2)
%10 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %0, i64 0, i32 0
%11 = load i64, i64* %10
%12 = load i64, i64* @AF_INET
%13 = icmp eq i64 %11, %12
br i1 %13, label %14, label %18
14: ; preds = %3
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %0, i64 0, i32 2
%16 = tail call i8* @inet_ntop(i64 %11, i32* %15, i8* %1, i64 %2)
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %0, i64 0, i32 1
br label %26
18: ; preds = %3
%19 = load i64, i64* @AF_INET6
%20 = icmp eq i64 %11, %19
br i1 %20, label %21, label %40
21: ; preds = %18
%22 = bitcast %struct.sockaddr_in* %0 to %struct.sockaddr_in6*
%23 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %22, i64 0, i32 1
%24 = tail call i8* @inet_ntop(i64 %11, i32* %23, i8* %1, i64 %2)
%25 = bitcast %struct.sockaddr_in* %0 to i32*
br label %26
26: ; preds = %21, %14
%27 = phi i8* [ %16, %14 ], [ %24, %21 ]
%28 = phi i32* [ %17, %14 ], [ %25, %21 ]
%29 = icmp eq i8* %27, null
br i1 %29, label %40, label %30
30: ; preds = %26
%31 = load i32, i32* %28
%32 = icmp eq i8* %27, %1
%33 = zext i1 %32 to i32
%34 = tail call i32 @assert(i32 %33)
%35 = tail call i64 @strlen(i8* %27)
%36 = getelementptr inbounds i8, i8* %1, i64 %35
%37 = sub i64 %2, %35
%38 = tail call i32 @ntohs(i32 %31)
%39 = tail call i32 @snprintf(i8* %36, i64 %37, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %38)
br label %42
40: ; preds = %18, %26
%41 = call i32 @strcpy(i8* %1, i8* %5)
br label %42
42: ; preds = %40, %30
ret i8* %1
}