#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 j = 0x7fffffff;
  volatile int i = -1;
  volatile int k = j + i;
  check(k, 2147483646);
  k = i + j;
  check(k, 2147483646);
  j--;
  check(j, 2147483646);
  i = 1;
  j = (-0x7fffffff - 1);
  k = i + j;
  check(k, -2147483647);
  k = j + i;
  check(k, -2147483647);
  j++;
  check(j, -2147483647);
  volatile signed char a = 0x7f;
  volatile signed char b = 1;
  volatile signed char c = a + b;
  check(c, -128);
  a++;
  check(a, -128);
  volatile short d = 0x7fff;
  volatile short e = 1;
  volatile short f = d + e;
  check(f, -32768);
  d++;
  check(d, -32768);
  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   $0x7fffffff,-0x8(%rbp)
movl   $0xffffffff,-0xc(%rbp)
mov    -0x8(%rbp),%eax
mov    -0xc(%rbp),%ecx
add    %ecx,%eax
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%eax
mov    $0x7ffffffe,%esi
movswl %ax,%edi
call   1040 <check@plt>
mov    -0xc(%rbp),%eax
mov    -0x8(%rbp),%ecx
add    %ecx,%eax
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%eax
mov    $0x7ffffffe,%esi
movswl %ax,%edi
call   1040 <check@plt>
mov    -0x8(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%eax
mov    $0x7ffffffe,%esi
movswl %ax,%edi
call   1040 <check@plt>
movl   $0x1,-0xc(%rbp)
movl   $0x80000000,-0x8(%rbp)
mov    -0xc(%rbp),%eax
mov    -0x8(%rbp),%ecx
add    %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
add    %ecx,%eax
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%eax
mov    $0x80000001,%esi
movswl %ax,%edi
call   1040 <check@plt>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%eax
mov    $0x80000001,%esi
movswl %ax,%edi
call   1040 <check@plt>
movb   $0x7f,-0x11(%rbp)
movb   $0x1,-0x12(%rbp)
mov    -0x11(%rbp),%al
movsbl %al,%eax
mov    -0x12(%rbp),%cl
movsbl %cl,%ecx
add    %ecx,%eax
mov    %al,-0x13(%rbp)
mov    -0x13(%rbp),%al
movsbl %al,%eax
mov    $0xffffff80,%esi
movswl %ax,%edi
call   1040 <check@plt>
mov    -0x11(%rbp),%al
add    $0x1,%al
mov    %al,-0x11(%rbp)
mov    -0x11(%rbp),%al
movsbl %al,%eax
mov    $0xffffff80,%esi
movswl %ax,%edi
call   1040 <check@plt>
movw   $0x7fff,-0x16(%rbp)
movw   $0x1,-0x18(%rbp)
mov    -0x16(%rbp),%ax
cwtl
mov    -0x18(%rbp),%cx
movswl %cx,%ecx
add    %ecx,%eax
mov    %ax,-0x1a(%rbp)
mov    -0x1a(%rbp),%ax
mov    $0xffff8000,%esi
movswl %ax,%edi
call   1040 <check@plt>
mov    -0x16(%rbp),%ax
add    $0x1,%ax
mov    %ax,-0x16(%rbp)
mov    -0x16(%rbp),%ax
mov    $0xffff8000,%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   $0x7fffffff,0xc(%rsp)
7f
movl   $0xffffffff,0x18(%rsp)
ff
mov    0xc(%rsp),%eax
add    0x18(%rsp),%eax
mov    %eax,0x10(%rsp)
mov    0x10(%rsp),%eax
movswl %ax,%edi
mov    $0x7ffffffe,%esi
call   1040 <check@plt>
mov    0x18(%rsp),%eax
add    0xc(%rsp),%eax
mov    %eax,0x10(%rsp)
mov    0x10(%rsp),%eax
movswl %ax,%edi
mov    $0x7ffffffe,%esi
call   1040 <check@plt>
addl   $0xffffffff,0xc(%rsp)
mov    0xc(%rsp),%eax
movswl %ax,%edi
mov    $0x7ffffffe,%esi
call   1040 <check@plt>
movl   $0x1,0x18(%rsp)
movl   $0x80000000,0xc(%rsp)
80
mov    0x18(%rsp),%eax
add    0xc(%rsp),%eax
mov    %eax,0x10(%rsp)
mov    0x10(%rsp),%eax
movswl %ax,%edi
mov    $0x80000001,%esi
call   1040 <check@plt>
mov    0xc(%rsp),%eax
add    0x18(%rsp),%eax
mov    %eax,0x10(%rsp)
mov    0x10(%rsp),%eax
movswl %ax,%edi
mov    $0x80000001,%esi
call   1040 <check@plt>
addl   $0x1,0xc(%rsp)
mov    0xc(%rsp),%eax
movswl %ax,%edi
mov    $0x80000001,%esi
call   1040 <check@plt>
movb   $0x7f,0xb(%rsp)
movb   $0x1,0x17(%rsp)
mov    0xb(%rsp),%al
add    0x17(%rsp),%al
mov    %al,0x16(%rsp)
movsbl 0x16(%rsp),%edi
mov    $0xffffff80,%esi
call   1040 <check@plt>
addb   $0x1,0xb(%rsp)
movsbl 0xb(%rsp),%edi
mov    $0xffffff80,%esi
call   1040 <check@plt>
movw   $0x7fff,0x14(%rsp)
movw   $0x1,0x1e(%rsp)
movzwl 0x14(%rsp),%eax
add    0x1e(%rsp),%ax
mov    %ax,0x1c(%rsp)
movswl 0x1c(%rsp),%edi
mov    $0xffff8000,%esi
call   1040 <check@plt>
addw   $0x1,0x14(%rsp)
movswl 0x14(%rsp),%edi
mov    $0xffff8000,%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 2147483647, i32* %2
store volatile i32 -1, i32* %3
%13 = load volatile i32, i32* %2
%14 = load volatile i32, i32* %3
%15 = add 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 2147483646)
%19 = load volatile i32, i32* %3
%20 = load volatile i32, i32* %2
%21 = add 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 2147483646)
%25 = load volatile i32, i32* %2
%26 = add nsw i32 %25, -1
store volatile i32 %26, i32* %2
%27 = load volatile i32, i32* %2
%28 = trunc i32 %27 to i16
%29 = call i32 @check(i16 signext %28, i32 2147483646)
store volatile i32 1, i32* %3
store volatile i32 -2147483648, i32* %2
%30 = load volatile i32, i32* %3
%31 = load volatile i32, i32* %2
%32 = add 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 -2147483647)
%36 = load volatile i32, i32* %2
%37 = load volatile i32, i32* %3
%38 = add 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 -2147483647)
%42 = load volatile i32, i32* %2
%43 = add nsw i32 %42, 1
store volatile i32 %43, i32* %2
%44 = load volatile i32, i32* %2
%45 = trunc i32 %44 to i16
%46 = call i32 @check(i16 signext %45, i32 -2147483647)
store volatile i8 127, 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 = add 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 -128)
%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 -128)
store volatile i16 32767, 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 = add 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 -32768)
%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 -32768)
%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 2147483647, i32* %1
%13 = bitcast i32* %2 to i8*
store volatile i32 -1, i32* %2
%14 = bitcast i32* %3 to i8*
%15 = load volatile i32, i32* %1
%16 = load volatile i32, i32* %2
%17 = add nsw i32 %16, %15
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 2147483646)
%21 = load volatile i32, i32* %2
%22 = load volatile i32, i32* %1
%23 = add nsw i32 %22, %21
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 2147483646)
%27 = load volatile i32, i32* %1
%28 = add nsw i32 %27, -1
store volatile i32 %28, i32* %1
%29 = load volatile i32, i32* %1
%30 = trunc i32 %29 to i16
%31 = tail call i32 @check(i16 signext %30, i32 2147483646)
store volatile i32 1, i32* %2
store volatile i32 -2147483648, i32* %1
%32 = load volatile i32, i32* %2
%33 = load volatile i32, i32* %1
%34 = add nsw i32 %33, %32
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 -2147483647)
%38 = load volatile i32, i32* %1
%39 = load volatile i32, i32* %2
%40 = add nsw i32 %39, %38
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 -2147483647)
%44 = load volatile i32, i32* %1
%45 = add nsw i32 %44, 1
store volatile i32 %45, i32* %1
%46 = load volatile i32, i32* %1
%47 = trunc i32 %46 to i16
%48 = tail call i32 @check(i16 signext %47, i32 -2147483647)
store volatile i8 127, i8* %4
store volatile i8 1, i8* %5
%49 = load volatile i8, i8* %4
%50 = load volatile i8, i8* %5
%51 = add i8 %50, %49
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 -128)
%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 -128)
%60 = bitcast i16* %7 to i8*
store volatile i16 32767, 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 = add i16 %64, %63
store volatile i16 %65, i16* %9
%66 = load volatile i16, i16* %9
%67 = tail call i32 @check(i16 signext %66, i32 -32768)
%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 -32768)
%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
}