#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 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 0x80004005;
  if (str == NULL) {
    strcpy(buffer, "inet_ntop failed\n");
    return 0x80004005;
  }
  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    0x2e36(%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    11f7 <kvp_process_ip_address+0xb7>
movl   $0x80004005,-0x4(%rbp)
jmp    1283 <kvp_process_ip_address+0x143>
cmpq   $0x0,-0x88(%rbp)
jne    1221 <kvp_process_ip_address+0xe1>
mov    -0x20(%rbp),%rdi
lea    0xdf0(%rip),%rsi
call   1040 <strcpy@plt>
movl   $0x80004005,-0x4(%rbp)
jmp    1283 <kvp_process_ip_address+0x143>
mov    -0x30(%rbp),%rax
cmpl   $0x0,(%rax)
jne    1240 <kvp_process_ip_address+0x100>
mov    -0x20(%rbp),%rdi
lea    -0x80(%rbp),%rsi
call   1040 <strcpy@plt>
jmp    125d <kvp_process_ip_address+0x11d>
mov    -0x20(%rbp),%rdi
lea    0xdc7(%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    0x2e4d(%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
mov    $0x80004005,%ebx
cmp    %ecx,%ebp
jl     11f8 <kvp_process_ip_address+0xb8>
test   %r15,%r15
je     11c5 <kvp_process_ip_address+0x85>
test   %eax,%eax
je     11d6 <kvp_process_ip_address+0x96>
lea    0xe62(%rip),%rsi
mov    %r14,%rdi
call   1060 <strcat@plt>
mov    %rsp,%rsi
mov    %r14,%rdi
call   1060 <strcat@plt>
jmp    11e1 <kvp_process_ip_address+0xa1>
lea    0xe34(%rip),%rsi
mov    %r14,%rdi
call   1040 <strcpy@plt>
jmp    11f8 <kvp_process_ip_address+0xb8>
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    %ebx,%ebx
mov    %ebx,%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
@.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 %48
47: ; preds = %39
store i32 -2147467259, i32* %6
br label %77
48: ; preds = %39
%49 = load i8*, i8** %16
%50 = icmp eq i8* %49, null
br i1 %50, label %51, label %54
51: ; preds = %48
%52 = load i8*, i8** %9
%53 = call i32 @strcpy(i8* %52, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
store i32 -2147467259, i32* %6
br label %77
54: ; preds = %48
%55 = load i32*, i32** %11
%56 = load i32, i32* %55
%57 = icmp eq i32 %56, 0
br i1 %57, label %58, label %62
58: ; preds = %54
%59 = load i8*, i8** %9
%60 = getelementptr inbounds [50 x i8], [50 x i8]* %15, i64 0, i64 0
%61 = call i32 @strcpy(i8* %59, i8* %60)
br label %68
62: ; preds = %54
%63 = load i8*, i8** %9
%64 = call i32 @strcat(i8* %63, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%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 = %62, %58
%69 = load i8*, i8** %16
%70 = call i64 @strlen(i8* %69)
%71 = add nsw i64 %70, 1
%72 = load i32*, i32** %11
%73 = load i32, i32* %72
%74 = sext i32 %73 to i64
%75 = add nsw i64 %74, %71
%76 = trunc i64 %75 to i32
store i32 %76, i32* %72
store i32 0, i32* %6
br label %77
77: ; preds = %68, %51, %47
%78 = load i32, i32* %6
ret i32 %78
}

LLVM IR (-O2):
@AF_INET = global i32 0
@INET_ADDRSTRLEN = global i32 0
@INET6_ADDRSTRLEN = 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 %36, label %19
19: ; preds = %5
%20 = icmp eq i8* %11, null
br i1 %20, label %21, label %23
21: ; preds = %19
%22 = call i32 @strcpy(i8* %2, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
br label %36
23: ; preds = %19
%24 = icmp eq i32 %15, 0
br i1 %24, label %25, label %27
25: ; preds = %23
%26 = call i32 @strcpy(i8* %2, i8* %7)
br label %30
27: ; preds = %23
%28 = call i32 @strcat(i8* %2, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%29 = call i32 @strcat(i8* %2, i8* %7)
br label %30
30: ; preds = %27, %25
%31 = call i64 @strlen(i8* %11)
%32 = load i32, i32* %4
%33 = trunc i64 %31 to i32
%34 = add i32 %33, 1
%35 = add i32 %34, %32
store i32 %35, i32* %4
br label %36
36: ; preds = %5, %30, %21
%37 = phi i32 [ -2147467259, %21 ], [ 0, %30 ], [ -2147467259, %5 ]
ret i32 %37
}