#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.0 / (alpha / 255.0);
    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    11ed <unpremultiply_bgra+0xdd>
cmpl   $0xff,-0x8(%rbp)
jne    1189 <unpremultiply_bgra+0x79>
jmp    11e8 <unpremultiply_bgra+0xd8>
cvtsi2sdl -0x8(%rbp),%xmm1
movsd  0xe72(%rip),%xmm0
divsd  %xmm0,%xmm1
movsd  0xe5e(%rip),%xmm0
divsd  %xmm1,%xmm0
cvtsd2ss %xmm0,%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    11ed <unpremultiply_bgra+0xdd>
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     11b0 <unpremultiply_bgra+0xa0>
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     11b0 <unpremultiply_bgra+0xa0>
cvtsi2sd %r12d,%xmm0
divsd  0xea1(%rip),%xmm0
movsd  0xea1(%rip),%xmm1
divsd  %xmm0,%xmm1
xorps  %xmm0,%xmm0
cvtsd2ss %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 %47
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 %46
28: ; preds = %24
%29 = load i32, i32* %3
%30 = sitofp i32 %29 to double
%31 = fdiv double %30, 2.550000e+02
%32 = fdiv double 1.000000e+00, %31
%33 = fptrunc double %32 to float
store float %33, float* %7
%34 = load i32, i32* %4
%35 = load float, float* %7
%36 = load i32, i32* %3
%37 = call i32 @unpremultiply(i32 %34, float %35, i32 %36)
store i32 %37, i32* %4
%38 = load i32, i32* %5
%39 = load float, float* %7
%40 = load i32, i32* %3
%41 = call i32 @unpremultiply(i32 %38, float %39, i32 %40)
store i32 %41, i32* %5
%42 = load i32, i32* %6
%43 = load float, float* %7
%44 = load i32, i32* %3
%45 = call i32 @unpremultiply(i32 %42, float %43, i32 %44)
store i32 %45, i32* %6
br label %46
46: ; preds = %28, %27
br label %47
47: ; preds = %46, %23
%48 = load i32, i32* %3
%49 = shl i32 %48, 24
%50 = load i32, i32* %4
%51 = shl i32 %50, 16
%52 = or i32 %49, %51
%53 = load i32, i32* %5
%54 = shl i32 %53, 8
%55 = or i32 %52, %54
%56 = load i32, i32* %6
%57 = or i32 %55, %56
store i32 %57, i32* %8
%58 = load i32, i32* %8
ret i32 %58
}

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 %19, label %9
9: ; preds = %1
%10 = icmp eq i32 %2, 255
br i1 %10, label %19, label %11
11: ; preds = %9
%12 = sitofp i32 %2 to double
%13 = fdiv double %12, 2.550000e+02
%14 = fdiv double 1.000000e+00, %13
%15 = fptrunc double %14 to float
%16 = tail call i32 @unpremultiply(i32 %4, float %15, i32 %2)
%17 = tail call i32 @unpremultiply(i32 %6, float %15, i32 %2)
%18 = tail call i32 @unpremultiply(i32 %7, float %15, i32 %2)
br label %19
19: ; preds = %1, %11, %9
%20 = phi i32 [ %7, %9 ], [ %18, %11 ], [ 0, %1 ]
%21 = phi i32 [ %6, %9 ], [ %17, %11 ], [ 0, %1 ]
%22 = phi i32 [ %4, %9 ], [ %16, %11 ], [ 0, %1 ]
%23 = and i32 %0, -16777216
%24 = shl i32 %22, 16
%25 = shl i32 %21, 8
%26 = or i32 %20, %23
%27 = or i32 %26, %25
%28 = or i32 %27, %24
ret i32 %28
}