#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 LONG_MAX;
int LONG_MIN;
int strtol(char const *, char **, int);
int tail_read_size_t(const char *str, size_t *val_p) {
  char *end;
  int long_val = strtol(str, &end, 10);
  if (long_val == LONG_MIN || long_val == LONG_MAX || *end != '\0') {
    return -1;
  }
  *val_p = (size_t)long_val;
  return 0;
}


x86 assembly (clang, -O0):
<tail_read_size_t>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x20(%rbp),%rsi
mov    $0xa,%edx
call   1030 <strtol@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
mov    0x2ea9(%rip),%rcx
cmp    (%rcx),%eax
je     1169 <tail_read_size_t+0x59>
mov    -0x24(%rbp),%eax
mov    0x2e87(%rip),%rcx
cmp    (%rcx),%eax
je     1169 <tail_read_size_t+0x59>
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     1175 <tail_read_size_t+0x65>
movl   $0xffffffff,-0x4(%rbp)
jmp    1187 <tail_read_size_t+0x77>
movslq -0x24(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tail_read_size_t>:
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%rbx
lea    0x8(%rsp),%rsi
mov    $0xa,%edx
call   1030 <strtol@plt>
mov    %eax,%ecx
mov    0x2eb8(%rip),%rdx
mov    $0xffffffff,%eax
cmp    (%rdx),%ecx
je     1156 <tail_read_size_t+0x46>
mov    0x2e98(%rip),%rdx
cmp    (%rdx),%ecx
je     1156 <tail_read_size_t+0x46>
mov    0x8(%rsp),%rdx
cmpb   $0x0,(%rdx)
jne    1156 <tail_read_size_t+0x46>
movslq %ecx,%rax
mov    %rax,(%rbx)
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
ret

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

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