#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 */
long EINVAL;
long ERANGE;
long LONG_MAX;
long my_strtol(char const *, char **, int, int *);
long strtomemory(const char *str) {
  int err;
  char *end;
  long x = my_strtol(str, &end, 10, &err);
  if (err > 0)
    return -err;
  if (x <= 0)
    return -ERANGE;
  long multiplier;
  switch (*end) {
  case 't':
  case 'T':
    multiplier = 1L << 40;
    end++;
    break;
  case 'g':
  case 'G':
    multiplier = 1L << 30;
    end++;
    break;
  case 'm':
  case 'M':
    multiplier = 1L << 20;
    end++;
    break;
  case 'k':
  case 'K':
    multiplier = 1L << 10;
    end++;
    break;
  case '\0':
    multiplier = 1;
    break;
  default:
    return -EINVAL;
  }
  if (*end)
    return -EINVAL;
  if (x > LONG_MAX / multiplier)
    return -ERANGE;
  return x * multiplier;
}


x86 assembly (clang, -O0):
<strtomemory>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x20(%rbp),%rsi
mov    $0xa,%edx
lea    -0x14(%rbp),%rcx
call   1030 <my_strtol@plt>
mov    %rax,-0x28(%rbp)
cmpl   $0x0,-0x14(%rbp)
jle    1150 <strtomemory+0x40>
xor    %eax,%eax
sub    -0x14(%rbp),%eax
cltq
mov    %rax,-0x8(%rbp)
jmp    1307 <strtomemory+0x1f7>
cmpq   $0x0,-0x28(%rbp)
jg     1170 <strtomemory+0x60>
xor    %eax,%eax
mov    0x2e84(%rip),%rcx
sub    (%rcx),%rax
mov    %rax,-0x8(%rbp)
jmp    1307 <strtomemory+0x1f7>
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
mov    %eax,-0x34(%rbp)
test   %eax,%eax
je     1279 <strtomemory+0x169>
jmp    1187 <strtomemory+0x77>
mov    -0x34(%rbp),%eax
sub    $0x47,%eax
je     122e <strtomemory+0x11e>
jmp    1198 <strtomemory+0x88>
mov    -0x34(%rbp),%eax
sub    $0x4b,%eax
je     1260 <strtomemory+0x150>
jmp    11a9 <strtomemory+0x99>
mov    -0x34(%rbp),%eax
sub    $0x4d,%eax
je     1247 <strtomemory+0x137>
jmp    11ba <strtomemory+0xaa>
mov    -0x34(%rbp),%eax
sub    $0x54,%eax
je     120f <strtomemory+0xff>
jmp    11cb <strtomemory+0xbb>
mov    -0x34(%rbp),%eax
sub    $0x67,%eax
je     122e <strtomemory+0x11e>
jmp    11dc <strtomemory+0xcc>
mov    -0x34(%rbp),%eax
sub    $0x6b,%eax
je     1260 <strtomemory+0x150>
jmp    11ed <strtomemory+0xdd>
mov    -0x34(%rbp),%eax
sub    $0x6d,%eax
je     1247 <strtomemory+0x137>
jmp    11fe <strtomemory+0xee>
mov    -0x34(%rbp),%eax
sub    $0x74,%eax
jne    1286 <strtomemory+0x176>
jmp    120f <strtomemory+0xff>
movabs $0x10000000000,%rax
mov    %rax,-0x30(%rbp)
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    129b <strtomemory+0x18b>
movq   $0x40000000,-0x30(%rbp)
40
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    129b <strtomemory+0x18b>
movq   $0x100000,-0x30(%rbp)
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    129b <strtomemory+0x18b>
movq   $0x400,-0x30(%rbp)
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    129b <strtomemory+0x18b>
movq   $0x1,-0x30(%rbp)
jmp    129b <strtomemory+0x18b>
xor    %eax,%eax
mov    0x2d41(%rip),%rcx
sub    (%rcx),%rax
mov    %rax,-0x8(%rbp)
jmp    1307 <strtomemory+0x1f7>
mov    -0x20(%rbp),%rax
cmpb   $0x0,(%rax)
je     12bd <strtomemory+0x1ad>
xor    %eax,%eax
mov    0x2d1f(%rip),%rcx
sub    (%rcx),%rax
mov    %rax,-0x8(%rbp)
jmp    1307 <strtomemory+0x1f7>
mov    -0x28(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    0x2d0c(%rip),%rax
mov    (%rax),%rax
cqto
idivq  -0x30(%rbp)
mov    %rax,%rcx
mov    -0x40(%rbp),%rax
cmp    %rcx,%rax
jle    12fa <strtomemory+0x1ea>
xor    %eax,%eax
mov    0x2cfa(%rip),%rcx
sub    (%rcx),%rax
mov    %rax,-0x8(%rbp)
jmp    1307 <strtomemory+0x1f7>
mov    -0x28(%rbp),%rax
imul   -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strtomemory>:
sub    $0x18,%rsp
lea    0x8(%rsp),%rsi
lea    0x14(%rsp),%rcx
mov    $0xa,%edx
call   1030 <my_strtol@plt>
movslq 0x14(%rsp),%rsi
test   %rsi,%rsi
jle    113a <strtomemory+0x2a>
neg    %rsi
jmp    11ea <strtomemory+0xda>
mov    %rax,%rcx
test   %rax,%rax
jle    11de <strtomemory+0xce>
mov    0x8(%rsp),%rax
mov    (%rax),%dl
movsbl %dl,%edi
lea    -0x47(%rdi),%esi
cmp    $0x2d,%esi
ja     11f2 <strtomemory+0xe2>
lea    0xe9d(%rip),%rdx
movslq (%rdx,%rsi,4),%rsi
add    %rdx,%rsi
jmp    *%rsi
add    $0x1,%rax
mov    %rax,0x8(%rsp)
mov    $0x40000000,%esi
jmp    11af <strtomemory+0x9f>
add    $0x1,%rax
mov    %rax,0x8(%rsp)
mov    $0x400,%esi
jmp    11af <strtomemory+0x9f>
add    $0x1,%rax
mov    %rax,0x8(%rsp)
mov    $0x100000,%esi
jmp    11af <strtomemory+0x9f>
add    $0x1,%rax
mov    %rax,0x8(%rsp)
movabs $0x10000000000,%rsi
mov    (%rax),%dl
test   %dl,%dl
je     11be <strtomemory+0xae>
mov    0x2e14(%rip),%rax
jmp    11e5 <strtomemory+0xd5>
mov    0x2e13(%rip),%rax
mov    (%rax),%rax
mov    %rax,%rdx
or     %rsi,%rdx
shr    $0x20,%rdx
je     11fd <strtomemory+0xed>
cqto
idiv   %rsi
cmp    %rax,%rcx
jle    1206 <strtomemory+0xf6>
mov    0x2e03(%rip),%rax
xor    %esi,%esi
sub    (%rax),%rsi
mov    %rsi,%rax
add    $0x18,%rsp
ret
mov    $0x1,%esi
test   %edi,%edi
je     11b1 <strtomemory+0xa1>
jmp    11b5 <strtomemory+0xa5>
xor    %edx,%edx
div    %esi
cmp    %rax,%rcx
jg     11de <strtomemory+0xce>
imul   %rcx,%rsi
jmp    11ea <strtomemory+0xda>

LLVM IR (-O0):
@ERANGE = global i64 0
@EINVAL = global i64 0
@LONG_MAX = global i64 0
define i64 @strtomemory(i8* %0) {
%2 = alloca i64
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i64
store i8* %0, i8** %3
%8 = load i8*, i8** %3
%9 = call i64 @my_strtol(i8* %8, i8** %5, i32 10, i32* %4)
store i64 %9, i64* %6
%10 = load i32, i32* %4
%11 = icmp sgt i32 %10, 0
br i1 %11, label %12, label %16
12: ; preds = %1
%13 = load i32, i32* %4
%14 = sub nsw i32 0, %13
%15 = sext i32 %14 to i64
store i64 %15, i64* %2
br label %62
16: ; preds = %1
%17 = load i64, i64* %6
%18 = icmp sle i64 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %16
%20 = load i64, i64* @ERANGE
%21 = sub nsw i64 0, %20
store i64 %21, i64* %2
br label %62
22: ; preds = %16
%23 = load i8*, i8** %5
%24 = load i8, i8* %23
%25 = sext i8 %24 to i32
switch i32 %25, label %39 [
i32 116, label %26
i32 84, label %26
i32 103, label %29
i32 71, label %29
i32 109, label %32
i32 77, label %32
i32 107, label %35
i32 75, label %35
i32 0, label %38
]
26: ; preds = %22, %22
store i64 1099511627776, i64* %7
%27 = load i8*, i8** %5
%28 = getelementptr inbounds i8, i8* %27, i32 1
store i8* %28, i8** %5
br label %42
29: ; preds = %22, %22
store i64 1073741824, i64* %7
%30 = load i8*, i8** %5
%31 = getelementptr inbounds i8, i8* %30, i32 1
store i8* %31, i8** %5
br label %42
32: ; preds = %22, %22
store i64 1048576, i64* %7
%33 = load i8*, i8** %5
%34 = getelementptr inbounds i8, i8* %33, i32 1
store i8* %34, i8** %5
br label %42
35: ; preds = %22, %22
store i64 1024, i64* %7
%36 = load i8*, i8** %5
%37 = getelementptr inbounds i8, i8* %36, i32 1
store i8* %37, i8** %5
br label %42
38: ; preds = %22
store i64 1, i64* %7
br label %42
39: ; preds = %22
%40 = load i64, i64* @EINVAL
%41 = sub nsw i64 0, %40
store i64 %41, i64* %2
br label %62
42: ; preds = %38, %35, %32, %29, %26
%43 = load i8*, i8** %5
%44 = load i8, i8* %43
%45 = icmp ne i8 %44, 0
br i1 %45, label %46, label %49
46: ; preds = %42
%47 = load i64, i64* @EINVAL
%48 = sub nsw i64 0, %47
store i64 %48, i64* %2
br label %62
49: ; preds = %42
%50 = load i64, i64* %6
%51 = load i64, i64* @LONG_MAX
%52 = load i64, i64* %7
%53 = sdiv i64 %51, %52
%54 = icmp sgt i64 %50, %53
br i1 %54, label %55, label %58
55: ; preds = %49
%56 = load i64, i64* @ERANGE
%57 = sub nsw i64 0, %56
store i64 %57, i64* %2
br label %62
58: ; preds = %49
%59 = load i64, i64* %6
%60 = load i64, i64* %7
%61 = mul nsw i64 %59, %60
store i64 %61, i64* %2
br label %62
62: ; preds = %58, %55, %46, %39, %19, %12
%63 = load i64, i64* %2
ret i64 %63
}

LLVM IR (-O2):
@ERANGE = global i64 0
@EINVAL = global i64 0
@LONG_MAX = global i64 0
define i64 @strtomemory(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = bitcast i32* %2 to i8*
%5 = bitcast i8** %3 to i8*
%6 = call i64 @my_strtol(i8* %0, i8** %3, i32 10, i32* %2)
%7 = load i32, i32* %2
%8 = icmp sgt i32 %7, 0
br i1 %8, label %9, label %12
9: ; preds = %1
%10 = sub nsw i32 0, %7
%11 = sext i32 %10 to i64
br label %52
12: ; preds = %1
%13 = icmp slt i64 %6, 1
br i1 %13, label %14, label %17
14: ; preds = %12
%15 = load i64, i64* @ERANGE
%16 = sub nsw i64 0, %15
br label %52
17: ; preds = %12
%18 = load i8*, i8** %3
%19 = load i8, i8* %18
%20 = sext i8 %19 to i32
switch i32 %20, label %29 [
i32 116, label %21
i32 84, label %21
i32 103, label %23
i32 71, label %23
i32 109, label %25
i32 77, label %25
i32 107, label %27
i32 75, label %27
i32 0, label %36
]
21: ; preds = %17, %17
%22 = getelementptr inbounds i8, i8* %18, i64 1
store i8* %22, i8** %3
br label %32
23: ; preds = %17, %17
%24 = getelementptr inbounds i8, i8* %18, i64 1
store i8* %24, i8** %3
br label %32
25: ; preds = %17, %17
%26 = getelementptr inbounds i8, i8* %18, i64 1
store i8* %26, i8** %3
br label %32
27: ; preds = %17, %17
%28 = getelementptr inbounds i8, i8* %18, i64 1
store i8* %28, i8** %3
br label %32
29: ; preds = %17
%30 = load i64, i64* @EINVAL
%31 = sub nsw i64 0, %30
br label %52
32: ; preds = %21, %23, %25, %27
%33 = phi i8* [ %22, %21 ], [ %24, %23 ], [ %26, %25 ], [ %28, %27 ]
%34 = phi i64 [ 1099511627776, %21 ], [ 1073741824, %23 ], [ 1048576, %25 ], [ 1024, %27 ]
%35 = load i8, i8* %33
br label %36
36: ; preds = %32, %17
%37 = phi i8 [ %35, %32 ], [ %19, %17 ]
%38 = phi i64 [ %34, %32 ], [ 1, %17 ]
%39 = icmp eq i8 %37, 0
br i1 %39, label %43, label %40
40: ; preds = %36
%41 = load i64, i64* @EINVAL
%42 = sub nsw i64 0, %41
br label %52
43: ; preds = %36
%44 = load i64, i64* @LONG_MAX
%45 = sdiv i64 %44, %38
%46 = icmp sgt i64 %6, %45
br i1 %46, label %47, label %50
47: ; preds = %43
%48 = load i64, i64* @ERANGE
%49 = sub nsw i64 0, %48
br label %52
50: ; preds = %43
%51 = mul nsw i64 %38, %6
br label %52
52: ; preds = %29, %40, %47, %50, %14, %9
%53 = phi i64 [ %11, %9 ], [ %16, %14 ], [ %31, %29 ], [ %42, %40 ], [ %49, %47 ], [ %51, %50 ]
ret i64 %53
}