#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 */

/* Variables and functions */
int /*<<< orphan*/ asprintf(char **, char *, char const *, char const *);
int /*<<< orphan*/ audit_encode_value(char *, char const *, unsigned int);
scalar_t__ audit_value_needs_encoding(char const *, unsigned int);
int /*<<< orphan*/ free(char *);
char *malloc(int);
unsigned int strlen(char const *);
char *audit_encode_nv_string(const char *name, const char *value,
                             unsigned int vlen) {
  char *str;
  if (vlen == 0 && value)
    vlen = strlen(value);
  if (value && audit_value_needs_encoding(value, vlen)) {
    char *tmp = malloc(2 * vlen + 1);
    if (tmp) {
      audit_encode_value(tmp, value, vlen);
      asprintf(&str, "%s=%s", name, tmp);
      free(tmp);
    } else
      str = NULL;
  } else
    asprintf(&str, "%s=\"%s\"", name, value ? value : "?");
  return str;
}


x86 assembly (clang, -O0):
<audit_encode_nv_string>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jne    1194 <audit_encode_nv_string+0x34>
cmpq   $0x0,-0x10(%rbp)
je     1194 <audit_encode_nv_string+0x34>
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x14(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1215 <audit_encode_nv_string+0xb5>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1040 <audit_value_needs_encoding@plt>
cmp    $0x0,%rax
je     1215 <audit_encode_nv_string+0xb5>
mov    -0x14(%rbp),%edi
shl    $0x1,%edi
add    $0x1,%edi
call   1060 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     1208 <audit_encode_nv_string+0xa8>
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1080 <audit_encode_value@plt>
mov    -0x8(%rbp),%rdx
mov    -0x28(%rbp),%rcx
lea    -0x20(%rbp),%rdi
lea    0xe0b(%rip),%rsi
call   1070 <asprintf@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
jmp    1210 <audit_encode_nv_string+0xb0>
movq   $0x0,-0x20(%rbp)
jmp    125d <audit_encode_nv_string+0xfd>
mov    -0x8(%rbp),%rax
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1235 <audit_encode_nv_string+0xd5>
mov    -0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
jmp    1245 <audit_encode_nv_string+0xe5>
lea    0xdd2(%rip),%rax
mov    %rax,-0x38(%rbp)
jmp    1245 <audit_encode_nv_string+0xe5>
mov    -0x30(%rbp),%rdx
mov    -0x38(%rbp),%rcx
lea    -0x20(%rbp),%rdi
lea    0xdae(%rip),%rsi
call   1070 <asprintf@plt>
mov    -0x20(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<audit_encode_nv_string>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %edx,%r15d
mov    %rsi,%rbx
mov    %rdi,%r14
test   %rsi,%rsi
je     1186 <audit_encode_nv_string+0x26>
test   %r15d,%r15d
jne    1186 <audit_encode_nv_string+0x26>
mov    %rbx,%rdi
call   1050 <strlen@plt>
mov    %eax,%r15d
test   %rbx,%rbx
je     11dc <audit_encode_nv_string+0x7c>
mov    %rbx,%rdi
mov    %r15d,%esi
call   1040 <audit_value_needs_encoding@plt>
test   %rax,%rax
je     11e3 <audit_encode_nv_string+0x83>
lea    (%r15,%r15,1),%edi
add    $0x1,%edi
call   1060 <malloc@plt>
test   %rax,%rax
je     11fa <audit_encode_nv_string+0x9a>
mov    %rax,%r12
mov    %rax,%rdi
mov    %rbx,%rsi
mov    %r15d,%edx
call   1080 <audit_encode_value@plt>
lea    0xe3c(%rip),%rsi
mov    %rsp,%rdi
mov    %r14,%rdx
mov    %r12,%rcx
call   1070 <asprintf@plt>
mov    %r12,%rdi
call   1030 <free@plt>
jmp    1202 <audit_encode_nv_string+0xa2>
lea    0xe2b(%rip),%rbx
lea    0xe1c(%rip),%rsi
mov    %rsp,%rdi
mov    %r14,%rdx
mov    %rbx,%rcx
call   1070 <asprintf@plt>
jmp    1202 <audit_encode_nv_string+0xa2>
movq   $0x0,(%rsp)
mov    (%rsp),%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [6 x i8] c"%s=%s\00"
@.str.1 = [8 x i8] c"%s=\22%s\22\00"
@.str.2 = [2 x i8] c"?\00"
define i8* @audit_encode_nv_string(i8* %0, i8* %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i8*
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32 %2, i32* %6
%9 = load i32, i32* %6
%10 = icmp eq i32 %9, 0
br i1 %10, label %11, label %17
11: ; preds = %3
%12 = load i8*, i8** %5
%13 = icmp ne i8* %12, null
br i1 %13, label %14, label %17
14: ; preds = %11
%15 = load i8*, i8** %5
%16 = call i32 @strlen(i8* %15)
store i32 %16, i32* %6
br label %17
17: ; preds = %14, %11, %3
%18 = load i8*, i8** %5
%19 = icmp ne i8* %18, null
br i1 %19, label %20, label %44
20: ; preds = %17
%21 = load i8*, i8** %5
%22 = load i32, i32* %6
%23 = call i64 @audit_value_needs_encoding(i8* %21, i32 %22)
%24 = icmp ne i64 %23, 0
br i1 %24, label %25, label %44
25: ; preds = %20
%26 = load i32, i32* %6
%27 = mul i32 2, %26
%28 = add i32 %27, 1
%29 = call i8* @malloc(i32 %28)
store i8* %29, i8** %8
%30 = load i8*, i8** %8
%31 = icmp ne i8* %30, null
br i1 %31, label %32, label %42
32: ; preds = %25
%33 = load i8*, i8** %8
%34 = load i8*, i8** %5
%35 = load i32, i32* %6
%36 = call i32 @audit_encode_value(i8* %33, i8* %34, i32 %35)
%37 = load i8*, i8** %4
%38 = load i8*, i8** %8
%39 = call i32 @asprintf(i8** %7, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %37, i8* %38)
%40 = load i8*, i8** %8
%41 = call i32 @free(i8* %40)
br label %43
42: ; preds = %25
store i8* null, i8** %7
br label %43
43: ; preds = %42, %32
br label %54
44: ; preds = %20, %17
%45 = load i8*, i8** %4
%46 = load i8*, i8** %5
%47 = icmp ne i8* %46, null
br i1 %47, label %48, label %50
48: ; preds = %44
%49 = load i8*, i8** %5
br label %51
50: ; preds = %44
br label %51
51: ; preds = %50, %48
%52 = phi i8* [ %49, %48 ], [ getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), %50 ]
%53 = call i32 @asprintf(i8** %7, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i8* %45, i8* %52)
br label %54
54: ; preds = %51, %43
%55 = load i8*, i8** %7
ret i8* %55
}

LLVM IR (-O2):
@.str = [6 x i8] c"%s=%s\00"
@.str.1 = [8 x i8] c"%s=\22%s\22\00"
@.str.2 = [2 x i8] c"?\00"
define i8* @audit_encode_nv_string(i8* %0, i8* %1, i32 %2) {
%4 = alloca i8*
%5 = bitcast i8** %4 to i8*
%6 = icmp eq i32 %2, 0
%7 = icmp ne i8* %1, null
%8 = and i1 %7, %6
br i1 %8, label %9, label %11
9: ; preds = %3
%10 = tail call i32 @strlen(i8* %1)
br label %11
11: ; preds = %9, %3
%12 = phi i32 [ %10, %9 ], [ %2, %3 ]
br i1 %7, label %13, label %26
13: ; preds = %11
%14 = tail call i64 @audit_value_needs_encoding(i8* %1, i32 %12)
%15 = icmp eq i64 %14, 0
br i1 %15, label %26, label %16
16: ; preds = %13
%17 = shl i32 %12, 1
%18 = or i32 %17, 1
%19 = tail call i8* @malloc(i32 %18)
%20 = icmp eq i8* %19, null
br i1 %20, label %25, label %21
21: ; preds = %16
%22 = tail call i32 @audit_encode_value(i8* %19, i8* %1, i32 %12)
%23 = call i32 @asprintf(i8** %4, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %19)
%24 = call i32 @free(i8* %19)
br label %29
25: ; preds = %16
store i8* null, i8** %4
br label %29
26: ; preds = %13, %11
%27 = phi i8* [ %1, %13 ], [ getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), %11 ]
%28 = call i32 @asprintf(i8** %4, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i8* %0, i8* %27)
br label %29
29: ; preds = %21, %25, %26
%30 = load i8*, i8** %4
ret i8* %30
}