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

/* Variables and functions */
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int hamming_weight(int /*<<< orphan*/ *, int);
int /*<<< orphan*/ stderr;
bool balance_vidpid(uint32_t *vidpid, char *name) {
  bool good_vidpid = true;
  if (!name) {
    name = "unknown";
  }
  if (*vidpid != 0) {
    if (*vidpid <= 0x00007fff) {
      fprintf(stderr, "Warning: %s upconverted from %x", name, *vidpid);
      *vidpid |= ~*vidpid << 16;
      fprintf(stderr, " to %08x\n", *vidpid);
    } else {
      good_vidpid = (hamming_weight((uint8_t *)vidpid, sizeof(*vidpid)) == 16);
    }
    if (!good_vidpid) {
      fprintf(stderr, "ERROR: %s (%08x) has an invalid Hamming weight\n", name,
              *vidpid);
    }
  }
  return good_vidpid;
}


x86 assembly (clang, -O0):
<balance_vidpid>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x1,-0x14(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    114d <balance_vidpid+0x2d>
lea    0xeb7(%rip),%rax
mov    %rax,-0x10(%rbp)
mov    -0x8(%rbp),%rax
cmpl   $0x0,(%rax)
je     120d <balance_vidpid+0xed>
mov    -0x8(%rbp),%rax
cmpl   $0x7fff,(%rax)
jg     11c1 <balance_vidpid+0xa1>
mov    0x2e6f(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
mov    -0x8(%rbp),%rax
mov    (%rax),%ecx
lea    0xe84(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
mov    -0x8(%rbp),%rax
mov    (%rax),%ecx
xor    $0xffffffff,%ecx
shl    $0x10,%ecx
mov    -0x8(%rbp),%rax
or     (%rax),%ecx
mov    %ecx,(%rax)
mov    0x2e3a(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rax
mov    (%rax),%edx
lea    0xe73(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
jmp    11dd <balance_vidpid+0xbd>
mov    -0x8(%rbp),%rdi
mov    $0x4,%esi
call   1040 <hamming_weight@plt>
cmp    $0x10,%eax
sete   %al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jne    1208 <balance_vidpid+0xe8>
mov    0x2df2(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
mov    -0x8(%rbp),%rax
mov    (%rax),%ecx
lea    0xe31(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
jmp    120d <balance_vidpid+0xed>
mov    -0x14(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<balance_vidpid>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
test   %rsi,%rsi
lea    0xecf(%rip),%rbp
cmovne %rsi,%rbp
mov    (%rdi),%ecx
mov    $0x1,%r14d
test   %ecx,%ecx
je     11b8 <balance_vidpid+0x98>
mov    %rdi,%rbx
cmp    $0x7fff,%ecx
jg     1187 <balance_vidpid+0x67>
mov    0x2e8d(%rip),%r15
mov    (%r15),%edi
lea    0xeab(%rip),%rsi
mov    %rbp,%rdx
xor    %eax,%eax
call   1030 <fprintf@plt>
mov    (%rbx),%eax
mov    %eax,%edx
not    %edx
shl    $0x10,%edx
or     %eax,%edx
mov    %edx,(%rbx)
mov    (%r15),%edi
lea    0xeaa(%rip),%rsi
xor    %eax,%eax
call   1030 <fprintf@plt>
jmp    11b8 <balance_vidpid+0x98>
mov    %rbx,%rdi
mov    $0x4,%esi
call   1040 <hamming_weight@plt>
cmp    $0x10,%eax
je     11b8 <balance_vidpid+0x98>
mov    0x2e40(%rip),%rax
mov    (%rax),%edi
mov    (%rbx),%ecx
lea    0xe87(%rip),%rsi
xor    %r14d,%r14d
mov    %rbp,%rdx
xor    %eax,%eax
call   1030 <fprintf@plt>
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [8 x i8] c"unknown\00"
@stderr = global i32 0
@.str.1 = [32 x i8] c"Warning: %s upconverted from %x\00"
@.str.2 = [10 x i8] c" to %08x\0A\00"
@.str.3 = [48 x i8] c"ERROR: %s (%08x) has an invalid Hamming weight\0A\00"
define i32 @balance_vidpid(i32* %0, i8* %1) {
%3 = alloca i32*
%4 = alloca i8*
%5 = alloca i32
store i32* %0, i32** %3
store i8* %1, i8** %4
store i32 1, i32* %5
%6 = load i8*, i8** %4
%7 = icmp ne i8* %6, null
br i1 %7, label %9, label %8
8: ; preds = %2
store i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8** %4
br label %9
9: ; preds = %8, %2
%10 = load i32*, i32** %3
%11 = load i32, i32* %10
%12 = icmp ne i32 %11, 0
br i1 %12, label %13, label %49
13: ; preds = %9
%14 = load i32*, i32** %3
%15 = load i32, i32* %14
%16 = icmp sle i32 %15, 32767
br i1 %16, label %17, label %34
17: ; preds = %13
%18 = load i32, i32* @stderr
%19 = load i8*, i8** %4
%20 = load i32*, i32** %3
%21 = load i32, i32* %20
%22 = call i32 (i32, i8*, ...) @fprintf(i32 %18, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), i8* %19, i32 %21)
%23 = load i32*, i32** %3
%24 = load i32, i32* %23
%25 = xor i32 %24, -1
%26 = shl i32 %25, 16
%27 = load i32*, i32** %3
%28 = load i32, i32* %27
%29 = or i32 %28, %26
store i32 %29, i32* %27
%30 = load i32, i32* @stderr
%31 = load i32*, i32** %3
%32 = load i32, i32* %31
%33 = call i32 (i32, i8*, ...) @fprintf(i32 %30, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i32 %32)
br label %39
34: ; preds = %13
%35 = load i32*, i32** %3
%36 = call i32 @hamming_weight(i32* %35, i32 4)
%37 = icmp eq i32 %36, 16
%38 = zext i1 %37 to i32
store i32 %38, i32* %5
br label %39
39: ; preds = %34, %17
%40 = load i32, i32* %5
%41 = icmp ne i32 %40, 0
br i1 %41, label %48, label %42
42: ; preds = %39
%43 = load i32, i32* @stderr
%44 = load i8*, i8** %4
%45 = load i32*, i32** %3
%46 = load i32, i32* %45
%47 = call i32 (i32, i8*, ...) @fprintf(i32 %43, i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.3, i64 0, i64 0), i8* %44, i32 %46)
br label %48
48: ; preds = %42, %39
br label %49
49: ; preds = %48, %9
%50 = load i32, i32* %5
ret i32 %50
}

LLVM IR (-O2):
@.str = [8 x i8] c"unknown\00"
@stderr = global i32 0
@.str.1 = [32 x i8] c"Warning: %s upconverted from %x\00"
@.str.2 = [10 x i8] c" to %08x\0A\00"
@.str.3 = [48 x i8] c"ERROR: %s (%08x) has an invalid Hamming weight\0A\00"
define i32 @balance_vidpid(i32* %0, i8* %1) {
%3 = icmp eq i8* %1, null
%4 = select i1 %3, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* %1
%5 = load i32, i32* %0
%6 = icmp eq i32 %5, 0
br i1 %6, label %25, label %7
7: ; preds = %2
%8 = icmp slt i32 %5, 32768
br i1 %8, label %9, label %18
9: ; preds = %7
%10 = load i32, i32* @stderr
%11 = tail call i32 (i32, i8*, ...) @fprintf(i32 %10, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), i8* %4, i32 %5)
%12 = load i32, i32* %0
%13 = xor i32 %12, -1
%14 = shl i32 %13, 16
%15 = or i32 %14, %12
store i32 %15, i32* %0
%16 = load i32, i32* @stderr
%17 = tail call i32 (i32, i8*, ...) @fprintf(i32 %16, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i32 %15)
br label %25
18: ; preds = %7
%19 = tail call i32 @hamming_weight(i32* %0, i32 4)
%20 = icmp eq i32 %19, 16
br i1 %20, label %25, label %21
21: ; preds = %18
%22 = load i32, i32* @stderr
%23 = load i32, i32* %0
%24 = tail call i32 (i32, i8*, ...) @fprintf(i32 %22, i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.3, i64 0, i64 0), i8* %4, i32 %23)
br label %25
25: ; preds = %9, %18, %21, %2
%26 = phi i32 [ 1, %18 ], [ 0, %21 ], [ 1, %2 ], [ 1, %9 ]
ret i32 %26
}