#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 */
typedef struct TYPE_2__ TYPE_1__;

/* Type definitions */
struct TYPE_2__ {
  int /*<<< orphan*/ s6_addr;
};
struct sockaddr_in6 {
  TYPE_1__ sin6_addr;
};
struct sockaddr_in {
  int /*<<< orphan*/ sin_addr;
};

/* Variables and functions */
int AF_INET;
int HV_E_FAIL;
int INET6_ADDRSTRLEN;
int INET_ADDRSTRLEN;
char *inet_ntop(int, int /*<<< orphan*/ *, char *, int);
int /*<<< orphan*/ strcat(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
scalar_t__ strlen(char const *);
int kvp_process_ip_address(void *addrp, int family, char *buffer, int length,
                           int *offset) {
  struct sockaddr_in *addr;
  struct sockaddr_in6 *addr6;
  int addr_length;
  char tmp[50];
  const char *str;
  if (family == AF_INET) {
    addr = (struct sockaddr_in *)addrp;
    str = inet_ntop(family, &addr->sin_addr, tmp, 50);
    addr_length = INET_ADDRSTRLEN;
  } else {
    addr6 = (struct sockaddr_in6 *)addrp;
    str = inet_ntop(family, &addr6->sin6_addr.s6_addr, tmp, 50);
    addr_length = INET6_ADDRSTRLEN;
  }
  if ((length - *offset) < addr_length + 1)
    return HV_E_FAIL;
  if (str == NULL) {
    strcpy(buffer, "inet_ntop failed\n");
    return HV_E_FAIL;
  }
  if (*offset == 0)
    strcpy(buffer, tmp);
  else
    strcat(buffer, tmp);
  strcat(buffer, ";");
  *offset += strlen(str) + 1;
  return 0;
}


x86 assembly (clang, -O0):
<kvp_process_ip_address>:
push   %rbp
mov    %rsp,%rbp
sub    $0x90,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %ecx,-0x24(%rbp)
mov    %r8,-0x30(%rbp)
mov    -0x14(%rbp),%eax
mov    0x2e79(%rip),%rcx
cmp    (%rcx),%eax
jne    11a4 <kvp_process_ip_address+0x64>
mov    -0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x14(%rbp),%edi
mov    -0x38(%rbp),%rsi
lea    -0x80(%rbp),%rdx
mov    $0x32,%ecx
call   1030 <inet_ntop@plt>
mov    %rax,-0x88(%rbp)
mov    0x2e2e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x44(%rbp)
jmp    11d4 <kvp_process_ip_address+0x94>
mov    -0x10(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x14(%rbp),%edi
mov    -0x40(%rbp),%rsi
lea    -0x80(%rbp),%rdx
mov    $0x32,%ecx
call   1030 <inet_ntop@plt>
mov    %rax,-0x88(%rbp)
mov    0x2e19(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x44(%rbp)
mov    -0x24(%rbp),%eax
mov    -0x30(%rbp),%rcx
sub    (%rcx),%eax
mov    -0x44(%rbp),%ecx
add    $0x1,%ecx
cmp    %ecx,%eax
jge    11fc <kvp_process_ip_address+0xbc>
mov    0x2dde(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    128d <kvp_process_ip_address+0x14d>
cmpq   $0x0,-0x88(%rbp)
jne    122b <kvp_process_ip_address+0xeb>
mov    -0x20(%rbp),%rdi
lea    0xdeb(%rip),%rsi
call   1040 <strcpy@plt>
mov    0x2daf(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    128d <kvp_process_ip_address+0x14d>
mov    -0x30(%rbp),%rax
cmpl   $0x0,(%rax)
jne    124a <kvp_process_ip_address+0x10a>
mov    -0x20(%rbp),%rdi
lea    -0x80(%rbp),%rsi
call   1040 <strcpy@plt>
jmp    1257 <kvp_process_ip_address+0x117>
mov    -0x20(%rbp),%rdi
lea    -0x80(%rbp),%rsi
call   1060 <strcat@plt>
mov    -0x20(%rbp),%rdi
lea    0xdb0(%rip),%rsi
call   1060 <strcat@plt>
mov    -0x88(%rbp),%rdi
call   1050 <strlen@plt>
mov    %rax,%rdx
add    $0x1,%rdx
mov    -0x30(%rbp),%rax
movslq (%rax),%rcx
add    %rdx,%rcx
mov    %ecx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x90,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<kvp_process_ip_address>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    %r8,%r12
mov    %ecx,%ebp
mov    %rdx,%r14
mov    %esi,%ebx
mov    %rdi,%rsi
mov    0x2e7e(%rip),%rax
mov    (%rax),%r13d
mov    %rsp,%rdx
mov    %ebx,%edi
mov    $0x32,%ecx
call   1030 <inet_ntop@plt>
mov    %rax,%r15
cmp    %ebx,%r13d
jne    1185 <kvp_process_ip_address+0x45>
mov    0x2e45(%rip),%rcx
jmp    118c <kvp_process_ip_address+0x4c>
mov    0x2e5c(%rip),%rcx
mov    (%r12),%eax
sub    %eax,%ebp
cmp    (%rcx),%ebp
jle    11bb <kvp_process_ip_address+0x7b>
test   %r15,%r15
je     11ac <kvp_process_ip_address+0x6c>
mov    %rsp,%rsi
mov    %r14,%rdi
test   %eax,%eax
je     11c6 <kvp_process_ip_address+0x86>
call   1060 <strcat@plt>
jmp    11cb <kvp_process_ip_address+0x8b>
lea    0xe4d(%rip),%rsi
mov    %r14,%rdi
call   1040 <strcpy@plt>
mov    0x2e0e(%rip),%rax
mov    (%rax),%eax
jmp    11f1 <kvp_process_ip_address+0xb1>
call   1040 <strcpy@plt>
lea    0xe40(%rip),%rsi
mov    %r14,%rdi
call   1060 <strcat@plt>
mov    %r15,%rdi
call   1050 <strlen@plt>
mov    (%r12),%ecx
add    %ecx,%eax
add    $0x1,%eax
mov    %eax,(%r12)
xor    %eax,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32 }
%struct.sockaddr_in6 = type { %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i32 }
@AF_INET = global i32 0
@INET_ADDRSTRLEN = global i32 0
@INET6_ADDRSTRLEN = global i32 0
@HV_E_FAIL = global i32 0
@.str = [18 x i8] c"inet_ntop failed\0A\00"
@.str.1 = [2 x i8] c";\00"
define i32 @kvp_process_ip_address(i8* %0, i32 %1, i8* %2, i32 %3, i32* %4) {
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i32*
%12 = alloca %struct.sockaddr_in*
%13 = alloca %struct.sockaddr_in6*
%14 = alloca i32
%15 = alloca [50 x i8]
%16 = alloca i8*
store i8* %0, i8** %7
store i32 %1, i32* %8
store i8* %2, i8** %9
store i32 %3, i32* %10
store i32* %4, i32** %11
%17 = load i32, i32* %8
%18 = load i32, i32* @AF_INET
%19 = icmp eq i32 %17, %18
br i1 %19, label %20, label %29
20: ; preds = %5
%21 = load i8*, i8** %7
%22 = bitcast i8* %21 to %struct.sockaddr_in*
store %struct.sockaddr_in* %22, %struct.sockaddr_in** %12
%23 = load i32, i32* %8
%24 = load %struct.sockaddr_in*, %struct.sockaddr_in** %12
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %24, i32 0, i32 0
%26 = getelementptr inbounds [50 x i8], [50 x i8]* %15, i64 0, i64 0
%27 = call i8* @inet_ntop(i32 %23, i32* %25, i8* %26, i32 50)
store i8* %27, i8** %16
%28 = load i32, i32* @INET_ADDRSTRLEN
store i32 %28, i32* %14
br label %39
29: ; preds = %5
%30 = load i8*, i8** %7
%31 = bitcast i8* %30 to %struct.sockaddr_in6*
store %struct.sockaddr_in6* %31, %struct.sockaddr_in6** %13
%32 = load i32, i32* %8
%33 = load %struct.sockaddr_in6*, %struct.sockaddr_in6** %13
%34 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %33, i32 0, i32 0
%35 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %34, i32 0, i32 0
%36 = getelementptr inbounds [50 x i8], [50 x i8]* %15, i64 0, i64 0
%37 = call i8* @inet_ntop(i32 %32, i32* %35, i8* %36, i32 50)
store i8* %37, i8** %16
%38 = load i32, i32* @INET6_ADDRSTRLEN
store i32 %38, i32* %14
br label %39
39: ; preds = %29, %20
%40 = load i32, i32* %10
%41 = load i32*, i32** %11
%42 = load i32, i32* %41
%43 = sub nsw i32 %40, %42
%44 = load i32, i32* %14
%45 = add nsw i32 %44, 1
%46 = icmp slt i32 %43, %45
br i1 %46, label %47, label %49
47: ; preds = %39
%48 = load i32, i32* @HV_E_FAIL
store i32 %48, i32* %6
br label %79
49: ; preds = %39
%50 = load i8*, i8** %16
%51 = icmp eq i8* %50, null
br i1 %51, label %52, label %56
52: ; preds = %49
%53 = load i8*, i8** %9
%54 = call i32 @strcpy(i8* %53, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
%55 = load i32, i32* @HV_E_FAIL
store i32 %55, i32* %6
br label %79
56: ; preds = %49
%57 = load i32*, i32** %11
%58 = load i32, i32* %57
%59 = icmp eq i32 %58, 0
br i1 %59, label %60, label %64
60: ; preds = %56
%61 = load i8*, i8** %9
%62 = getelementptr inbounds [50 x i8], [50 x i8]* %15, i64 0, i64 0
%63 = call i32 @strcpy(i8* %61, i8* %62)
br label %68
64: ; preds = %56
%65 = load i8*, i8** %9
%66 = getelementptr inbounds [50 x i8], [50 x i8]* %15, i64 0, i64 0
%67 = call i32 @strcat(i8* %65, i8* %66)
br label %68
68: ; preds = %64, %60
%69 = load i8*, i8** %9
%70 = call i32 @strcat(i8* %69, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%71 = load i8*, i8** %16
%72 = call i64 @strlen(i8* %71)
%73 = add nsw i64 %72, 1
%74 = load i32*, i32** %11
%75 = load i32, i32* %74
%76 = sext i32 %75 to i64
%77 = add nsw i64 %76, %73
%78 = trunc i64 %77 to i32
store i32 %78, i32* %74
store i32 0, i32* %6
br label %79
79: ; preds = %68, %52, %47
%80 = load i32, i32* %6
ret i32 %80
}

LLVM IR (-O2):
@AF_INET = global i32 0
@INET_ADDRSTRLEN = global i32 0
@INET6_ADDRSTRLEN = global i32 0
@HV_E_FAIL = global i32 0
@.str = [18 x i8] c"inet_ntop failed\0A\00"
@.str.1 = [2 x i8] c";\00"
define i32 @kvp_process_ip_address(i8* %0, i32 %1, i8* %2, i32 %3, i32* nocapture %4) {
%6 = alloca [50 x i8]
%7 = getelementptr inbounds [50 x i8], [50 x i8]* %6, i64 0, i64 0
%8 = load i32, i32* @AF_INET
%9 = icmp eq i32 %8, %1
%10 = bitcast i8* %0 to i32*
%11 = call i8* @inet_ntop(i32 %1, i32* %10, i8* %7, i32 50)
%12 = load i32, i32* @INET_ADDRSTRLEN
%13 = load i32, i32* @INET6_ADDRSTRLEN
%14 = select i1 %9, i32 %12, i32 %13
%15 = load i32, i32* %4
%16 = sub nsw i32 %3, %15
%17 = icmp sgt i32 %16, %14
br i1 %17, label %20, label %18
18: ; preds = %5
%19 = load i32, i32* @HV_E_FAIL
br label %38
20: ; preds = %5
%21 = icmp eq i8* %11, null
br i1 %21, label %22, label %25
22: ; preds = %20
%23 = call i32 @strcpy(i8* %2, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
%24 = load i32, i32* @HV_E_FAIL
br label %38
25: ; preds = %20
%26 = icmp eq i32 %15, 0
br i1 %26, label %27, label %29
27: ; preds = %25
%28 = call i32 @strcpy(i8* %2, i8* %7)
br label %31
29: ; preds = %25
%30 = call i32 @strcat(i8* %2, i8* %7)
br label %31
31: ; preds = %29, %27
%32 = call i32 @strcat(i8* %2, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%33 = call i64 @strlen(i8* %11)
%34 = load i32, i32* %4
%35 = trunc i64 %33 to i32
%36 = add i32 %35, 1
%37 = add i32 %36, %34
store i32 %37, i32* %4
br label %38
38: ; preds = %31, %22, %18
%39 = phi i32 [ %19, %18 ], [ %24, %22 ], [ 0, %31 ]
ret i32 %39
}