#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 uint16_t;

/* Variables and functions */
char *calloc(int, int);
char *capability_name(int);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ memset(char *, int, int);
int /*<<< orphan*/ sprintf(char *, char *, char *);
int /*<<< orphan*/ strncat(char *, char *, int);
char *decode_tlv_system_capabilities(uint16_t system_capabilities,
                                     uint16_t enabled_capabilities) {
  int capability = 0;
  char *result = calloc(1, 2048);
  char *capability_string = calloc(1, 2048);
  for (capability = 1; capability <= 65535; capability *= 2) {
    memset(capability_string, 0x0, 2048);
    if ((system_capabilities & capability)) {
      if (enabled_capabilities & capability) {
        sprintf(capability_string, "\n\t\t\t\t%s (enabled)",
                capability_name(capability));
        strncat(result, capability_string, 2048);
      } else {
        sprintf(capability_string, "\n\t\t\t\t%s (disabled)",
                capability_name(capability));
        strncat(result, capability_string, 2048);
      }
    }
  }
  free(capability_string);
  return result;
}


x86 assembly (clang, -O0):
<decode_tlv_system_capabilities>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
mov    $0x1,%edi
mov    $0x800,%esi
call   1070 <calloc@plt>
mov    %rax,-0x18(%rbp)
mov    $0x1,%edi
mov    $0x800,%esi
call   1070 <calloc@plt>
mov    %rax,-0x20(%rbp)
movl   $0x1,-0xc(%rbp)
cmpl   $0xffff,-0xc(%rbp)
jg     1264 <decode_tlv_system_capabilities+0x104>
mov    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x800,%edx
call   1050 <memset@plt>
mov    -0x4(%rbp),%eax
and    -0xc(%rbp),%eax
cmp    $0x0,%eax
je     1251 <decode_tlv_system_capabilities+0xf1>
mov    -0x8(%rbp),%eax
and    -0xc(%rbp),%eax
cmp    $0x0,%eax
je     1217 <decode_tlv_system_capabilities+0xb7>
mov    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0xc(%rbp),%edi
call   1040 <capability_name@plt>
mov    -0x28(%rbp),%rdi
mov    %rax,%rdx
lea    0xe05(%rip),%rsi
call   1080 <sprintf@plt>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    $0x800,%edx
call   1060 <strncat@plt>
jmp    124c <decode_tlv_system_capabilities+0xec>
mov    -0x20(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0xc(%rbp),%edi
call   1040 <capability_name@plt>
mov    -0x30(%rbp),%rdi
mov    %rax,%rdx
lea    0xddd(%rip),%rsi
call   1080 <sprintf@plt>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    $0x800,%edx
call   1060 <strncat@plt>
jmp    1251 <decode_tlv_system_capabilities+0xf1>
jmp    1256 <decode_tlv_system_capabilities+0xf6>
mov    -0xc(%rbp),%eax
shl    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    11a2 <decode_tlv_system_capabilities+0x42>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x18(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<decode_tlv_system_capabilities>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %esi,%r14d
mov    %edi,%r12d
mov    $0x1,%ebx
mov    $0x1,%edi
mov    $0x800,%esi
call   1070 <calloc@plt>
mov    %rax,%r15
mov    $0x1,%edi
mov    $0x800,%esi
call   1070 <calloc@plt>
mov    %rax,%rbp
lea    0xe5f(%rip),%r13
jmp    11bd <decode_tlv_system_capabilities+0x5d>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
lea    (%rbx,%rbx,1),%eax
cmp    $0x8000,%ebx
mov    %eax,%ebx
jae    1206 <decode_tlv_system_capabilities+0xa6>
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0x800,%edx
call   1050 <memset@plt>
test   %r12d,%ebx
je     11b0 <decode_tlv_system_capabilities+0x50>
mov    %ebx,%edi
call   1040 <capability_name@plt>
test   %r14d,%ebx
mov    %r13,%rsi
lea    0xe2d(%rip),%rcx
cmove  %rcx,%rsi
mov    %rbp,%rdi
mov    %rax,%rdx
call   1080 <sprintf@plt>
mov    %r15,%rdi
mov    %rbp,%rsi
mov    $0x800,%edx
call   1060 <strncat@plt>
jmp    11b0 <decode_tlv_system_capabilities+0x50>
mov    %rbp,%rdi
call   1030 <free@plt>
mov    %r15,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [18 x i8] c"\0A\09\09\09\09%s (enabled)\00"
@.str.1 = [19 x i8] c"\0A\09\09\09\09%s (disabled)\00"
define i8* @decode_tlv_system_capabilities(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
store i32 %0, i32* %3
store i32 %1, i32* %4
store i32 0, i32* %5
%8 = call i8* @calloc(i32 1, i32 2048)
store i8* %8, i8** %6
%9 = call i8* @calloc(i32 1, i32 2048)
store i8* %9, i8** %7
store i32 1, i32* %5
br label %10
10: ; preds = %43, %2
%11 = load i32, i32* %5
%12 = icmp sle i32 %11, 65535
br i1 %12, label %13, label %46
13: ; preds = %10
%14 = load i8*, i8** %7
%15 = call i32 @memset(i8* %14, i32 0, i32 2048)
%16 = load i32, i32* %3
%17 = load i32, i32* %5
%18 = and i32 %16, %17
%19 = icmp ne i32 %18, 0
br i1 %19, label %20, label %42
20: ; preds = %13
%21 = load i32, i32* %4
%22 = load i32, i32* %5
%23 = and i32 %21, %22
%24 = icmp ne i32 %23, 0
br i1 %24, label %25, label %33
25: ; preds = %20
%26 = load i8*, i8** %7
%27 = load i32, i32* %5
%28 = call i8* @capability_name(i32 %27)
%29 = call i32 @sprintf(i8* %26, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %28)
%30 = load i8*, i8** %6
%31 = load i8*, i8** %7
%32 = call i32 @strncat(i8* %30, i8* %31, i32 2048)
br label %41
33: ; preds = %20
%34 = load i8*, i8** %7
%35 = load i32, i32* %5
%36 = call i8* @capability_name(i32 %35)
%37 = call i32 @sprintf(i8* %34, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8* %36)
%38 = load i8*, i8** %6
%39 = load i8*, i8** %7
%40 = call i32 @strncat(i8* %38, i8* %39, i32 2048)
br label %41
41: ; preds = %33, %25
br label %42
42: ; preds = %41, %13
br label %43
43: ; preds = %42
%44 = load i32, i32* %5
%45 = mul nsw i32 %44, 2
store i32 %45, i32* %5
br label %10
46: ; preds = %10
%47 = load i8*, i8** %7
%48 = call i32 @free(i8* %47)
%49 = load i8*, i8** %6
ret i8* %49
}

LLVM IR (-O2):
@.str = [18 x i8] c"\0A\09\09\09\09%s (enabled)\00"
@.str.1 = [19 x i8] c"\0A\09\09\09\09%s (disabled)\00"
define i8* @decode_tlv_system_capabilities(i32 %0, i32 %1) {
%3 = tail call dereferenceable_or_null(2048) i8* @calloc(i32 1, i32 2048)
%4 = tail call dereferenceable_or_null(2048) i8* @calloc(i32 1, i32 2048)
br label %5
5: ; preds = %2, %17
%6 = phi i32 [ 1, %2 ], [ %18, %17 ]
%7 = tail call i32 @memset(i8* %4, i32 0, i32 2048)
%8 = and i32 %6, %0
%9 = icmp eq i32 %8, 0
br i1 %9, label %17, label %10
10: ; preds = %5
%11 = and i32 %6, %1
%12 = icmp eq i32 %11, 0
%13 = tail call i8* @capability_name(i32 %6)
%14 = select i1 %12, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0)
%15 = tail call i32 @sprintf(i8* dereferenceable(1) %4, i8* dereferenceable(1) %14, i8* %13)
%16 = tail call i32 @strncat(i8* %3, i8* %4, i32 2048)
br label %17
17: ; preds = %10, %5
%18 = shl nuw nsw i32 %6, 1
%19 = icmp ult i32 %6, 32768
br i1 %19, label %5, label %20
20: ; preds = %17
%21 = tail call i32 @free(i8* %4)
ret i8* %3
}