#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 u_int8_t;
struct in_addr {
  int /*<<< orphan*/ s_addr;
};
typedef int socklen_t;

/* Variables and functions */
int /*<<< orphan*/ EAFNOSUPPORT;
int /*<<< orphan*/ ENOSPC;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ memcpy(char *, char *, int);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ snprintf(char *, int, char *, int const);
int strlen(char *);
const char *inet_ntop4(const struct in_addr *addr, char *dst, socklen_t size) {
  char tmp[16], *p;
  const u_int8_t *ap = (u_int8_t *)&addr->s_addr;
  int i, ql, len;
  if (addr == NULL) {
    errno = EAFNOSUPPORT;
    return NULL;
  }
  if (dst == NULL) {
    errno = ENOSPC;
    return NULL;
  }
  memset(tmp, 0, 16);
  len = 4;
  p = tmp;
  for (i = 0; i < 4; i++, ap++) {
    snprintf(p, 4, "%d", *ap);
    ql = strlen(p);
    len += ql;
    p += ql;
    if (i < 3)
      *p++ = '.';
  }
  if (len > size) {
    errno = ENOSPC;
    return NULL;
  }
  memcpy(dst, tmp, len);
  return dst;
}


x86 assembly (clang, -O0):
<inet_ntop4>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1185 <inet_ntop4+0x45>
mov    0x2e63(%rip),%rax
mov    (%rax),%ecx
mov    0x2e6a(%rip),%rax
mov    %ecx,(%rax)
movq   $0x0,-0x8(%rbp)
jmp    129e <inet_ntop4+0x15e>
cmpq   $0x0,-0x18(%rbp)
jne    11af <inet_ntop4+0x6f>
mov    0x2e41(%rip),%rax
mov    (%rax),%ecx
mov    0x2e40(%rip),%rax
mov    %ecx,(%rax)
movq   $0x0,-0x8(%rbp)
jmp    129e <inet_ntop4+0x15e>
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1050 <memset@plt>
movl   $0x4,-0x4c(%rbp)
lea    -0x30(%rbp),%rax
mov    %rax,-0x38(%rbp)
movl   $0x0,-0x44(%rbp)
cmpl   $0x4,-0x44(%rbp)
jge    125b <inet_ntop4+0x11b>
mov    -0x38(%rbp),%rdi
mov    -0x40(%rbp),%rax
mov    (%rax),%ecx
mov    $0x4,%esi
lea    0xe0b(%rip),%rdx
call   1040 <snprintf@plt>
mov    -0x38(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x48(%rbp)
mov    -0x48(%rbp),%eax
add    -0x4c(%rbp),%eax
mov    %eax,-0x4c(%rbp)
mov    -0x48(%rbp),%ecx
mov    -0x38(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x38(%rbp)
cmpl   $0x3,-0x44(%rbp)
jge    123c <inet_ntop4+0xfc>
mov    -0x38(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x38(%rbp)
movb   $0x2e,(%rax)
jmp    1241 <inet_ntop4+0x101>
mov    -0x44(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x44(%rbp)
mov    -0x40(%rbp),%rax
add    $0x4,%rax
mov    %rax,-0x40(%rbp)
jmp    11d5 <inet_ntop4+0x95>
mov    -0x4c(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jle    1286 <inet_ntop4+0x146>
mov    0x2d6a(%rip),%rax
mov    (%rax),%ecx
mov    0x2d69(%rip),%rax
mov    %ecx,(%rax)
movq   $0x0,-0x8(%rbp)
jmp    129e <inet_ntop4+0x15e>
mov    -0x18(%rbp),%rdi
lea    -0x30(%rbp),%rsi
mov    -0x4c(%rbp),%edx
call   1060 <memcpy@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<inet_ntop4>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
test   %rdi,%rdi
je     1251 <inet_ntop4+0x111>
mov    %rsi,%r13
test   %rsi,%rsi
je     1248 <inet_ntop4+0x108>
mov    %rdi,%rbp
lea    0x10(%rsp),%r12
mov    %r12,%rdi
xor    %esi,%esi
mov    %edx,0xc(%rsp)
mov    $0x10,%edx
call   1050 <memset@plt>
mov    0x0(%rbp),%ecx
lea    0xe78(%rip),%r15
mov    %r12,%rdi
mov    $0x4,%esi
mov    %r15,%rdx
call   1040 <snprintf@plt>
mov    %r12,%rdi
call   1030 <strlen@plt>
movslq %eax,%r14
lea    (%rsp,%r14,1),%rbx
add    $0x11,%rbx
movb   $0x2e,0x10(%rsp,%r14,1)
mov    0x4(%rbp),%ecx
mov    %rbx,%rdi
mov    $0x4,%esi
mov    %r15,%rdx
call   1040 <snprintf@plt>
mov    %rbx,%rdi
call   1030 <strlen@plt>
lea    (%r14,%rax,1),%r12d
lea    (%rsp,%r14,1),%rbx
add    $0x10,%rbx
movslq %eax,%r15
lea    0x2(%r15,%rbx,1),%r14
movb   $0x2e,0x1(%r15,%rbx,1)
mov    0x8(%rbp),%ecx
mov    %r14,%rdi
mov    $0x4,%esi
lea    0xe08(%rip),%rdx
call   1040 <snprintf@plt>
mov    %r14,%rdi
call   1030 <strlen@plt>
add    %eax,%r12d
lea    (%r15,%rbx,1),%rcx
add    $0x1,%rcx
cltq
lea    0x2(%rax,%rcx,1),%rbx
movb   $0x2e,0x1(%rax,%rcx,1)
mov    0xc(%rbp),%ecx
mov    %rbx,%rdi
mov    $0x4,%esi
lea    0xdd2(%rip),%rdx
call   1040 <snprintf@plt>
mov    %rbx,%rdi
call   1030 <strlen@plt>
lea    (%rax,%r12,1),%edx
add    $0x4,%edx
cmp    0xc(%rsp),%edx
jle    1268 <inet_ntop4+0x128>
mov    0x2d89(%rip),%rax
jmp    1258 <inet_ntop4+0x118>
mov    0x2d78(%rip),%rax
mov    (%rax),%eax
mov    0x2d7f(%rip),%rcx
mov    %eax,(%rcx)
xor    %r13d,%r13d
jmp    1275 <inet_ntop4+0x135>
lea    0x10(%rsp),%rsi
mov    %r13,%rdi
call   1060 <memcpy@plt>
mov    %r13,%rax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.in_addr = type { i32 }
@EAFNOSUPPORT = global i32 0
@errno = global i32 0
@ENOSPC = global i32 0
@.str = [3 x i8] c"%d\00"
define i8* @inet_ntop4(%struct.in_addr* %0, i8* %1, i32 %2) {
%4 = alloca i8*
%5 = alloca %struct.in_addr*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca [16 x i8]
%9 = alloca i8*
%10 = alloca i32*
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
store %struct.in_addr* %0, %struct.in_addr** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%14 = load %struct.in_addr*, %struct.in_addr** %5
%15 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %14, i32 0, i32 0
store i32* %15, i32** %10
%16 = load %struct.in_addr*, %struct.in_addr** %5
%17 = icmp eq %struct.in_addr* %16, null
br i1 %17, label %18, label %20
18: ; preds = %3
%19 = load i32, i32* @EAFNOSUPPORT
store i32 %19, i32* @errno
store i8* null, i8** %4
br label %69
20: ; preds = %3
%21 = load i8*, i8** %6
%22 = icmp eq i8* %21, null
br i1 %22, label %23, label %25
23: ; preds = %20
%24 = load i32, i32* @ENOSPC
store i32 %24, i32* @errno
store i8* null, i8** %4
br label %69
25: ; preds = %20
%26 = getelementptr inbounds [16 x i8], [16 x i8]* %8, i64 0, i64 0
%27 = call i32 @memset(i8* %26, i32 0, i32 16)
store i32 4, i32* %13
%28 = getelementptr inbounds [16 x i8], [16 x i8]* %8, i64 0, i64 0
store i8* %28, i8** %9
store i32 0, i32* %11
br label %29
29: ; preds = %52, %25
%30 = load i32, i32* %11
%31 = icmp slt i32 %30, 4
br i1 %31, label %32, label %57
32: ; preds = %29
%33 = load i8*, i8** %9
%34 = load i32*, i32** %10
%35 = load i32, i32* %34
%36 = call i32 @snprintf(i8* %33, i32 4, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %35)
%37 = load i8*, i8** %9
%38 = call i32 @strlen(i8* %37)
store i32 %38, i32* %12
%39 = load i32, i32* %12
%40 = load i32, i32* %13
%41 = add nsw i32 %40, %39
store i32 %41, i32* %13
%42 = load i32, i32* %12
%43 = load i8*, i8** %9
%44 = sext i32 %42 to i64
%45 = getelementptr inbounds i8, i8* %43, i64 %44
store i8* %45, i8** %9
%46 = load i32, i32* %11
%47 = icmp slt i32 %46, 3
br i1 %47, label %48, label %51
48: ; preds = %32
%49 = load i8*, i8** %9
%50 = getelementptr inbounds i8, i8* %49, i32 1
store i8* %50, i8** %9
store i8 46, i8* %49
br label %51
51: ; preds = %48, %32
br label %52
52: ; preds = %51
%53 = load i32, i32* %11
%54 = add nsw i32 %53, 1
store i32 %54, i32* %11
%55 = load i32*, i32** %10
%56 = getelementptr inbounds i32, i32* %55, i32 1
store i32* %56, i32** %10
br label %29
57: ; preds = %29
%58 = load i32, i32* %13
%59 = load i32, i32* %7
%60 = icmp sgt i32 %58, %59
br i1 %60, label %61, label %63
61: ; preds = %57
%62 = load i32, i32* @ENOSPC
store i32 %62, i32* @errno
store i8* null, i8** %4
br label %69
63: ; preds = %57
%64 = load i8*, i8** %6
%65 = getelementptr inbounds [16 x i8], [16 x i8]* %8, i64 0, i64 0
%66 = load i32, i32* %13
%67 = call i32 @memcpy(i8* %64, i8* %65, i32 %66)
%68 = load i8*, i8** %6
store i8* %68, i8** %4
br label %69
69: ; preds = %63, %61, %23, %18
%70 = load i8*, i8** %4
ret i8* %70
}

LLVM IR (-O2):
%struct.in_addr = type { i32 }
@EAFNOSUPPORT = global i32 0
@errno = global i32 0
@ENOSPC = global i32 0
@.str = [3 x i8] c"%d\00"
define i8* @inet_ntop4(%struct.in_addr* %0, i8* %1, i32 %2) {
%4 = alloca [16 x i8]
%5 = getelementptr inbounds [16 x i8], [16 x i8]* %4, i64 0, i64 0
%6 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %0, i64 0, i32 0
%7 = icmp eq %struct.in_addr* %0, null
br i1 %7, label %8, label %10
8: ; preds = %3
%9 = load i32, i32* @EAFNOSUPPORT
store i32 %9, i32* @errno
br label %49
10: ; preds = %3
%11 = icmp eq i8* %1, null
br i1 %11, label %12, label %14
12: ; preds = %10
%13 = load i32, i32* @ENOSPC
store i32 %13, i32* @errno
br label %49
14: ; preds = %10
%15 = call i32 @memset(i8* %5, i32 0, i32 16)
%16 = load i32, i32* %6
%17 = call i32 @snprintf(i8* %5, i32 4, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %16)
%18 = call i32 @strlen(i8* %5)
%19 = add nsw i32 %18, 4
%20 = sext i32 %18 to i64
%21 = getelementptr inbounds [16 x i8], [16 x i8]* %4, i64 0, i64 %20
%22 = getelementptr inbounds i8, i8* %21, i64 1
store i8 46, i8* %21
%23 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %0, i64 1, i32 0
%24 = load i32, i32* %23
%25 = call i32 @snprintf(i8* %22, i32 4, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %24)
%26 = call i32 @strlen(i8* %22)
%27 = add nsw i32 %26, %19
%28 = sext i32 %26 to i64
%29 = getelementptr inbounds i8, i8* %22, i64 %28
%30 = getelementptr inbounds i8, i8* %29, i64 1
store i8 46, i8* %29
%31 = getelementptr inbounds i32, i32* %23, i64 1
%32 = load i32, i32* %31
%33 = call i32 @snprintf(i8* %30, i32 4, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %32)
%34 = call i32 @strlen(i8* %30)
%35 = add nsw i32 %34, %27
%36 = sext i32 %34 to i64
%37 = getelementptr inbounds i8, i8* %30, i64 %36
%38 = getelementptr inbounds i8, i8* %37, i64 1
store i8 46, i8* %37
%39 = getelementptr inbounds i32, i32* %23, i64 2
%40 = load i32, i32* %39
%41 = call i32 @snprintf(i8* %38, i32 4, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %40)
%42 = call i32 @strlen(i8* %38)
%43 = add nsw i32 %42, %35
%44 = icmp sgt i32 %43, %2
br i1 %44, label %45, label %47
45: ; preds = %14
%46 = load i32, i32* @ENOSPC
store i32 %46, i32* @errno
br label %49
47: ; preds = %14
%48 = call i32 @memcpy(i8* %1, i8* %5, i32 %43)
br label %49
49: ; preds = %47, %45, %12, %8
%50 = phi i8* [ null, %8 ], [ null, %12 ], [ null, %45 ], [ %1, %47 ]
ret i8* %50
}