#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 /*<<< orphan*/ die(int, char *, char, char, char const *);
int /*<<< orphan*/ die_errno(int, char *, char const *);
scalar_t__ errno;
int strtoul(char const *, char **, int /*<<< orphan*/);
int parse_int(const char *str, char **rest_, char expected_stop) {
  char *rest;
  errno = 0;
  int v = strtoul(str, &rest, 0);
  if (errno)
    die_errno(1, "error parsing \"%s\" as int", str);
  if (rest && *rest != expected_stop)
    die(1, "unexpected char '%c' (%d) while parsing int from \"%s\"", *rest,
        *rest, str);
  if (rest_)
    *rest_ = rest;
  return v;
}


x86 assembly (clang, -O0):
<parse_int>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %dl,%al
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %al,-0x11(%rbp)
mov    0x2e94(%rip),%rax
movq   $0x0,(%rax)
mov    -0x8(%rbp),%rdi
lea    -0x20(%rbp),%rsi
xor    %edx,%edx
call   1050 <strtoul@plt>
mov    %eax,-0x24(%rbp)
mov    0x2e74(%rip),%rax
cmpq   $0x0,(%rax)
je     118b <parse_int+0x5b>
mov    -0x8(%rbp),%rdx
mov    $0x1,%edi
lea    0xe7a(%rip),%rsi
call   1030 <die_errno@plt>
cmpq   $0x0,-0x20(%rbp)
je     11d0 <parse_int+0xa0>
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
movsbl -0x11(%rbp),%ecx
cmp    %ecx,%eax
je     11d0 <parse_int+0xa0>
mov    -0x20(%rbp),%rax
mov    (%rax),%cl
mov    -0x20(%rbp),%rax
mov    (%rax),%al
mov    -0x8(%rbp),%r8
mov    $0x1,%edi
lea    0xe55(%rip),%rsi
movsbl %cl,%edx
movsbl %al,%ecx
call   1040 <die@plt>
cmpq   $0x0,-0x10(%rbp)
je     11e6 <parse_int+0xb6>
mov    -0x20(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x24(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<parse_int>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %edx,%r15d
mov    %rsi,%r12
mov    %rdi,%rbp
mov    0x2e94(%rip),%rbx
movq   $0x0,(%rbx)
lea    0x8(%rsp),%rsi
xor    %edx,%edx
call   1050 <strtoul@plt>
mov    %eax,%r14d
cmpq   $0x0,(%rbx)
je     117c <parse_int+0x4c>
lea    0xe91(%rip),%rsi
mov    $0x1,%edi
mov    %rbp,%rdx
call   1030 <die_errno@plt>
mov    0x8(%rsp),%rbx
test   %rbx,%rbx
je     11a6 <parse_int+0x76>
mov    (%rbx),%al
cmp    %r15b,%al
je     11a6 <parse_int+0x76>
movsbl %al,%ecx
lea    0xe83(%rip),%rsi
mov    $0x1,%edi
mov    %ecx,%edx
mov    %rbp,%r8
call   1040 <die@plt>
test   %r12,%r12
je     11af <parse_int+0x7f>
mov    %rbx,(%r12)
mov    %r14d,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@errno = global i64 0
@.str = [26 x i8] c"error parsing \22%s\22 as int\00"
@.str.1 = [54 x i8] c"unexpected char '%c' (%d) while parsing int from \22%s\22\00"
define i32 @parse_int(i8* %0, i8** %1, i8 signext %2) {
%4 = alloca i8*
%5 = alloca i8**
%6 = alloca i8
%7 = alloca i8*
%8 = alloca i32
store i8* %0, i8** %4
store i8** %1, i8*** %5
store i8 %2, i8* %6
store i64 0, i64* @errno
%9 = load i8*, i8** %4
%10 = call i32 @strtoul(i8* %9, i8** %7, i32 0)
store i32 %10, i32* %8
%11 = load i64, i64* @errno
%12 = icmp ne i64 %11, 0
br i1 %12, label %13, label %16
13: ; preds = %3
%14 = load i8*, i8** %4
%15 = call i32 @die_errno(i32 1, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i8* %14)
br label %16
16: ; preds = %13, %3
%17 = load i8*, i8** %7
%18 = icmp ne i8* %17, null
br i1 %18, label %19, label %33
19: ; preds = %16
%20 = load i8*, i8** %7
%21 = load i8, i8* %20
%22 = sext i8 %21 to i32
%23 = load i8, i8* %6
%24 = sext i8 %23 to i32
%25 = icmp ne i32 %22, %24
br i1 %25, label %26, label %33
26: ; preds = %19
%27 = load i8*, i8** %7
%28 = load i8, i8* %27
%29 = load i8*, i8** %7
%30 = load i8, i8* %29
%31 = load i8*, i8** %4
%32 = call i32 @die(i32 1, i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str.1, i64 0, i64 0), i8 signext %28, i8 signext %30, i8* %31)
br label %33
33: ; preds = %26, %19, %16
%34 = load i8**, i8*** %5
%35 = icmp ne i8** %34, null
br i1 %35, label %36, label %39
36: ; preds = %33
%37 = load i8*, i8** %7
%38 = load i8**, i8*** %5
store i8* %37, i8** %38
br label %39
39: ; preds = %36, %33
%40 = load i32, i32* %8
ret i32 %40
}

LLVM IR (-O2):
@errno = global i64 0
@.str = [26 x i8] c"error parsing \22%s\22 as int\00"
@.str.1 = [54 x i8] c"unexpected char '%c' (%d) while parsing int from \22%s\22\00"
define i32 @parse_int(i8* %0, i8** writeonly %1, i8 signext %2) {
%4 = alloca i8*
%5 = bitcast i8** %4 to i8*
store i64 0, i64* @errno
%6 = call i32 @strtoul(i8* %0, i8** %4, i32 0)
%7 = load i64, i64* @errno
%8 = icmp eq i64 %7, 0
br i1 %8, label %11, label %9
9: ; preds = %3
%10 = tail call i32 @die_errno(i32 1, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %11
11: ; preds = %9, %3
%12 = load i8*, i8** %4
%13 = icmp eq i8* %12, null
br i1 %13, label %19, label %14
14: ; preds = %11
%15 = load i8, i8* %12
%16 = icmp eq i8 %15, %2
br i1 %16, label %19, label %17
17: ; preds = %14
%18 = tail call i32 @die(i32 1, i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str.1, i64 0, i64 0), i8 signext %15, i8 signext %15, i8* %0)
br label %19
19: ; preds = %17, %14, %11
%20 = icmp eq i8** %1, null
br i1 %20, label %22, label %21
21: ; preds = %19
store i8* %12, i8** %1
br label %22
22: ; preds = %21, %19
ret i32 %6
}