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


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    1169 <strict_strtoul+0x49>
xor    %eax,%eax
mov    0x2e81(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    11de <strict_strtoul+0xbe>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%edx
lea    -0x28(%rbp),%rsi
call   1040 <simple_strtoul@plt>
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     11b9 <strict_strtoul+0x99>
mov    -0x38(%rbp),%rax
mov    -0x28(%rbp),%rcx
mov    -0x10(%rbp),%rdx
sub    %rdx,%rcx
add    $0x1,%rcx
cmp    %rcx,%rax
jne    11d0 <strict_strtoul+0xb0>
mov    -0x28(%rbp),%rax
movsbl (%rax),%eax
cmp    $0xa,%eax
jne    11d0 <strict_strtoul+0xb0>
mov    -0x30(%rbp),%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x0,-0x4(%rbp)
jmp    11de <strict_strtoul+0xbe>
xor    %eax,%eax
mov    0x2e07(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-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   %rbx
push   %rax
mov    %rdx,%r14
mov    %esi,%ebp
mov    %rdi,%rbx
movq   $0x0,(%rdx)
call   1030 <strlen@plt>
test   %rax,%rax
je     1172 <strict_strtoul+0x52>
mov    %rax,%r15
mov    %rsp,%rsi
mov    %rbx,%rdi
mov    %ebp,%edx
call   1040 <simple_strtoul@plt>
mov    (%rsp),%rdx
mov    (%rdx),%cl
test   %cl,%cl
je     116b <strict_strtoul+0x4b>
sub    %rbx,%rdx
add    $0x1,%rdx
cmp    %rdx,%r15
jne    1172 <strict_strtoul+0x52>
cmp    $0xa,%cl
jne    1172 <strict_strtoul+0x52>
mov    %rax,(%r14)
xor    %eax,%eax
jmp    117d <strict_strtoul+0x5d>
mov    0x2e67(%rip),%rcx
xor    %eax,%eax
sub    (%rcx),%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@EINVAL = global i32 0
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 %19
16: ; preds = %3
%17 = load i32, i32* @EINVAL
%18 = sub nsw i32 0, %17
store i32 %18, i32* %4
br label %47
19: ; preds = %3
%20 = load i8*, i8** %5
%21 = load i32, i32* %6
%22 = call i64 @simple_strtoul(i8* %20, i8** %8, i32 %21)
store i64 %22, i64* %9
%23 = load i8*, i8** %8
%24 = load i8, i8* %23
%25 = sext i8 %24 to i32
%26 = icmp eq i32 %25, 0
br i1 %26, label %41, label %27
27: ; preds = %19
%28 = load i64, i64* %10
%29 = load i8*, i8** %8
%30 = load i8*, i8** %5
%31 = ptrtoint i8* %29 to i64
%32 = ptrtoint i8* %30 to i64
%33 = sub i64 %31, %32
%34 = add i64 %33, 1
%35 = icmp eq i64 %28, %34
br i1 %35, label %36, label %44
36: ; preds = %27
%37 = load i8*, i8** %8
%38 = load i8, i8* %37
%39 = sext i8 %38 to i32
%40 = icmp eq i32 %39, 10
br i1 %40, label %41, label %44
41: ; preds = %36, %19
%42 = load i64, i64* %9
%43 = load i64*, i64** %7
store i64 %42, i64* %43
store i32 0, i32* %4
br label %47
44: ; preds = %36, %27
%45 = load i32, i32* @EINVAL
%46 = sub nsw i32 0, %45
store i32 %46, i32* %4
br label %47
47: ; preds = %44, %41, %16
%48 = load i32, i32* %4
ret i32 %48
}

LLVM IR (-O2):
@EINVAL = global i32 0
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 %8, label %11
8: ; preds = %3
%9 = load i32, i32* @EINVAL
%10 = sub nsw i32 0, %9
br label %28
11: ; preds = %3
%12 = call i64 @simple_strtoul(i8* %0, i8** %4, i32 %1)
%13 = load i8*, i8** %4
%14 = load i8, i8* %13
%15 = icmp eq i8 %14, 0
br i1 %15, label %24, label %16
16: ; preds = %11
%17 = ptrtoint i8* %13 to i64
%18 = ptrtoint i8* %0 to i64
%19 = sub i64 1, %18
%20 = add i64 %19, %17
%21 = icmp eq i64 %6, %20
%22 = icmp eq i8 %14, 10
%23 = select i1 %21, i1 %22, i1 false
br i1 %23, label %24, label %25
24: ; preds = %16, %11
store i64 %12, i64* %2
br label %28
25: ; preds = %16
%26 = load i32, i32* @EINVAL
%27 = sub nsw i32 0, %26
br label %28
28: ; preds = %25, %24, %8
%29 = phi i32 [ %10, %8 ], [ 0, %24 ], [ %27, %25 ]
ret i32 %29
}