#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_in {
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_addr;
};
typedef size_t ssize_t;
typedef int /*<<< orphan*/ port;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ ENOSPC;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ *inet_ntop(int /*<<< orphan*/, int /*<<< orphan*/ *, char *,
                              size_t);
int /*<<< orphan*/ memcpy(char *, char *, size_t);
int ntohs(int /*<<< orphan*/);
size_t snprintf(char *, int, char *, int);
size_t strlen(char *);
ssize_t sancus_socket_ntop_ipv4(char *str, size_t size,
                                const struct sockaddr_in *sin) {
  if (inet_ntop(AF_INET, &sin->sin_addr, str, size) != NULL) {
    char port[8];
    size_t l = strlen(str);
    size_t lp = snprintf(port, sizeof(port), ":%u", ntohs(sin->sin_port));
    if (l + lp < size) {
      memcpy(str + l, port, lp + 1);
      return l + lp;
    }
    errno = ENOSPC;
  }
  return -1;
}


x86 assembly (clang, -O0):
<sancus_socket_ntop_ipv4>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    0x2e7d(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%rsi
add    $0x4,%rsi
mov    -0x10(%rbp),%rdx
mov    -0x18(%rbp),%rcx
call   1030 <inet_ntop@plt>
cmp    $0x0,%rax
je     1215 <sancus_socket_ntop_ipv4+0xc5>
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x30(%rbp)
lea    -0x28(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x20(%rbp),%rax
mov    (%rax),%edi
call   1070 <ntohs@plt>
mov    -0x40(%rbp),%rdi
mov    %eax,%ecx
mov    $0x8,%esi
lea    0xe42(%rip),%rdx
call   1050 <snprintf@plt>
mov    %rax,-0x38(%rbp)
mov    -0x30(%rbp),%rax
add    -0x38(%rbp),%rax
cmp    -0x18(%rbp),%rax
jae    1203 <sancus_socket_ntop_ipv4+0xb3>
mov    -0x10(%rbp),%rdi
add    -0x30(%rbp),%rdi
lea    -0x28(%rbp),%rsi
mov    -0x38(%rbp),%rdx
add    $0x1,%rdx
call   1060 <memcpy@plt>
mov    -0x30(%rbp),%rax
add    -0x38(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    121d <sancus_socket_ntop_ipv4+0xcd>
mov    0x2dc6(%rip),%rax
mov    (%rax),%ecx
mov    0x2dc5(%rip),%rax
mov    %ecx,(%rax)
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sancus_socket_ntop_ipv4>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rdx,%rbx
mov    %rsi,%r15
mov    %rdi,%r13
mov    0x2e7b(%rip),%rax
mov    (%rax),%edi
lea    0x4(%rdx),%rsi
mov    %r13,%rdx
mov    %r15,%rcx
call   1030 <inet_ntop@plt>
mov    $0xffffffffffffffff,%r14
test   %rax,%rax
je     11eb <sancus_socket_ntop_ipv4+0x9b>
mov    %r13,%rdi
call   1040 <strlen@plt>
mov    %rax,%r12
mov    (%rbx),%edi
call   1070 <ntohs@plt>
lea    0xe5d(%rip),%rdx
lea    0x8(%rsp),%rdi
mov    $0x8,%esi
mov    %eax,%ecx
call   1050 <snprintf@plt>
lea    (%rax,%r12,1),%rbx
cmp    %r15,%rbx
jae    11d9 <sancus_socket_ntop_ipv4+0x89>
add    %r12,%r13
add    $0x1,%rax
lea    0x8(%rsp),%rsi
mov    %r13,%rdi
mov    %rax,%rdx
call   1060 <memcpy@plt>
mov    %rbx,%r14
jmp    11eb <sancus_socket_ntop_ipv4+0x9b>
mov    0x2df0(%rip),%rax
mov    (%rax),%eax
mov    0x2def(%rip),%rcx
mov    %eax,(%rcx)
mov    %r14,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32 }
@AF_INET = global i32 0
@.str = [4 x i8] c":%u\00"
@ENOSPC = global i32 0
@errno = global i32 0
define i64 @sancus_socket_ntop_ipv4(i8* %0, i64 %1, %struct.sockaddr_in* %2) {
%4 = alloca i64
%5 = alloca i8*
%6 = alloca i64
%7 = alloca %struct.sockaddr_in*
%8 = alloca [8 x i8]
%9 = alloca i64
%10 = alloca i64
store i8* %0, i8** %5
store i64 %1, i64* %6
store %struct.sockaddr_in* %2, %struct.sockaddr_in** %7
%11 = load i32, i32* @AF_INET
%12 = load %struct.sockaddr_in*, %struct.sockaddr_in** %7
%13 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %12, i32 0, i32 1
%14 = load i8*, i8** %5
%15 = load i64, i64* %6
%16 = call i32* @inet_ntop(i32 %11, i32* %13, i8* %14, i64 %15)
%17 = icmp ne i32* %16, null
br i1 %17, label %18, label %45
18: ; preds = %3
%19 = load i8*, i8** %5
%20 = call i64 @strlen(i8* %19)
store i64 %20, i64* %9
%21 = getelementptr inbounds [8 x i8], [8 x i8]* %8, i64 0, i64 0
%22 = load %struct.sockaddr_in*, %struct.sockaddr_in** %7
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %22, i32 0, i32 0
%24 = load i32, i32* %23
%25 = call i32 @ntohs(i32 %24)
%26 = call i64 @snprintf(i8* %21, i32 8, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %25)
store i64 %26, i64* %10
%27 = load i64, i64* %9
%28 = load i64, i64* %10
%29 = add i64 %27, %28
%30 = load i64, i64* %6
%31 = icmp ult i64 %29, %30
br i1 %31, label %32, label %43
32: ; preds = %18
%33 = load i8*, i8** %5
%34 = load i64, i64* %9
%35 = getelementptr inbounds i8, i8* %33, i64 %34
%36 = getelementptr inbounds [8 x i8], [8 x i8]* %8, i64 0, i64 0
%37 = load i64, i64* %10
%38 = add i64 %37, 1
%39 = call i32 @memcpy(i8* %35, i8* %36, i64 %38)
%40 = load i64, i64* %9
%41 = load i64, i64* %10
%42 = add i64 %40, %41
store i64 %42, i64* %4
br label %46
43: ; preds = %18
%44 = load i32, i32* @ENOSPC
store i32 %44, i32* @errno
br label %45
45: ; preds = %43, %3
store i64 -1, i64* %4
br label %46
46: ; preds = %45, %32
%47 = load i64, i64* %4
ret i64 %47
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32 }
@AF_INET = global i32 0
@.str = [4 x i8] c":%u\00"
@ENOSPC = global i32 0
@errno = global i32 0
define i64 @sancus_socket_ntop_ipv4(i8* %0, i64 %1, %struct.sockaddr_in* %2) {
%4 = alloca [8 x i8]
%5 = load i32, i32* @AF_INET
%6 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 1
%7 = tail call i32* @inet_ntop(i32 %5, i32* %6, i8* %0, i64 %1)
%8 = icmp eq i32* %7, null
br i1 %8, label %24, label %9
9: ; preds = %3
%10 = getelementptr inbounds [8 x i8], [8 x i8]* %4, i64 0, i64 0
%11 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%12 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 0
%13 = load i32, i32* %12
%14 = tail call i32 @ntohs(i32 %13)
%15 = call i64 @snprintf(i8* %10, i32 8, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %14)
%16 = add i64 %15, %11
%17 = icmp ult i64 %16, %1
br i1 %17, label %20, label %18
18: ; preds = %9
%19 = load i32, i32* @ENOSPC
store i32 %19, i32* @errno
br label %24
20: ; preds = %9
%21 = getelementptr inbounds i8, i8* %0, i64 %11
%22 = add i64 %15, 1
%23 = call i32 @memcpy(i8* %21, i8* %10, i64 %22)
br label %24
24: ; preds = %3, %18, %20
%25 = phi i64 [ %16, %20 ], [ -1, %18 ], [ -1, %3 ]
ret i64 %25
}