#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 uint8_t;
typedef int /*<<< orphan*/ uint32_t;
typedef scalar_t__ uint16_t;
struct in_addr {
  int /*<<< orphan*/ member_0;
};

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ inet_ntop(int /*<<< orphan*/, struct in_addr *, char *, int);
int /*<<< orphan*/ snprintf(char *, int, char *, int const, int const,
                            int const, int const, scalar_t__);
const char *ip2str(uint32_t ip, uint16_t port, char *cache) {
  ip = htonl(ip);
  if (port == 0) {
    struct in_addr addr = {ip};
    inet_ntop(AF_INET, &addr, cache, 16);
  } else {
    const uint8_t *v = (const uint8_t *)&ip;
    snprintf(cache, 24, "%d.%d.%d.%d:%d", v[0], v[1], v[2], v[3], port);
  }
  return cache;
}


x86 assembly (clang, -O0):
<ip2str>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x4(%rbp),%edi
call   1050 <htonl@plt>
mov    %eax,-0x4(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    117f <ip2str+0x4f>
mov    -0x4(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    0x2e82(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rdx
lea    -0x20(%rbp),%rsi
mov    $0x10,%ecx
call   1030 <inet_ntop@plt>
jmp    11c7 <ip2str+0x97>
lea    -0x4(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x28(%rbp),%rax
mov    (%rax),%ecx
mov    -0x28(%rbp),%rax
mov    0x4(%rax),%r8d
mov    -0x28(%rbp),%rax
mov    0x8(%rax),%r9d
mov    -0x28(%rbp),%rax
mov    0xc(%rax),%r10d
mov    -0x10(%rbp),%rax
mov    $0x18,%esi
lea    0xe47(%rip),%rdx
mov    %r10d,(%rsp)
mov    %rax,0x8(%rsp)
call   1040 <snprintf@plt>
mov    -0x18(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ip2str>:
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rdx,%r14
mov    %rsi,%rbx
call   1050 <htonl@plt>
test   %rbx,%rbx
je     1164 <ip2str+0x34>
mov    %rbx,0x8(%rsp)
lea    0xead(%rip),%rdx
mov    %r14,%rdi
mov    $0x18,%esi
mov    %eax,%ecx
call   1040 <snprintf@plt>
jmp    1183 <ip2str+0x53>
mov    %eax,0x10(%rsp)
mov    0x2e79(%rip),%rax
mov    (%rax),%edi
lea    0x10(%rsp),%rsi
mov    %r14,%rdx
mov    $0x10,%ecx
call   1030 <inet_ntop@plt>
mov    %r14,%rax
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.in_addr = type { i32 }
@AF_INET = global i32 0
@.str = [15 x i8] c"%d.%d.%d.%d:%d\00"
define i8* @ip2str(i32 %0, i64 %1, i8* %2) {
%4 = alloca i32
%5 = alloca i64
%6 = alloca i8*
%7 = alloca %struct.in_addr
%8 = alloca i32*
store i32 %0, i32* %4
store i64 %1, i64* %5
store i8* %2, i8** %6
%9 = load i32, i32* %4
%10 = call i32 @htonl(i32 %9)
store i32 %10, i32* %4
%11 = load i64, i64* %5
%12 = icmp eq i64 %11, 0
br i1 %12, label %13, label %19
13: ; preds = %3
%14 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %7, i32 0, i32 0
%15 = load i32, i32* %4
store i32 %15, i32* %14
%16 = load i32, i32* @AF_INET
%17 = load i8*, i8** %6
%18 = call i32 @inet_ntop(i32 %16, %struct.in_addr* %7, i8* %17, i32 16)
br label %35
19: ; preds = %3
store i32* %4, i32** %8
%20 = load i8*, i8** %6
%21 = load i32*, i32** %8
%22 = getelementptr inbounds i32, i32* %21, i64 0
%23 = load i32, i32* %22
%24 = load i32*, i32** %8
%25 = getelementptr inbounds i32, i32* %24, i64 1
%26 = load i32, i32* %25
%27 = load i32*, i32** %8
%28 = getelementptr inbounds i32, i32* %27, i64 2
%29 = load i32, i32* %28
%30 = load i32*, i32** %8
%31 = getelementptr inbounds i32, i32* %30, i64 3
%32 = load i32, i32* %31
%33 = load i64, i64* %5
%34 = call i32 @snprintf(i8* %20, i32 24, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %23, i32 %26, i32 %29, i32 %32, i64 %33)
br label %35
35: ; preds = %19, %13
%36 = load i8*, i8** %6
ret i8* %36
}

LLVM IR (-O2):
%struct.in_addr = type { i32 }
@AF_INET = global i32 0
@.str = [15 x i8] c"%d.%d.%d.%d:%d\00"
define i8* @ip2str(i32 %0, i64 %1, i8* returned %2) {
%4 = alloca %struct.in_addr
%5 = tail call i32 @htonl(i32 %0)
%6 = icmp eq i64 %1, 0
br i1 %6, label %7, label %12
7: ; preds = %3
%8 = bitcast %struct.in_addr* %4 to i8*
%9 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %4, i64 0, i32 0
store i32 %5, i32* %9
%10 = load i32, i32* @AF_INET
%11 = call i32 @inet_ntop(i32 %10, %struct.in_addr* %4, i8* %2, i32 16)
br label %14
12: ; preds = %3
%13 = tail call i32 @snprintf(i8* %2, i32 24, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %5, i32 poison, i32 poison, i32 poison, i64 %1)
br label %14
14: ; preds = %12, %7
ret i8* %2
}