#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 */
int /*<<< orphan*/ check(int volatile, int);
int /*<<< orphan*/ fputs(char *, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int main(void) {
  fputs("UBSAN TEST START\n", stderr);
  volatile signed char a = -2;
  volatile signed char b = 0x7f;
  volatile signed char c = a * b;
  check(c, 2);
  volatile short d = 0x7fff;
  volatile short e = 2;
  volatile short f = d * e;
  check(f, -2);
  volatile int m = 0x7fffffff;
  volatile int n = 1;
  volatile int o = m * n;
  check(o, 0x7fffffff);
  m = (-0x7fffffff - 1);
  o = m * n;
  check(o, (-0x7fffffff - 1));
  fputs("UBSAN TEST END\n", stderr);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2eaa(%rip),%rax
mov    (%rax),%esi
lea    0xec1(%rip),%rdi
call   1030 <fputs@plt>
movb   $0xfe,-0x5(%rbp)
movb   $0x7f,-0x6(%rbp)
mov    -0x5(%rbp),%al
movsbl %al,%eax
mov    -0x6(%rbp),%cl
movsbl %cl,%ecx
imul   %ecx,%eax
mov    %al,-0x7(%rbp)
mov    -0x7(%rbp),%al
movsbl %al,%edi
mov    $0x2,%esi
call   1040 <check@plt>
movw   $0x7fff,-0xa(%rbp)
movw   $0x2,-0xc(%rbp)
mov    -0xa(%rbp),%ax
cwtl
mov    -0xc(%rbp),%cx
movswl %cx,%ecx
imul   %ecx,%eax
mov    %ax,-0xe(%rbp)
mov    -0xe(%rbp),%ax
movswl %ax,%edi
mov    $0xfffffffe,%esi
call   1040 <check@plt>
movl   $0x7fffffff,-0x14(%rbp)
movl   $0x1,-0x18(%rbp)
mov    -0x14(%rbp),%eax
mov    -0x18(%rbp),%ecx
imul   %ecx,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%edi
mov    $0x7fffffff,%esi
call   1040 <check@plt>
movl   $0x80000000,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    -0x18(%rbp),%ecx
imul   %ecx,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%edi
mov    $0x80000000,%esi
call   1040 <check@plt>
mov    0x2df4(%rip),%rax
mov    (%rax),%esi
lea    0xe1d(%rip),%rdi
call   1030 <fputs@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x20,%rsp
mov    0x2eb4(%rip),%rbx
mov    (%rbx),%esi
lea    0xecb(%rip),%rdi
call   1030 <fputs@plt>
movb   $0xfe,0xb(%rsp)
movb   $0x7f,0xa(%rsp)
mov    0xb(%rsp),%cl
mov    0xa(%rsp),%al
mul    %cl
mov    %al,0x9(%rsp)
movsbl 0x9(%rsp),%edi
mov    $0x2,%esi
call   1040 <check@plt>
movw   $0x7fff,0x1a(%rsp)
movw   $0x2,0x18(%rsp)
movzwl 0x1a(%rsp),%eax
imul   0x18(%rsp),%ax
mov    %ax,0x16(%rsp)
movswl 0x16(%rsp),%edi
mov    $0xfffffffe,%esi
call   1040 <check@plt>
movl   $0x7fffffff,0x10(%rsp)
7f
movl   $0x1,0x1c(%rsp)
mov    0x10(%rsp),%eax
imul   0x1c(%rsp),%eax
mov    %eax,0xc(%rsp)
mov    0xc(%rsp),%edi
mov    $0x7fffffff,%esi
call   1040 <check@plt>
movl   $0x80000000,0x10(%rsp)
80
mov    0x10(%rsp),%eax
imul   0x1c(%rsp),%eax
mov    %eax,0xc(%rsp)
mov    0xc(%rsp),%edi
mov    $0x80000000,%esi
call   1040 <check@plt>
mov    (%rbx),%esi
lea    0xe2d(%rip),%rdi
call   1030 <fputs@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@.str = [18 x i8] c"UBSAN TEST START\0A\00"
@stderr = global i32 0
@.str.1 = [16 x i8] c"UBSAN TEST END\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i8
%3 = alloca i8
%4 = alloca i8
%5 = alloca i16
%6 = alloca i16
%7 = alloca i16
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i32 0, i32* %1
%11 = load i32, i32* @stderr
%12 = call i32 @fputs(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i32 %11)
store volatile i8 -2, i8* %2
store volatile i8 127, i8* %3
%13 = load volatile i8, i8* %2
%14 = sext i8 %13 to i32
%15 = load volatile i8, i8* %3
%16 = sext i8 %15 to i32
%17 = mul nsw i32 %14, %16
%18 = trunc i32 %17 to i8
store volatile i8 %18, i8* %4
%19 = load volatile i8, i8* %4
%20 = sext i8 %19 to i32
%21 = call i32 @check(i32 %20, i32 2)
store volatile i16 32767, i16* %5
store volatile i16 2, i16* %6
%22 = load volatile i16, i16* %5
%23 = sext i16 %22 to i32
%24 = load volatile i16, i16* %6
%25 = sext i16 %24 to i32
%26 = mul nsw i32 %23, %25
%27 = trunc i32 %26 to i16
store volatile i16 %27, i16* %7
%28 = load volatile i16, i16* %7
%29 = sext i16 %28 to i32
%30 = call i32 @check(i32 %29, i32 -2)
store volatile i32 2147483647, i32* %8
store volatile i32 1, i32* %9
%31 = load volatile i32, i32* %8
%32 = load volatile i32, i32* %9
%33 = mul nsw i32 %31, %32
store volatile i32 %33, i32* %10
%34 = load volatile i32, i32* %10
%35 = call i32 @check(i32 %34, i32 2147483647)
store volatile i32 -2147483648, i32* %8
%36 = load volatile i32, i32* %8
%37 = load volatile i32, i32* %9
%38 = mul nsw i32 %36, %37
store volatile i32 %38, i32* %10
%39 = load volatile i32, i32* %10
%40 = call i32 @check(i32 %39, i32 -2147483648)
%41 = load i32, i32* @stderr
%42 = call i32 @fputs(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i32 %41)
ret i32 0
}

LLVM IR (-O2):
@.str = [18 x i8] c"UBSAN TEST START\0A\00"
@stderr = global i32 0
@.str.1 = [16 x i8] c"UBSAN TEST END\0A\00"
define i32 @main() {
%1 = alloca i8
%2 = alloca i8
%3 = alloca i8
%4 = alloca i16
%5 = alloca i16
%6 = alloca i16
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = load i32, i32* @stderr
%11 = tail call i32 @fputs(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i32 %10)
store volatile i8 -2, i8* %1
store volatile i8 127, i8* %2
%12 = load volatile i8, i8* %1
%13 = load volatile i8, i8* %2
%14 = mul i8 %13, %12
store volatile i8 %14, i8* %3
%15 = load volatile i8, i8* %3
%16 = sext i8 %15 to i32
%17 = tail call i32 @check(i32 %16, i32 2)
%18 = bitcast i16* %4 to i8*
store volatile i16 32767, i16* %4
%19 = bitcast i16* %5 to i8*
store volatile i16 2, i16* %5
%20 = bitcast i16* %6 to i8*
%21 = load volatile i16, i16* %4
%22 = load volatile i16, i16* %5
%23 = mul i16 %22, %21
store volatile i16 %23, i16* %6
%24 = load volatile i16, i16* %6
%25 = sext i16 %24 to i32
%26 = tail call i32 @check(i32 %25, i32 -2)
%27 = bitcast i32* %7 to i8*
store volatile i32 2147483647, i32* %7
%28 = bitcast i32* %8 to i8*
store volatile i32 1, i32* %8
%29 = bitcast i32* %9 to i8*
%30 = load volatile i32, i32* %7
%31 = load volatile i32, i32* %8
%32 = mul nsw i32 %31, %30
store volatile i32 %32, i32* %9
%33 = load volatile i32, i32* %9
%34 = tail call i32 @check(i32 %33, i32 2147483647)
store volatile i32 -2147483648, i32* %7
%35 = load volatile i32, i32* %7
%36 = load volatile i32, i32* %8
%37 = mul nsw i32 %36, %35
store volatile i32 %37, i32* %9
%38 = load volatile i32, i32* %9
%39 = tail call i32 @check(i32 %38, i32 -2147483648)
%40 = load i32, i32* @stderr
%41 = tail call i32 @fputs(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i32 %40)
ret i32 0
}