#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 uint32_t;
typedef int /*<<< orphan*/ cpu_set_t;

/* Variables and functions */
int CPU_ISSET(int, int /*<<< orphan*/ *);
int cpuset_last_bit(int /*<<< orphan*/ *);
scalar_t__ snprintf(char *, size_t, char *, char const *, int, int);
int cpuset_to_hex(cpu_set_t *mask, char *str, size_t len) {
  int chunk;
  int cnt = 0;
  int lastchunk = cpuset_last_bit(mask) / 32;
  const char *sep = "";
  if (len <= 0)
    return 0;
  str[0] = 0;
  for (chunk = lastchunk; chunk >= 0; chunk--) {
    uint32_t val = 0;
    int bit;
    for (bit = 32 - 1; bit >= 0; bit--)
      val = val << 1 | CPU_ISSET(chunk * 32 + bit, mask);
    cnt += snprintf(str + cnt, ((len - cnt) > (0) ? (len - cnt) : (0)),
                    "%s%0*x", sep, 8, val);
    sep = ",";
  }
  return cnt;
}


x86 assembly (clang, -O0):
<cpuset_to_hex>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movl   $0x0,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <cpuset_last_bit@plt>
mov    $0x20,%ecx
cltd
idiv   %ecx
mov    %eax,-0x2c(%rbp)
lea    0xea2(%rip),%rax
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x20(%rbp)
ja     1181 <cpuset_to_hex+0x51>
movl   $0x0,-0x4(%rbp)
jmp    1274 <cpuset_to_hex+0x144>
mov    -0x18(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x2c(%rbp),%eax
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jl     126e <cpuset_to_hex+0x13e>
movl   $0x0,-0x3c(%rbp)
movl   $0x1f,-0x40(%rbp)
cmpl   $0x0,-0x40(%rbp)
jl     11e3 <cpuset_to_hex+0xb3>
mov    -0x3c(%rbp),%eax
shl    $0x1,%eax
mov    %eax,-0x44(%rbp)
mov    -0x24(%rbp),%edi
shl    $0x5,%edi
add    -0x40(%rbp),%edi
mov    -0x10(%rbp),%rsi
call   1050 <CPU_ISSET@plt>
mov    %eax,%ecx
mov    -0x44(%rbp),%eax
or     %ecx,%eax
mov    %eax,-0x3c(%rbp)
mov    -0x40(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x40(%rbp)
jmp    11a6 <cpuset_to_hex+0x76>
mov    -0x18(%rbp),%rax
movslq -0x28(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x50(%rbp)
mov    -0x20(%rbp),%rax
movslq -0x28(%rbp),%rcx
sub    %rcx,%rax
cmp    $0x0,%rax
jbe    121b <cpuset_to_hex+0xeb>
mov    -0x20(%rbp),%rax
movslq -0x28(%rbp),%rcx
sub    %rcx,%rax
mov    %rax,-0x58(%rbp)
jmp    1226 <cpuset_to_hex+0xf6>
xor    %eax,%eax
mov    %rax,-0x58(%rbp)
jmp    1226 <cpuset_to_hex+0xf6>
mov    -0x50(%rbp),%rdi
mov    -0x58(%rbp),%rsi
mov    -0x38(%rbp),%rcx
mov    -0x3c(%rbp),%r9d
lea    0xdc3(%rip),%rdx
mov    $0x8,%r8d
call   1040 <snprintf@plt>
mov    %rax,%rcx
movslq -0x28(%rbp),%rax
add    %rcx,%rax
mov    %eax,-0x28(%rbp)
lea    0xdab(%rip),%rax
mov    %rax,-0x38(%rbp)
mov    -0x24(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x24(%rbp)
jmp    118e <cpuset_to_hex+0x5e>
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cpuset_to_hex>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdx,%r14
mov    %rsi,%rbp
mov    %rdi,%rbx
call   1030 <cpuset_last_bit@plt>
xor    %r12d,%r12d
mov    %r14,0x8(%rsp)
test   %r14,%r14
je     11ff <cpuset_to_hex+0xcf>
movb   $0x0,0x0(%rbp)
cmp    $0xffffffe1,%eax
jl     11ff <cpuset_to_hex+0xcf>
lea    0x1f(%rax),%r13d
test   %eax,%eax
cmovns %eax,%r13d
mov    %r13d,%r14d
sar    $0x5,%r14d
and    $0xffffffe0,%r13d
xor    %r12d,%r12d
lea    0xe7f(%rip),%rcx
mov    %rbp,(%rsp)
nopl   (%rax)
mov    %rcx,0x10(%rsp)
mov    $0x1f,%r15d
xor    %eax,%eax
nopl   (%rax)
mov    %eax,%ebp
add    %eax,%ebp
lea    (%r15,%r13,1),%edi
mov    %rbx,%rsi
call   1050 <CPU_ISSET@plt>
or     %ebp,%eax
add    $0xffffffff,%r15d
jb     11a0 <cpuset_to_hex+0x70>
movslq %r12d,%r12
mov    (%rsp),%rbp
lea    (%r12,%rbp,1),%rdi
mov    0x8(%rsp),%rsi
sub    %r12,%rsi
lea    0xe2e(%rip),%rdx
mov    0x10(%rsp),%rcx
mov    $0x8,%r8d
mov    %eax,%r9d
call   1040 <snprintf@plt>
add    %eax,%r12d
lea    -0x1(%r14),%eax
add    $0xffffffe0,%r13d
lea    0xe10(%rip),%rcx
test   %r14d,%r14d
mov    %eax,%r14d
jg     1190 <cpuset_to_hex+0x60>
mov    %r12d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [1 x i8] zeroinitializer
@.str.1 = [7 x i8] c"%s%0*x\00"
@.str.2 = [2 x i8] c",\00"
define i32 @cpuset_to_hex(i32* %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i8*
%12 = alloca i32
%13 = alloca i32
store i32* %0, i32** %5
store i8* %1, i8** %6
store i64 %2, i64* %7
store i32 0, i32* %9
%14 = load i32*, i32** %5
%15 = call i32 @cpuset_last_bit(i32* %14)
%16 = sdiv i32 %15, 32
store i32 %16, i32* %10
store i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0), i8** %11
%17 = load i64, i64* %7
%18 = icmp ule i64 %17, 0
br i1 %18, label %19, label %20
19: ; preds = %3
store i32 0, i32* %4
br label %74
20: ; preds = %3
%21 = load i8*, i8** %6
%22 = getelementptr inbounds i8, i8* %21, i64 0
store i8 0, i8* %22
%23 = load i32, i32* %10
store i32 %23, i32* %8
br label %24
24: ; preds = %69, %20
%25 = load i32, i32* %8
%26 = icmp sge i32 %25, 0
br i1 %26, label %27, label %72
27: ; preds = %24
store i32 0, i32* %12
store i32 31, i32* %13
br label %28
28: ; preds = %41, %27
%29 = load i32, i32* %13
%30 = icmp sge i32 %29, 0
br i1 %30, label %31, label %44
31: ; preds = %28
%32 = load i32, i32* %12
%33 = shl i32 %32, 1
%34 = load i32, i32* %8
%35 = mul nsw i32 %34, 32
%36 = load i32, i32* %13
%37 = add nsw i32 %35, %36
%38 = load i32*, i32** %5
%39 = call i32 @CPU_ISSET(i32 %37, i32* %38)
%40 = or i32 %33, %39
store i32 %40, i32* %12
br label %41
41: ; preds = %31
%42 = load i32, i32* %13
%43 = add nsw i32 %42, -1
store i32 %43, i32* %13
br label %28
44: ; preds = %28
%45 = load i8*, i8** %6
%46 = load i32, i32* %9
%47 = sext i32 %46 to i64
%48 = getelementptr inbounds i8, i8* %45, i64 %47
%49 = load i64, i64* %7
%50 = load i32, i32* %9
%51 = sext i32 %50 to i64
%52 = sub i64 %49, %51
%53 = icmp ugt i64 %52, 0
br i1 %53, label %54, label %59
54: ; preds = %44
%55 = load i64, i64* %7
%56 = load i32, i32* %9
%57 = sext i32 %56 to i64
%58 = sub i64 %55, %57
br label %60
59: ; preds = %44
br label %60
60: ; preds = %59, %54
%61 = phi i64 [ %58, %54 ], [ 0, %59 ]
%62 = load i8*, i8** %11
%63 = load i32, i32* %12
%64 = call i64 @snprintf(i8* %48, i64 %61, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i8* %62, i32 8, i32 %63)
%65 = load i32, i32* %9
%66 = sext i32 %65 to i64
%67 = add nsw i64 %66, %64
%68 = trunc i64 %67 to i32
store i32 %68, i32* %9
store i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i8** %11
br label %69
69: ; preds = %60
%70 = load i32, i32* %8
%71 = add nsw i32 %70, -1
store i32 %71, i32* %8
br label %24
72: ; preds = %24
%73 = load i32, i32* %9
store i32 %73, i32* %4
br label %74
74: ; preds = %72, %19
%75 = load i32, i32* %4
ret i32 %75
}

LLVM IR (-O2):
@.str = [1 x i8] zeroinitializer
@.str.1 = [7 x i8] c"%s%0*x\00"
@.str.2 = [2 x i8] c",\00"
define i32 @cpuset_to_hex(i32* %0, i8* %1, i64 %2) {
%4 = tail call i32 @cpuset_last_bit(i32* %0)
%5 = icmp eq i64 %2, 0
br i1 %5, label %33, label %6
6: ; preds = %3
store i8 0, i8* %1
%7 = icmp sgt i32 %4, -32
br i1 %7, label %8, label %33
8: ; preds = %6
%9 = sdiv i32 %4, 32
br label %10
10: ; preds = %8, %24
%11 = phi i8* [ getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), %24 ], [ getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0), %8 ]
%12 = phi i32 [ %30, %24 ], [ 0, %8 ]
%13 = phi i32 [ %31, %24 ], [ %9, %8 ]
%14 = shl nsw i32 %13, 5
br label %15
15: ; preds = %10, %15
%16 = phi i32 [ 31, %10 ], [ %22, %15 ]
%17 = phi i32 [ 0, %10 ], [ %21, %15 ]
%18 = shl i32 %17, 1
%19 = add nsw i32 %16, %14
%20 = tail call i32 @CPU_ISSET(i32 %19, i32* %0)
%21 = or i32 %20, %18
%22 = add nsw i32 %16, -1
%23 = icmp eq i32 %16, 0
br i1 %23, label %24, label %15
24: ; preds = %15
%25 = sext i32 %12 to i64
%26 = getelementptr inbounds i8, i8* %1, i64 %25
%27 = sub i64 %2, %25
%28 = tail call i64 @snprintf(i8* %26, i64 %27, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i8* %11, i32 8, i32 %21)
%29 = trunc i64 %28 to i32
%30 = add i32 %12, %29
%31 = add nsw i32 %13, -1
%32 = icmp sgt i32 %13, 0
br i1 %32, label %10, label %33
33: ; preds = %24, %6, %3
%34 = phi i32 [ 0, %3 ], [ 0, %6 ], [ %30, %24 ]
ret i32 %34
}