#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 uint16_t;
typedef int int64_t;

/* Variables and functions */
void transfer_YUVA16161616_to_YUV422(unsigned char *(*output), uint16_t *input,
                                     int j) {
  int64_t opacity = input[3];
  int64_t transparency = 0xffff - opacity;
  if (!(j & 1)) {
    (*output)[0] = ((int64_t)input[0] * opacity) / 0xffff00;
    (*output)[1] =
        ((int64_t)input[1] * opacity + 0x8000 * transparency) / 0xffff00;
    (*output)[3] =
        ((int64_t)input[2] * opacity + 0x8000 * transparency) / 0xffff00;
  } else {
    (*output)[2] = (input[0] * opacity) / 0xffff00;
    (*output) += 4;
  }
}


x86 assembly (clang, -O0):
<transfer_YUVA16161616_to_YUV422>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x10(%rbp),%rax
mov    0xc(%rax),%eax
mov    %eax,-0x18(%rbp)
mov    $0xffff,%eax
sub    -0x18(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x14(%rbp),%eax
and    $0x1,%eax
cmp    $0x0,%eax
jne    11a3 <transfer_YUVA16161616_to_YUV422+0xa3>
mov    -0x10(%rbp),%rax
mov    (%rax),%eax
imul   -0x18(%rbp),%eax
mov    $0xffff00,%ecx
cltd
idiv   %ecx
mov    %al,%cl
mov    -0x8(%rbp),%rax
mov    (%rax),%rax
mov    %cl,(%rax)
mov    -0x10(%rbp),%rax
mov    0x4(%rax),%eax
imul   -0x18(%rbp),%eax
mov    -0x1c(%rbp),%ecx
shl    $0xf,%ecx
add    %ecx,%eax
mov    $0xffff00,%ecx
cltd
idiv   %ecx
mov    %al,%cl
mov    -0x8(%rbp),%rax
mov    (%rax),%rax
mov    %cl,0x1(%rax)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%eax
imul   -0x18(%rbp),%eax
mov    -0x1c(%rbp),%ecx
shl    $0xf,%ecx
add    %ecx,%eax
mov    $0xffff00,%ecx
cltd
idiv   %ecx
mov    %al,%cl
mov    -0x8(%rbp),%rax
mov    (%rax),%rax
mov    %cl,0x3(%rax)
jmp    11cf <transfer_YUVA16161616_to_YUV422+0xcf>
mov    -0x10(%rbp),%rax
mov    (%rax),%eax
imul   -0x18(%rbp),%eax
mov    $0xffff00,%ecx
cltd
idiv   %ecx
mov    %al,%cl
mov    -0x8(%rbp),%rax
mov    (%rax),%rax
mov    %cl,0x2(%rax)
mov    -0x8(%rbp),%rax
mov    (%rax),%rcx
add    $0x4,%rcx
mov    %rcx,(%rax)
pop    %rbp
ret

x86 assembly (clang, -O2):
<transfer_YUVA16161616_to_YUV422>:
mov    0xc(%rsi),%r9d
mov    (%rsi),%ecx
imul   %r9d,%ecx
movslq %ecx,%rcx
imul   $0xffffffff80008001,%rcx,%rax
shr    $0x20,%rax
add    %eax,%ecx
mov    %ecx,%eax
shr    $0x1f,%eax
shr    $0x17,%ecx
add    %eax,%ecx
mov    (%rdi),%r8
test   $0x1,%dl
jne    118d <transfer_YUVA16161616_to_YUV422+0x8d>
mov    %cl,(%r8)
mov    0x4(%rsi),%eax
imul   %r9d,%eax
mov    %r9d,%ecx
shl    $0xf,%ecx
mov    $0x7fff8000,%edx
sub    %ecx,%edx
add    %edx,%eax
cltq
imul   $0xffffffff80008001,%rax,%rcx
shr    $0x20,%rcx
add    %ecx,%eax
mov    %eax,%ecx
shr    $0x1f,%ecx
shr    $0x17,%eax
add    %ecx,%eax
mov    (%rdi),%rcx
mov    %al,0x1(%rcx)
imul   0x8(%rsi),%r9d
add    %edx,%r9d
movslq %r9d,%rax
imul   $0xffffffff80008001,%rax,%rcx
shr    $0x20,%rcx
add    %ecx,%eax
mov    %eax,%ecx
shr    $0x1f,%ecx
shr    $0x17,%eax
add    %ecx,%eax
mov    (%rdi),%rcx
mov    %al,0x3(%rcx)
ret
mov    %cl,0x2(%r8)
addq   $0x4,(%rdi)
ret

LLVM IR (-O0):
define void @transfer_YUVA16161616_to_YUV422(i8** %0, i32* %1, i32 %2) {
%4 = alloca i8**
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i8** %0, i8*** %4
store i32* %1, i32** %5
store i32 %2, i32* %6
%9 = load i32*, i32** %5
%10 = getelementptr inbounds i32, i32* %9, i64 3
%11 = load i32, i32* %10
store i32 %11, i32* %7
%12 = load i32, i32* %7
%13 = sub nsw i32 65535, %12
store i32 %13, i32* %8
%14 = load i32, i32* %6
%15 = and i32 %14, 1
%16 = icmp ne i32 %15, 0
br i1 %16, label %54, label %17
17: ; preds = %3
%18 = load i32*, i32** %5
%19 = getelementptr inbounds i32, i32* %18, i64 0
%20 = load i32, i32* %19
%21 = load i32, i32* %7
%22 = mul nsw i32 %20, %21
%23 = sdiv i32 %22, 16776960
%24 = trunc i32 %23 to i8
%25 = load i8**, i8*** %4
%26 = load i8*, i8** %25
%27 = getelementptr inbounds i8, i8* %26, i64 0
store i8 %24, i8* %27
%28 = load i32*, i32** %5
%29 = getelementptr inbounds i32, i32* %28, i64 1
%30 = load i32, i32* %29
%31 = load i32, i32* %7
%32 = mul nsw i32 %30, %31
%33 = load i32, i32* %8
%34 = mul nsw i32 32768, %33
%35 = add nsw i32 %32, %34
%36 = sdiv i32 %35, 16776960
%37 = trunc i32 %36 to i8
%38 = load i8**, i8*** %4
%39 = load i8*, i8** %38
%40 = getelementptr inbounds i8, i8* %39, i64 1
store i8 %37, i8* %40
%41 = load i32*, i32** %5
%42 = getelementptr inbounds i32, i32* %41, i64 2
%43 = load i32, i32* %42
%44 = load i32, i32* %7
%45 = mul nsw i32 %43, %44
%46 = load i32, i32* %8
%47 = mul nsw i32 32768, %46
%48 = add nsw i32 %45, %47
%49 = sdiv i32 %48, 16776960
%50 = trunc i32 %49 to i8
%51 = load i8**, i8*** %4
%52 = load i8*, i8** %51
%53 = getelementptr inbounds i8, i8* %52, i64 3
store i8 %50, i8* %53
br label %68
54: ; preds = %3
%55 = load i32*, i32** %5
%56 = getelementptr inbounds i32, i32* %55, i64 0
%57 = load i32, i32* %56
%58 = load i32, i32* %7
%59 = mul nsw i32 %57, %58
%60 = sdiv i32 %59, 16776960
%61 = trunc i32 %60 to i8
%62 = load i8**, i8*** %4
%63 = load i8*, i8** %62
%64 = getelementptr inbounds i8, i8* %63, i64 2
store i8 %61, i8* %64
%65 = load i8**, i8*** %4
%66 = load i8*, i8** %65
%67 = getelementptr inbounds i8, i8* %66, i64 4
store i8* %67, i8** %65
br label %68
68: ; preds = %54, %17
ret void
}

LLVM IR (-O2):
define void @transfer_YUVA16161616_to_YUV422(i8** nocapture %0, i32* nocapture %1, i32 %2) {
%4 = getelementptr inbounds i32, i32* %1, i64 3
%5 = load i32, i32* %4
%6 = and i32 %2, 1
%7 = icmp eq i32 %6, 0
%8 = load i32, i32* %1
%9 = mul nsw i32 %8, %5
%10 = sdiv i32 %9, 16776960
%11 = trunc i32 %10 to i8
%12 = load i8*, i8** %0
br i1 %7, label %13, label %32
13: ; preds = %3
store i8 %11, i8* %12
%14 = getelementptr inbounds i32, i32* %1, i64 1
%15 = load i32, i32* %14
%16 = mul nsw i32 %15, %5
%17 = mul i32 %5, -32768
%18 = add i32 %17, 2147450880
%19 = add nsw i32 %16, %18
%20 = sdiv i32 %19, 16776960
%21 = trunc i32 %20 to i8
%22 = load i8*, i8** %0
%23 = getelementptr inbounds i8, i8* %22, i64 1
store i8 %21, i8* %23
%24 = getelementptr inbounds i32, i32* %1, i64 2
%25 = load i32, i32* %24
%26 = mul nsw i32 %25, %5
%27 = add nsw i32 %26, %18
%28 = sdiv i32 %27, 16776960
%29 = trunc i32 %28 to i8
%30 = load i8*, i8** %0
%31 = getelementptr inbounds i8, i8* %30, i64 3
store i8 %29, i8* %31
br label %36
32: ; preds = %3
%33 = getelementptr inbounds i8, i8* %12, i64 2
store i8 %11, i8* %33
%34 = load i8*, i8** %0
%35 = getelementptr inbounds i8, i8* %34, i64 4
store i8* %35, i8** %0
br label %36
36: ; preds = %32, %13
ret void
}