#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 errno;
int strtol(char *, char **, int);
int strto_int(char *s, int *val) {
  char *end;
  int status = 0;
  int v;
  int save_errno;
  save_errno = errno;
  errno = 0;
  v = strtol(s, &end, 10);
  if ((end == s) || (*end != '\0') || (errno != 0))
    status = 1;
  if (status == 0)
    *val = v;
  errno = save_errno;
  return (status);
}


x86 assembly (clang, -O0):
<strto_int>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    0x2eb2(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    0x2ea6(%rip),%rax
movl   $0x0,(%rax)
mov    -0x8(%rbp),%rdi
lea    -0x18(%rbp),%rsi
mov    $0xa,%edx
call   1030 <strtol@plt>
mov    %eax,-0x20(%rbp)
mov    -0x18(%rbp),%rax
cmp    -0x8(%rbp),%rax
je     1183 <strto_int+0x73>
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    1183 <strto_int+0x73>
mov    0x2e66(%rip),%rax
cmpl   $0x0,(%rax)
je     118a <strto_int+0x7a>
movl   $0x1,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jne    119d <strto_int+0x8d>
mov    -0x20(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x24(%rbp),%ecx
mov    0x2e39(%rip),%rax
mov    %ecx,(%rax)
mov    -0x1c(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strto_int>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbx
mov    0x2ebc(%rip),%r15
mov    (%r15),%ebp
movl   $0x0,(%r15)
mov    %rsp,%rsi
mov    $0xa,%edx
call   1030 <strtol@plt>
mov    %eax,%ecx
mov    (%rsp),%rdx
mov    $0x1,%eax
cmp    %rbx,%rdx
je     115b <strto_int+0x4b>
cmpb   $0x0,(%rdx)
jne    115b <strto_int+0x4b>
cmpl   $0x0,(%r15)
jne    115b <strto_int+0x4b>
mov    %ecx,(%r14)
xor    %eax,%eax
mov    %ebp,(%r15)
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

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