#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 strtol(char *, char **, int);
long str2dec_positive(char *s) {
  char *p = NULL;
  long ret;
  if (s == NULL || s[0] == '\0') {
    return -1;
  }
  ret = strtol(s, &p, 10);
  if (p != NULL && p[0] != '\0') {
    return -1;
  }
  return ret >= 0 ? ret : -1;
}


x86 assembly (clang, -O0):
<str2dec_positive>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     113f <str2dec_positive+0x2f>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    114c <str2dec_positive+0x3c>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    11ba <str2dec_positive+0xaa>
mov    -0x10(%rbp),%rdi
lea    -0x18(%rbp),%rsi
mov    $0xa,%edx
call   1030 <strtol@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     118a <str2dec_positive+0x7a>
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     118a <str2dec_positive+0x7a>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    11ba <str2dec_positive+0xaa>
cmpq   $0x0,-0x20(%rbp)
jl     11a2 <str2dec_positive+0x92>
mov    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
jmp    11b2 <str2dec_positive+0xa2>
mov    $0xffffffffffffffff,%rax
mov    %rax,-0x28(%rbp)
jmp    11b2 <str2dec_positive+0xa2>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<str2dec_positive>:
push   %rbx
sub    $0x10,%rsp
movq   $0x0,0x8(%rsp)
mov    $0xffffffffffffffff,%rbx
test   %rdi,%rdi
je     115b <str2dec_positive+0x4b>
cmpb   $0x0,(%rdi)
je     115b <str2dec_positive+0x4b>
lea    0x8(%rsp),%rsi
mov    $0xa,%edx
call   1030 <strtol@plt>
mov    0x8(%rsp),%rcx
test   %rcx,%rcx
je     114d <str2dec_positive+0x3d>
cmpb   $0x0,(%rcx)
jne    115b <str2dec_positive+0x4b>
test   %rax,%rax
mov    $0xffffffffffffffff,%rbx
cmovns %rax,%rbx
mov    %rbx,%rax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
define i64 @str2dec_positive(i8* %0) {
%2 = alloca i64
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i64
store i8* %0, i8** %3
store i8* null, i8** %4
%6 = load i8*, i8** %3
%7 = icmp eq i8* %6, null
br i1 %7, label %14, label %8
8: ; preds = %1
%9 = load i8*, i8** %3
%10 = getelementptr inbounds i8, i8* %9, i64 0
%11 = load i8, i8* %10
%12 = sext i8 %11 to i32
%13 = icmp eq i32 %12, 0
br i1 %13, label %14, label %15
14: ; preds = %8, %1
store i64 -1, i64* %2
br label %35
15: ; preds = %8
%16 = load i8*, i8** %3
%17 = call i64 @strtol(i8* %16, i8** %4, i32 10)
store i64 %17, i64* %5
%18 = load i8*, i8** %4
%19 = icmp ne i8* %18, null
br i1 %19, label %20, label %27
20: ; preds = %15
%21 = load i8*, i8** %4
%22 = getelementptr inbounds i8, i8* %21, i64 0
%23 = load i8, i8* %22
%24 = sext i8 %23 to i32
%25 = icmp ne i32 %24, 0
br i1 %25, label %26, label %27
26: ; preds = %20
store i64 -1, i64* %2
br label %35
27: ; preds = %20, %15
%28 = load i64, i64* %5
%29 = icmp sge i64 %28, 0
br i1 %29, label %30, label %32
30: ; preds = %27
%31 = load i64, i64* %5
br label %33
32: ; preds = %27
br label %33
33: ; preds = %32, %30
%34 = phi i64 [ %31, %30 ], [ -1, %32 ]
store i64 %34, i64* %2
br label %35
35: ; preds = %33, %26, %14
%36 = load i64, i64* %2
ret i64 %36
}

LLVM IR (-O2):
define i64 @str2dec_positive(i8* %0) {
%2 = alloca i8*
%3 = bitcast i8** %2 to i8*
store i8* null, i8** %2
%4 = icmp eq i8* %0, null
br i1 %4, label %18, label %5
5: ; preds = %1
%6 = load i8, i8* %0
%7 = icmp eq i8 %6, 0
br i1 %7, label %18, label %8
8: ; preds = %5
%9 = call i64 @strtol(i8* %0, i8** %2, i32 10)
%10 = load i8*, i8** %2
%11 = icmp eq i8* %10, null
br i1 %11, label %15, label %12
12: ; preds = %8
%13 = load i8, i8* %10
%14 = icmp eq i8 %13, 0
br i1 %14, label %15, label %18
15: ; preds = %12, %8
%16 = icmp sgt i64 %9, -1
%17 = select i1 %16, i64 %9, i64 -1
br label %18
18: ; preds = %12, %1, %5, %15
%19 = phi i64 [ %17, %15 ], [ -1, %5 ], [ -1, %1 ], [ -1, %12 ]
ret i64 %19
}