#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 */
short atoi(char *);
short check_atoi(char *str) {
  short answer;
  char *pch = str;
  if ((answer = atoi(str)) > 0)
    return answer;
  if (answer < 0)
    return (-2);
  if (*pch == '\0')
    return (-3);
  pch--;
  while (*(++pch) != '\0') {
    switch (*pch) {
    case ' ':
    case '0':
      break;
    case 'q':
    case 'Q':
      return (-1);
      break;
    default:
      return (-2);
      break;
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<check_atoi>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <atoi@plt>
mov    %ax,-0x12(%rbp)
cwtl
cmp    $0x0,%eax
jle    1148 <check_atoi+0x38>
mov    -0x12(%rbp),%ax
mov    %ax,-0x2(%rbp)
jmp    1214 <check_atoi+0x104>
movswl -0x12(%rbp),%eax
cmp    $0x0,%eax
jge    1160 <check_atoi+0x50>
movw   $0xfffe,-0x2(%rbp)
jmp    1214 <check_atoi+0x104>
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    117b <check_atoi+0x6b>
movw   $0xfffd,-0x2(%rbp)
jmp    1214 <check_atoi+0x104>
mov    -0x20(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x20(%rbp)
movsbl 0x1(%rax),%eax
cmp    $0x0,%eax
je     120e <check_atoi+0xfe>
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
mov    %eax,-0x24(%rbp)
sub    $0x20,%eax
je     11ee <check_atoi+0xde>
jmp    11bb <check_atoi+0xab>
mov    -0x24(%rbp),%eax
sub    $0x30,%eax
je     11ee <check_atoi+0xde>
jmp    11cc <check_atoi+0xbc>
mov    -0x24(%rbp),%eax
sub    $0x51,%eax
je     11f3 <check_atoi+0xe3>
jmp    11dd <check_atoi+0xcd>
mov    -0x24(%rbp),%eax
sub    $0x71,%eax
je     11f3 <check_atoi+0xe3>
jmp    11fe <check_atoi+0xee>
jmp    1209 <check_atoi+0xf9>
movw   $0xffff,-0x2(%rbp)
jmp    1214 <check_atoi+0x104>
movw   $0xfffe,-0x2(%rbp)
jmp    1214 <check_atoi+0x104>
jmp    1187 <check_atoi+0x77>
movw   $0x0,-0x2(%rbp)
movswl -0x2(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<check_atoi>:
push   %rbx
mov    %rdi,%rbx
call   1030 <atoi@plt>
test   %ax,%ax
jle    1120 <check_atoi+0x10>
pop    %rbx
ret
mov    $0xfffe,%ax
js     111e <check_atoi+0xe>
mov    (%rbx),%al
test   %al,%al
je     1171 <check_atoi+0x61>
add    $0x1,%rbx
jmp    1150 <check_atoi+0x40>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
cmp    $0x20,%ecx
jne    116b <check_atoi+0x5b>
movzbl (%rbx),%eax
add    $0x1,%rbx
test   %al,%al
je     1177 <check_atoi+0x67>
movsbl %al,%ecx
cmp    $0x2f,%ecx
jle    1140 <check_atoi+0x30>
cmp    $0x30,%ecx
je     1145 <check_atoi+0x35>
mov    $0xffff,%ax
cmp    $0x51,%ecx
je     111e <check_atoi+0xe>
cmp    $0x71,%ecx
je     111e <check_atoi+0xe>
mov    $0xfffe,%ax
pop    %rbx
ret
mov    $0xfffd,%ax
pop    %rbx
ret
xor    %eax,%eax
pop    %rbx
ret

LLVM IR (-O0):
define signext i16 @check_atoi(i8* %0) {
%2 = alloca i16
%3 = alloca i8*
%4 = alloca i16
%5 = alloca i8*
store i8* %0, i8** %3
%6 = load i8*, i8** %3
store i8* %6, i8** %5
%7 = load i8*, i8** %3
%8 = call signext i16 @atoi(i8* %7)
store i16 %8, i16* %4
%9 = sext i16 %8 to i32
%10 = icmp sgt i32 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %1
%12 = load i16, i16* %4
store i16 %12, i16* %2
br label %42
13: ; preds = %1
%14 = load i16, i16* %4
%15 = sext i16 %14 to i32
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %18
17: ; preds = %13
store i16 -2, i16* %2
br label %42
18: ; preds = %13
%19 = load i8*, i8** %5
%20 = load i8, i8* %19
%21 = sext i8 %20 to i32
%22 = icmp eq i32 %21, 0
br i1 %22, label %23, label %24
23: ; preds = %18
store i16 -3, i16* %2
br label %42
24: ; preds = %18
%25 = load i8*, i8** %5
%26 = getelementptr inbounds i8, i8* %25, i32 -1
store i8* %26, i8** %5
br label %27
27: ; preds = %40, %24
%28 = load i8*, i8** %5
%29 = getelementptr inbounds i8, i8* %28, i32 1
store i8* %29, i8** %5
%30 = load i8, i8* %29
%31 = sext i8 %30 to i32
%32 = icmp ne i32 %31, 0
br i1 %32, label %33, label %41
33: ; preds = %27
%34 = load i8*, i8** %5
%35 = load i8, i8* %34
%36 = sext i8 %35 to i32
switch i32 %36, label %39 [
i32 32, label %37
i32 48, label %37
i32 113, label %38
i32 81, label %38
]
37: ; preds = %33, %33
br label %40
38: ; preds = %33, %33
store i16 -1, i16* %2
br label %42
39: ; preds = %33
store i16 -2, i16* %2
br label %42
40: ; preds = %37
br label %27
41: ; preds = %27
store i16 0, i16* %2
br label %42
42: ; preds = %41, %39, %38, %23, %17, %11
%43 = load i16, i16* %2
ret i16 %43
}

LLVM IR (-O2):
define signext i16 @check_atoi(i8* nocapture %0) {
%2 = tail call signext i16 @atoi(i8* %0)
%3 = icmp sgt i16 %2, 0
br i1 %3, label %19, label %4
4: ; preds = %1
%5 = icmp slt i16 %2, 0
br i1 %5, label %19, label %6
6: ; preds = %4
%7 = load i8, i8* %0
%8 = icmp eq i8 %7, 0
br i1 %8, label %19, label %9
9: ; preds = %6, %13
%10 = phi i8 [ %15, %13 ], [ %7, %6 ]
%11 = phi i8* [ %14, %13 ], [ %0, %6 ]
%12 = sext i8 %10 to i32
switch i32 %12, label %19 [
i32 32, label %13
i32 48, label %13
i32 113, label %17
i32 81, label %17
]
13: ; preds = %9, %9
%14 = getelementptr inbounds i8, i8* %11, i64 1
%15 = load i8, i8* %14
%16 = icmp eq i8 %15, 0
br i1 %16, label %17, label %9
17: ; preds = %13, %9, %9
%18 = phi i16 [ 0, %13 ], [ -1, %9 ], [ -1, %9 ]
br label %19
19: ; preds = %9, %17, %6, %4, %1
%20 = phi i16 [ %2, %1 ], [ -2, %4 ], [ -3, %6 ], [ %18, %17 ], [ -2, %9 ]
ret i16 %20
}