#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 */
typedef int socklen_t;

/* Variables and functions */
int AF_INET;
int /*<<< orphan*/ EAFNOSUPPORT;
int /*<<< orphan*/ EINVAL;
int /*<<< orphan*/ ENOSPC;
int /*<<< orphan*/ errno;
int snprintf(char *, int, char *, unsigned char const, unsigned char const,
             unsigned char const, unsigned char const);
const char *inet_ntop(int af, const void *src, char *dst, socklen_t cnt) {
  const unsigned char *p = src;
  int n;
  if (af != AF_INET) {
    errno = EAFNOSUPPORT;
    return (NULL);
  }
  if (!src || !dst) {
    errno = EINVAL;
    return (NULL);
  }
  n = snprintf(dst, cnt, "%d.%d.%d.%d", p[0], p[1], p[2], p[3]);
  if ((n < 0) || (n >= cnt)) {
    errno = ENOSPC;
    return (NULL);
  }
  return (dst);
}


x86 assembly (clang, -O0):
<inet_ntop>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0xc(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %ecx,-0x24(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0xc(%rbp),%eax
mov    0x2eb0(%rip),%rcx
cmp    (%rcx),%eax
je     115f <inet_ntop+0x4f>
mov    0x2e79(%rip),%rax
mov    (%rax),%ecx
mov    0x2e88(%rip),%rax
mov    %ecx,(%rax)
movq   $0x0,-0x8(%rbp)
jmp    1213 <inet_ntop+0x103>
cmpq   $0x0,-0x18(%rbp)
je     1175 <inet_ntop+0x65>
cmpq   $0x0,-0x20(%rbp)
jne    1194 <inet_ntop+0x84>
mov    0x2e54(%rip),%rax
mov    (%rax),%ecx
mov    0x2e53(%rip),%rax
mov    %ecx,(%rax)
movq   $0x0,-0x8(%rbp)
jmp    1213 <inet_ntop+0x103>
mov    -0x20(%rbp),%rdi
mov    -0x24(%rbp),%esi
mov    -0x30(%rbp),%rax
mov    (%rax),%cl
mov    -0x30(%rbp),%rax
mov    0x1(%rax),%r8b
mov    -0x30(%rbp),%rax
mov    0x2(%rax),%r9b
mov    -0x30(%rbp),%rax
lea    0xe44(%rip),%rdx
movzbl %cl,%ecx
movzbl %r8b,%r8d
movzbl %r9b,%r9d
movzbl 0x3(%rax),%eax
mov    %eax,(%rsp)
call   1030 <snprintf@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
jl     11ec <inet_ntop+0xdc>
mov    -0x34(%rbp),%eax
cmp    -0x24(%rbp),%eax
jl     120b <inet_ntop+0xfb>
mov    0x2dd5(%rip),%rax
mov    (%rax),%ecx
mov    0x2ddc(%rip),%rax
mov    %ecx,(%rax)
movq   $0x0,-0x8(%rbp)
jmp    1213 <inet_ntop+0x103>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<inet_ntop>:
push   %rbp
push   %rbx
push   %rax
mov    0x2ece(%rip),%rax
cmp    %edi,(%rax)
jne    116a <inet_ntop+0x5a>
mov    %ecx,%ebp
mov    0x2ea9(%rip),%rcx
test   %rsi,%rsi
je     1171 <inet_ntop+0x61>
mov    %rdx,%rbx
test   %rdx,%rdx
je     1171 <inet_ntop+0x61>
movzbl (%rsi),%ecx
movzbl 0x1(%rsi),%r8d
movzbl 0x2(%rsi),%r9d
movzbl 0x3(%rsi),%eax
mov    %eax,(%rsp)
lea    0xeb1(%rip),%rdx
mov    %rbx,%rdi
mov    %ebp,%esi
call   1030 <snprintf@plt>
mov    0x2e68(%rip),%rcx
test   %eax,%eax
js     1171 <inet_ntop+0x61>
cmp    %ebp,%eax
jl     117e <inet_ntop+0x6e>
jmp    1171 <inet_ntop+0x61>
mov    0x2e4f(%rip),%rcx
mov    (%rcx),%eax
mov    0x2e5e(%rip),%rcx
mov    %eax,(%rcx)
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@AF_INET = global i32 0
@EAFNOSUPPORT = global i32 0
@errno = global i32 0
@EINVAL = global i32 0
@.str = [12 x i8] c"%d.%d.%d.%d\00"
@ENOSPC = global i32 0
define i8* @inet_ntop(i32 %0, i8* %1, i8* %2, i32 %3) {
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i32
store i32 %0, i32* %6
store i8* %1, i8** %7
store i8* %2, i8** %8
store i32 %3, i32* %9
%12 = load i8*, i8** %7
store i8* %12, i8** %10
%13 = load i32, i32* %6
%14 = load i32, i32* @AF_INET
%15 = icmp ne i32 %13, %14
br i1 %15, label %16, label %18
16: ; preds = %4
%17 = load i32, i32* @EAFNOSUPPORT
store i32 %17, i32* @errno
store i8* null, i8** %5
br label %52
18: ; preds = %4
%19 = load i8*, i8** %7
%20 = icmp ne i8* %19, null
br i1 %20, label %21, label %24
21: ; preds = %18
%22 = load i8*, i8** %8
%23 = icmp ne i8* %22, null
br i1 %23, label %26, label %24
24: ; preds = %21, %18
%25 = load i32, i32* @EINVAL
store i32 %25, i32* @errno
store i8* null, i8** %5
br label %52
26: ; preds = %21
%27 = load i8*, i8** %8
%28 = load i32, i32* %9
%29 = load i8*, i8** %10
%30 = getelementptr inbounds i8, i8* %29, i64 0
%31 = load i8, i8* %30
%32 = load i8*, i8** %10
%33 = getelementptr inbounds i8, i8* %32, i64 1
%34 = load i8, i8* %33
%35 = load i8*, i8** %10
%36 = getelementptr inbounds i8, i8* %35, i64 2
%37 = load i8, i8* %36
%38 = load i8*, i8** %10
%39 = getelementptr inbounds i8, i8* %38, i64 3
%40 = load i8, i8* %39
%41 = call i32 @snprintf(i8* %27, i32 %28, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8 zeroext %31, i8 zeroext %34, i8 zeroext %37, i8 zeroext %40)
store i32 %41, i32* %11
%42 = load i32, i32* %11
%43 = icmp slt i32 %42, 0
br i1 %43, label %48, label %44
44: ; preds = %26
%45 = load i32, i32* %11
%46 = load i32, i32* %9
%47 = icmp sge i32 %45, %46
br i1 %47, label %48, label %50
48: ; preds = %44, %26
%49 = load i32, i32* @ENOSPC
store i32 %49, i32* @errno
store i8* null, i8** %5
br label %52
50: ; preds = %44
%51 = load i8*, i8** %8
store i8* %51, i8** %5
br label %52
52: ; preds = %50, %48, %24, %16
%53 = load i8*, i8** %5
ret i8* %53
}

LLVM IR (-O2):
@AF_INET = global i32 0
@EAFNOSUPPORT = global i32 0
@errno = global i32 0
@EINVAL = global i32 0
@.str = [12 x i8] c"%d.%d.%d.%d\00"
@ENOSPC = global i32 0
define i8* @inet_ntop(i32 %0, i8* %1, i8* %2, i32 %3) {
%5 = load i32, i32* @AF_INET
%6 = icmp eq i32 %5, %0
br i1 %6, label %7, label %23
7: ; preds = %4
%8 = icmp ne i8* %1, null
%9 = icmp ne i8* %2, null
%10 = and i1 %8, %9
br i1 %10, label %11, label %23
11: ; preds = %7
%12 = load i8, i8* %1
%13 = getelementptr inbounds i8, i8* %1, i64 1
%14 = load i8, i8* %13
%15 = getelementptr inbounds i8, i8* %1, i64 2
%16 = load i8, i8* %15
%17 = getelementptr inbounds i8, i8* %1, i64 3
%18 = load i8, i8* %17
%19 = tail call i32 @snprintf(i8* %2, i32 %3, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8 zeroext %12, i8 zeroext %14, i8 zeroext %16, i8 zeroext %18)
%20 = icmp sgt i32 %19, -1
%21 = icmp slt i32 %19, %3
%22 = and i1 %20, %21
br i1 %22, label %26, label %23
23: ; preds = %11, %7, %4
%24 = phi i32* [ @EAFNOSUPPORT, %4 ], [ @EINVAL, %7 ], [ @ENOSPC, %11 ]
%25 = load i32, i32* %24
store i32 %25, i32* @errno
br label %26
26: ; preds = %23, %11
%27 = phi i8* [ %2, %11 ], [ null, %23 ]
ret i8* %27
}