#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 */
typedef char u_int8_t;
typedef int /*<<< orphan*/ u_int32_t;

/* Variables and functions */
int /*<<< orphan*/ inBounds(char *, char *, char *);
int /*<<< orphan*/ strtoul(char *, char **, int);
int string_extract(int bytes_to_grab, int base, u_int8_t *ptr, u_int8_t *start,
                   u_int8_t *end, u_int32_t *value) {
  char byte_array[(10 + 1)];
  char *parse_helper;
  int x;
  if (bytes_to_grab > ((10 + 1) - 1) || bytes_to_grab <= 0) {
    return -1;
  }
  if (!inBounds(start, end, ptr + (bytes_to_grab - 1))) {
    return -3;
  }
  if (!inBounds(start, end, ptr)) {
    return -3;
  }
  for (x = 0; x < bytes_to_grab; x++) {
    byte_array[x] = *(ptr + x);
  }
  byte_array[bytes_to_grab] = '\0';
  *value = strtoul(byte_array, &parse_helper, base);
  if (byte_array == parse_helper) {
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<string_extract>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
mov    %r9,-0x30(%rbp)
cmpl   $0xa,-0x8(%rbp)
jg     1152 <string_extract+0x32>
cmpl   $0x0,-0x8(%rbp)
jg     115e <string_extract+0x3e>
movl   $0xffffffff,-0x4(%rbp)
jmp    122b <string_extract+0x10b>
mov    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x18(%rbp),%rdx
mov    -0x8(%rbp),%eax
sub    $0x1,%eax
cltq
add    %rax,%rdx
call   1030 <inBounds@plt>
cmp    $0x0,%eax
jne    118f <string_extract+0x6f>
movl   $0xfffffffd,-0x4(%rbp)
jmp    122b <string_extract+0x10b>
mov    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1030 <inBounds@plt>
cmp    $0x0,%eax
jne    11b5 <string_extract+0x95>
movl   $0xfffffffd,-0x4(%rbp)
jmp    122b <string_extract+0x10b>
movl   $0x0,-0x4c(%rbp)
mov    -0x4c(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    11e9 <string_extract+0xc9>
mov    -0x18(%rbp),%rax
movslq -0x4c(%rbp),%rcx
mov    (%rax,%rcx,1),%cl
movslq -0x4c(%rbp),%rax
mov    %cl,-0x3b(%rbp,%rax,1)
mov    -0x4c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x4c(%rbp)
jmp    11bc <string_extract+0x9c>
movslq -0x8(%rbp),%rax
movb   $0x0,-0x3b(%rbp,%rax,1)
lea    -0x3b(%rbp),%rdi
mov    -0xc(%rbp),%edx
lea    -0x48(%rbp),%rsi
call   1040 <strtoul@plt>
mov    %eax,%ecx
mov    -0x30(%rbp),%rax
mov    %ecx,(%rax)
lea    -0x3b(%rbp),%rax
cmp    -0x48(%rbp),%rax
jne    1224 <string_extract+0x104>
movl   $0xffffffff,-0x4(%rbp)
jmp    122b <string_extract+0x10b>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<string_extract>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %r9,0x8(%rsp)
mov    %edi,%ebp
lea    -0xb(%rbp),%eax
mov    $0xffffffff,%ebx
cmp    $0xfffffff6,%eax
jb     11c7 <string_extract+0x97>
mov    %r8,%r13
mov    %rcx,%r14
mov    %rdx,%r12
mov    %esi,%r15d
lea    -0x1(%rbp),%edx
add    %r12,%rdx
mov    %rcx,%rdi
mov    %r8,%rsi
call   1030 <inBounds@plt>
mov    $0xfffffffd,%ebx
test   %eax,%eax
je     11c7 <string_extract+0x97>
mov    %r14,%rdi
mov    %r13,%rsi
mov    %r12,%rdx
call   1030 <inBounds@plt>
test   %eax,%eax
je     11c7 <string_extract+0x97>
mov    %ebp,%ebx
lea    0x1d(%rsp),%rbp
mov    %rbp,%rdi
mov    %r12,%rsi
mov    %rbx,%rdx
call   1040 <memcpy@plt>
movb   $0x0,0x1d(%rsp,%rbx,1)
lea    0x10(%rsp),%rsi
mov    %rbp,%rdi
mov    %r15d,%edx
call   1050 <strtoul@plt>
mov    0x8(%rsp),%rcx
mov    %eax,(%rcx)
xor    %ebx,%ebx
cmp    0x10(%rsp),%rbp
sete   %bl
neg    %ebx
mov    %ebx,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @string_extract(i32 %0, i32 %1, i8* %2, i8* %3, i8* %4, i32* %5) {
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i8*
%12 = alloca i8*
%13 = alloca i32*
%14 = alloca [11 x i8]
%15 = alloca i8*
%16 = alloca i32
store i32 %0, i32* %8
store i32 %1, i32* %9
store i8* %2, i8** %10
store i8* %3, i8** %11
store i8* %4, i8** %12
store i32* %5, i32** %13
%17 = load i32, i32* %8
%18 = icmp sgt i32 %17, 10
br i1 %18, label %22, label %19
19: ; preds = %6
%20 = load i32, i32* %8
%21 = icmp sle i32 %20, 0
br i1 %21, label %22, label %23
22: ; preds = %19, %6
store i32 -1, i32* %7
br label %71
23: ; preds = %19
%24 = load i8*, i8** %11
%25 = load i8*, i8** %12
%26 = load i8*, i8** %10
%27 = load i32, i32* %8
%28 = sub nsw i32 %27, 1
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds i8, i8* %26, i64 %29
%31 = call i32 @inBounds(i8* %24, i8* %25, i8* %30)
%32 = icmp ne i32 %31, 0
br i1 %32, label %34, label %33
33: ; preds = %23
store i32 -3, i32* %7
br label %71
34: ; preds = %23
%35 = load i8*, i8** %11
%36 = load i8*, i8** %12
%37 = load i8*, i8** %10
%38 = call i32 @inBounds(i8* %35, i8* %36, i8* %37)
%39 = icmp ne i32 %38, 0
br i1 %39, label %41, label %40
40: ; preds = %34
store i32 -3, i32* %7
br label %71
41: ; preds = %34
store i32 0, i32* %16
br label %42
42: ; preds = %55, %41
%43 = load i32, i32* %16
%44 = load i32, i32* %8
%45 = icmp slt i32 %43, %44
br i1 %45, label %46, label %58
46: ; preds = %42
%47 = load i8*, i8** %10
%48 = load i32, i32* %16
%49 = sext i32 %48 to i64
%50 = getelementptr inbounds i8, i8* %47, i64 %49
%51 = load i8, i8* %50
%52 = load i32, i32* %16
%53 = sext i32 %52 to i64
%54 = getelementptr inbounds [11 x i8], [11 x i8]* %14, i64 0, i64 %53
store i8 %51, i8* %54
br label %55
55: ; preds = %46
%56 = load i32, i32* %16
%57 = add nsw i32 %56, 1
store i32 %57, i32* %16
br label %42
58: ; preds = %42
%59 = load i32, i32* %8
%60 = sext i32 %59 to i64
%61 = getelementptr inbounds [11 x i8], [11 x i8]* %14, i64 0, i64 %60
store i8 0, i8* %61
%62 = getelementptr inbounds [11 x i8], [11 x i8]* %14, i64 0, i64 0
%63 = load i32, i32* %9
%64 = call i32 @strtoul(i8* %62, i8** %15, i32 %63)
%65 = load i32*, i32** %13
store i32 %64, i32* %65
%66 = getelementptr inbounds [11 x i8], [11 x i8]* %14, i64 0, i64 0
%67 = load i8*, i8** %15
%68 = icmp eq i8* %66, %67
br i1 %68, label %69, label %70
69: ; preds = %58
store i32 -1, i32* %7
br label %71
70: ; preds = %58
store i32 0, i32* %7
br label %71
71: ; preds = %70, %69, %40, %33, %22
%72 = load i32, i32* %7
ret i32 %72
}

LLVM IR (-O2):
define i32 @string_extract(i32 %0, i32 %1, i8* %2, i8* %3, i8* %4, i32* nocapture writeonly %5) {
%7 = alloca [11 x i8]
%8 = getelementptr inbounds [11 x i8], [11 x i8]* %7, i64 0, i64 0
%9 = alloca i8*
%10 = getelementptr inbounds [11 x i8], [11 x i8]* %7, i64 0, i64 0
%11 = bitcast i8** %9 to i8*
%12 = add i32 %0, -11
%13 = icmp ult i32 %12, -10
br i1 %13, label %31, label %14
14: ; preds = %6
%15 = add nsw i32 %0, -1
%16 = zext i32 %15 to i64
%17 = getelementptr inbounds i8, i8* %2, i64 %16
%18 = tail call i32 @inBounds(i8* %3, i8* %4, i8* %17)
%19 = icmp eq i32 %18, 0
br i1 %19, label %31, label %20
20: ; preds = %14
%21 = tail call i32 @inBounds(i8* %3, i8* %4, i8* %2)
%22 = icmp eq i32 %21, 0
br i1 %22, label %31, label %23
23: ; preds = %20
%24 = zext i32 %0 to i64
%25 = zext i32 %0 to i64
%26 = getelementptr inbounds [11 x i8], [11 x i8]* %7, i64 0, i64 %25
store i8 0, i8* %26
%27 = call i32 @strtoul(i8* %10, i8** %9, i32 %1)
store i32 %27, i32* %5
%28 = load i8*, i8** %9
%29 = icmp eq i8* %10, %28
%30 = sext i1 %29 to i32
br label %31
31: ; preds = %23, %20, %14, %6
%32 = phi i32 [ -1, %6 ], [ -3, %14 ], [ -3, %20 ], [ %30, %23 ]
ret i32 %32
}