#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 isDigit(char);
int strlen(char *);
int str2int(char *s) {
  int i, l, r, d;
  l = strlen(s);
  r = 0;
  if (l > 3)
    return -1;
  for (i = 0; i < l; i++) {
    d = isDigit(s[i]);
    if (d >= 0)
      r = r * 10 + d;
    else
      return -1;
  }
  return r;
}


x86 assembly (clang, -O0):
<str2int>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
cmpl   $0x3,-0x18(%rbp)
jle    1155 <str2int+0x35>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ba <str2int+0x9a>
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x18(%rbp),%eax
jge    11b4 <str2int+0x94>
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
movsbl (%rax,%rcx,1),%edi
call   1040 <isDigit@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jl     1195 <str2int+0x75>
imul   $0xa,-0x1c(%rbp),%eax
add    -0x20(%rbp),%eax
mov    %eax,-0x1c(%rbp)
jmp    11a1 <str2int+0x81>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ba <str2int+0x9a>
jmp    11a6 <str2int+0x86>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    115c <str2int+0x3c>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<str2int>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r14
call   1030 <strlen@plt>
cmp    $0x3,%eax
jle    113d <str2int+0x1d>
mov    $0xffffffff,%r12d
jmp    1174 <str2int+0x54>
test   %eax,%eax
jle    1171 <str2int+0x51>
mov    %eax,%r15d
xor    %ebx,%ebx
xor    %r12d,%r12d
nopl   0x0(%rax)
movsbl (%r14,%rbx,1),%edi
call   1040 <isDigit@plt>
test   %eax,%eax
js     1135 <str2int+0x15>
lea    (%r12,%r12,4),%ecx
lea    (%rax,%rcx,2),%r12d
add    $0x1,%rbx
cmp    %rbx,%r15
jne    1150 <str2int+0x30>
jmp    1174 <str2int+0x54>
xor    %r12d,%r12d
mov    %r12d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i32 @str2int(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i8* %0, i8** %3
%8 = load i8*, i8** %3
%9 = call i32 @strlen(i8* %8)
store i32 %9, i32* %5
store i32 0, i32* %6
%10 = load i32, i32* %5
%11 = icmp sgt i32 %10, 3
br i1 %11, label %12, label %13
12: ; preds = %1
store i32 -1, i32* %2
br label %39
13: ; preds = %1
store i32 0, i32* %4
br label %14
14: ; preds = %34, %13
%15 = load i32, i32* %4
%16 = load i32, i32* %5
%17 = icmp slt i32 %15, %16
br i1 %17, label %18, label %37
18: ; preds = %14
%19 = load i8*, i8** %3
%20 = load i32, i32* %4
%21 = sext i32 %20 to i64
%22 = getelementptr inbounds i8, i8* %19, i64 %21
%23 = load i8, i8* %22
%24 = call i32 @isDigit(i8 signext %23)
store i32 %24, i32* %7
%25 = load i32, i32* %7
%26 = icmp sge i32 %25, 0
br i1 %26, label %27, label %32
27: ; preds = %18
%28 = load i32, i32* %6
%29 = mul nsw i32 %28, 10
%30 = load i32, i32* %7
%31 = add nsw i32 %29, %30
store i32 %31, i32* %6
br label %33
32: ; preds = %18
store i32 -1, i32* %2
br label %39
33: ; preds = %27
br label %34
34: ; preds = %33
%35 = load i32, i32* %4
%36 = add nsw i32 %35, 1
store i32 %36, i32* %4
br label %14
37: ; preds = %14
%38 = load i32, i32* %6
store i32 %38, i32* %2
br label %39
39: ; preds = %37, %32, %12
%40 = load i32, i32* %2
ret i32 %40
}

LLVM IR (-O2):
define i32 @str2int(i8* %0) {
%2 = tail call i32 @strlen(i8* %0)
%3 = icmp sgt i32 %2, 3
br i1 %3, label %20, label %4
4: ; preds = %1
%5 = icmp sgt i32 %2, 0
br i1 %5, label %6, label %20
6: ; preds = %4
%7 = zext i32 %2 to i64
br label %8
8: ; preds = %6, %15
%9 = phi i64 [ 0, %6 ], [ %18, %15 ]
%10 = phi i32 [ 0, %6 ], [ %17, %15 ]
%11 = getelementptr inbounds i8, i8* %0, i64 %9
%12 = load i8, i8* %11
%13 = tail call i32 @isDigit(i8 signext %12)
%14 = icmp sgt i32 %13, -1
br i1 %14, label %15, label %20
15: ; preds = %8
%16 = mul nsw i32 %10, 10
%17 = add nsw i32 %13, %16
%18 = add nuw nsw i64 %9, 1
%19 = icmp eq i64 %18, %7
br i1 %19, label %20, label %8
20: ; preds = %8, %15, %4, %1
%21 = phi i32 [ -1, %1 ], [ 0, %4 ], [ -1, %8 ], [ %17, %15 ]
ret i32 %21
}