#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 size_t socklen_t;

/* Variables and functions */
int /*<<< orphan*/ EAFNOSUPPORT;
int /*<<< orphan*/ ENOSPC;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ memcpy(char *, char *, size_t);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ snprintf(char *, int, char *, int const);
size_t 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;
  size_t 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    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1186 <inet_ntop4+0x46>
mov    0x2e62(%rip),%rax
mov    (%rax),%ecx
mov    0x2e69(%rip),%rax
mov    %ecx,(%rax)
movq   $0x0,-0x8(%rbp)
jmp    12a2 <inet_ntop4+0x162>
cmpq   $0x0,-0x18(%rbp)
jne    11b0 <inet_ntop4+0x70>
mov    0x2e40(%rip),%rax
mov    (%rax),%ecx
mov    0x2e3f(%rip),%rax
mov    %ecx,(%rax)
movq   $0x0,-0x8(%rbp)
jmp    12a2 <inet_ntop4+0x162>
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1050 <memset@plt>
movq   $0x4,-0x58(%rbp)
lea    -0x30(%rbp),%rax
mov    %rax,-0x38(%rbp)
movl   $0x0,-0x44(%rbp)
cmpl   $0x4,-0x44(%rbp)
jge    125c <inet_ntop4+0x11c>
mov    -0x38(%rbp),%rdi
mov    -0x40(%rbp),%rax
mov    (%rax),%ecx
mov    $0x4,%esi
lea    0xe09(%rip),%rdx
call   1040 <snprintf@plt>
mov    -0x38(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x50(%rbp)
mov    -0x50(%rbp),%rax
add    -0x58(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    -0x50(%rbp),%rax
add    -0x38(%rbp),%rax
mov    %rax,-0x38(%rbp)
cmpl   $0x3,-0x44(%rbp)
jge    123d <inet_ntop4+0xfd>
mov    -0x38(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x38(%rbp)
movb   $0x2e,(%rax)
jmp    1242 <inet_ntop4+0x102>
mov    -0x44(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x44(%rbp)
mov    -0x40(%rbp),%rax
add    $0x4,%rax
mov    %rax,-0x40(%rbp)
jmp    11d7 <inet_ntop4+0x97>
mov    -0x58(%rbp),%rax
cmp    -0x20(%rbp),%rax
jbe    1289 <inet_ntop4+0x149>
mov    0x2d67(%rip),%rax
mov    (%rax),%ecx
mov    0x2d66(%rip),%rax
mov    %ecx,(%rax)
movq   $0x0,-0x8(%rbp)
jmp    12a2 <inet_ntop4+0x162>
mov    -0x18(%rbp),%rdi
lea    -0x30(%rbp),%rsi
mov    -0x58(%rbp),%rdx
call   1060 <memcpy@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<inet_ntop4>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
test   %rdi,%rdi
je     1250 <inet_ntop4+0x110>
mov    %rsi,%r15
test   %rsi,%rsi
je     1247 <inet_ntop4+0x107>
mov    %rdi,%r13
mov    %rsp,%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    %rdx,0x10(%rsp)
mov    $0x10,%edx
call   1050 <memset@plt>
mov    0x0(%r13),%ecx
lea    0xe78(%rip),%r14
mov    %rbx,%rdi
mov    $0x4,%esi
mov    %r14,%rdx
call   1040 <snprintf@plt>
mov    %rbx,%rdi
call   1030 <strlen@plt>
mov    %rax,%rbx
lea    (%rsp,%rax,1),%rbp
add    $0x1,%rbp
movb   $0x2e,(%rsp,%rax,1)
mov    0x4(%r13),%ecx
mov    %rbp,%rdi
mov    $0x4,%esi
mov    %r14,%rdx
call   1040 <snprintf@plt>
mov    %rbp,%rdi
call   1030 <strlen@plt>
mov    %rax,%rbp
lea    (%rsp,%rbx,1),%r12
add    %rax,%rbx
lea    (%rax,%r12,1),%r14
add    $0x2,%r14
movb   $0x2e,0x1(%rax,%r12,1)
mov    0x8(%r13),%ecx
mov    %r14,%rdi
mov    $0x4,%esi
lea    0xe0a(%rip),%rdx
call   1040 <snprintf@plt>
mov    %r14,%rdi
call   1030 <strlen@plt>
add    %rax,%rbx
lea    (%r12,%rbp,1),%rcx
add    $0x1,%rcx
lea    0x2(%rax,%rcx,1),%rbp
movb   $0x2e,0x1(%rax,%rcx,1)
mov    0xc(%r13),%ecx
mov    %rbp,%rdi
mov    $0x4,%esi
lea    0xdd5(%rip),%rdx
call   1040 <snprintf@plt>
mov    %rbp,%rdi
call   1030 <strlen@plt>
lea    (%rax,%rbx,1),%rdx
add    $0x4,%rdx
cmp    0x10(%rsp),%rdx
jbe    1267 <inet_ntop4+0x127>
mov    0x2d8a(%rip),%rax
jmp    1257 <inet_ntop4+0x117>
mov    0x2d79(%rip),%rax
mov    (%rax),%eax
mov    0x2d80(%rip),%rcx
mov    %eax,(%rcx)
xor    %r15d,%r15d
jmp    1272 <inet_ntop4+0x132>
mov    %rsp,%rsi
mov    %r15,%rdi
call   1060 <memcpy@plt>
mov    %r15,%rax
add    $0x18,%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, i64 %2) {
%4 = alloca i8*
%5 = alloca %struct.in_addr*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca [16 x i8]
%9 = alloca i8*
%10 = alloca i32*
%11 = alloca i32
%12 = alloca i64
%13 = alloca i64
store %struct.in_addr* %0, %struct.in_addr** %5
store i8* %1, i8** %6
store i64 %2, i64* %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 %68
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 %68
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 i64 4, i64* %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 = %51, %25
%30 = load i32, i32* %11
%31 = icmp slt i32 %30, 4
br i1 %31, label %32, label %56
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 i64 @strlen(i8* %37)
store i64 %38, i64* %12
%39 = load i64, i64* %12
%40 = load i64, i64* %13
%41 = add i64 %40, %39
store i64 %41, i64* %13
%42 = load i64, i64* %12
%43 = load i8*, i8** %9
%44 = getelementptr inbounds i8, i8* %43, i64 %42
store i8* %44, i8** %9
%45 = load i32, i32* %11
%46 = icmp slt i32 %45, 3
br i1 %46, label %47, label %50
47: ; preds = %32
%48 = load i8*, i8** %9
%49 = getelementptr inbounds i8, i8* %48, i32 1
store i8* %49, i8** %9
store i8 46, i8* %48
br label %50
50: ; preds = %47, %32
br label %51
51: ; preds = %50
%52 = load i32, i32* %11
%53 = add nsw i32 %52, 1
store i32 %53, i32* %11
%54 = load i32*, i32** %10
%55 = getelementptr inbounds i32, i32* %54, i32 1
store i32* %55, i32** %10
br label %29
56: ; preds = %29
%57 = load i64, i64* %13
%58 = load i64, i64* %7
%59 = icmp ugt i64 %57, %58
br i1 %59, label %60, label %62
60: ; preds = %56
%61 = load i32, i32* @ENOSPC
store i32 %61, i32* @errno
store i8* null, i8** %4
br label %68
62: ; preds = %56
%63 = load i8*, i8** %6
%64 = getelementptr inbounds [16 x i8], [16 x i8]* %8, i64 0, i64 0
%65 = load i64, i64* %13
%66 = call i32 @memcpy(i8* %63, i8* %64, i64 %65)
%67 = load i8*, i8** %6
store i8* %67, i8** %4
br label %68
68: ; preds = %62, %60, %23, %18
%69 = load i8*, i8** %4
ret i8* %69
}

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, i64 %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 %46
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 %46
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 i64 @strlen(i8* dereferenceable(1) %5)
%19 = add i64 %18, 4
%20 = getelementptr inbounds [16 x i8], [16 x i8]* %4, i64 0, i64 %18
%21 = getelementptr inbounds i8, i8* %20, i64 1
store i8 46, i8* %20
%22 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %0, i64 1, i32 0
%23 = load i32, i32* %22
%24 = call i32 @snprintf(i8* %21, i32 4, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %23)
%25 = call i64 @strlen(i8* dereferenceable(1) %21)
%26 = add i64 %25, %19
%27 = getelementptr inbounds i8, i8* %21, i64 %25
%28 = getelementptr inbounds i8, i8* %27, i64 1
store i8 46, i8* %27
%29 = getelementptr inbounds i32, i32* %22, i64 1
%30 = load i32, i32* %29
%31 = call i32 @snprintf(i8* %28, i32 4, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %30)
%32 = call i64 @strlen(i8* dereferenceable(1) %28)
%33 = add i64 %32, %26
%34 = getelementptr inbounds i8, i8* %28, i64 %32
%35 = getelementptr inbounds i8, i8* %34, i64 1
store i8 46, i8* %34
%36 = getelementptr inbounds i32, i32* %22, i64 2
%37 = load i32, i32* %36
%38 = call i32 @snprintf(i8* %35, i32 4, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %37)
%39 = call i64 @strlen(i8* dereferenceable(1) %35)
%40 = add i64 %39, %33
%41 = icmp ugt i64 %40, %2
br i1 %41, label %42, label %44
42: ; preds = %14
%43 = load i32, i32* @ENOSPC
store i32 %43, i32* @errno
br label %46
44: ; preds = %14
%45 = call i32 @memcpy(i8* %1, i8* %5, i64 %40)
br label %46
46: ; preds = %44, %42, %12, %8
%47 = phi i8* [ null, %8 ], [ null, %12 ], [ null, %42 ], [ %1, %44 ]
ret i8* %47
}