#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 */
size_t strlen(char const *);
unsigned long strtoul(char const *, char **, unsigned int);
int strict_strtoul(const char *cp, unsigned int base, unsigned long *res) {
  char *tail;
  unsigned long val;
  size_t len;
  *res = 0;
  len = strlen(cp);
  if (len == 0)
    return -22;
  val = strtoul(cp, &tail, base);
  if (tail == cp)
    return -22;
  if ((*tail == '\0') ||
      ((len == (size_t)(tail - cp) + 1) && (*tail == '\n'))) {
    *res = val;
    return 0;
  }
  return -22;
}


x86 assembly (clang, -O0):
<strict_strtoul>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x20(%rbp),%rax
movq   $0x0,(%rax)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    1162 <strict_strtoul+0x42>
movl   $0xffffffea,-0x4(%rbp)
jmp    11ea <strict_strtoul+0xca>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%edx
lea    -0x28(%rbp),%rsi
call   1040 <strtoul@plt>
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x10(%rbp),%rax
jne    1190 <strict_strtoul+0x70>
movl   $0xffffffea,-0x4(%rbp)
jmp    11ea <strict_strtoul+0xca>
mov    -0x28(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     11cc <strict_strtoul+0xac>
mov    -0x38(%rbp),%rax
mov    -0x28(%rbp),%rcx
mov    -0x10(%rbp),%rdx
sub    %rdx,%rcx
add    $0x1,%rcx
cmp    %rcx,%rax
jne    11e3 <strict_strtoul+0xc3>
mov    -0x28(%rbp),%rax
movsbl (%rax),%eax
cmp    $0xa,%eax
jne    11e3 <strict_strtoul+0xc3>
mov    -0x30(%rbp),%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x0,-0x4(%rbp)
jmp    11ea <strict_strtoul+0xca>
movl   $0xffffffea,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strict_strtoul>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rdx,%r15
mov    %esi,%r12d
mov    %rdi,%rbx
movq   $0x0,(%rdx)
call   1030 <strlen@plt>
mov    $0xffffffea,%r14d
test   %rax,%rax
je     1186 <strict_strtoul+0x66>
mov    %rax,%rbp
lea    0x8(%rsp),%rsi
mov    %rbx,%rdi
mov    %r12d,%edx
call   1040 <strtoul@plt>
mov    0x8(%rsp),%rdx
mov    %rdx,%rcx
sub    %rbx,%rcx
je     1186 <strict_strtoul+0x66>
mov    (%rdx),%dl
test   %dl,%dl
je     1180 <strict_strtoul+0x60>
add    $0x1,%rcx
cmp    %rcx,%rbp
jne    1186 <strict_strtoul+0x66>
cmp    $0xa,%dl
jne    1186 <strict_strtoul+0x66>
mov    %rax,(%r15)
xor    %r14d,%r14d
mov    %r14d,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @strict_strtoul(i8* %0, i32 %1, i64* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i64*
%8 = alloca i8*
%9 = alloca i64
%10 = alloca i64
store i8* %0, i8** %5
store i32 %1, i32* %6
store i64* %2, i64** %7
%11 = load i64*, i64** %7
store i64 0, i64* %11
%12 = load i8*, i8** %5
%13 = call i64 @strlen(i8* %12)
store i64 %13, i64* %10
%14 = load i64, i64* %10
%15 = icmp eq i64 %14, 0
br i1 %15, label %16, label %17
16: ; preds = %3
store i32 -22, i32* %4
br label %48
17: ; preds = %3
%18 = load i8*, i8** %5
%19 = load i32, i32* %6
%20 = call i64 @strtoul(i8* %18, i8** %8, i32 %19)
store i64 %20, i64* %9
%21 = load i8*, i8** %8
%22 = load i8*, i8** %5
%23 = icmp eq i8* %21, %22
br i1 %23, label %24, label %25
24: ; preds = %17
store i32 -22, i32* %4
br label %48
25: ; preds = %17
%26 = load i8*, i8** %8
%27 = load i8, i8* %26
%28 = sext i8 %27 to i32
%29 = icmp eq i32 %28, 0
br i1 %29, label %44, label %30
30: ; preds = %25
%31 = load i64, i64* %10
%32 = load i8*, i8** %8
%33 = load i8*, i8** %5
%34 = ptrtoint i8* %32 to i64
%35 = ptrtoint i8* %33 to i64
%36 = sub i64 %34, %35
%37 = add i64 %36, 1
%38 = icmp eq i64 %31, %37
br i1 %38, label %39, label %47
39: ; preds = %30
%40 = load i8*, i8** %8
%41 = load i8, i8* %40
%42 = sext i8 %41 to i32
%43 = icmp eq i32 %42, 10
br i1 %43, label %44, label %47
44: ; preds = %39, %25
%45 = load i64, i64* %9
%46 = load i64*, i64** %7
store i64 %45, i64* %46
store i32 0, i32* %4
br label %48
47: ; preds = %39, %30
store i32 -22, i32* %4
br label %48
48: ; preds = %47, %44, %24, %16
%49 = load i32, i32* %4
ret i32 %49
}

LLVM IR (-O2):
define i32 @strict_strtoul(i8* %0, i32 %1, i64* nocapture writeonly %2) {
%4 = alloca i8*
%5 = bitcast i8** %4 to i8*
store i64 0, i64* %2
%6 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%7 = icmp eq i64 %6, 0
br i1 %7, label %24, label %8
8: ; preds = %3
%9 = call i64 @strtoul(i8* %0, i8** %4, i32 %1)
%10 = load i8*, i8** %4
%11 = icmp eq i8* %10, %0
br i1 %11, label %24, label %12
12: ; preds = %8
%13 = load i8, i8* %10
%14 = icmp eq i8 %13, 0
br i1 %14, label %23, label %15
15: ; preds = %12
%16 = ptrtoint i8* %10 to i64
%17 = ptrtoint i8* %0 to i64
%18 = sub i64 1, %17
%19 = add i64 %18, %16
%20 = icmp eq i64 %6, %19
%21 = icmp eq i8 %13, 10
%22 = select i1 %20, i1 %21, i1 false
br i1 %22, label %23, label %24
23: ; preds = %15, %12
store i64 %9, i64* %2
br label %24
24: ; preds = %15, %8, %3, %23
%25 = phi i32 [ 0, %23 ], [ -22, %3 ], [ -22, %8 ], [ -22, %15 ]
ret i32 %25
}