#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 */
long long CHAR_MAX;
long long CHAR_MIN;
int /*<<< orphan*/ LLONG_MAX;
int /*<<< orphan*/ LLONG_MIN;
int errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ stderr;
long long strtonum(char *, int /*<<< orphan*/, int /*<<< orphan*/,
                   char const **);
char stringtochar(char *string) {
  const char *err;
  long long ll;
  char answer;
  errno = 0;
  ll = strtonum(string, LLONG_MIN, LLONG_MAX, &err);
  if (err != NULL) {
    errno = -2;
    fprintf(stderr, "strtonum error: %s\n", err);
    return 0;
  }
  if (ll > CHAR_MAX) {
    errno = 1;
    fprintf(stderr,
            "stringtochar: number too big for char! (Number was %lld)\n", ll);
    return 0;
  } else if (ll < CHAR_MIN) {
    errno = -1;
    fprintf(stderr,
            "stringtochar: number too big for char! (Number was %lld)\n", ll);
    return 0;
  }
  answer = (char)ll;
  errno = 0;
  return answer;
}


x86 assembly (clang, -O0):
<stringtochar>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e85(%rip),%rax
movl   $0x0,(%rax)
mov    -0x10(%rbp),%rdi
mov    0x2e84(%rip),%rax
mov    (%rax),%esi
mov    0x2e8b(%rip),%rax
mov    (%rax),%edx
lea    -0x18(%rbp),%rcx
call   1040 <strtonum@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     1198 <stringtochar+0x78>
mov    0x2e4a(%rip),%rax
movl   $0xfffffffe,(%rax)
mov    0x2e45(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rdx
lea    0xe78(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
movb   $0x0,-0x1(%rbp)
jmp    1241 <stringtochar+0x121>
mov    -0x20(%rbp),%rax
mov    0x2e45(%rip),%rcx
cmp    (%rcx),%rax
jle    11dd <stringtochar+0xbd>
mov    0x2e05(%rip),%rax
movl   $0x1,(%rax)
mov    0x2e00(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%rdx
lea    0xe47(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
movb   $0x0,-0x1(%rbp)
jmp    1241 <stringtochar+0x121>
mov    -0x20(%rbp),%rax
mov    0x2df8(%rip),%rcx
cmp    (%rcx),%rax
jge    1222 <stringtochar+0x102>
mov    0x2dc0(%rip),%rax
movl   $0xffffffff,(%rax)
mov    0x2dbb(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%rdx
lea    0xe02(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
movb   $0x0,-0x1(%rbp)
jmp    1241 <stringtochar+0x121>
jmp    1227 <stringtochar+0x107>
mov    -0x20(%rbp),%rax
mov    %al,-0x21(%rbp)
mov    0x2d83(%rip),%rax
movl   $0x0,(%rax)
mov    -0x21(%rbp),%al
mov    %al,-0x1(%rbp)
movsbl -0x1(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<stringtochar>:
push   %rbx
sub    $0x10,%rsp
mov    0x2e8c(%rip),%rbx
movl   $0x0,(%rbx)
mov    0x2e8f(%rip),%rax
mov    (%rax),%esi
mov    0x2e96(%rip),%rax
mov    (%rax),%edx
lea    0x8(%rsp),%rcx
call   1040 <strtonum@plt>
mov    0x8(%rsp),%rdx
test   %rdx,%rdx
je     1172 <stringtochar+0x52>
movl   $0xfffffffe,(%rbx)
mov    0x2e5b(%rip),%rax
mov    (%rax),%edi
lea    0xe92(%rip),%rsi
xor    %ebx,%ebx
jmp    11ad <stringtochar+0x8d>
mov    0x2e6f(%rip),%rcx
cmp    (%rcx),%rax
jle    1186 <stringtochar+0x66>
movl   $0x1,(%rbx)
jmp    1198 <stringtochar+0x78>
mov    0x2e53(%rip),%rcx
cmp    (%rcx),%rax
jge    11bc <stringtochar+0x9c>
movl   $0xffffffff,(%rbx)
mov    0x2e21(%rip),%rcx
mov    (%rcx),%edi
lea    0xe6c(%rip),%rsi
xor    %ebx,%ebx
mov    %rax,%rdx
xor    %eax,%eax
call   1030 <fprintf@plt>
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
ret
movl   $0x0,(%rbx)
mov    %eax,%ebx
jmp    11b4 <stringtochar+0x94>

LLVM IR (-O0):
@errno = global i32 0
@LLONG_MIN = global i32 0
@LLONG_MAX = global i32 0
@stderr = global i32 0
@.str = [20 x i8] c"strtonum error: %s\0A\00"
@CHAR_MAX = global i64 0
@.str.1 = [58 x i8] c"stringtochar: number too big for char! (Number was %lld)\0A\00"
@CHAR_MIN = global i64 0
define signext i8 @stringtochar(i8* %0) {
%2 = alloca i8
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i8
store i8* %0, i8** %3
store i32 0, i32* @errno
%7 = load i8*, i8** %3
%8 = load i32, i32* @LLONG_MIN
%9 = load i32, i32* @LLONG_MAX
%10 = call i64 @strtonum(i8* %7, i32 %8, i32 %9, i8** %4)
store i64 %10, i64* %5
%11 = load i8*, i8** %4
%12 = icmp ne i8* %11, null
br i1 %12, label %13, label %17
13: ; preds = %1
store i32 -2, i32* @errno
%14 = load i32, i32* @stderr
%15 = load i8*, i8** %4
%16 = call i32 (i32, i8*, ...) @fprintf(i32 %14, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %15)
store i8 0, i8* %2
br label %38
17: ; preds = %1
%18 = load i64, i64* %5
%19 = load i64, i64* @CHAR_MAX
%20 = icmp sgt i64 %18, %19
br i1 %20, label %21, label %25
21: ; preds = %17
store i32 1, i32* @errno
%22 = load i32, i32* @stderr
%23 = load i64, i64* %5
%24 = call i32 (i32, i8*, ...) @fprintf(i32 %22, i8* getelementptr inbounds ([58 x i8], [58 x i8]* @.str.1, i64 0, i64 0), i64 %23)
store i8 0, i8* %2
br label %38
25: ; preds = %17
%26 = load i64, i64* %5
%27 = load i64, i64* @CHAR_MIN
%28 = icmp slt i64 %26, %27
br i1 %28, label %29, label %33
29: ; preds = %25
store i32 -1, i32* @errno
%30 = load i32, i32* @stderr
%31 = load i64, i64* %5
%32 = call i32 (i32, i8*, ...) @fprintf(i32 %30, i8* getelementptr inbounds ([58 x i8], [58 x i8]* @.str.1, i64 0, i64 0), i64 %31)
store i8 0, i8* %2
br label %38
33: ; preds = %25
br label %34
34: ; preds = %33
%35 = load i64, i64* %5
%36 = trunc i64 %35 to i8
store i8 %36, i8* %6
store i32 0, i32* @errno
%37 = load i8, i8* %6
store i8 %37, i8* %2
br label %38
38: ; preds = %34, %29, %21, %13
%39 = load i8, i8* %2
ret i8 %39
}

LLVM IR (-O2):
@errno = global i32 0
@LLONG_MIN = global i32 0
@LLONG_MAX = global i32 0
@stderr = global i32 0
@.str = [20 x i8] c"strtonum error: %s\0A\00"
@CHAR_MAX = global i64 0
@.str.1 = [58 x i8] c"stringtochar: number too big for char! (Number was %lld)\0A\00"
@CHAR_MIN = global i64 0
define signext i8 @stringtochar(i8* %0) {
%2 = alloca i8*
%3 = bitcast i8** %2 to i8*
store i32 0, i32* @errno
%4 = load i32, i32* @LLONG_MIN
%5 = load i32, i32* @LLONG_MAX
%6 = call i64 @strtonum(i8* %0, i32 %4, i32 %5, i8** %2)
%7 = load i8*, i8** %2
%8 = icmp eq i8* %7, null
br i1 %8, label %12, label %9
9: ; preds = %1
store i32 -2, i32* @errno
%10 = load i32, i32* @stderr
%11 = call i32 (i32, i8*, ...) @fprintf(i32 %10, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %7)
br label %26
12: ; preds = %1
%13 = load i64, i64* @CHAR_MAX
%14 = icmp sgt i64 %6, %13
br i1 %14, label %15, label %18
15: ; preds = %12
store i32 1, i32* @errno
%16 = load i32, i32* @stderr
%17 = call i32 (i32, i8*, ...) @fprintf(i32 %16, i8* getelementptr inbounds ([58 x i8], [58 x i8]* @.str.1, i64 0, i64 0), i64 %6)
br label %26
18: ; preds = %12
%19 = load i64, i64* @CHAR_MIN
%20 = icmp slt i64 %6, %19
br i1 %20, label %21, label %24
21: ; preds = %18
store i32 -1, i32* @errno
%22 = load i32, i32* @stderr
%23 = call i32 (i32, i8*, ...) @fprintf(i32 %22, i8* getelementptr inbounds ([58 x i8], [58 x i8]* @.str.1, i64 0, i64 0), i64 %6)
br label %26
24: ; preds = %18
%25 = trunc i64 %6 to i8
store i32 0, i32* @errno
br label %26
26: ; preds = %24, %21, %15, %9
%27 = phi i8 [ 0, %9 ], [ 0, %15 ], [ 0, %21 ], [ %25, %24 ]
ret i8 %27
}