#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 */
char *alloca(int);
int /*<<< orphan*/ strncpy(char *, char const *, int);
int strtoul(char *, char **, int);
int strntoul(const char *str, char **endptr, int base, int num) {
  char *buf, *end;
  unsigned long ret;
  buf = alloca(num + 1);
  strncpy(buf, str, num);
  buf[num] = '\0';
  ret = strtoul(buf, &end, base);
  if (*end)
    *endptr = (char *)(str + (end - buf));
  else
    *endptr = "";
  return strtoul(buf, endptr, base);
}


x86 assembly (clang, -O0):
<strntoul>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
mov    -0x18(%rbp),%edi
add    $0x1,%edi
call   1040 <alloca@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x18(%rbp),%edx
call   1030 <strncpy@plt>
mov    -0x20(%rbp),%rax
movslq -0x18(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x20(%rbp),%rdi
mov    -0x14(%rbp),%edx
lea    -0x28(%rbp),%rsi
call   1050 <strtoul@plt>
cltq
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%rax
cmpb   $0x0,(%rax)
je     11b2 <strntoul+0x82>
mov    -0x8(%rbp),%rcx
mov    -0x28(%rbp),%rax
mov    -0x20(%rbp),%rdx
sub    %rdx,%rax
add    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
jmp    11c0 <strntoul+0x90>
mov    -0x10(%rbp),%rax
lea    0xe43(%rip),%rcx
mov    %rcx,(%rax)
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1050 <strtoul@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strntoul>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %ecx,%ebp
mov    %edx,%r14d
mov    %rsi,%r15
mov    %rdi,%r12
lea    0x1(%rbp),%edi
call   1040 <alloca@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r12,%rsi
mov    %ebp,%edx
call   1030 <strncpy@plt>
movslq %ebp,%rax
movb   $0x0,(%rbx,%rax,1)
lea    0x8(%rsp),%rsi
mov    %rbx,%rdi
mov    %r14d,%edx
call   1050 <strtoul@plt>
mov    0x8(%rsp),%rax
mov    %rax,%rcx
sub    %rbx,%rcx
add    %r12,%rcx
cmpb   $0x0,(%rax)
lea    0xe72(%rip),%rax
cmovne %rcx,%rax
mov    %rax,(%r15)
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %r14d,%edx
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1050 <strtoul@plt>

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

LLVM IR (-O2):
@.str = [1 x i8] zeroinitializer
define i32 @strntoul(i8* %0, i8** nocapture %1, i32 %2, i32 %3) {
%5 = alloca i8*
%6 = bitcast i8** %5 to i8*
%7 = add nsw i32 %3, 1
%8 = tail call i8* @alloca(i32 %7)
%9 = tail call i32 @strncpy(i8* %8, i8* %0, i32 %3)
%10 = sext i32 %3 to i64
%11 = getelementptr inbounds i8, i8* %8, i64 %10
store i8 0, i8* %11
%12 = call i32 @strtoul(i8* %8, i8** %5, i32 %2)
%13 = load i8*, i8** %5
%14 = load i8, i8* %13
%15 = icmp eq i8 %14, 0
%16 = ptrtoint i8* %13 to i64
%17 = ptrtoint i8* %8 to i64
%18 = sub i64 %16, %17
%19 = getelementptr inbounds i8, i8* %0, i64 %18
%20 = select i1 %15, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0), i8* %19
store i8* %20, i8** %1
%21 = tail call i32 @strtoul(i8* %8, i8** %1, i32 %2)
ret i32 %21
}