#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*/ clean_up();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
scalar_t__ isdigit(char);
int /*<<< orphan*/ sscanf(char *, char *, int *);
int /*<<< orphan*/ stderr;
unsigned int strlen(char *);
int parse_int_value(char *ptr) {
  int value;
  unsigned int i;
  value = 0;
  if (ptr == NULL)
    return -1;
  sscanf(ptr, "%d", &value);
  if (value == 0) {
    fprintf(stderr, "parsing error\n");
    clean_up();
  }
  for (i = 0; i < strlen(ptr); i++) {
    if (isdigit(ptr[i]) == 0)
      return -1;
  }
  return value;
}


x86 assembly (clang, -O0):
<parse_int_value>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    117a <parse_int_value+0x2a>
movl   $0xffffffff,-0x4(%rbp)
jmp    1216 <parse_int_value+0xc6>
mov    -0x10(%rbp),%rdi
lea    0xe7b(%rip),%rsi
lea    -0x14(%rbp),%rdx
call   1070 <sscanf@plt>
cmpl   $0x0,-0x14(%rbp)
jne    11b4 <parse_int_value+0x64>
mov    0x2e41(%rip),%rax
mov    (%rax),%edi
lea    0xe5b(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x0,%al
call   1050 <clean_up@plt>
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x1c(%rbp),%eax
cmp    %ecx,%eax
jae    1210 <parse_int_value+0xc0>
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%ecx
movsbl (%rax,%rcx,1),%edi
call   1030 <isdigit@plt>
cmp    $0x0,%rax
jne    11fd <parse_int_value+0xad>
movl   $0xffffffff,-0x4(%rbp)
jmp    1216 <parse_int_value+0xc6>
jmp    1202 <parse_int_value+0xb2>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    11bb <parse_int_value+0x6b>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<parse_int_value>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
movl   $0x0,0xc(%rsp)
mov    $0xffffffff,%r14d
test   %rdi,%rdi
je     11d6 <parse_int_value+0x86>
mov    %rdi,%rbx
lea    0xe8b(%rip),%rsi
lea    0xc(%rsp),%rdx
call   1070 <sscanf@plt>
cmpl   $0x0,0xc(%rsp)
jne    11a2 <parse_int_value+0x52>
mov    0x2e53(%rip),%rax
mov    (%rax),%edi
lea    0xe6d(%rip),%rsi
call   1060 <fprintf@plt>
xor    %eax,%eax
call   1050 <clean_up@plt>
mov    %rbx,%rdi
call   1040 <strlen@plt>
test   %eax,%eax
je     11d1 <parse_int_value+0x81>
xor    %ebp,%ebp
movsbl (%rbx,%rbp,1),%edi
call   1030 <isdigit@plt>
test   %rax,%rax
je     11d6 <parse_int_value+0x86>
add    $0x1,%rbp
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %eax,%eax
cmp    %rax,%rbp
jb     11b0 <parse_int_value+0x60>
mov    0xc(%rsp),%r14d
mov    %r14d,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"%d\00"
@stderr = global i32 0
@.str.1 = [15 x i8] c"parsing error\0A\00"
define i32 @parse_int_value(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
store i8* %0, i8** %3
store i32 0, i32* %4
%6 = load i8*, i8** %3
%7 = icmp eq i8* %6, null
br i1 %7, label %8, label %9
8: ; preds = %1
store i32 -1, i32* %2
br label %39
9: ; preds = %1
%10 = load i8*, i8** %3
%11 = call i32 @sscanf(i8* %10, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %4)
%12 = load i32, i32* %4
%13 = icmp eq i32 %12, 0
br i1 %13, label %14, label %18
14: ; preds = %9
%15 = load i32, i32* @stderr
%16 = call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
%17 = call i32 (...) @clean_up()
br label %18
18: ; preds = %14, %9
store i32 0, i32* %5
br label %19
19: ; preds = %34, %18
%20 = load i32, i32* %5
%21 = load i8*, i8** %3
%22 = call i32 @strlen(i8* %21)
%23 = icmp ult i32 %20, %22
br i1 %23, label %24, label %37
24: ; preds = %19
%25 = load i8*, i8** %3
%26 = load i32, i32* %5
%27 = zext i32 %26 to i64
%28 = getelementptr inbounds i8, i8* %25, i64 %27
%29 = load i8, i8* %28
%30 = call i64 @isdigit(i8 signext %29)
%31 = icmp eq i64 %30, 0
br i1 %31, label %32, label %33
32: ; preds = %24
store i32 -1, i32* %2
br label %39
33: ; preds = %24
br label %34
34: ; preds = %33
%35 = load i32, i32* %5
%36 = add i32 %35, 1
store i32 %36, i32* %5
br label %19
37: ; preds = %19
%38 = load i32, i32* %4
store i32 %38, i32* %2
br label %39
39: ; preds = %37, %32, %8
%40 = load i32, i32* %2
ret i32 %40
}

LLVM IR (-O2):
@.str = [3 x i8] c"%d\00"
@stderr = global i32 0
@.str.1 = [15 x i8] c"parsing error\0A\00"
define i32 @parse_int_value(i8* %0) {
%2 = alloca i32
%3 = bitcast i32* %2 to i8*
store i32 0, i32* %2
%4 = icmp eq i8* %0, null
br i1 %4, label %29, label %5
5: ; preds = %1
%6 = call i32 @sscanf(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %2)
%7 = load i32, i32* %2
%8 = icmp eq i32 %7, 0
br i1 %8, label %9, label %13
9: ; preds = %5
%10 = load i32, i32* @stderr
%11 = call i32 @fprintf(i32 %10, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
%12 = call i32 (...) @clean_up()
br label %13
13: ; preds = %9, %5
%14 = call i32 @strlen(i8* %0)
%15 = icmp eq i32 %14, 0
br i1 %15, label %27, label %20
16: ; preds = %20
%17 = call i32 @strlen(i8* %0)
%18 = zext i32 %17 to i64
%19 = icmp ult i64 %26, %18
br i1 %19, label %20, label %27
20: ; preds = %13, %16
%21 = phi i64 [ %26, %16 ], [ 0, %13 ]
%22 = getelementptr inbounds i8, i8* %0, i64 %21
%23 = load i8, i8* %22
%24 = call i64 @isdigit(i8 signext %23)
%25 = icmp eq i64 %24, 0
%26 = add nuw nsw i64 %21, 1
br i1 %25, label %29, label %16
27: ; preds = %16, %13
%28 = load i32, i32* %2
br label %29
29: ; preds = %20, %1, %27
%30 = phi i32 [ %28, %27 ], [ -1, %1 ], [ -1, %20 ]
ret i32 %30
}