#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 sscanf(char *, char *, int *, char *);
int /*<<< orphan*/ suicidef(char *, ...);
int string_to_int(const char *s) {
  char *ss;
  int v;
  char extra;
  ss = (char *)s;
  while ((*ss == ' ') || (*ss == '\t') || (*ss == '\n'))
    ss++;
  if (*ss == 0)
    goto empty_string;
  if (sscanf(ss, "%d%c", &v, &extra) != 1)
    goto not_an_integer;
  if ((v < 0) && (*ss != '-'))
    goto out_of_range;
  if ((v > 0) && (*ss == '-'))
    goto out_of_range;
  return v;
empty_string:
  suicidef("an empty string is not an integer");
not_an_integer:
  suicidef("\"%s\" is not an integer", s);
out_of_range:
  suicidef("\"%s\" is outside the range of a signed integer", s);
  return 0;
}


x86 assembly (clang, -O0):
<string_to_int>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
movsbl (%rax),%ecx
mov    $0x1,%al
cmp    $0x20,%ecx
mov    %al,-0x1e(%rbp)
je     116e <string_to_int+0x4e>
mov    -0x18(%rbp),%rax
movsbl (%rax),%ecx
mov    $0x1,%al
cmp    $0x9,%ecx
mov    %al,-0x1e(%rbp)
je     116e <string_to_int+0x4e>
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
cmp    $0xa,%eax
sete   %al
mov    %al,-0x1e(%rbp)
mov    -0x1e(%rbp),%al
test   $0x1,%al
jne    117e <string_to_int+0x5e>
jmp    118f <string_to_int+0x6f>
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
jmp    1134 <string_to_int+0x14>
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    11a4 <string_to_int+0x84>
jmp    1213 <string_to_int+0xf3>
mov    -0x18(%rbp),%rdi
lea    0xe51(%rip),%rsi
lea    -0x1c(%rbp),%rdx
lea    -0x1d(%rbp),%rcx
call   1040 <sscanf@plt>
cmp    $0x1,%eax
je     11ca <string_to_int+0xaa>
jmp    1221 <string_to_int+0x101>
cmpl   $0x0,-0x1c(%rbp)
jge    11e9 <string_to_int+0xc9>
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2d,%eax
je     11e9 <string_to_int+0xc9>
jmp    1233 <string_to_int+0x113>
cmpl   $0x0,-0x1c(%rbp)
jle    1208 <string_to_int+0xe8>
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2d,%eax
jne    1208 <string_to_int+0xe8>
jmp    1233 <string_to_int+0x113>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    124c <string_to_int+0x12c>
lea    0xdeb(%rip),%rdi
mov    $0x0,%al
call   1030 <suicidef@plt>
mov    -0x10(%rbp),%rsi
lea    0xdfb(%rip),%rdi
mov    $0x0,%al
call   1030 <suicidef@plt>
mov    -0x10(%rbp),%rsi
lea    0xe00(%rip),%rdi
mov    $0x0,%al
call   1030 <suicidef@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<string_to_int>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%r14
mov    %rdi,%rbx
jmp    1134 <string_to_int+0x14>
add    $0x1,%rbx
movzbl (%rbx),%eax
lea    -0x9(%rax),%ecx
cmp    $0x2,%cl
jb     1130 <string_to_int+0x10>
cmp    $0x20,%al
je     1130 <string_to_int+0x10>
test   %al,%al
jne    1157 <string_to_int+0x37>
lea    0xeb7(%rip),%rdi
xor    %eax,%eax
call   1030 <suicidef@plt>
jmp    1186 <string_to_int+0x66>
lea    0xea2(%rip),%rsi
lea    0xc(%rsp),%rdx
lea    0xb(%rsp),%rcx
mov    %rbx,%rdi
call   1040 <sscanf@plt>
cmp    $0x1,%eax
jne    1186 <string_to_int+0x66>
mov    0xc(%rsp),%ebp
test   %ebp,%ebp
js     11b5 <string_to_int+0x95>
je     11bc <string_to_int+0x9c>
cmpb   $0x2d,(%rbx)
je     1197 <string_to_int+0x77>
jmp    11aa <string_to_int+0x8a>
lea    0xe9a(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1030 <suicidef@plt>
lea    0xea0(%rip),%rdi
xor    %ebp,%ebp
mov    %r14,%rsi
xor    %eax,%eax
call   1030 <suicidef@plt>
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
cmpb   $0x2d,(%rbx)
jne    1197 <string_to_int+0x77>
jmp    11aa <string_to_int+0x8a>
xor    %ebp,%ebp
jmp    11aa <string_to_int+0x8a>

LLVM IR (-O0):
@.str = [5 x i8] c"%d%c\00"
@.str.1 = [34 x i8] c"an empty string is not an integer\00"
@.str.2 = [23 x i8] c"\22%s\22 is not an integer\00"
@.str.3 = [46 x i8] c"\22%s\22 is outside the range of a signed integer\00"
define i32 @string_to_int(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i8
store i8* %0, i8** %3
%7 = load i8*, i8** %3
store i8* %7, i8** %4
br label %8
8: ; preds = %25, %1
%9 = load i8*, i8** %4
%10 = load i8, i8* %9
%11 = sext i8 %10 to i32
%12 = icmp eq i32 %11, 32
br i1 %12, label %23, label %13
13: ; preds = %8
%14 = load i8*, i8** %4
%15 = load i8, i8* %14
%16 = sext i8 %15 to i32
%17 = icmp eq i32 %16, 9
br i1 %17, label %23, label %18
18: ; preds = %13
%19 = load i8*, i8** %4
%20 = load i8, i8* %19
%21 = sext i8 %20 to i32
%22 = icmp eq i32 %21, 10
br label %23
23: ; preds = %18, %13, %8
%24 = phi i1 [ true, %13 ], [ true, %8 ], [ %22, %18 ]
br i1 %24, label %25, label %28
25: ; preds = %23
%26 = load i8*, i8** %4
%27 = getelementptr inbounds i8, i8* %26, i32 1
store i8* %27, i8** %4
br label %8
28: ; preds = %23
%29 = load i8*, i8** %4
%30 = load i8, i8* %29
%31 = sext i8 %30 to i32
%32 = icmp eq i32 %31, 0
br i1 %32, label %33, label %34
33: ; preds = %28
br label %59
34: ; preds = %28
%35 = load i8*, i8** %4
%36 = call i32 @sscanf(i8* %35, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32* %5, i8* %6)
%37 = icmp ne i32 %36, 1
br i1 %37, label %38, label %39
38: ; preds = %34
br label %61
39: ; preds = %34
%40 = load i32, i32* %5
%41 = icmp slt i32 %40, 0
br i1 %41, label %42, label %48
42: ; preds = %39
%43 = load i8*, i8** %4
%44 = load i8, i8* %43
%45 = sext i8 %44 to i32
%46 = icmp ne i32 %45, 45
br i1 %46, label %47, label %48
47: ; preds = %42
br label %64
48: ; preds = %42, %39
%49 = load i32, i32* %5
%50 = icmp sgt i32 %49, 0
br i1 %50, label %51, label %57
51: ; preds = %48
%52 = load i8*, i8** %4
%53 = load i8, i8* %52
%54 = sext i8 %53 to i32
%55 = icmp eq i32 %54, 45
br i1 %55, label %56, label %57
56: ; preds = %51
br label %64
57: ; preds = %51, %48
%58 = load i32, i32* %5
store i32 %58, i32* %2
br label %67
59: ; preds = %33
%60 = call i32 (i8*, ...) @suicidef(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0))
br label %61
61: ; preds = %59, %38
%62 = load i8*, i8** %3
%63 = call i32 (i8*, ...) @suicidef(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i8* %62)
br label %64
64: ; preds = %61, %56, %47
%65 = load i8*, i8** %3
%66 = call i32 (i8*, ...) @suicidef(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.3, i64 0, i64 0), i8* %65)
store i32 0, i32* %2
br label %67
67: ; preds = %64, %57
%68 = load i32, i32* %2
ret i32 %68
}

LLVM IR (-O2):
@.str = [5 x i8] c"%d%c\00"
@.str.1 = [34 x i8] c"an empty string is not an integer\00"
@.str.2 = [23 x i8] c"\22%s\22 is not an integer\00"
@.str.3 = [46 x i8] c"\22%s\22 is outside the range of a signed integer\00"
define i32 @string_to_int(i8* %0) {
%2 = alloca i32
%3 = alloca i8
%4 = bitcast i32* %2 to i8*
br label %5
5: ; preds = %8, %1
%6 = phi i8* [ %0, %1 ], [ %9, %8 ]
%7 = load i8, i8* %6
switch i8 %7, label %10 [
i8 32, label %8
i8 9, label %8
i8 10, label %8
i8 0, label %24
]
8: ; preds = %5, %5, %5
%9 = getelementptr inbounds i8, i8* %6, i64 1
br label %5
10: ; preds = %5
%11 = call i32 @sscanf(i8* %6, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32* %2, i8* %3)
%12 = icmp eq i32 %11, 1
br i1 %12, label %13, label %26
13: ; preds = %10
%14 = load i32, i32* %2
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %19
16: ; preds = %13
%17 = load i8, i8* %6
%18 = icmp eq i8 %17, 45
br i1 %18, label %30, label %28
19: ; preds = %13
%20 = icmp eq i32 %14, 0
br i1 %20, label %30, label %21
21: ; preds = %19
%22 = load i8, i8* %6
%23 = icmp eq i8 %22, 45
br i1 %23, label %28, label %30
24: ; preds = %5
%25 = tail call i32 (i8*, ...) @suicidef(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0))
br label %26
26: ; preds = %10, %24
%27 = call i32 (i8*, ...) @suicidef(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i8* %0)
br label %28
28: ; preds = %21, %16, %26
%29 = call i32 (i8*, ...) @suicidef(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.3, i64 0, i64 0), i8* %0)
br label %30
30: ; preds = %16, %19, %21, %28
%31 = phi i32 [ 0, %28 ], [ %14, %21 ], [ 0, %19 ], [ %14, %16 ]
ret i32 %31
}