#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 int /*<<< orphan*/ ip;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ assert(char *);
int /*<<< orphan*/ inet_ntop(int /*<<< orphan*/, int /*<<< orphan*/ *, char *,
                             int);
int ntohs(int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ sprintf(char *, char *, char *, int);
int net_addr2ip(char *out_ip, struct sockaddr_in *addr) {
  assert(out_ip);
  char ip[30];
  if (!inet_ntop(AF_INET, &(addr->sin_addr), ip, sizeof(ip))) {
    perror("fail to addr convert ip");
    return -1;
  }
  sprintf(out_ip, "%s:%d", ip, ntohs(addr->sin_port));
  return 0;
}


x86 assembly (clang, -O0):
<net_addr2ip>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <assert@plt>
mov    0x2e78(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rsi
add    $0x4,%rsi
lea    -0x40(%rbp),%rdx
mov    $0x1e,%ecx
call   1030 <inet_ntop@plt>
cmp    $0x0,%eax
jne    11a9 <net_addr2ip+0x59>
lea    0xe68(%rip),%rdi
call   1060 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11e1 <net_addr2ip+0x91>
mov    -0x10(%rbp),%rax
mov    %rax,-0x50(%rbp)
lea    -0x40(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x18(%rbp),%rax
mov    (%rax),%edi
call   1050 <ntohs@plt>
mov    -0x50(%rbp),%rdi
mov    -0x48(%rbp),%rdx
mov    %eax,%ecx
lea    0xe43(%rip),%rsi
call   1070 <sprintf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<net_addr2ip>:
push   %r14
push   %rbx
sub    $0x28,%rsp
mov    %rsi,%rbx
mov    %rdi,%r14
call   1040 <assert@plt>
mov    0x2e7f(%rip),%rax
mov    (%rax),%edi
lea    0x4(%rbx),%rsi
mov    %rsp,%rdx
mov    $0x1e,%ecx
call   1030 <inet_ntop@plt>
test   %eax,%eax
je     11a5 <net_addr2ip+0x55>
mov    (%rbx),%edi
call   1050 <ntohs@plt>
lea    0xe8a(%rip),%rsi
mov    %rsp,%rdx
mov    %r14,%rdi
mov    %eax,%ecx
call   1070 <sprintf@plt>
xor    %eax,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r14
ret
lea    0xe54(%rip),%rdi
call   1060 <perror@plt>
mov    $0xffffffff,%eax
jmp    119d <net_addr2ip+0x4d>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32 }
@AF_INET = global i32 0
@.str = [24 x i8] c"fail to addr convert ip\00"
@.str.1 = [6 x i8] c"%s:%d\00"
define i32 @net_addr2ip(i8* %0, %struct.sockaddr_in* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca %struct.sockaddr_in*
%6 = alloca [30 x i8]
store i8* %0, i8** %4
store %struct.sockaddr_in* %1, %struct.sockaddr_in** %5
%7 = load i8*, i8** %4
%8 = call i32 @assert(i8* %7)
%9 = load i32, i32* @AF_INET
%10 = load %struct.sockaddr_in*, %struct.sockaddr_in** %5
%11 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 1
%12 = getelementptr inbounds [30 x i8], [30 x i8]* %6, i64 0, i64 0
%13 = call i32 @inet_ntop(i32 %9, i32* %11, i8* %12, i32 30)
%14 = icmp ne i32 %13, 0
br i1 %14, label %17, label %15
15: ; preds = %2
%16 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %25
17: ; preds = %2
%18 = load i8*, i8** %4
%19 = getelementptr inbounds [30 x i8], [30 x i8]* %6, i64 0, i64 0
%20 = load %struct.sockaddr_in*, %struct.sockaddr_in** %5
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %20, i32 0, i32 0
%22 = load i32, i32* %21
%23 = call i32 @ntohs(i32 %22)
%24 = call i32 @sprintf(i8* %18, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* %19, i32 %23)
store i32 0, i32* %3
br label %25
25: ; preds = %17, %15
%26 = load i32, i32* %3
ret i32 %26
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32 }
@AF_INET = global i32 0
@.str = [24 x i8] c"fail to addr convert ip\00"
@.str.1 = [6 x i8] c"%s:%d\00"
define i32 @net_addr2ip(i8* %0, %struct.sockaddr_in* %1) {
%3 = alloca [30 x i8]
%4 = tail call i32 @assert(i8* %0)
%5 = getelementptr inbounds [30 x i8], [30 x i8]* %3, i64 0, i64 0
%6 = load i32, i32* @AF_INET
%7 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 1
%8 = call i32 @inet_ntop(i32 %6, i32* %7, i8* %5, i32 30)
%9 = icmp eq i32 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %2
%11 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
br label %17
12: ; preds = %2
%13 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 0
%14 = load i32, i32* %13
%15 = call i32 @ntohs(i32 %14)
%16 = call i32 @sprintf(i8* dereferenceable(1) %0, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* %5, i32 %15)
br label %17
17: ; preds = %12, %10
%18 = phi i32 [ 0, %12 ], [ -1, %10 ]
ret i32 %18
}