#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 */
char *findflag(unsigned long *, char const *);
char *reallocate(char *, int);
int /*<<< orphan*/ sprintf(char *, char *, char const *);
char *strhex(unsigned long);
int strlen(char const *);
char const *strdefflags(unsigned long flags, char const *prefix) {
  static char *buf = NULL;
  static int bufsize = 0;
  char const *name;
  int size, n;
  if (flags == 0)
    return "0";
  size = 0;
  while (flags) {
    name = findflag(&flags, prefix);
    if (!name)
      break;
    n = 3 + strlen(name);
    if (size + n > bufsize) {
      bufsize += n;
      buf = reallocate(buf, bufsize + 32);
    }
    sprintf(buf + size, " | %s", name);
    size += n;
  }
  if (size == 0)
    return strhex(flags);
  if (flags)
    sprintf(buf + size, " | %s", strhex(flags));
  return buf + 3;
}


x86 assembly (clang, -O0):
<strdefflags>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    117b <strdefflags+0x2b>
lea    0xe8e(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    128a <strdefflags+0x13a>
movl   $0x0,-0x24(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1226 <strdefflags+0xd6>
mov    -0x18(%rbp),%rsi
lea    -0x10(%rbp),%rdi
call   1060 <findflag@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11ae <strdefflags+0x5e>
jmp    1226 <strdefflags+0xd6>
mov    -0x20(%rbp),%rdi
call   1030 <strlen@plt>
add    $0x3,%eax
mov    %eax,-0x28(%rbp)
mov    -0x24(%rbp),%eax
add    -0x28(%rbp),%eax
cmp    0x2e8f(%rip),%eax
jle    11fa <strdefflags+0xaa>
mov    -0x28(%rbp),%eax
add    0x2e80(%rip),%eax
mov    %eax,0x2e7a(%rip)
mov    0x2e6b(%rip),%rdi
mov    0x2e6d(%rip),%esi
add    $0x20,%esi
call   1050 <reallocate@plt>
mov    %rax,0x2e56(%rip)
mov    0x2e4f(%rip),%rdi
movslq -0x24(%rbp),%rax
add    %rax,%rdi
mov    -0x20(%rbp),%rdx
lea    0xdef(%rip),%rsi
call   1070 <sprintf@plt>
mov    -0x28(%rbp),%eax
add    -0x24(%rbp),%eax
mov    %eax,-0x24(%rbp)
jmp    1182 <strdefflags+0x32>
cmpl   $0x0,-0x24(%rbp)
jne    1242 <strdefflags+0xf2>
mov    -0x10(%rbp),%rdi
call   1040 <strhex@plt>
mov    %rax,-0x8(%rbp)
jmp    128a <strdefflags+0x13a>
cmpq   $0x0,-0x10(%rbp)
je     127b <strdefflags+0x12b>
mov    0x2dfc(%rip),%rax
movslq -0x24(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strhex@plt>
mov    -0x30(%rbp),%rdi
mov    %rax,%rdx
lea    0xd8c(%rip),%rsi
call   1070 <sprintf@plt>
mov    0x2dce(%rip),%rax
add    $0x3,%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strdefflags>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,(%rsp)
test   %rdi,%rdi
je     11f5 <strdefflags+0xa5>
mov    %rsi,%r12
xor    %r13d,%r13d
mov    %rsp,%r14
lea    0xe8a(%rip),%r15
jmp    11c0 <strdefflags+0x70>
nopw   0x0(%rax,%rax,1)
add    $0x3,%eax
lea    (%rcx,%rax,1),%edx
mov    %edx,0x2ecc(%rip)
mov    0x2ebd(%rip),%rdi
lea    (%rcx,%rax,1),%esi
add    $0x20,%esi
call   1050 <reallocate@plt>
mov    %rax,0x2eab(%rip)
movslq %r13d,%rdi
add    %rax,%rdi
mov    %r15,%rsi
mov    %rbx,%rdx
call   1070 <sprintf@plt>
cmpq   $0x0,(%rsp)
mov    %ebp,%r13d
je     1201 <strdefflags+0xb1>
mov    %r14,%rdi
mov    %r12,%rsi
call   1060 <findflag@plt>
test   %rax,%rax
je     11fe <strdefflags+0xae>
mov    %rax,%rbx
mov    %rax,%rdi
call   1030 <strlen@plt>
lea    (%rax,%r13,1),%ebp
add    $0x3,%ebp
mov    0x2e70(%rip),%ecx
cmp    %ecx,%ebp
jg     1180 <strdefflags+0x30>
mov    0x2e5d(%rip),%rax
jmp    11a5 <strdefflags+0x55>
lea    0xe04(%rip),%rax
jmp    1241 <strdefflags+0xf1>
mov    %r13d,%ebp
mov    (%rsp),%rdi
test   %ebp,%ebp
je     123c <strdefflags+0xec>
test   %rdi,%rdi
je     122f <strdefflags+0xdf>
movslq %ebp,%rbx
add    0x2e38(%rip),%rbx
call   1040 <strhex@plt>
lea    0xdde(%rip),%rsi
mov    %rbx,%rdi
mov    %rax,%rdx
call   1070 <sprintf@plt>
mov    0x2e1a(%rip),%rax
add    $0x3,%rax
jmp    1241 <strdefflags+0xf1>
call   1040 <strhex@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@strdefflags.buf = internal unnamed_addr global i8* null
@strdefflags.bufsize = internal unnamed_addr global i32 0
@.str = [2 x i8] c"0\00"
@.str.1 = [6 x i8] c" | %s\00"
define i8* @strdefflags(i64 %0, i8* %1) {
%3 = alloca i64
store i64 %0, i64* %3
%4 = icmp eq i64 %0, 0
br i1 %4, label %46, label %5
5: ; preds = %2, %22
%6 = phi i32 [ %12, %22 ], [ 0, %2 ]
%7 = call i8* @findflag(i64* %3, i8* %1)
%8 = icmp eq i8* %7, null
br i1 %8, label %29, label %9
9: ; preds = %5
%10 = call i32 @strlen(i8* %7)
%11 = add nsw i32 %10, 3
%12 = add nsw i32 %11, %6
%13 = load i32, i32* @strdefflags.bufsize
%14 = icmp sgt i32 %12, %13
br i1 %14, label %17, label %15
15: ; preds = %9
%16 = load i8*, i8** @strdefflags.buf
br label %22
17: ; preds = %9
%18 = add nsw i32 %13, %11
store i32 %18, i32* @strdefflags.bufsize
%19 = load i8*, i8** @strdefflags.buf
%20 = add nsw i32 %18, 32
%21 = call i8* @reallocate(i8* %19, i32 %20)
store i8* %21, i8** @strdefflags.buf
br label %22
22: ; preds = %15, %17
%23 = phi i8* [ %16, %15 ], [ %21, %17 ]
%24 = sext i32 %6 to i64
%25 = getelementptr inbounds i8, i8* %23, i64 %24
%26 = call i32 @sprintf(i8* dereferenceable(1) %25, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* %7)
%27 = load i64, i64* %3
%28 = icmp eq i64 %27, 0
br i1 %28, label %29, label %5
29: ; preds = %22, %5
%30 = phi i32 [ %6, %5 ], [ %12, %22 ]
%31 = icmp eq i32 %30, 0
%32 = load i64, i64* %3
br i1 %31, label %33, label %35
33: ; preds = %29
%34 = call i8* @strhex(i64 %32)
br label %46
35: ; preds = %29
%36 = icmp eq i64 %32, 0
br i1 %36, label %43, label %37
37: ; preds = %35
%38 = load i8*, i8** @strdefflags.buf
%39 = sext i32 %30 to i64
%40 = getelementptr inbounds i8, i8* %38, i64 %39
%41 = call i8* @strhex(i64 %32)
%42 = call i32 @sprintf(i8* %40, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* %41)
br label %43
43: ; preds = %37, %35
%44 = load i8*, i8** @strdefflags.buf
%45 = getelementptr inbounds i8, i8* %44, i64 3
br label %46
46: ; preds = %2, %43, %33
%47 = phi i8* [ %34, %33 ], [ %45, %43 ], [ getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), %2 ]
ret i8* %47
}