#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 dummy;
};
struct sockaddr {
  int dummy;
};

/* Variables and functions */
scalar_t__ INET6_ADDRSTRLEN;
int NI_NUMERICHOST;
int NI_NUMERICSERV;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
char *gai_strerror(int);
int getnameinfo(struct sockaddr *, int, char *, scalar_t__, char *, int, int);
int /*<<< orphan*/ sprintf(char *, char *, char *);
int /*<<< orphan*/ stderr;
int strlen(char *);
const char *pretty_sockaddr_ipv4(struct sockaddr_in *sa, char *buf) {
  char servname[20];
  int result;
  result = getnameinfo((struct sockaddr *)sa, sizeof(struct sockaddr_in), buf,
                       (INET6_ADDRSTRLEN + 20 + 3), servname, 20,
                       NI_NUMERICHOST | NI_NUMERICSERV);
  if (result == 0) {
    char *cp = buf + strlen(buf);
    sprintf(cp, ":%s", servname);
    return buf;
  } else {
    fprintf(stderr, "Error pretty-printing IPv6 address: %s\n",
            gai_strerror(result));
    return 0;
  }
}


x86 assembly (clang, -O0):
<pretty_sockaddr_ipv4>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    0x2e71(%rip),%rax
mov    (%rax),%rcx
add    $0x14,%rcx
add    $0x3,%rcx
lea    -0x30(%rbp),%r8
mov    0x2e43(%rip),%rax
mov    (%rax),%eax
mov    0x2e5a(%rip),%rsi
or     (%rsi),%eax
mov    $0x4,%esi
mov    $0x14,%r9d
mov    %eax,(%rsp)
call   1050 <getnameinfo@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
jne    11f2 <pretty_sockaddr_ipv4+0xa2>
mov    -0x18(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x48(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rdi
lea    -0x30(%rbp),%rdx
lea    0xe20(%rip),%rsi
call   1070 <sprintf@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1220 <pretty_sockaddr_ipv4+0xd0>
mov    0x2dd7(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4c(%rbp)
mov    -0x34(%rbp),%edi
call   1040 <gai_strerror@plt>
mov    -0x4c(%rbp),%edi
mov    %rax,%rdx
lea    0xdf1(%rip),%rsi
call   1060 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pretty_sockaddr_ipv4>:
push   %rbx
sub    $0x30,%rsp
mov    %rsi,%rbx
mov    0x2e81(%rip),%rax
mov    (%rax),%rcx
add    $0x17,%rcx
mov    0x2e5b(%rip),%rax
mov    0x2e74(%rip),%rdx
mov    (%rdx),%edx
or     (%rax),%edx
mov    %edx,(%rsp)
lea    0x10(%rsp),%r8
mov    $0x4,%esi
mov    %rbx,%rdx
mov    $0x14,%r9d
call   1050 <getnameinfo@plt>
test   %eax,%eax
je     11bc <pretty_sockaddr_ipv4+0x6c>
mov    0x2e32(%rip),%rcx
mov    (%rcx),%ebx
mov    %eax,%edi
call   1040 <gai_strerror@plt>
lea    0xe56(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1060 <fprintf@plt>
xor    %ebx,%ebx
jmp    11db <pretty_sockaddr_ipv4+0x8b>
mov    %rbx,%rdi
call   1030 <strlen@plt>
movslq %eax,%rdi
add    %rbx,%rdi
lea    0xe2f(%rip),%rsi
lea    0x10(%rsp),%rdx
call   1070 <sprintf@plt>
mov    %rbx,%rax
add    $0x30,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32 }
%struct.sockaddr = type { i32 }
@INET6_ADDRSTRLEN = global i64 0
@NI_NUMERICHOST = global i32 0
@NI_NUMERICSERV = global i32 0
@.str = [4 x i8] c":%s\00"
@stderr = global i32 0
@.str.1 = [40 x i8] c"Error pretty-printing IPv6 address: %s\0A\00"
define i8* @pretty_sockaddr_ipv4(%struct.sockaddr_in* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca %struct.sockaddr_in*
%5 = alloca i8*
%6 = alloca [20 x i8]
%7 = alloca i32
%8 = alloca i8*
store %struct.sockaddr_in* %0, %struct.sockaddr_in** %4
store i8* %1, i8** %5
%9 = load %struct.sockaddr_in*, %struct.sockaddr_in** %4
%10 = bitcast %struct.sockaddr_in* %9 to %struct.sockaddr*
%11 = load i8*, i8** %5
%12 = load i64, i64* @INET6_ADDRSTRLEN
%13 = add nsw i64 %12, 20
%14 = add nsw i64 %13, 3
%15 = getelementptr inbounds [20 x i8], [20 x i8]* %6, i64 0, i64 0
%16 = load i32, i32* @NI_NUMERICHOST
%17 = load i32, i32* @NI_NUMERICSERV
%18 = or i32 %16, %17
%19 = call i32 @getnameinfo(%struct.sockaddr* %10, i32 4, i8* %11, i64 %14, i8* %15, i32 20, i32 %18)
store i32 %19, i32* %7
%20 = load i32, i32* %7
%21 = icmp eq i32 %20, 0
br i1 %21, label %22, label %32
22: ; preds = %2
%23 = load i8*, i8** %5
%24 = load i8*, i8** %5
%25 = call i32 @strlen(i8* %24)
%26 = sext i32 %25 to i64
%27 = getelementptr inbounds i8, i8* %23, i64 %26
store i8* %27, i8** %8
%28 = load i8*, i8** %8
%29 = getelementptr inbounds [20 x i8], [20 x i8]* %6, i64 0, i64 0
%30 = call i32 @sprintf(i8* %28, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* %29)
%31 = load i8*, i8** %5
store i8* %31, i8** %3
br label %37
32: ; preds = %2
%33 = load i32, i32* @stderr
%34 = load i32, i32* %7
%35 = call i8* @gai_strerror(i32 %34)
%36 = call i32 @fprintf(i32 %33, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.1, i64 0, i64 0), i8* %35)
store i8* null, i8** %3
br label %37
37: ; preds = %32, %22
%38 = load i8*, i8** %3
ret i8* %38
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32 }
%struct.sockaddr = type { i32 }
@INET6_ADDRSTRLEN = global i64 0
@NI_NUMERICHOST = global i32 0
@NI_NUMERICSERV = global i32 0
@.str = [4 x i8] c":%s\00"
@stderr = global i32 0
@.str.1 = [40 x i8] c"Error pretty-printing IPv6 address: %s\0A\00"
define i8* @pretty_sockaddr_ipv4(%struct.sockaddr_in* %0, i8* %1) {
%3 = alloca [20 x i8]
%4 = getelementptr inbounds [20 x i8], [20 x i8]* %3, i64 0, i64 0
%5 = bitcast %struct.sockaddr_in* %0 to %struct.sockaddr*
%6 = load i64, i64* @INET6_ADDRSTRLEN
%7 = add nsw i64 %6, 23
%8 = load i32, i32* @NI_NUMERICHOST
%9 = load i32, i32* @NI_NUMERICSERV
%10 = or i32 %9, %8
%11 = call i32 @getnameinfo(%struct.sockaddr* %5, i32 4, i8* %1, i64 %7, i8* %4, i32 20, i32 %10)
%12 = icmp eq i32 %11, 0
br i1 %12, label %13, label %18
13: ; preds = %2
%14 = call i32 @strlen(i8* %1)
%15 = sext i32 %14 to i64
%16 = getelementptr inbounds i8, i8* %1, i64 %15
%17 = call i32 @sprintf(i8* dereferenceable(1) %16, i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* %4)
br label %22
18: ; preds = %2
%19 = load i32, i32* @stderr
%20 = call i8* @gai_strerror(i32 %11)
%21 = call i32 @fprintf(i32 %19, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.1, i64 0, i64 0), i8* %20)
br label %22
22: ; preds = %18, %13
%23 = phi i8* [ %1, %13 ], [ null, %18 ]
ret i8* %23
}