#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 */
unsigned char BMP_Clamp(unsigned int);
unsigned int BMP_ToBW(unsigned int color) {
  unsigned char y;
  y = BMP_Clamp(((color >> 16) & 0xFF) * 0.333 + ((color >> 8) & 0xFF) * 0.586 +
                (color & 0xFF) * 0.114);
  return (y << 16) | (y << 8) | y;
}


x86 assembly (clang, -O0):
<BMP_ToBW>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%eax
mov    %eax,%ecx
shr    $0x10,%ecx
movzbl %cl,%ecx
cvtsi2sd %ecx,%xmm1
mov    %ah,%cl
movzbl %cl,%ecx
cvtsi2sd %ecx,%xmm0
movsd  0xec5(%rip),%xmm2
mulsd  %xmm2,%xmm0
movsd  0xec1(%rip),%xmm2
mulsd  %xmm2,%xmm1
addsd  %xmm0,%xmm1
movzbl %al,%eax
cvtsi2sd %eax,%xmm0
movsd  0xeb2(%rip),%xmm2
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
cvttsd2si %xmm0,%rax
mov    %eax,%edi
call   1030 <BMP_Clamp@plt>
mov    %al,-0x5(%rbp)
movzbl -0x5(%rbp),%eax
shl    $0x10,%eax
movzbl -0x5(%rbp),%ecx
shl    $0x8,%ecx
or     %ecx,%eax
movzbl -0x5(%rbp),%ecx
or     %ecx,%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<BMP_ToBW>:
push   %rax
mov    %edi,%eax
movzbl %ah,%ecx
movzbl %al,%edx
shr    $0x10,%eax
movzbl %al,%eax
cvtsi2sd %eax,%xmm0
cvtsi2sd %ecx,%xmm1
mulsd  0xed1(%rip),%xmm1
mulsd  0xed1(%rip),%xmm0
cvtsi2sd %edx,%xmm2
mulsd  0xecd(%rip),%xmm2
addsd  %xmm1,%xmm0
addsd  %xmm0,%xmm2
cvttsd2si %xmm2,%rdi
call   1030 <BMP_Clamp@plt>
movzbl %al,%eax
imul   $0x10101,%eax,%eax
pop    %rcx
ret

LLVM IR (-O0):
define i32 @BMP_ToBW(i32 %0) {
%2 = alloca i32
%3 = alloca i8
store i32 %0, i32* %2
%4 = load i32, i32* %2
%5 = lshr i32 %4, 16
%6 = and i32 %5, 255
%7 = uitofp i32 %6 to double
%8 = load i32, i32* %2
%9 = lshr i32 %8, 8
%10 = and i32 %9, 255
%11 = uitofp i32 %10 to double
%12 = fmul double %11, 5.860000e-01
%13 = call double @llvm.fmuladd.f64(double %7, double 3.330000e-01, double %12)
%14 = load i32, i32* %2
%15 = and i32 %14, 255
%16 = uitofp i32 %15 to double
%17 = call double @llvm.fmuladd.f64(double %16, double 1.140000e-01, double %13)
%18 = fptoui double %17 to i32
%19 = call zeroext i8 @BMP_Clamp(i32 %18)
store i8 %19, i8* %3
%20 = load i8, i8* %3
%21 = zext i8 %20 to i32
%22 = shl i32 %21, 16
%23 = load i8, i8* %3
%24 = zext i8 %23 to i32
%25 = shl i32 %24, 8
%26 = or i32 %22, %25
%27 = load i8, i8* %3
%28 = zext i8 %27 to i32
%29 = or i32 %26, %28
ret i32 %29
}

LLVM IR (-O2):
define i32 @BMP_ToBW(i32 %0) {
%2 = lshr i32 %0, 16
%3 = and i32 %2, 255
%4 = uitofp i32 %3 to double
%5 = lshr i32 %0, 8
%6 = and i32 %5, 255
%7 = uitofp i32 %6 to double
%8 = fmul double %7, 5.860000e-01
%9 = tail call double @llvm.fmuladd.f64(double %4, double 3.330000e-01, double %8)
%10 = and i32 %0, 255
%11 = uitofp i32 %10 to double
%12 = tail call double @llvm.fmuladd.f64(double %11, double 1.140000e-01, double %9)
%13 = fptoui double %12 to i32
%14 = tail call zeroext i8 @BMP_Clamp(i32 %13)
%15 = zext i8 %14 to i32
%16 = mul nuw nsw i32 %15, 65793
ret i32 %16
}