#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 INT_MAX;
int INT_MIN;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ perror(char *);
int safe_add(int a, int b) {
  if (a > 0 && b > INT_MAX - a) {
    perror("Overflow Error");
    exit(-1);
  } else if (a < 0 && b < INT_MIN - a) {
    perror("Overflow Error");
    exit(-1);
  }
  return a + b;
}


x86 assembly (clang, -O0):
<safe_add>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
cmpl   $0x0,-0x4(%rbp)
jle    1165 <safe_add+0x45>
mov    -0x8(%rbp),%eax
mov    0x2ea6(%rip),%rcx
mov    (%rcx),%ecx
sub    -0x4(%rbp),%ecx
cmp    %ecx,%eax
jle    1165 <safe_add+0x45>
lea    0xeaa(%rip),%rdi
call   1030 <perror@plt>
mov    $0xffffffff,%edi
call   1040 <exit@plt>
cmpl   $0x0,-0x4(%rbp)
jge    119c <safe_add+0x7c>
mov    -0x8(%rbp),%eax
mov    0x2e67(%rip),%rcx
mov    (%rcx),%ecx
sub    -0x4(%rbp),%ecx
cmp    %ecx,%eax
jge    119c <safe_add+0x7c>
lea    0xe73(%rip),%rdi
call   1030 <perror@plt>
mov    $0xffffffff,%edi
call   1040 <exit@plt>
jmp    11a1 <safe_add+0x81>
mov    -0x4(%rbp),%eax
add    -0x8(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<safe_add>:
push   %rax
test   %edi,%edi
jle    114a <safe_add+0x2a>
mov    0x2ebc(%rip),%rax
mov    (%rax),%eax
sub    %edi,%eax
cmp    %esi,%eax
jge    115b <safe_add+0x3b>
lea    0xec5(%rip),%rdi
call   1030 <perror@plt>
mov    $0xffffffff,%edi
call   1040 <exit@plt>
jns    115b <safe_add+0x3b>
mov    0x2e8d(%rip),%rax
mov    (%rax),%eax
sub    %edi,%eax
cmp    %esi,%eax
jg     1134 <safe_add+0x14>
add    %edi,%esi
mov    %esi,%eax
pop    %rcx
ret

LLVM IR (-O0):
@INT_MAX = global i32 0
@.str = [15 x i8] c"Overflow Error\00"
@INT_MIN = global i32 0
define i32 @safe_add(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
store i32 %0, i32* %3
store i32 %1, i32* %4
%5 = load i32, i32* %3
%6 = icmp sgt i32 %5, 0
br i1 %6, label %7, label %16
7: ; preds = %2
%8 = load i32, i32* %4
%9 = load i32, i32* @INT_MAX
%10 = load i32, i32* %3
%11 = sub nsw i32 %9, %10
%12 = icmp sgt i32 %8, %11
br i1 %12, label %13, label %16
13: ; preds = %7
%14 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%15 = call i32 @exit(i32 -1)
unreachable
16: ; preds = %7, %2
%17 = load i32, i32* %3
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %28
19: ; preds = %16
%20 = load i32, i32* %4
%21 = load i32, i32* @INT_MIN
%22 = load i32, i32* %3
%23 = sub nsw i32 %21, %22
%24 = icmp slt i32 %20, %23
br i1 %24, label %25, label %28
25: ; preds = %19
%26 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%27 = call i32 @exit(i32 -1)
unreachable
28: ; preds = %19, %16
br label %29
29: ; preds = %28
%30 = load i32, i32* %3
%31 = load i32, i32* %4
%32 = add nsw i32 %30, %31
ret i32 %32
}

LLVM IR (-O2):
@INT_MAX = global i32 0
@.str = [15 x i8] c"Overflow Error\00"
@INT_MIN = global i32 0
define i32 @safe_add(i32 %0, i32 %1) {
%3 = icmp sgt i32 %0, 0
br i1 %3, label %4, label %11
4: ; preds = %2
%5 = load i32, i32* @INT_MAX
%6 = sub nsw i32 %5, %0
%7 = icmp slt i32 %6, %1
br i1 %7, label %8, label %20
8: ; preds = %4
%9 = tail call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%10 = tail call i32 @exit(i32 -1)
unreachable
11: ; preds = %2
%12 = icmp slt i32 %0, 0
br i1 %12, label %13, label %20
13: ; preds = %11
%14 = load i32, i32* @INT_MIN
%15 = sub nsw i32 %14, %0
%16 = icmp sgt i32 %15, %1
br i1 %16, label %17, label %20
17: ; preds = %13
%18 = tail call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%19 = tail call i32 @exit(i32 -1)
unreachable
20: ; preds = %4, %11, %13
%21 = add nsw i32 %1, %0
ret i32 %21
}