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

/* Variables and functions */
scalar_t__ AF_INET;
scalar_t__ AF_INET6;
int /*<<< orphan*/ NI_NUMERICHOST;
int getnameinfo(struct sockaddr *, int, char *, int, int /*<<< orphan*/ *,
                int /*<<< orphan*/, int /*<<< orphan*/);
int sa2str(char *buf, int bufmax, struct sockaddr *sa) {
  return getnameinfo(sa,
                     (((struct sockaddr *)(sa))->sa_family == AF_INET)
                         ? sizeof(struct sockaddr_in)
                         : ((((struct sockaddr *)(sa))->sa_family == AF_INET6)
                                ? sizeof(struct sockaddr_in6)
                                : 0),
                     buf, bufmax, NULL, 0, NI_NUMERICHOST);
}


x86 assembly (clang, -O0):
<sa2str>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
mov    0x2eaf(%rip),%rcx
cmp    (%rcx),%rax
jne    1150 <sa2str+0x40>
mov    $0x4,%eax
mov    %rax,-0x28(%rbp)
jmp    1173 <sa2str+0x63>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdx
mov    0x2e82(%rip),%rax
mov    (%rax),%rsi
xor    %eax,%eax
mov    $0x4,%ecx
cmp    %rsi,%rdx
cmove  %rcx,%rax
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%rax
mov    %eax,%esi
mov    -0x8(%rbp),%rdx
mov    -0xc(%rbp),%ecx
mov    0x2e45(%rip),%rax
mov    (%rax),%eax
xor    %r8d,%r8d
xor    %r9d,%r9d
mov    %eax,(%rsp)
call   1030 <getnameinfo@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sa2str>:
push   %rax
mov    %rdx,%r9
mov    %esi,%ecx
mov    %rdi,%r8
mov    (%rdx),%rsi
mov    0x2ec5(%rip),%rdi
cmp    (%rdi),%rsi
sete   %al
mov    0x2eb0(%rip),%rdi
cmp    (%rdi),%rsi
sete   %dl
or     %al,%dl
movzbl %dl,%esi
shl    $0x2,%esi
mov    0x2e8b(%rip),%rax
mov    (%rax),%eax
mov    %eax,(%rsp)
mov    %r9,%rdi
mov    %r8,%rdx
xor    %r8d,%r8d
xor    %r9d,%r9d
call   1030 <getnameinfo@plt>
pop    %rcx
ret

LLVM IR (-O0):
%struct.sockaddr = type { i64 }
@AF_INET = global i64 0
@AF_INET6 = global i64 0
@NI_NUMERICHOST = global i32 0
define i32 @sa2str(i8* %0, i32 %1, %struct.sockaddr* %2) {
%4 = alloca i8*
%5 = alloca i32
%6 = alloca %struct.sockaddr*
store i8* %0, i8** %4
store i32 %1, i32* %5
store %struct.sockaddr* %2, %struct.sockaddr** %6
%7 = load %struct.sockaddr*, %struct.sockaddr** %6
%8 = load %struct.sockaddr*, %struct.sockaddr** %6
%9 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %8, i32 0, i32 0
%10 = load i64, i64* %9
%11 = load i64, i64* @AF_INET
%12 = icmp eq i64 %10, %11
br i1 %12, label %13, label %14
13: ; preds = %3
br label %22
14: ; preds = %3
%15 = load %struct.sockaddr*, %struct.sockaddr** %6
%16 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %15, i32 0, i32 0
%17 = load i64, i64* %16
%18 = load i64, i64* @AF_INET6
%19 = icmp eq i64 %17, %18
%20 = zext i1 %19 to i64
%21 = select i1 %19, i64 4, i64 0
br label %22
22: ; preds = %14, %13
%23 = phi i64 [ 4, %13 ], [ %21, %14 ]
%24 = trunc i64 %23 to i32
%25 = load i8*, i8** %4
%26 = load i32, i32* %5
%27 = load i32, i32* @NI_NUMERICHOST
%28 = call i32 @getnameinfo(%struct.sockaddr* %7, i32 %24, i8* %25, i32 %26, i32* null, i32 0, i32 %27)
ret i32 %28
}

LLVM IR (-O2):
%struct.sockaddr = type { i64 }
@AF_INET = global i64 0
@AF_INET6 = global i64 0
@NI_NUMERICHOST = global i32 0
define i32 @sa2str(i8* %0, i32 %1, %struct.sockaddr* %2) {
%4 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %2, i64 0, i32 0
%5 = load i64, i64* %4
%6 = load i64, i64* @AF_INET
%7 = icmp eq i64 %5, %6
%8 = load i64, i64* @AF_INET6
%9 = icmp eq i64 %5, %8
%10 = select i1 %7, i1 true, i1 %9
%11 = select i1 %10, i32 4, i32 0
%12 = load i32, i32* @NI_NUMERICHOST
%13 = tail call i32 @getnameinfo(%struct.sockaddr* %2, i32 %11, i8* %0, i32 %1, i32* null, i32 0, i32 %12)
ret i32 %13
}