#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_in6 {
  int /*<<< orphan*/ sin6_addr;
};
struct sockaddr_in {
  int /*<<< orphan*/ sin_addr;
};
struct sockaddr {
  scalar_t__ sa_family;
};
typedef int /*<<< orphan*/ socklen_t;

/* Variables and functions */
scalar_t__ AF_INET;
scalar_t__ AF_INET6;
int /*<<< orphan*/ INET6_ADDRSTRLEN;
int /*<<< orphan*/ INET_ADDRSTRLEN;
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ inet_ntop(scalar_t__, void *, char *, int /*<<< orphan*/);
scalar_t__ malloc(int /*<<< orphan*/);
char *sockaddr2str(const struct sockaddr *saddr) {
  char *str;
  socklen_t len;
  void *addr;
  if (saddr->sa_family == AF_INET) {
    len = INET_ADDRSTRLEN;
    addr = &((struct sockaddr_in *)saddr)->sin_addr;
  } else if (saddr->sa_family == AF_INET6) {
    len = INET6_ADDRSTRLEN;
    addr = &((struct sockaddr_in6 *)saddr)->sin6_addr;
  } else
    return NULL;
  str = (char *)malloc(len);
  if (!str)
    return NULL;
  if (!inet_ntop(saddr->sa_family, addr, str, len)) {
    free(str);
    return NULL;
  }
  return str;
}


x86 assembly (clang, -O0):
<sockaddr2str>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    0x2e96(%rip),%rcx
cmp    (%rcx),%rax
jne    116c <sockaddr2str+0x3c>
mov    0x2e6e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
jmp    11ae <sockaddr2str+0x7e>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    0x2e5e(%rip),%rcx
cmp    (%rcx),%rax
jne    119c <sockaddr2str+0x6c>
mov    0x2e5e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
jmp    11a9 <sockaddr2str+0x79>
movq   $0x0,-0x8(%rbp)
jmp    1210 <sockaddr2str+0xe0>
jmp    11ae <sockaddr2str+0x7e>
mov    -0x1c(%rbp),%edi
call   1050 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11d2 <sockaddr2str+0xa2>
movq   $0x0,-0x8(%rbp)
jmp    1210 <sockaddr2str+0xe0>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x18(%rbp),%rdx
mov    -0x1c(%rbp),%ecx
call   1030 <inet_ntop@plt>
cmp    $0x0,%eax
jne    1208 <sockaddr2str+0xd8>
mov    -0x18(%rbp),%rdi
call   1040 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    1210 <sockaddr2str+0xe0>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sockaddr2str>:
push   %rbp
push   %r14
push   %rbx
mov    %rdi,%r14
mov    (%rdi),%rax
mov    0x2e9f(%rip),%rcx
cmp    (%rcx),%rax
jne    114f <sockaddr2str+0x1f>
mov    0x2e7b(%rip),%rax
jmp    1162 <sockaddr2str+0x32>
mov    0x2e82(%rip),%rcx
cmp    (%rcx),%rax
jne    118f <sockaddr2str+0x5f>
mov    0x2e86(%rip),%rax
mov    (%rax),%ebp
mov    %ebp,%edi
call   1050 <malloc@plt>
test   %rax,%rax
je     118f <sockaddr2str+0x5f>
mov    %rax,%rbx
mov    (%r14),%rdi
mov    %r14,%rsi
mov    %rax,%rdx
mov    %ebp,%ecx
call   1030 <inet_ntop@plt>
test   %eax,%eax
jne    1191 <sockaddr2str+0x61>
mov    %rbx,%rdi
call   1040 <free@plt>
xor    %ebx,%ebx
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr = type { i64 }
%struct.sockaddr_in = type { i32 }
%struct.sockaddr_in6 = type { i32 }
@AF_INET = global i64 0
@INET_ADDRSTRLEN = global i32 0
@AF_INET6 = global i64 0
@INET6_ADDRSTRLEN = global i32 0
define i8* @sockaddr2str(%struct.sockaddr* %0) {
%2 = alloca i8*
%3 = alloca %struct.sockaddr*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i8*
store %struct.sockaddr* %0, %struct.sockaddr** %3
%7 = load %struct.sockaddr*, %struct.sockaddr** %3
%8 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %7, i32 0, i32 0
%9 = load i64, i64* %8
%10 = load i64, i64* @AF_INET
%11 = icmp eq i64 %9, %10
br i1 %11, label %12, label %18
12: ; preds = %1
%13 = load i32, i32* @INET_ADDRSTRLEN
store i32 %13, i32* %5
%14 = load %struct.sockaddr*, %struct.sockaddr** %3
%15 = bitcast %struct.sockaddr* %14 to %struct.sockaddr_in*
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %15, i32 0, i32 0
%17 = bitcast i32* %16 to i8*
store i8* %17, i8** %6
br label %32
18: ; preds = %1
%19 = load %struct.sockaddr*, %struct.sockaddr** %3
%20 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %19, i32 0, i32 0
%21 = load i64, i64* %20
%22 = load i64, i64* @AF_INET6
%23 = icmp eq i64 %21, %22
br i1 %23, label %24, label %30
24: ; preds = %18
%25 = load i32, i32* @INET6_ADDRSTRLEN
store i32 %25, i32* %5
%26 = load %struct.sockaddr*, %struct.sockaddr** %3
%27 = bitcast %struct.sockaddr* %26 to %struct.sockaddr_in6*
%28 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %27, i32 0, i32 0
%29 = bitcast i32* %28 to i8*
store i8* %29, i8** %6
br label %31
30: ; preds = %18
store i8* null, i8** %2
br label %53
31: ; preds = %24
br label %32
32: ; preds = %31, %12
%33 = load i32, i32* %5
%34 = call i64 @malloc(i32 %33)
%35 = inttoptr i64 %34 to i8*
store i8* %35, i8** %4
%36 = load i8*, i8** %4
%37 = icmp ne i8* %36, null
br i1 %37, label %39, label %38
38: ; preds = %32
store i8* null, i8** %2
br label %53
39: ; preds = %32
%40 = load %struct.sockaddr*, %struct.sockaddr** %3
%41 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %40, i32 0, i32 0
%42 = load i64, i64* %41
%43 = load i8*, i8** %6
%44 = load i8*, i8** %4
%45 = load i32, i32* %5
%46 = call i32 @inet_ntop(i64 %42, i8* %43, i8* %44, i32 %45)
%47 = icmp ne i32 %46, 0
br i1 %47, label %51, label %48
48: ; preds = %39
%49 = load i8*, i8** %4
%50 = call i32 @free(i8* %49)
store i8* null, i8** %2
br label %53
51: ; preds = %39
%52 = load i8*, i8** %4
store i8* %52, i8** %2
br label %53
53: ; preds = %51, %48, %38, %30
%54 = load i8*, i8** %2
ret i8* %54
}

LLVM IR (-O2):
%struct.sockaddr = type { i64 }
@AF_INET = global i64 0
@INET_ADDRSTRLEN = global i32 0
@AF_INET6 = global i64 0
@INET6_ADDRSTRLEN = global i32 0
define i8* @sockaddr2str(%struct.sockaddr* %0) {
%2 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %0, i64 0, i32 0
%3 = load i64, i64* %2
%4 = load i64, i64* @AF_INET
%5 = icmp eq i64 %3, %4
br i1 %5, label %9, label %6
6: ; preds = %1
%7 = load i64, i64* @AF_INET6
%8 = icmp eq i64 %3, %7
br i1 %8, label %9, label %22
9: ; preds = %6, %1
%10 = phi i32* [ @INET_ADDRSTRLEN, %1 ], [ @INET6_ADDRSTRLEN, %6 ]
%11 = load i32, i32* %10
%12 = tail call i64 @malloc(i32 %11)
%13 = inttoptr i64 %12 to i8*
%14 = icmp eq i64 %12, 0
br i1 %14, label %22, label %15
15: ; preds = %9
%16 = bitcast %struct.sockaddr* %0 to i8*
%17 = load i64, i64* %2
%18 = tail call i32 @inet_ntop(i64 %17, i8* %16, i8* %13, i32 %11)
%19 = icmp eq i32 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %15
%21 = tail call i32 @free(i8* %13)
br label %22
22: ; preds = %15, %9, %6, %20
%23 = phi i8* [ null, %20 ], [ null, %6 ], [ null, %9 ], [ %13, %15 ]
ret i8* %23
}