#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;
};
struct sockaddr {
  int sa_family;
};
typedef int /*<<< orphan*/ str;
typedef int socklen_t;
typedef int /*<<< orphan*/ portstr;

/* Variables and functions */
#define AF_INET 128
int /*<<< orphan*/ *inet_ntop(int const, int /*<<< orphan*/ *, char *, int);
int ntohs(int /*<<< orphan*/);
int /*<<< orphan*/ snprintf(char *, int, char *, int, ...);
int /*<<< orphan*/ strcat(char *, char *);
char *sock_ntop(const struct sockaddr *sa, socklen_t salen) {
  char portstr[8];
  static char str[128];
  switch (sa->sa_family) {
  case AF_INET: {
    struct sockaddr_in *sin = (struct sockaddr_in *)sa;
    if (inet_ntop(AF_INET, &sin->sin_addr, str, sizeof(str)) == NULL) {
      return NULL;
    }
    if (ntohs(sin->sin_port) != 0) {
      snprintf(portstr, sizeof(portstr), ":%d", ntohs(sin->sin_port));
      strcat(str, portstr);
    }
    return str;
  }
  default:
    snprintf(str, sizeof(str),
             "sock_ntop: unknown AF_xxx: %d, "
             "len: %d",
             sa->sa_family, salen);
    return str;
  }
  return NULL;
}


x86 assembly (clang, -O0):
<sock_ntop>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%eax
sub    $0x80,%eax
jne    11ff <sock_ntop+0xbf>
jmp    1165 <sock_ntop+0x25>
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rsi
add    $0x4,%rsi
mov    $0x80,%ecx
lea    0x2ecf(%rip),%rdx
mov    %ecx,%edi
call   1030 <inet_ntop@plt>
cmp    $0x0,%rax
jne    119f <sock_ntop+0x5f>
movq   $0x0,-0x8(%rbp)
jmp    122e <sock_ntop+0xee>
mov    -0x28(%rbp),%rax
mov    (%rax),%edi
call   1050 <ntohs@plt>
cmp    $0x0,%eax
je     11ef <sock_ntop+0xaf>
lea    -0x1c(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%rax
mov    (%rax),%edi
call   1050 <ntohs@plt>
mov    -0x30(%rbp),%rdi
mov    %eax,%ecx
mov    $0x8,%esi
lea    0xe28(%rip),%rdx
mov    $0x0,%al
call   1040 <snprintf@plt>
lea    -0x1c(%rbp),%rsi
lea    0x2e66(%rip),%rdi
call   1060 <strcat@plt>
lea    0x2e5a(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    122e <sock_ntop+0xee>
mov    -0x10(%rbp),%rax
mov    (%rax),%ecx
mov    -0x14(%rbp),%r8d
lea    0x2e40(%rip),%rdi
mov    $0x80,%esi
lea    0xde8(%rip),%rdx
mov    $0x0,%al
call   1040 <snprintf@plt>
lea    0x2e26(%rip),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sock_ntop>:
push   %r14
push   %rbx
push   %rax
mov    (%rdi),%ecx
cmp    $0x80,%ecx
jne    11b4 <sock_ntop+0x74>
mov    %rdi,%rbx
lea    0x4(%rdi),%rsi
lea    0x2ef4(%rip),%r14
mov    $0x80,%edi
mov    %r14,%rdx
mov    $0x80,%ecx
call   1030 <inet_ntop@plt>
test   %rax,%rax
je     11d6 <sock_ntop+0x96>
mov    (%rbx),%edi
call   1050 <ntohs@plt>
test   %eax,%eax
je     11d9 <sock_ntop+0x99>
mov    (%rbx),%edi
call   1050 <ntohs@plt>
lea    0xe74(%rip),%rdx
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x8,%esi
mov    %eax,%ecx
xor    %eax,%eax
call   1040 <snprintf@plt>
lea    0x2ea9(%rip),%r14
mov    %r14,%rdi
mov    %rbx,%rsi
call   1060 <strcat@plt>
jmp    11d9 <sock_ntop+0x99>
mov    %esi,%r8d
lea    0x2e92(%rip),%r14
lea    0xe3f(%rip),%rdx
mov    %r14,%rdi
mov    $0x80,%esi
xor    %eax,%eax
call   1040 <snprintf@plt>
jmp    11d9 <sock_ntop+0x99>
xor    %r14d,%r14d
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.sockaddr = type { i32 }
%struct.sockaddr_in = type { i32, i32 }
@sock_ntop.str = internal global [128 x i8] zeroinitializer
@.str = [4 x i8] c":%d\00"
@.str.1 = [39 x i8] c"sock_ntop: unknown AF_xxx: %d, len: %d\00"
define i8* @sock_ntop(%struct.sockaddr* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca %struct.sockaddr*
%5 = alloca i32
%6 = alloca [8 x i8]
%7 = alloca %struct.sockaddr_in*
store %struct.sockaddr* %0, %struct.sockaddr** %4
store i32 %1, i32* %5
%8 = load %struct.sockaddr*, %struct.sockaddr** %4
%9 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %8, i32 0, i32 0
%10 = load i32, i32* %9
switch i32 %10, label %35 [
i32 128, label %11
]
11: ; preds = %2
%12 = load %struct.sockaddr*, %struct.sockaddr** %4
%13 = bitcast %struct.sockaddr* %12 to %struct.sockaddr_in*
store %struct.sockaddr_in* %13, %struct.sockaddr_in** %7
%14 = load %struct.sockaddr_in*, %struct.sockaddr_in** %7
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %14, i32 0, i32 1
%16 = call i32* @inet_ntop(i32 128, i32* %15, i8* getelementptr inbounds ([128 x i8], [128 x i8]* @sock_ntop.str, i64 0, i64 0), i32 128)
%17 = icmp eq i32* %16, null
br i1 %17, label %18, label %19
18: ; preds = %11
store i8* null, i8** %3
br label %41
19: ; preds = %11
%20 = load %struct.sockaddr_in*, %struct.sockaddr_in** %7
%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 = icmp ne i32 %23, 0
br i1 %24, label %25, label %34
25: ; preds = %19
%26 = getelementptr inbounds [8 x i8], [8 x i8]* %6, i64 0, i64 0
%27 = load %struct.sockaddr_in*, %struct.sockaddr_in** %7
%28 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %27, i32 0, i32 0
%29 = load i32, i32* %28
%30 = call i32 @ntohs(i32 %29)
%31 = call i32 (i8*, i32, i8*, i32, ...) @snprintf(i8* %26, i32 8, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %30)
%32 = getelementptr inbounds [8 x i8], [8 x i8]* %6, i64 0, i64 0
%33 = call i32 @strcat(i8* getelementptr inbounds ([128 x i8], [128 x i8]* @sock_ntop.str, i64 0, i64 0), i8* %32)
br label %34
34: ; preds = %25, %19
store i8* getelementptr inbounds ([128 x i8], [128 x i8]* @sock_ntop.str, i64 0, i64 0), i8** %3
br label %41
35: ; preds = %2
%36 = load %struct.sockaddr*, %struct.sockaddr** %4
%37 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %36, i32 0, i32 0
%38 = load i32, i32* %37
%39 = load i32, i32* %5
%40 = call i32 (i8*, i32, i8*, i32, ...) @snprintf(i8* getelementptr inbounds ([128 x i8], [128 x i8]* @sock_ntop.str, i64 0, i64 0), i32 128, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0), i32 %38, i32 %39)
store i8* getelementptr inbounds ([128 x i8], [128 x i8]* @sock_ntop.str, i64 0, i64 0), i8** %3
br label %41
41: ; preds = %35, %34, %18
%42 = load i8*, i8** %3
ret i8* %42
}

LLVM IR (-O2):
%struct.sockaddr = type { i32 }
@sock_ntop.str = internal global [128 x i8] zeroinitializer
@.str = [4 x i8] c":%d\00"
@.str.1 = [39 x i8] c"sock_ntop: unknown AF_xxx: %d, len: %d\00"
define i8* @sock_ntop(%struct.sockaddr* %0, i32 %1) {
%3 = alloca [8 x i8]
%4 = getelementptr inbounds [8 x i8], [8 x i8]* %3, i64 0, i64 0
%5 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %0, i64 0, i32 0
%6 = load i32, i32* %5
%7 = icmp eq i32 %6, 128
br i1 %7, label %8, label %21
8: ; preds = %2
%9 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %0, i64 1, i32 0
%10 = tail call i32* @inet_ntop(i32 128, i32* %9, i8* getelementptr inbounds ([128 x i8], [128 x i8]* @sock_ntop.str, i64 0, i64 0), i32 128)
%11 = icmp eq i32* %10, null
br i1 %11, label %23, label %12
12: ; preds = %8
%13 = load i32, i32* %5
%14 = tail call i32 @ntohs(i32 %13)
%15 = icmp eq i32 %14, 0
br i1 %15, label %23, label %16
16: ; preds = %12
%17 = load i32, i32* %5
%18 = tail call i32 @ntohs(i32 %17)
%19 = call i32 (i8*, i32, i8*, i32, ...) @snprintf(i8* %4, i32 8, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %18)
%20 = call i32 @strcat(i8* getelementptr inbounds ([128 x i8], [128 x i8]* @sock_ntop.str, i64 0, i64 0), i8* %4)
br label %23
21: ; preds = %2
%22 = tail call i32 (i8*, i32, i8*, i32, ...) @snprintf(i8* getelementptr inbounds ([128 x i8], [128 x i8]* @sock_ntop.str, i64 0, i64 0), i32 128, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0), i32 %6, i32 %1)
br label %23
23: ; preds = %8, %16, %12, %21
%24 = phi i8* [ getelementptr inbounds ([128 x i8], [128 x i8]* @sock_ntop.str, i64 0, i64 0), %21 ], [ null, %8 ], [ getelementptr inbounds ([128 x i8], [128 x i8]* @sock_ntop.str, i64 0, i64 0), %16 ], [ getelementptr inbounds ([128 x i8], [128 x i8]* @sock_ntop.str, i64 0, i64 0), %12 ]
ret i8* %24
}