#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(short volatile, int);
int /*<<< orphan*/ fputs(char *, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int main(void) {
  fputs("UBSAN TEST START\n", stderr);
  volatile int i = -1;
  volatile int j = (-0x7fffffff - 1);
  volatile int k = j - i;
  check(k, -2147483647);
  k = i - j;
  check(k, 2147483647);
  j++;
  check(j, -2147483647);
  i = 1;
  j = 0x7fffffff;
  k = i - j;
  check(k, -2147483646);
  k = j - i;
  check(k, 2147483646);
  j--;
  check(k, 2147483646);
  volatile signed char a = (-0x7f - 1);
  volatile signed char b = 1;
  volatile signed char c = a - b;
  check(c, 127);
  a--;
  check(a, 127);
  volatile short d = (-0x7fff - 1);
  volatile short e = 1;
  volatile short f = d - e;
  check(f, 32767);
  d--;
  check(d, 32767);
  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>
movl   $0xffffffff,-0x8(%rbp)
movl   $0x80000000,-0xc(%rbp)
mov    -0xc(%rbp),%eax
mov    -0x8(%rbp),%ecx
sub    %ecx,%eax
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%eax
mov    $0x80000001,%esi
movswl %ax,%edi
call   1040 <check@plt>
mov    -0x8(%rbp),%eax
mov    -0xc(%rbp),%ecx
sub    %ecx,%eax
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%eax
mov    $0x7fffffff,%esi
movswl %ax,%edi
call   1040 <check@plt>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%eax
mov    $0x80000001,%esi
movswl %ax,%edi
call   1040 <check@plt>
movl   $0x1,-0x8(%rbp)
movl   $0x7fffffff,-0xc(%rbp)
mov    -0x8(%rbp),%eax
mov    -0xc(%rbp),%ecx
sub    %ecx,%eax
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%eax
mov    $0x80000002,%esi
movswl %ax,%edi
call   1040 <check@plt>
mov    -0xc(%rbp),%eax
mov    -0x8(%rbp),%ecx
sub    %ecx,%eax
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%eax
mov    $0x7ffffffe,%esi
movswl %ax,%edi
call   1040 <check@plt>
mov    -0xc(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0xc(%rbp)
mov    -0x10(%rbp),%eax
mov    $0x7ffffffe,%esi
movswl %ax,%edi
call   1040 <check@plt>
movb   $0x80,-0x11(%rbp)
movb   $0x1,-0x12(%rbp)
mov    -0x11(%rbp),%al
movsbl %al,%eax
mov    -0x12(%rbp),%cl
movsbl %cl,%ecx
sub    %ecx,%eax
mov    %al,-0x13(%rbp)
mov    -0x13(%rbp),%al
movsbl %al,%eax
mov    $0x7f,%esi
movswl %ax,%edi
call   1040 <check@plt>
mov    -0x11(%rbp),%al
add    $0xff,%al
mov    %al,-0x11(%rbp)
mov    -0x11(%rbp),%al
movsbl %al,%eax
mov    $0x7f,%esi
movswl %ax,%edi
call   1040 <check@plt>
movw   $0x8000,-0x16(%rbp)
movw   $0x1,-0x18(%rbp)
mov    -0x16(%rbp),%ax
cwtl
mov    -0x18(%rbp),%cx
movswl %cx,%ecx
sub    %ecx,%eax
mov    %ax,-0x1a(%rbp)
mov    -0x1a(%rbp),%ax
mov    $0x7fff,%esi
movswl %ax,%edi
call   1040 <check@plt>
mov    -0x16(%rbp),%ax
add    $0xffff,%ax
mov    %ax,-0x16(%rbp)
mov    -0x16(%rbp),%ax
mov    $0x7fff,%esi
movswl %ax,%edi
call   1040 <check@plt>
mov    0x2d48(%rip),%rax
mov    (%rax),%esi
lea    0xd71(%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>
movl   $0xffffffff,0x18(%rsp)
ff
movl   $0x80000000,0x10(%rsp)
80
mov    0x10(%rsp),%eax
sub    0x18(%rsp),%eax
mov    %eax,0xc(%rsp)
mov    0xc(%rsp),%eax
movswl %ax,%edi
mov    $0x80000001,%esi
call   1040 <check@plt>
mov    0x18(%rsp),%eax
sub    0x10(%rsp),%eax
mov    %eax,0xc(%rsp)
mov    0xc(%rsp),%eax
movswl %ax,%edi
mov    $0x7fffffff,%esi
call   1040 <check@plt>
addl   $0x1,0x10(%rsp)
mov    0x10(%rsp),%eax
movswl %ax,%edi
mov    $0x80000001,%esi
call   1040 <check@plt>
movl   $0x1,0x18(%rsp)
movl   $0x7fffffff,0x10(%rsp)
7f
mov    0x18(%rsp),%eax
sub    0x10(%rsp),%eax
mov    %eax,0xc(%rsp)
mov    0xc(%rsp),%eax
movswl %ax,%edi
mov    $0x80000002,%esi
call   1040 <check@plt>
mov    0x10(%rsp),%eax
sub    0x18(%rsp),%eax
mov    %eax,0xc(%rsp)
mov    0xc(%rsp),%eax
movswl %ax,%edi
mov    $0x7ffffffe,%esi
call   1040 <check@plt>
addl   $0xffffffff,0x10(%rsp)
mov    0xc(%rsp),%eax
movswl %ax,%edi
mov    $0x7ffffffe,%esi
call   1040 <check@plt>
movb   $0x80,0xb(%rsp)
movb   $0x1,0x17(%rsp)
mov    0xb(%rsp),%al
sub    0x17(%rsp),%al
mov    %al,0x16(%rsp)
movsbl 0x16(%rsp),%edi
mov    $0x7f,%esi
call   1040 <check@plt>
addb   $0xff,0xb(%rsp)
movsbl 0xb(%rsp),%edi
mov    $0x7f,%esi
call   1040 <check@plt>
movw   $0x8000,0x14(%rsp)
movw   $0x1,0x1e(%rsp)
movzwl 0x14(%rsp),%eax
sub    0x1e(%rsp),%ax
mov    %ax,0x1c(%rsp)
movswl 0x1c(%rsp),%edi
mov    $0x7fff,%esi
call   1040 <check@plt>
addw   $0xffff,0x14(%rsp)
movswl 0x14(%rsp),%edi
mov    $0x7fff,%esi
call   1040 <check@plt>
mov    (%rbx),%esi
lea    0xd95(%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 i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8
%6 = alloca i8
%7 = alloca i8
%8 = alloca i16
%9 = alloca i16
%10 = alloca i16
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 i32 -1, i32* %2
store volatile i32 -2147483648, i32* %3
%13 = load volatile i32, i32* %3
%14 = load volatile i32, i32* %2
%15 = sub nsw i32 %13, %14
store volatile i32 %15, i32* %4
%16 = load volatile i32, i32* %4
%17 = trunc i32 %16 to i16
%18 = call i32 @check(i16 signext %17, i32 -2147483647)
%19 = load volatile i32, i32* %2
%20 = load volatile i32, i32* %3
%21 = sub nsw i32 %19, %20
store volatile i32 %21, i32* %4
%22 = load volatile i32, i32* %4
%23 = trunc i32 %22 to i16
%24 = call i32 @check(i16 signext %23, i32 2147483647)
%25 = load volatile i32, i32* %3
%26 = add nsw i32 %25, 1
store volatile i32 %26, i32* %3
%27 = load volatile i32, i32* %3
%28 = trunc i32 %27 to i16
%29 = call i32 @check(i16 signext %28, i32 -2147483647)
store volatile i32 1, i32* %2
store volatile i32 2147483647, i32* %3
%30 = load volatile i32, i32* %2
%31 = load volatile i32, i32* %3
%32 = sub nsw i32 %30, %31
store volatile i32 %32, i32* %4
%33 = load volatile i32, i32* %4
%34 = trunc i32 %33 to i16
%35 = call i32 @check(i16 signext %34, i32 -2147483646)
%36 = load volatile i32, i32* %3
%37 = load volatile i32, i32* %2
%38 = sub nsw i32 %36, %37
store volatile i32 %38, i32* %4
%39 = load volatile i32, i32* %4
%40 = trunc i32 %39 to i16
%41 = call i32 @check(i16 signext %40, i32 2147483646)
%42 = load volatile i32, i32* %3
%43 = add nsw i32 %42, -1
store volatile i32 %43, i32* %3
%44 = load volatile i32, i32* %4
%45 = trunc i32 %44 to i16
%46 = call i32 @check(i16 signext %45, i32 2147483646)
store volatile i8 -128, i8* %5
store volatile i8 1, i8* %6
%47 = load volatile i8, i8* %5
%48 = sext i8 %47 to i32
%49 = load volatile i8, i8* %6
%50 = sext i8 %49 to i32
%51 = sub nsw i32 %48, %50
%52 = trunc i32 %51 to i8
store volatile i8 %52, i8* %7
%53 = load volatile i8, i8* %7
%54 = sext i8 %53 to i16
%55 = call i32 @check(i16 signext %54, i32 127)
%56 = load volatile i8, i8* %5
%57 = add i8 %56, -1
store volatile i8 %57, i8* %5
%58 = load volatile i8, i8* %5
%59 = sext i8 %58 to i16
%60 = call i32 @check(i16 signext %59, i32 127)
store volatile i16 -32768, i16* %8
store volatile i16 1, i16* %9
%61 = load volatile i16, i16* %8
%62 = sext i16 %61 to i32
%63 = load volatile i16, i16* %9
%64 = sext i16 %63 to i32
%65 = sub nsw i32 %62, %64
%66 = trunc i32 %65 to i16
store volatile i16 %66, i16* %10
%67 = load volatile i16, i16* %10
%68 = call i32 @check(i16 signext %67, i32 32767)
%69 = load volatile i16, i16* %8
%70 = add i16 %69, -1
store volatile i16 %70, i16* %8
%71 = load volatile i16, i16* %8
%72 = call i32 @check(i16 signext %71, i32 32767)
%73 = load i32, i32* @stderr
%74 = call i32 @fputs(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i32 %73)
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 i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i8
%5 = alloca i8
%6 = alloca i8
%7 = alloca i16
%8 = alloca i16
%9 = alloca i16
%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)
%12 = bitcast i32* %1 to i8*
store volatile i32 -1, i32* %1
%13 = bitcast i32* %2 to i8*
store volatile i32 -2147483648, i32* %2
%14 = bitcast i32* %3 to i8*
%15 = load volatile i32, i32* %2
%16 = load volatile i32, i32* %1
%17 = sub nsw i32 %15, %16
store volatile i32 %17, i32* %3
%18 = load volatile i32, i32* %3
%19 = trunc i32 %18 to i16
%20 = tail call i32 @check(i16 signext %19, i32 -2147483647)
%21 = load volatile i32, i32* %1
%22 = load volatile i32, i32* %2
%23 = sub nsw i32 %21, %22
store volatile i32 %23, i32* %3
%24 = load volatile i32, i32* %3
%25 = trunc i32 %24 to i16
%26 = tail call i32 @check(i16 signext %25, i32 2147483647)
%27 = load volatile i32, i32* %2
%28 = add nsw i32 %27, 1
store volatile i32 %28, i32* %2
%29 = load volatile i32, i32* %2
%30 = trunc i32 %29 to i16
%31 = tail call i32 @check(i16 signext %30, i32 -2147483647)
store volatile i32 1, i32* %1
store volatile i32 2147483647, i32* %2
%32 = load volatile i32, i32* %1
%33 = load volatile i32, i32* %2
%34 = sub nsw i32 %32, %33
store volatile i32 %34, i32* %3
%35 = load volatile i32, i32* %3
%36 = trunc i32 %35 to i16
%37 = tail call i32 @check(i16 signext %36, i32 -2147483646)
%38 = load volatile i32, i32* %2
%39 = load volatile i32, i32* %1
%40 = sub nsw i32 %38, %39
store volatile i32 %40, i32* %3
%41 = load volatile i32, i32* %3
%42 = trunc i32 %41 to i16
%43 = tail call i32 @check(i16 signext %42, i32 2147483646)
%44 = load volatile i32, i32* %2
%45 = add nsw i32 %44, -1
store volatile i32 %45, i32* %2
%46 = load volatile i32, i32* %3
%47 = trunc i32 %46 to i16
%48 = tail call i32 @check(i16 signext %47, i32 2147483646)
store volatile i8 -128, i8* %4
store volatile i8 1, i8* %5
%49 = load volatile i8, i8* %4
%50 = load volatile i8, i8* %5
%51 = sub i8 %49, %50
store volatile i8 %51, i8* %6
%52 = load volatile i8, i8* %6
%53 = sext i8 %52 to i16
%54 = tail call i32 @check(i16 signext %53, i32 127)
%55 = load volatile i8, i8* %4
%56 = add i8 %55, -1
store volatile i8 %56, i8* %4
%57 = load volatile i8, i8* %4
%58 = sext i8 %57 to i16
%59 = tail call i32 @check(i16 signext %58, i32 127)
%60 = bitcast i16* %7 to i8*
store volatile i16 -32768, i16* %7
%61 = bitcast i16* %8 to i8*
store volatile i16 1, i16* %8
%62 = bitcast i16* %9 to i8*
%63 = load volatile i16, i16* %7
%64 = load volatile i16, i16* %8
%65 = sub i16 %63, %64
store volatile i16 %65, i16* %9
%66 = load volatile i16, i16* %9
%67 = tail call i32 @check(i16 signext %66, i32 32767)
%68 = load volatile i16, i16* %7
%69 = add i16 %68, -1
store volatile i16 %69, i16* %7
%70 = load volatile i16, i16* %7
%71 = tail call i32 @check(i16 signext %70, i32 32767)
%72 = load i32, i32* @stderr
%73 = tail call i32 @fputs(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i32 %72)
ret i32 0
}