#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*/ snprintf(char *, int, char *, ...);
int /*<<< orphan*/ sprintf(char *, char *, unsigned int);
int /*<<< orphan*/ strlen(char *);
void print_bitfield(unsigned long value, char **bit_names, char *buf,
                    size_t size) {
  unsigned int bit, mask;
  const char *sepa = "";
  char *buf_end;
  if (value == 0) {
    snprintf(buf, size, "<NIL>");
    return;
  }
  buf_end = buf + size;
  for (bit = 0, mask = 1; mask; ++bit, mask <<= 1) {
    char namebuf[16], *name;
    if (!(value & mask))
      continue;
    if ((name = bit_names[bit]) == NULL) {
      sprintf(namebuf, "bit%u", bit);
      name = namebuf;
    }
    snprintf(buf, buf_end - buf, "%s%s", sepa, name);
    buf += strlen(buf);
    sepa = ", ";
  }
}


x86 assembly (clang, -O0):
<print_bitfield>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
lea    0xec4(%rip),%rax
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    117b <print_bitfield+0x4b>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rax
mov    %eax,%esi
lea    0xe91(%rip),%rdx
mov    $0x0,%al
call   1040 <snprintf@plt>
jmp    124e <print_bitfield+0x11e>
mov    -0x18(%rbp),%rax
add    -0x20(%rbp),%rax
mov    %rax,-0x38(%rbp)
movl   $0x0,-0x24(%rbp)
movl   $0x1,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
je     124e <print_bitfield+0x11e>
mov    -0x8(%rbp),%rax
mov    -0x28(%rbp),%ecx
and    %rcx,%rax
cmp    $0x0,%rax
jne    11b8 <print_bitfield+0x88>
jmp    1237 <print_bitfield+0x107>
mov    -0x10(%rbp),%rax
mov    -0x24(%rbp),%ecx
mov    (%rax,%rcx,8),%rax
mov    %rax,-0x58(%rbp)
cmp    $0x0,%rax
jne    11ec <print_bitfield+0xbc>
lea    -0x50(%rbp),%rdi
mov    -0x24(%rbp),%edx
lea    0xe27(%rip),%rsi
call   1050 <sprintf@plt>
lea    -0x50(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x38(%rbp),%rax
mov    -0x18(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,%esi
mov    -0x30(%rbp),%rcx
mov    -0x58(%rbp),%r8
lea    0xe00(%rip),%rdx
mov    $0x0,%al
call   1040 <snprintf@plt>
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x18(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x18(%rbp)
lea    0xdde(%rip),%rax
mov    %rax,-0x30(%rbp)
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
mov    -0x28(%rbp),%eax
shl    $0x1,%eax
mov    %eax,-0x28(%rbp)
jmp    1195 <print_bitfield+0x65>
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<print_bitfield>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rcx,%r14
mov    %rdx,%rbx
mov    %rsi,0x8(%rsp)
test   %rdi,%rdi
je     1206 <print_bitfield+0xd6>
mov    %rdi,%rbp
add    %rbx,%r14
mov    $0x1,%r15d
lea    0xeae(%rip),%rcx
lea    0xea0(%rip),%r12
xor    %r13d,%r13d
mov    %rdi,(%rsp)
jmp    11b3 <print_bitfield+0x83>
cs nopw 0x0(%rax,%rax,1)
nop
mov    %r14d,%esi
sub    %ebx,%esi
mov    %rbx,%rdi
mov    %r12,%rdx
xor    %eax,%eax
call   1040 <snprintf@plt>
mov    %rbx,%rdi
call   1030 <strlen@plt>
cltq
add    %rax,%rbx
lea    0xe6b(%rip),%rcx
add    $0x1,%r13
add    %r15d,%r15d
cmp    $0x20,%r13
je     11f7 <print_bitfield+0xc7>
test   %ebp,%r15d
je     11a6 <print_bitfield+0x76>
mov    0x8(%rsp),%rax
mov    (%rax,%r13,8),%r8
test   %r8,%r8
jne    1180 <print_bitfield+0x50>
mov    %r12,%rbp
mov    %r14,%r12
lea    0x10(%rsp),%rdi
lea    0xe2e(%rip),%rsi
mov    %r13d,%edx
mov    %rcx,%r14
call   1050 <sprintf@plt>
mov    %r14,%rcx
lea    0x10(%rsp),%r8
mov    %r12,%r14
mov    %rbp,%r12
mov    (%rsp),%rbp
jmp    1180 <print_bitfield+0x50>
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdf3(%rip),%rdx
mov    %rbx,%rdi
mov    %r14d,%esi
xor    %eax,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <snprintf@plt>

LLVM IR (-O0):
@.str = [1 x i8] zeroinitializer
@.str.1 = [6 x i8] c"<NIL>\00"
@.str.2 = [6 x i8] c"bit%u\00"
@.str.3 = [5 x i8] c"%s%s\00"
@.str.4 = [3 x i8] c", \00"
define void @print_bitfield(i64 %0, i8** %1, i8* %2, i64 %3) {
%5 = alloca i64
%6 = alloca i8**
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i32
%10 = alloca i32
%11 = alloca i8*
%12 = alloca i8*
%13 = alloca [16 x i8]
%14 = alloca i8*
store i64 %0, i64* %5
store i8** %1, i8*** %6
store i8* %2, i8** %7
store i64 %3, i64* %8
store i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0), i8** %11
%15 = load i64, i64* %5
%16 = icmp eq i64 %15, 0
br i1 %16, label %17, label %22
17: ; preds = %4
%18 = load i8*, i8** %7
%19 = load i64, i64* %8
%20 = trunc i64 %19 to i32
%21 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %18, i32 %20, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
br label %69
22: ; preds = %4
%23 = load i8*, i8** %7
%24 = load i64, i64* %8
%25 = getelementptr inbounds i8, i8* %23, i64 %24
store i8* %25, i8** %12
store i32 0, i32* %9
store i32 1, i32* %10
br label %26
26: ; preds = %64, %22
%27 = load i32, i32* %10
%28 = icmp ne i32 %27, 0
br i1 %28, label %29, label %69
29: ; preds = %26
%30 = load i64, i64* %5
%31 = load i32, i32* %10
%32 = zext i32 %31 to i64
%33 = and i64 %30, %32
%34 = icmp ne i64 %33, 0
br i1 %34, label %36, label %35
35: ; preds = %29
br label %64
36: ; preds = %29
%37 = load i8**, i8*** %6
%38 = load i32, i32* %9
%39 = zext i32 %38 to i64
%40 = getelementptr inbounds i8*, i8** %37, i64 %39
%41 = load i8*, i8** %40
store i8* %41, i8** %14
%42 = icmp eq i8* %41, null
br i1 %42, label %43, label %48
43: ; preds = %36
%44 = getelementptr inbounds [16 x i8], [16 x i8]* %13, i64 0, i64 0
%45 = load i32, i32* %9
%46 = call i32 @sprintf(i8* %44, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i32 %45)
%47 = getelementptr inbounds [16 x i8], [16 x i8]* %13, i64 0, i64 0
store i8* %47, i8** %14
br label %48
48: ; preds = %43, %36
%49 = load i8*, i8** %7
%50 = load i8*, i8** %12
%51 = load i8*, i8** %7
%52 = ptrtoint i8* %50 to i64
%53 = ptrtoint i8* %51 to i64
%54 = sub i64 %52, %53
%55 = trunc i64 %54 to i32
%56 = load i8*, i8** %11
%57 = load i8*, i8** %14
%58 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %49, i32 %55, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i8* %56, i8* %57)
%59 = load i8*, i8** %7
%60 = call i32 @strlen(i8* %59)
%61 = load i8*, i8** %7
%62 = sext i32 %60 to i64
%63 = getelementptr inbounds i8, i8* %61, i64 %62
store i8* %63, i8** %7
store i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i8** %11
br label %64
64: ; preds = %48, %35
%65 = load i32, i32* %9
%66 = add i32 %65, 1
store i32 %66, i32* %9
%67 = load i32, i32* %10
%68 = shl i32 %67, 1
store i32 %68, i32* %10
br label %26
69: ; preds = %17, %26
ret void
}

LLVM IR (-O2):
@.str = [1 x i8] zeroinitializer
@.str.1 = [6 x i8] c"<NIL>\00"
@.str.2 = [6 x i8] c"bit%u\00"
@.str.3 = [5 x i8] c"%s%s\00"
@.str.4 = [3 x i8] c", \00"
define void @print_bitfield(i64 %0, i8** nocapture %1, i8* %2, i64 %3) {
%5 = alloca [16 x i8]
%6 = icmp eq i64 %0, 0
br i1 %6, label %7, label %10
7: ; preds = %4
%8 = trunc i64 %3 to i32
%9 = tail call i32 (i8*, i32, i8*, ...) @snprintf(i8* %2, i32 %8, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
br label %44
10: ; preds = %4
%11 = getelementptr inbounds i8, i8* %2, i64 %3
%12 = getelementptr inbounds [16 x i8], [16 x i8]* %5, i64 0, i64 0
%13 = ptrtoint i8* %11 to i64
br label %14
14: ; preds = %10, %38
%15 = phi i64 [ 0, %10 ], [ %41, %38 ]
%16 = phi i8* [ %2, %10 ], [ %40, %38 ]
%17 = phi i8* [ getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0), %10 ], [ %39, %38 ]
%18 = phi i32 [ 1, %10 ], [ %42, %38 ]
%19 = zext i32 %18 to i64
%20 = and i64 %19, %0
%21 = icmp eq i64 %20, 0
br i1 %21, label %38, label %22
22: ; preds = %14
%23 = getelementptr inbounds i8*, i8** %1, i64 %15
%24 = load i8*, i8** %23
%25 = icmp eq i8* %24, null
br i1 %25, label %26, label %29
26: ; preds = %22
%27 = trunc i64 %15 to i32
%28 = call i32 @sprintf(i8* %12, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i32 %27)
br label %29
29: ; preds = %26, %22
%30 = phi i8* [ %12, %26 ], [ %24, %22 ]
%31 = ptrtoint i8* %16 to i64
%32 = sub i64 %13, %31
%33 = trunc i64 %32 to i32
%34 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %16, i32 %33, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i8* %17, i8* %30)
%35 = call i32 @strlen(i8* %16)
%36 = sext i32 %35 to i64
%37 = getelementptr inbounds i8, i8* %16, i64 %36
br label %38
38: ; preds = %14, %29
%39 = phi i8* [ getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), %29 ], [ %17, %14 ]
%40 = phi i8* [ %37, %29 ], [ %16, %14 ]
%41 = add nuw nsw i64 %15, 1
%42 = shl i32 %18, 1
%43 = icmp eq i64 %41, 32
br i1 %43, label %44, label %14
44: ; preds = %38, %7
ret void
}