#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 {
  int sa_family;
};

/* Variables and functions */
#define AF_INET 129
#define AF_INET6 128
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ *inet_ntop(int const, int /*<<< orphan*/ *, char *, int);
int /*<<< orphan*/ printf(char *, ...);
char *strerror(int /*<<< orphan*/);
void sock_ntop_host(struct sockaddr *sa, char ip[]) {
  struct sockaddr_in *sin;
  struct sockaddr_in6 *sin6;
  switch (sa->sa_family) {
  case AF_INET:
    printf("AF_INET.\n");
    sin = (struct sockaddr_in *)sa;
    if (NULL == inet_ntop(AF_INET, &(sin->sin_addr), ip, 16)) {
      printf("inet_ntop : %s\n", strerror(errno));
      exit(EXIT_FAILURE);
    }
    break;
  case AF_INET6:
    printf("AF_INET6.\n");
    sin6 = (struct sockaddr_in6 *)sa;
    if (NULL == inet_ntop(AF_INET6, &(sin6->sin6_addr), ip, 16)) {
      printf("inet_ntop : %s\n", strerror(errno));
      exit(EXIT_FAILURE);
    }
    break;
  default:
    printf("unknown type.\n");
    exit(EXIT_FAILURE);
  }
}


x86 assembly (clang, -O0):
<sock_ntop_host>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
sub    $0x80,%eax
je     11e9 <sock_ntop_host+0xa9>
jmp    1169 <sock_ntop_host+0x29>
mov    -0x24(%rbp),%eax
sub    $0x81,%eax
jne    1256 <sock_ntop_host+0x116>
jmp    117c <sock_ntop_host+0x3c>
lea    0xe7d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x8(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rsi
mov    -0x10(%rbp),%rdx
mov    $0x81,%edi
mov    $0x10,%ecx
call   1030 <inet_ntop@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
jne    11e4 <sock_ntop_host+0xa4>
mov    0x2e22(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
mov    %rax,%rsi
lea    0xe3b(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2dfb(%rip),%rax
mov    (%rax),%edi
call   1050 <exit@plt>
jmp    1272 <sock_ntop_host+0x132>
lea    0xe2a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x8(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rsi
mov    -0x10(%rbp),%rdx
mov    $0x80,%edi
mov    $0x10,%ecx
call   1030 <inet_ntop@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
jne    1251 <sock_ntop_host+0x111>
mov    0x2db5(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
mov    %rax,%rsi
lea    0xdce(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2d8e(%rip),%rax
mov    (%rax),%edi
call   1050 <exit@plt>
jmp    1272 <sock_ntop_host+0x132>
lea    0xdc8(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2d6d(%rip),%rax
mov    (%rax),%edi
call   1050 <exit@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sock_ntop_host>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbx
mov    (%rdi),%eax
cmp    $0x80,%eax
je     117d <sock_ntop_host+0x2d>
cmp    $0x81,%eax
jne    11cc <sock_ntop_host+0x7c>
lea    0xea9(%rip),%rdi
call   1040 <puts@plt>
mov    $0x81,%edi
jmp    118e <sock_ntop_host+0x3e>
lea    0xe8c(%rip),%rdi
call   1040 <puts@plt>
mov    $0x80,%edi
mov    %rbx,%rsi
mov    %r14,%rdx
mov    $0x10,%ecx
call   1030 <inet_ntop@plt>
test   %rax,%rax
je     11ab <sock_ntop_host+0x5b>
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
mov    0x2e2e(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
lea    0xe40(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    11d8 <sock_ntop_host+0x88>
lea    0xe50(%rip),%rdi
call   1040 <puts@plt>
mov    0x2df9(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr = type { i32 }
%struct.sockaddr_in = type { i32 }
%struct.sockaddr_in6 = type { i32 }
@.str = [10 x i8] c"AF_INET.\0A\00"
@.str.1 = [16 x i8] c"inet_ntop : %s\0A\00"
@errno = global i32 0
@EXIT_FAILURE = global i32 0
@.str.2 = [11 x i8] c"AF_INET6.\0A\00"
@.str.3 = [15 x i8] c"unknown type.\0A\00"
define void @sock_ntop_host(%struct.sockaddr* %0, i8* %1) {
%3 = alloca %struct.sockaddr*
%4 = alloca i8*
%5 = alloca %struct.sockaddr_in*
%6 = alloca %struct.sockaddr_in6*
store %struct.sockaddr* %0, %struct.sockaddr** %3
store i8* %1, i8** %4
%7 = load %struct.sockaddr*, %struct.sockaddr** %3
%8 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %7, i32 0, i32 0
%9 = load i32, i32* %8
switch i32 %9, label %42 [
i32 129, label %10
i32 128, label %26
]
10: ; preds = %2
%11 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%12 = load %struct.sockaddr*, %struct.sockaddr** %3
%13 = bitcast %struct.sockaddr* %12 to %struct.sockaddr_in*
store %struct.sockaddr_in* %13, %struct.sockaddr_in** %5
%14 = load %struct.sockaddr_in*, %struct.sockaddr_in** %5
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %14, i32 0, i32 0
%16 = load i8*, i8** %4
%17 = call i32* @inet_ntop(i32 129, i32* %15, i8* %16, i32 16)
%18 = icmp eq i32* null, %17
br i1 %18, label %19, label %25
19: ; preds = %10
%20 = load i32, i32* @errno
%21 = call i8* @strerror(i32 %20)
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8* %21)
%23 = load i32, i32* @EXIT_FAILURE
%24 = call i32 @exit(i32 %23)
unreachable
25: ; preds = %10
br label %46
26: ; preds = %2
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
%28 = load %struct.sockaddr*, %struct.sockaddr** %3
%29 = bitcast %struct.sockaddr* %28 to %struct.sockaddr_in6*
store %struct.sockaddr_in6* %29, %struct.sockaddr_in6** %6
%30 = load %struct.sockaddr_in6*, %struct.sockaddr_in6** %6
%31 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %30, i32 0, i32 0
%32 = load i8*, i8** %4
%33 = call i32* @inet_ntop(i32 128, i32* %31, i8* %32, i32 16)
%34 = icmp eq i32* null, %33
br i1 %34, label %35, label %41
35: ; preds = %26
%36 = load i32, i32* @errno
%37 = call i8* @strerror(i32 %36)
%38 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8* %37)
%39 = load i32, i32* @EXIT_FAILURE
%40 = call i32 @exit(i32 %39)
unreachable
41: ; preds = %26
br label %46
42: ; preds = %2
%43 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0))
%44 = load i32, i32* @EXIT_FAILURE
%45 = call i32 @exit(i32 %44)
unreachable
46: ; preds = %41, %25
ret void
}

LLVM IR (-O2):
%struct.sockaddr = type { i32 }
@.str.1 = [16 x i8] c"inet_ntop : %s\0A\00"
@errno = global i32 0
@EXIT_FAILURE = global i32 0
@str = [10 x i8] c"AF_INET6.\00"
@str.4 = [9 x i8] c"AF_INET.\00"
@str.5 = [14 x i8] c"unknown type.\00"
define void @sock_ntop_host(%struct.sockaddr* %0, i8* %1) {
%3 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %0, i64 0, i32 0
%4 = load i32, i32* %3
switch i32 %4, label %25 [
i32 129, label %5
i32 128, label %15
]
5: ; preds = %2
%6 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @str.4, i64 0, i64 0))
%7 = tail call i32* @inet_ntop(i32 129, i32* %3, i8* %1, i32 16)
%8 = icmp eq i32* %7, null
br i1 %8, label %9, label %29
9: ; preds = %5
%10 = load i32, i32* @errno
%11 = tail call i8* @strerror(i32 %10)
%12 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8* %11)
%13 = load i32, i32* @EXIT_FAILURE
%14 = tail call i32 @exit(i32 %13)
unreachable
15: ; preds = %2
%16 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @str, i64 0, i64 0))
%17 = tail call i32* @inet_ntop(i32 128, i32* %3, i8* %1, i32 16)
%18 = icmp eq i32* %17, null
br i1 %18, label %19, label %29
19: ; preds = %15
%20 = load i32, i32* @errno
%21 = tail call i8* @strerror(i32 %20)
%22 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8* %21)
%23 = load i32, i32* @EXIT_FAILURE
%24 = tail call i32 @exit(i32 %23)
unreachable
25: ; preds = %2
%26 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str.5, i64 0, i64 0))
%27 = load i32, i32* @EXIT_FAILURE
%28 = tail call i32 @exit(i32 %27)
unreachable
29: ; preds = %15, %5
ret void
}