#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 /*<<< orphan*/ u_int8_t;
typedef int u_int32_t;
struct constant_map {
  int dummy;
};

/* Variables and functions */
char *constant_name_maps(struct constant_map **, int);
scalar_t__ extract_val(int /*<<< orphan*/ *, size_t, int *);
int /*<<< orphan*/ free(char *);
char *malloc(size_t);
char *realloc(char *, size_t);
int /*<<< orphan*/ strlcat(char *, char *, size_t);
int /*<<< orphan*/ strlcpy(char *, char *, size_t);
scalar_t__ strlen(char *);
char *field_debug_mask(u_int8_t *buf, size_t len, struct constant_map **maps) {
  u_int32_t val;
  u_int32_t bit;
  char *retval, *new_buf, *name;
  size_t buf_sz;
  if (extract_val(buf, len, &val))
    return 0;
  buf_sz = 4;
  retval = malloc(buf_sz);
  if (!retval)
    return 0;
  strlcpy(retval, "[ ", buf_sz);
  for (bit = 1; bit; bit <<= 1) {
    if (val & bit) {
      name = constant_name_maps(maps, bit);
      buf_sz += strlen(name) + 1;
      new_buf = realloc(retval, buf_sz);
      if (!new_buf) {
        free(retval);
        return 0;
      }
      retval = new_buf;
      strlcat(retval, name, buf_sz);
      strlcat(retval, " ", buf_sz);
    }
  }
  strlcat(retval, "]", buf_sz);
  return retval;
}


x86 assembly (clang, -O0):
<field_debug_mask>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
lea    -0x24(%rbp),%rdx
call   10a0 <extract_val@plt>
cmp    $0x0,%rax
je     11bc <field_debug_mask+0x3c>
movq   $0x0,-0x8(%rbp)
jmp    12d0 <field_debug_mask+0x150>
movq   $0x4,-0x48(%rbp)
mov    -0x48(%rbp),%rdi
call   1060 <malloc@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    11e9 <field_debug_mask+0x69>
movq   $0x0,-0x8(%rbp)
jmp    12d0 <field_debug_mask+0x150>
mov    -0x30(%rbp),%rdi
mov    -0x48(%rbp),%rdx
lea    0xe08(%rip),%rsi
call   1070 <strlcpy@plt>
movl   $0x1,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
je     12b4 <field_debug_mask+0x134>
mov    -0x24(%rbp),%eax
and    -0x28(%rbp),%eax
cmp    $0x0,%eax
je     12a1 <field_debug_mask+0x121>
mov    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%esi
call   1090 <constant_name_maps@plt>
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rdi
call   1050 <strlen@plt>
add    $0x1,%rax
add    -0x48(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x48(%rbp),%rsi
call   1080 <realloc@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    1274 <field_debug_mask+0xf4>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    12d0 <field_debug_mask+0x150>
mov    -0x38(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x40(%rbp),%rsi
mov    -0x48(%rbp),%rdx
call   1040 <strlcat@plt>
mov    -0x30(%rbp),%rdi
mov    -0x48(%rbp),%rdx
lea    0xd65(%rip),%rsi
call   1040 <strlcat@plt>
jmp    12a6 <field_debug_mask+0x126>
mov    -0x28(%rbp),%eax
shl    $0x1,%eax
mov    %eax,-0x28(%rbp)
jmp    1204 <field_debug_mask+0x84>
mov    -0x30(%rbp),%rdi
mov    -0x48(%rbp),%rdx
lea    0xd40(%rip),%rsi
call   1040 <strlcat@plt>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<field_debug_mask>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r14
lea    0x4(%rsp),%rdx
call   10a0 <extract_val@plt>
test   %rax,%rax
je     11b2 <field_debug_mask+0x32>
xor    %r12d,%r12d
mov    %r12,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    $0x4,%r13d
mov    $0x4,%edi
call   1060 <malloc@plt>
test   %rax,%rax
je     119d <field_debug_mask+0x1d>
mov    %rax,%r12
lea    0xe2f(%rip),%rsi
mov    $0x4,%edx
mov    %rax,%rdi
call   1070 <strlcpy@plt>
mov    $0x1,%ebp
jmp    11f4 <field_debug_mask+0x74>
cs nopw 0x0(%rax,%rax,1)
nop
add    %ebp,%ebp
je     124e <field_debug_mask+0xce>
test   %ebp,0x4(%rsp)
je     11f0 <field_debug_mask+0x70>
mov    %r14,%rdi
mov    %ebp,%esi
call   1090 <constant_name_maps@plt>
mov    %rax,%rbx
mov    %rax,%rdi
call   1050 <strlen@plt>
add    %rax,%r13
add    $0x1,%r13
mov    %r12,%rdi
mov    %r13,%rsi
call   1080 <realloc@plt>
test   %rax,%rax
je     1265 <field_debug_mask+0xe5>
mov    %rax,%r15
mov    %rax,%rdi
mov    %rbx,%rsi
mov    %r13,%rdx
call   1040 <strlcat@plt>
mov    %r15,%rdi
lea    0xdc0(%rip),%rsi
mov    %r13,%rdx
call   1040 <strlcat@plt>
mov    %r15,%r12
jmp    11f0 <field_debug_mask+0x70>
lea    0xdae(%rip),%rsi
mov    %r12,%rdi
mov    %r13,%rdx
call   1040 <strlcat@plt>
jmp    11a0 <field_debug_mask+0x20>
mov    %r12,%rdi
call   1030 <free@plt>
jmp    119d <field_debug_mask+0x1d>

LLVM IR (-O0):
%struct.constant_map = type { i32 }
@.str = [3 x i8] c"[ \00"
@.str.1 = [2 x i8] c" \00"
@.str.2 = [2 x i8] c"]\00"
define i8* @field_debug_mask(i32* %0, i64 %1, %struct.constant_map** %2) {
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca i64
%7 = alloca %struct.constant_map**
%8 = alloca i32
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i8*
%12 = alloca i8*
%13 = alloca i64
store i32* %0, i32** %5
store i64 %1, i64* %6
store %struct.constant_map** %2, %struct.constant_map*** %7
%14 = load i32*, i32** %5
%15 = load i64, i64* %6
%16 = call i64 @extract_val(i32* %14, i64 %15, i32* %8)
%17 = icmp ne i64 %16, 0
br i1 %17, label %18, label %19
18: ; preds = %3
store i8* null, i8** %4
br label %72
19: ; preds = %3
store i64 4, i64* %13
%20 = load i64, i64* %13
%21 = call i8* @malloc(i64 %20)
store i8* %21, i8** %10
%22 = load i8*, i8** %10
%23 = icmp ne i8* %22, null
br i1 %23, label %25, label %24
24: ; preds = %19
store i8* null, i8** %4
br label %72
25: ; preds = %19
%26 = load i8*, i8** %10
%27 = load i64, i64* %13
%28 = call i32 @strlcpy(i8* %26, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i64 %27)
store i32 1, i32* %9
br label %29
29: ; preds = %64, %25
%30 = load i32, i32* %9
%31 = icmp ne i32 %30, 0
br i1 %31, label %32, label %67
32: ; preds = %29
%33 = load i32, i32* %8
%34 = load i32, i32* %9
%35 = and i32 %33, %34
%36 = icmp ne i32 %35, 0
br i1 %36, label %37, label %63
37: ; preds = %32
%38 = load %struct.constant_map**, %struct.constant_map*** %7
%39 = load i32, i32* %9
%40 = call i8* @constant_name_maps(%struct.constant_map** %38, i32 %39)
store i8* %40, i8** %12
%41 = load i8*, i8** %12
%42 = call i64 @strlen(i8* %41)
%43 = add nsw i64 %42, 1
%44 = load i64, i64* %13
%45 = add i64 %44, %43
store i64 %45, i64* %13
%46 = load i8*, i8** %10
%47 = load i64, i64* %13
%48 = call i8* @realloc(i8* %46, i64 %47)
store i8* %48, i8** %11
%49 = load i8*, i8** %11
%50 = icmp ne i8* %49, null
br i1 %50, label %54, label %51
51: ; preds = %37
%52 = load i8*, i8** %10
%53 = call i32 @free(i8* %52)
store i8* null, i8** %4
br label %72
54: ; preds = %37
%55 = load i8*, i8** %11
store i8* %55, i8** %10
%56 = load i8*, i8** %10
%57 = load i8*, i8** %12
%58 = load i64, i64* %13
%59 = call i32 @strlcat(i8* %56, i8* %57, i64 %58)
%60 = load i8*, i8** %10
%61 = load i64, i64* %13
%62 = call i32 @strlcat(i8* %60, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i64 %61)
br label %63
63: ; preds = %54, %32
br label %64
64: ; preds = %63
%65 = load i32, i32* %9
%66 = shl i32 %65, 1
store i32 %66, i32* %9
br label %29
67: ; preds = %29
%68 = load i8*, i8** %10
%69 = load i64, i64* %13
%70 = call i32 @strlcat(i8* %68, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i64 %69)
%71 = load i8*, i8** %10
store i8* %71, i8** %4
br label %72
72: ; preds = %67, %51, %24, %18
%73 = load i8*, i8** %4
ret i8* %73
}

LLVM IR (-O2):
%struct.constant_map = type { i32 }
@.str = [3 x i8] c"[ \00"
@.str.1 = [2 x i8] c" \00"
@.str.2 = [2 x i8] c"]\00"
define i8* @field_debug_mask(i32* %0, i64 %1, %struct.constant_map** %2) {
%4 = alloca i32
%5 = bitcast i32* %4 to i8*
%6 = call i64 @extract_val(i32* %0, i64 %1, i32* %4)
%7 = icmp eq i64 %6, 0
br i1 %7, label %8, label %39
8: ; preds = %3
%9 = call dereferenceable_or_null(4) i8* @malloc(i64 4)
%10 = icmp eq i8* %9, null
br i1 %10, label %39, label %11
11: ; preds = %8
%12 = call i32 @strlcpy(i8* %9, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i64 4)
br label %13
13: ; preds = %11, %32
%14 = phi i64 [ 4, %11 ], [ %34, %32 ]
%15 = phi i32 [ 1, %11 ], [ %35, %32 ]
%16 = phi i8* [ %9, %11 ], [ %33, %32 ]
%17 = load i32, i32* %4
%18 = and i32 %17, %15
%19 = icmp eq i32 %18, 0
br i1 %19, label %32, label %20
20: ; preds = %13
%21 = call i8* @constant_name_maps(%struct.constant_map** %2, i32 %15)
%22 = call i64 @strlen(i8* dereferenceable(1) %21)
%23 = add i64 %14, 1
%24 = add i64 %23, %22
%25 = call i8* @realloc(i8* %16, i64 %24)
%26 = icmp eq i8* %25, null
br i1 %26, label %27, label %29
27: ; preds = %20
%28 = call i32 @free(i8* %16)
br label %39
29: ; preds = %20
%30 = call i32 @strlcat(i8* %25, i8* %21, i64 %24)
%31 = call i32 @strlcat(i8* %25, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i64 %24)
br label %32
32: ; preds = %13, %29
%33 = phi i8* [ %25, %29 ], [ %16, %13 ]
%34 = phi i64 [ %24, %29 ], [ %14, %13 ]
%35 = shl i32 %15, 1
%36 = icmp eq i32 %35, 0
br i1 %36, label %37, label %13
37: ; preds = %32
%38 = call i32 @strlcat(i8* %33, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i64 %34)
br label %39
39: ; preds = %8, %3, %37, %27
%40 = phi i8* [ null, %27 ], [ %33, %37 ], [ null, %3 ], [ null, %8 ]
ret i8* %40
}