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

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, unsigned int);
int /*<<< orphan*/ stderr;
unsigned int mul_hi_op(unsigned int size, unsigned int a, unsigned int b) {
  uint64_t res = 0;
  unsigned int mask;
  switch (size) {
  case 8:
    mask = 0xff;
    break;
  case 16:
    mask = 0xffff;
    break;
  case 32:
    mask = 0xffffffff;
    break;
  default:
    fprintf(stderr, "inv size in mul %d\n", size);
    exit(EXIT_FAILURE);
  }
  a &= mask;
  b &= mask;
  res = ((uint64_t)a * (uint64_t)b);
  return (res >> 32) & mask;
}


x86 assembly (clang, -O0):
<mul_hi_op>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %edx,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
mov    -0x4(%rbp),%eax
mov    %eax,-0x18(%rbp)
sub    $0x8,%eax
je     116e <mul_hi_op+0x4e>
jmp    114c <mul_hi_op+0x2c>
mov    -0x18(%rbp),%eax
sub    $0x10,%eax
je     117a <mul_hi_op+0x5a>
jmp    115d <mul_hi_op+0x3d>
mov    -0x18(%rbp),%eax
sub    $0x20,%eax
je     1186 <mul_hi_op+0x66>
jmp    1192 <mul_hi_op+0x72>
movl   $0xff,-0x14(%rbp)
jmp    11b8 <mul_hi_op+0x98>
movl   $0xffff,-0x14(%rbp)
jmp    11b8 <mul_hi_op+0x98>
movl   $0xffffffff,-0x14(%rbp)
jmp    11b8 <mul_hi_op+0x98>
mov    0x2e47(%rip),%rax
mov    (%rax),%edi
mov    -0x4(%rbp),%edx
lea    0xe5b(%rip),%rsi
call   1030 <fprintf@plt>
mov    0x2e27(%rip),%rax
mov    (%rax),%edi
call   1040 <exit@plt>
mov    -0x14(%rbp),%eax
and    -0x8(%rbp),%eax
mov    %eax,-0x8(%rbp)
mov    -0x14(%rbp),%eax
and    -0xc(%rbp),%eax
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%eax
imul   -0xc(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%eax
mov    $0x20,%ecx
sar    %cl,%eax
and    -0x14(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mul_hi_op>:
mov    %edi,%edx
cmp    $0x20,%edi
ja     113a <mul_hi_op+0x1a>
mov    %edx,%eax
movabs $0x100010100,%rcx
bt     %rax,%rcx
jae    113a <mul_hi_op+0x1a>
ret
push   %rax
mov    0x2e9e(%rip),%rax
mov    (%rax),%edi
lea    0xeb5(%rip),%rsi
call   1030 <fprintf@plt>
mov    0x2e81(%rip),%rax
mov    (%rax),%edi
call   1040 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [20 x i8] c"inv size in mul %d\0A\00"
@EXIT_FAILURE = global i32 0
define i32 @mul_hi_op(i32 %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
store i32 %2, i32* %6
store i32 0, i32* %7
%9 = load i32, i32* %4
switch i32 %9, label %13 [
i32 8, label %10
i32 16, label %11
i32 32, label %12
]
10: ; preds = %3
store i32 255, i32* %8
br label %19
11: ; preds = %3
store i32 65535, i32* %8
br label %19
12: ; preds = %3
store i32 -1, i32* %8
br label %19
13: ; preds = %3
%14 = load i32, i32* @stderr
%15 = load i32, i32* %4
%16 = call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i32 %15)
%17 = load i32, i32* @EXIT_FAILURE
%18 = call i32 @exit(i32 %17)
unreachable
19: ; preds = %12, %11, %10
%20 = load i32, i32* %8
%21 = load i32, i32* %5
%22 = and i32 %21, %20
store i32 %22, i32* %5
%23 = load i32, i32* %8
%24 = load i32, i32* %6
%25 = and i32 %24, %23
store i32 %25, i32* %6
%26 = load i32, i32* %5
%27 = load i32, i32* %6
%28 = mul nsw i32 %26, %27
store i32 %28, i32* %7
%29 = load i32, i32* %7
%30 = ashr i32 %29, 32
%31 = load i32, i32* %8
%32 = and i32 %30, %31
ret i32 %32
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [20 x i8] c"inv size in mul %d\0A\00"
@EXIT_FAILURE = global i32 0
define i32 @mul_hi_op(i32 %0, i32 %1, i32 %2) {
switch i32 %0, label %4 [
i32 8, label %9
i32 16, label %9
i32 32, label %9
]
4: ; preds = %3
%5 = load i32, i32* @stderr
%6 = tail call i32 @fprintf(i32 %5, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i32 %0)
%7 = load i32, i32* @EXIT_FAILURE
%8 = tail call i32 @exit(i32 %7)
unreachable
9: ; preds = %3, %3, %3
ret i32 poison
}