#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 */
typedef char int32_t;

/* Variables and functions */
int ERANGE;
int /*<<< orphan*/ assert(char *);
int errno;
int mx_strtol(char *, long *);
int mx_strtoi32(char *str, int32_t *to) {
  signed long int l;
  int res;
  assert(str);
  assert(to);
  res = mx_strtol(str, &l);
  if (res < 0)
    return res;
  if ((signed long int)(int32_t)l != l)
    return -(errno = ERANGE);
  *to = (int32_t)l;
  return 0;
}


x86 assembly (clang, -O0):
<mx_strtoi32>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <assert@plt>
mov    -0x18(%rbp),%rdi
call   1040 <assert@plt>
mov    -0x10(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1030 <mx_strtol@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    1167 <mx_strtoi32+0x47>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11aa <mx_strtoi32+0x8a>
mov    -0x20(%rbp),%rax
movsbq %al,%rax
cmp    -0x20(%rbp),%rax
je     1197 <mx_strtoi32+0x77>
mov    0x2e68(%rip),%rax
mov    (%rax),%ecx
mov    0x2e4f(%rip),%rax
mov    %ecx,(%rax)
xor    %eax,%eax
sub    %ecx,%eax
mov    %eax,-0x4(%rbp)
jmp    11aa <mx_strtoi32+0x8a>
mov    -0x20(%rbp),%rax
mov    %al,%cl
mov    -0x18(%rbp),%rax
mov    %cl,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mx_strtoi32>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbx
call   1040 <assert@plt>
mov    %r14,%rdi
call   1040 <assert@plt>
mov    %rsp,%rsi
mov    %rbx,%rdi
call   1030 <mx_strtol@plt>
test   %eax,%eax
js     116e <mx_strtoi32+0x4e>
mov    (%rsp),%rax
movsbq %al,%rcx
cmp    %rax,%rcx
jne    115a <mx_strtoi32+0x3a>
mov    %al,(%r14)
xor    %eax,%eax
jmp    116e <mx_strtoi32+0x4e>
mov    0x2e87(%rip),%rax
mov    (%rax),%eax
mov    0x2e6e(%rip),%rcx
mov    %eax,(%rcx)
neg    %eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

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

LLVM IR (-O2):
@ERANGE = global i32 0
@errno = global i32 0
define i32 @mx_strtoi32(i8* %0, i8* %1) {
%3 = alloca i64
%4 = bitcast i64* %3 to i8*
%5 = tail call i32 @assert(i8* %0)
%6 = tail call i32 @assert(i8* %1)
%7 = call i32 @mx_strtol(i8* %0, i64* %3)
%8 = icmp slt i32 %7, 0
br i1 %8, label %18, label %9
9: ; preds = %2
%10 = load i64, i64* %3
%11 = add i64 %10, 128
%12 = icmp ult i64 %11, 256
br i1 %12, label %16, label %13
13: ; preds = %9
%14 = load i32, i32* @ERANGE
store i32 %14, i32* @errno
%15 = sub nsw i32 0, %14
br label %18
16: ; preds = %9
%17 = trunc i64 %10 to i8
store i8 %17, i8* %1
br label %18
18: ; preds = %2, %16, %13
%19 = phi i32 [ %15, %13 ], [ 0, %16 ], [ %7, %2 ]
ret i32 %19
}