#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 int /*<<< orphan*/ szTemp;

/* Variables and functions */
int strlen(char *);
int /*<<< orphan*/ strncpy(char *, char *, int);
char *strtok_r(char *, char *, char **);
unsigned int strtol(char *, int /*<<< orphan*/ *, int);
unsigned int mathStrtol(char *szSrc) {
  char szTemp[4096];
  char *pszToken;
  unsigned int retVal = 0;
  char prevOperator = '\0';
  char *pszLast;
  strncpy(szTemp, szSrc,
          ((strlen(szSrc) > sizeof(szTemp) - 1) ? sizeof(szTemp) - 1
                                                : strlen(szSrc)) +
              1);
  for (pszToken = strtok_r(szTemp, "+-", &pszLast); pszToken;
       pszToken = strtok_r(NULL, "+-", &pszLast)) {
    char chDeletedChar;
    chDeletedChar = szSrc[pszToken - szTemp + strlen(pszToken)];
    switch (prevOperator) {
    case '+':
      retVal += strtol(pszToken, NULL, 16);
      break;
    case '-':
      retVal -= strtol(pszToken, NULL, 16);
      break;
    default:
      retVal = strtol(pszToken, NULL, 16);
      break;
    }
    prevOperator = chDeletedChar;
  }
  return retVal;
}


x86 assembly (clang, -O0):
<mathStrtol>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1060,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0x101c(%rbp)
movb   $0x0,-0x101d(%rbp)
lea    -0x1010(%rbp),%rax
mov    %rax,-0x1040(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x1038(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
cltq
cmp    $0xfff,%rax
jbe    11a1 <mathStrtol+0x61>
mov    $0xfff,%eax
mov    %rax,-0x1048(%rbp)
jmp    11b3 <mathStrtol+0x73>
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
cltq
mov    %rax,-0x1048(%rbp)
mov    -0x1038(%rbp),%rsi
mov    -0x1040(%rbp),%rdi
mov    -0x1048(%rbp),%rax
add    $0x1,%rax
mov    %eax,%edx
call   1030 <strncpy@plt>
lea    -0x1010(%rbp),%rdi
lea    0xe1f(%rip),%rsi
lea    -0x1028(%rbp),%rdx
call   1050 <strtok_r@plt>
mov    %rax,-0x1018(%rbp)
cmpq   $0x0,-0x1018(%rbp)
je     1315 <mathStrtol+0x1d5>
mov    -0x8(%rbp),%rax
mov    %rax,-0x1058(%rbp)
mov    -0x1018(%rbp),%rdi
lea    -0x1010(%rbp),%rcx
mov    %rdi,%rax
sub    %rcx,%rax
mov    %rax,-0x1060(%rbp)
call   1040 <strlen@plt>
mov    -0x1060(%rbp),%rcx
mov    %eax,%edx
mov    -0x1058(%rbp),%rax
movslq %edx,%rdx
add    %rdx,%rcx
mov    (%rax,%rcx,1),%al
mov    %al,-0x1029(%rbp)
movsbl -0x101d(%rbp),%eax
mov    %eax,-0x104c(%rbp)
sub    $0x2b,%eax
je     127b <mathStrtol+0x13b>
jmp    1267 <mathStrtol+0x127>
mov    -0x104c(%rbp),%eax
sub    $0x2d,%eax
je     12a1 <mathStrtol+0x161>
jmp    12cb <mathStrtol+0x18b>
mov    -0x1018(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0x10,%edx
call   1060 <strtol@plt>
add    -0x101c(%rbp),%eax
mov    %eax,-0x101c(%rbp)
jmp    12e6 <mathStrtol+0x1a6>
mov    -0x1018(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0x10,%edx
call   1060 <strtol@plt>
mov    %eax,%ecx
mov    -0x101c(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x101c(%rbp)
jmp    12e6 <mathStrtol+0x1a6>
mov    -0x1018(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0x10,%edx
call   1060 <strtol@plt>
mov    %eax,-0x101c(%rbp)
mov    -0x1029(%rbp),%al
mov    %al,-0x101d(%rbp)
xor    %eax,%eax
mov    %eax,%edi
lea    0xd03(%rip),%rsi
lea    -0x1028(%rbp),%rdx
call   1050 <strtok_r@plt>
mov    %rax,-0x1018(%rbp)
jmp    11f4 <mathStrtol+0xb4>
mov    -0x101c(%rbp),%eax
add    $0x1060,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mathStrtol>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1018,%rsp
mov    %rdi,%r13
call   1040 <strlen@plt>
mov    $0x1000,%edx
cmp    $0xfff,%eax
ja     1172 <mathStrtol+0x32>
mov    %r13,%rdi
call   1040 <strlen@plt>
mov    %eax,%edx
add    $0x1,%edx
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    %r13,%rsi
call   1030 <strncpy@plt>
lea    0xe77(%rip),%rsi
lea    0x8(%rsp),%rdx
mov    %rbx,%rdi
call   1050 <strtok_r@plt>
test   %rax,%rax
je     1229 <mathStrtol+0xe9>
mov    %rax,%r12
xor    %r14d,%r14d
xor    %ebp,%ebp
jmp    11d2 <mathStrtol+0x92>
nopl   0x0(%rax)
call   1060 <strtol@plt>
mov    %eax,%ebp
xor    %edi,%edi
lea    0xe40(%rip),%rsi
lea    0x8(%rsp),%rdx
call   1050 <strtok_r@plt>
mov    %rax,%r12
test   %rax,%rax
je     122b <mathStrtol+0xeb>
mov    %r14d,%r15d
mov    %r12,%rbx
lea    0x10(%rsp),%rax
sub    %rax,%rbx
mov    %r12,%rdi
call   1040 <strlen@plt>
cltq
add    %rbx,%rax
movzbl 0x0(%r13,%rax,1),%r14d
movsbl %r15b,%eax
mov    %r12,%rdi
xor    %esi,%esi
mov    $0x10,%edx
cmp    $0x2d,%eax
je     1220 <mathStrtol+0xe0>
cmp    $0x2b,%eax
jne    11b0 <mathStrtol+0x70>
call   1060 <strtol@plt>
add    %eax,%ebp
jmp    11b7 <mathStrtol+0x77>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
call   1060 <strtol@plt>
sub    %eax,%ebp
jmp    11b7 <mathStrtol+0x77>
xor    %ebp,%ebp
mov    %ebp,%eax
add    $0x1018,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"+-\00"
define i32 @mathStrtol(i8* %0) {
%2 = alloca i8*
%3 = alloca [4096 x i8]
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i8
%7 = alloca i8*
%8 = alloca i8
store i8* %0, i8** %2
store i32 0, i32* %5
store i8 0, i8* %6
%9 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%10 = load i8*, i8** %2
%11 = load i8*, i8** %2
%12 = call i32 @strlen(i8* %11)
%13 = sext i32 %12 to i64
%14 = icmp ugt i64 %13, 4095
br i1 %14, label %15, label %16
15: ; preds = %1
br label %20
16: ; preds = %1
%17 = load i8*, i8** %2
%18 = call i32 @strlen(i8* %17)
%19 = sext i32 %18 to i64
br label %20
20: ; preds = %16, %15
%21 = phi i64 [ 4095, %15 ], [ %19, %16 ]
%22 = add i64 %21, 1
%23 = trunc i64 %22 to i32
%24 = call i32 @strncpy(i8* %9, i8* %10, i32 %23)
%25 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%26 = call i8* @strtok_r(i8* %25, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8** %7)
store i8* %26, i8** %4
br label %27
27: ; preds = %60, %20
%28 = load i8*, i8** %4
%29 = icmp ne i8* %28, null
br i1 %29, label %30, label %62
30: ; preds = %27
%31 = load i8*, i8** %2
%32 = load i8*, i8** %4
%33 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%34 = ptrtoint i8* %32 to i64
%35 = ptrtoint i8* %33 to i64
%36 = sub i64 %34, %35
%37 = load i8*, i8** %4
%38 = call i32 @strlen(i8* %37)
%39 = sext i32 %38 to i64
%40 = add nsw i64 %36, %39
%41 = getelementptr inbounds i8, i8* %31, i64 %40
%42 = load i8, i8* %41
store i8 %42, i8* %8
%43 = load i8, i8* %6
%44 = sext i8 %43 to i32
switch i32 %44, label %55 [
i32 43, label %45
i32 45, label %50
]
45: ; preds = %30
%46 = load i8*, i8** %4
%47 = call i32 @strtol(i8* %46, i32* null, i32 16)
%48 = load i32, i32* %5
%49 = add i32 %48, %47
store i32 %49, i32* %5
br label %58
50: ; preds = %30
%51 = load i8*, i8** %4
%52 = call i32 @strtol(i8* %51, i32* null, i32 16)
%53 = load i32, i32* %5
%54 = sub i32 %53, %52
store i32 %54, i32* %5
br label %58
55: ; preds = %30
%56 = load i8*, i8** %4
%57 = call i32 @strtol(i8* %56, i32* null, i32 16)
store i32 %57, i32* %5
br label %58
58: ; preds = %55, %50, %45
%59 = load i8, i8* %8
store i8 %59, i8* %6
br label %60
60: ; preds = %58
%61 = call i8* @strtok_r(i8* null, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8** %7)
store i8* %61, i8** %4
br label %27
62: ; preds = %27
%63 = load i32, i32* %5
ret i32 %63
}

LLVM IR (-O2):
@.str = [3 x i8] c"+-\00"
define i32 @mathStrtol(i8* %0) {
%2 = alloca [4096 x i8]
%3 = alloca i8*
%4 = getelementptr inbounds [4096 x i8], [4096 x i8]* %2, i64 0, i64 0
%5 = bitcast i8** %3 to i8*
%6 = tail call i32 @strlen(i8* %0)
%7 = icmp ugt i32 %6, 4095
br i1 %7, label %11, label %8
8: ; preds = %1
%9 = tail call i32 @strlen(i8* %0)
%10 = add i32 %9, 1
br label %11
11: ; preds = %1, %8
%12 = phi i32 [ %10, %8 ], [ 4096, %1 ]
%13 = call i32 @strncpy(i8* %4, i8* %0, i32 %12)
%14 = call i8* @strtok_r(i8* %4, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8** %3)
%15 = icmp eq i8* %14, null
br i1 %15, label %42, label %16
16: ; preds = %11
%17 = ptrtoint [4096 x i8]* %2 to i64
br label %18
18: ; preds = %16, %38
%19 = phi i8* [ %14, %16 ], [ %40, %38 ]
%20 = phi i8 [ 0, %16 ], [ %28, %38 ]
%21 = phi i32 [ 0, %16 ], [ %39, %38 ]
%22 = ptrtoint i8* %19 to i64
%23 = sub i64 %22, %17
%24 = call i32 @strlen(i8* %19)
%25 = sext i32 %24 to i64
%26 = add nsw i64 %23, %25
%27 = getelementptr inbounds i8, i8* %0, i64 %26
%28 = load i8, i8* %27
%29 = sext i8 %20 to i32
switch i32 %29, label %36 [
i32 43, label %30
i32 45, label %33
]
30: ; preds = %18
%31 = call i32 @strtol(i8* nocapture %19, i32* null, i32 16)
%32 = add i32 %31, %21
br label %38
33: ; preds = %18
%34 = call i32 @strtol(i8* nocapture %19, i32* null, i32 16)
%35 = sub i32 %21, %34
br label %38
36: ; preds = %18
%37 = call i32 @strtol(i8* nocapture %19, i32* null, i32 16)
br label %38
38: ; preds = %36, %33, %30
%39 = phi i32 [ %37, %36 ], [ %35, %33 ], [ %32, %30 ]
%40 = call i8* @strtok_r(i8* null, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8** %3)
%41 = icmp eq i8* %40, null
br i1 %41, label %42, label %18
42: ; preds = %38, %11
%43 = phi i32 [ 0, %11 ], [ %39, %38 ]
ret i32 %43
}