#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;

/* Variables and functions */
int unpremultiply(int, float, int);
uint32_t unpremultiply_bgra(uint32_t premultPixelBGRA) {
  uint32_t alpha = (premultPixelBGRA >> 24) & 0xFF;
  uint32_t red = (premultPixelBGRA >> 16) & 0xFF;
  uint32_t green = (premultPixelBGRA >> 8) & 0xFF;
  uint32_t blue = (premultPixelBGRA >> 0) & 0xFF;
  if (alpha == 0) {
    red = green = blue = 0;
  } else if (alpha == 0xFF) {
  } else {
    float alphaMult = 1.0f / (alpha / 255.0f);
    red = unpremultiply(red, alphaMult, alpha);
    green = unpremultiply(green, alphaMult, alpha);
    blue = unpremultiply(blue, alphaMult, alpha);
  }
  uint32_t result = (alpha << 24) | (red << 16) | (green << 8) | blue;
  return result;
}


x86 assembly (clang, -O0):
<unpremultiply_bgra>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%eax
sar    $0x18,%eax
and    $0xff,%eax
mov    %eax,-0x8(%rbp)
mov    -0x4(%rbp),%eax
sar    $0x10,%eax
and    $0xff,%eax
mov    %eax,-0xc(%rbp)
mov    -0x4(%rbp),%eax
sar    $0x8,%eax
and    $0xff,%eax
mov    %eax,-0x10(%rbp)
mov    -0x4(%rbp),%eax
sar    $0x0,%eax
and    $0xff,%eax
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x8(%rbp)
jne    1177 <unpremultiply_bgra+0x67>
movl   $0x0,-0x14(%rbp)
movl   $0x0,-0x10(%rbp)
movl   $0x0,-0xc(%rbp)
jmp    11e9 <unpremultiply_bgra+0xd9>
cmpl   $0xff,-0x8(%rbp)
jne    1189 <unpremultiply_bgra+0x79>
jmp    11e4 <unpremultiply_bgra+0xd4>
cvtsi2ssl -0x8(%rbp),%xmm1
movss  0xe6e(%rip),%xmm0
divss  %xmm0,%xmm1
movss  0xe5e(%rip),%xmm0
divss  %xmm1,%xmm0
movss  %xmm0,-0x18(%rbp)
mov    -0xc(%rbp),%edi
movss  -0x18(%rbp),%xmm0
mov    -0x8(%rbp),%esi
call   1030 <unpremultiply@plt>
mov    %eax,-0xc(%rbp)
mov    -0x10(%rbp),%edi
movss  -0x18(%rbp),%xmm0
mov    -0x8(%rbp),%esi
call   1030 <unpremultiply@plt>
mov    %eax,-0x10(%rbp)
mov    -0x14(%rbp),%edi
movss  -0x18(%rbp),%xmm0
mov    -0x8(%rbp),%esi
call   1030 <unpremultiply@plt>
mov    %eax,-0x14(%rbp)
jmp    11e9 <unpremultiply_bgra+0xd9>
mov    -0x8(%rbp),%eax
shl    $0x18,%eax
mov    -0xc(%rbp),%ecx
shl    $0x10,%ecx
or     %ecx,%eax
mov    -0x10(%rbp),%ecx
shl    $0x8,%ecx
or     %ecx,%eax
or     -0x14(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<unpremultiply_bgra>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %edi,%ecx
xor    %r14d,%r14d
mov    $0x0,%ebx
mov    $0x0,%ebp
cmp    $0x1000000,%edi
jb     11a9 <unpremultiply_bgra+0x99>
mov    %ecx,%r12d
shr    $0x18,%r12d
mov    %ecx,%eax
shr    $0x10,%eax
movzbl %al,%ebp
movzbl %ch,%ebx
movzbl %cl,%r14d
cmp    $0xff,%r12d
je     11a9 <unpremultiply_bgra+0x99>
cvtsi2ss %r12d,%xmm1
divss  0xea1(%rip),%xmm1
movss  0xe9d(%rip),%xmm0
divss  %xmm1,%xmm0
movss  %xmm0,0xc(%rsp)
mov    %ebp,%edi
mov    %r12d,%esi
mov    %ecx,%r15d
call   1030 <unpremultiply@plt>
mov    %eax,%ebp
mov    %ebx,%edi
movss  0xc(%rsp),%xmm0
mov    %r12d,%esi
call   1030 <unpremultiply@plt>
mov    %eax,%ebx
mov    %r14d,%edi
movss  0xc(%rsp),%xmm0
mov    %r12d,%esi
call   1030 <unpremultiply@plt>
mov    %r15d,%ecx
mov    %eax,%r14d
and    $0xff000000,%ecx
or     %r14d,%ecx
shl    $0x10,%ebp
shl    $0x8,%ebx
or     %ecx,%ebx
or     %ebp,%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @unpremultiply_bgra(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca float
%8 = alloca i32
store i32 %0, i32* %2
%9 = load i32, i32* %2
%10 = ashr i32 %9, 24
%11 = and i32 %10, 255
store i32 %11, i32* %3
%12 = load i32, i32* %2
%13 = ashr i32 %12, 16
%14 = and i32 %13, 255
store i32 %14, i32* %4
%15 = load i32, i32* %2
%16 = ashr i32 %15, 8
%17 = and i32 %16, 255
store i32 %17, i32* %5
%18 = load i32, i32* %2
%19 = ashr i32 %18, 0
%20 = and i32 %19, 255
store i32 %20, i32* %6
%21 = load i32, i32* %3
%22 = icmp eq i32 %21, 0
br i1 %22, label %23, label %24
23: ; preds = %1
store i32 0, i32* %6
store i32 0, i32* %5
store i32 0, i32* %4
br label %46
24: ; preds = %1
%25 = load i32, i32* %3
%26 = icmp eq i32 %25, 255
br i1 %26, label %27, label %28
27: ; preds = %24
br label %45
28: ; preds = %24
%29 = load i32, i32* %3
%30 = sitofp i32 %29 to float
%31 = fdiv float %30, 2.550000e+02
%32 = fdiv float 1.000000e+00, %31
store float %32, float* %7
%33 = load i32, i32* %4
%34 = load float, float* %7
%35 = load i32, i32* %3
%36 = call i32 @unpremultiply(i32 %33, float %34, i32 %35)
store i32 %36, i32* %4
%37 = load i32, i32* %5
%38 = load float, float* %7
%39 = load i32, i32* %3
%40 = call i32 @unpremultiply(i32 %37, float %38, i32 %39)
store i32 %40, i32* %5
%41 = load i32, i32* %6
%42 = load float, float* %7
%43 = load i32, i32* %3
%44 = call i32 @unpremultiply(i32 %41, float %42, i32 %43)
store i32 %44, i32* %6
br label %45
45: ; preds = %28, %27
br label %46
46: ; preds = %45, %23
%47 = load i32, i32* %3
%48 = shl i32 %47, 24
%49 = load i32, i32* %4
%50 = shl i32 %49, 16
%51 = or i32 %48, %50
%52 = load i32, i32* %5
%53 = shl i32 %52, 8
%54 = or i32 %51, %53
%55 = load i32, i32* %6
%56 = or i32 %54, %55
store i32 %56, i32* %8
%57 = load i32, i32* %8
ret i32 %57
}

LLVM IR (-O2):
define i32 @unpremultiply_bgra(i32 %0) {
%2 = lshr i32 %0, 24
%3 = lshr i32 %0, 16
%4 = and i32 %3, 255
%5 = lshr i32 %0, 8
%6 = and i32 %5, 255
%7 = and i32 %0, 255
%8 = icmp ult i32 %0, 16777216
br i1 %8, label %18, label %9
9: ; preds = %1
%10 = icmp eq i32 %2, 255
br i1 %10, label %18, label %11
11: ; preds = %9
%12 = sitofp i32 %2 to float
%13 = fdiv float %12, 2.550000e+02
%14 = fdiv float 1.000000e+00, %13
%15 = tail call i32 @unpremultiply(i32 %4, float %14, i32 %2)
%16 = tail call i32 @unpremultiply(i32 %6, float %14, i32 %2)
%17 = tail call i32 @unpremultiply(i32 %7, float %14, i32 %2)
br label %18
18: ; preds = %1, %11, %9
%19 = phi i32 [ %7, %9 ], [ %17, %11 ], [ 0, %1 ]
%20 = phi i32 [ %6, %9 ], [ %16, %11 ], [ 0, %1 ]
%21 = phi i32 [ %4, %9 ], [ %15, %11 ], [ 0, %1 ]
%22 = and i32 %0, -16777216
%23 = shl i32 %21, 16
%24 = shl i32 %20, 8
%25 = or i32 %19, %22
%26 = or i32 %25, %24
%27 = or i32 %26, %23
ret i32 %27
}