#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 + 2)
    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, ";");
    strcat(buffer, tmp);
  }
  *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    $0x2,%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    1267 <kvp_process_ip_address+0x127>
mov    -0x20(%rbp),%rdi
lea    0xdbd(%rip),%rsi
call   1060 <strcat@plt>
mov    -0x20(%rbp),%rdi
lea    -0x80(%rbp),%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),%rax
jmp    118c <kvp_process_ip_address+0x4c>
mov    0x2e5c(%rip),%rax
mov    (%rax),%ecx
mov    (%r12),%eax
sub    %eax,%ebp
add    $0x2,%ecx
cmp    %ecx,%ebp
jl     11cf <kvp_process_ip_address+0x8f>
test   %r15,%r15
je     11c0 <kvp_process_ip_address+0x80>
test   %eax,%eax
je     11da <kvp_process_ip_address+0x9a>
lea    0xe67(%rip),%rsi
mov    %r14,%rdi
call   1060 <strcat@plt>
mov    %rsp,%rsi
mov    %r14,%rdi
call   1060 <strcat@plt>
jmp    11e5 <kvp_process_ip_address+0xa5>
lea    0xe39(%rip),%rsi
mov    %r14,%rdi
call   1040 <strcpy@plt>
mov    0x2dfa(%rip),%rax
mov    (%rax),%eax
jmp    11fc <kvp_process_ip_address+0xbc>
mov    %rsp,%rsi
mov    %r14,%rdi
call   1040 <strcpy@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, 2
%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 %70
64: ; preds = %56
%65 = load i8*, i8** %9
%66 = call i32 @strcat(i8* %65, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%67 = load i8*, i8** %9
%68 = getelementptr inbounds [50 x i8], [50 x i8]* %15, i64 0, i64 0
%69 = call i32 @strcat(i8* %67, i8* %68)
br label %70
70: ; preds = %64, %60
%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 = %70, %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 = add nsw i32 %14, 2
%18 = icmp slt i32 %16, %17
br i1 %18, label %19, label %21
19: ; preds = %5
%20 = load i32, i32* @HV_E_FAIL
br label %39
21: ; preds = %5
%22 = icmp eq i8* %11, null
br i1 %22, label %23, label %26
23: ; preds = %21
%24 = call i32 @strcpy(i8* %2, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
%25 = load i32, i32* @HV_E_FAIL
br label %39
26: ; preds = %21
%27 = icmp eq i32 %15, 0
br i1 %27, label %28, label %30
28: ; preds = %26
%29 = call i32 @strcpy(i8* %2, i8* %7)
br label %33
30: ; preds = %26
%31 = call i32 @strcat(i8* %2, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%32 = call i32 @strcat(i8* %2, i8* %7)
br label %33
33: ; preds = %30, %28
%34 = call i64 @strlen(i8* %11)
%35 = load i32, i32* %4
%36 = trunc i64 %34 to i32
%37 = add i32 %36, 1
%38 = add i32 %37, %35
store i32 %38, i32* %4
br label %39
39: ; preds = %33, %23, %19
%40 = phi i32 [ %20, %19 ], [ %25, %23 ], [ 0, %33 ]
ret i32 %40
}