#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 unsigned int uint64_t;

/* Variables and functions */
int /*<<< orphan*/ EINVAL;
int /*<<< orphan*/ ERANGE;
int /*<<< orphan*/ errno;
unsigned int strtoumax(char const *, char **, int /*<<< orphan*/);
int tolower(unsigned char);
int expand_number(const char *buf, uint64_t *num) {
  uint64_t number;
  unsigned shift;
  char *endptr;
  number = strtoumax(buf, &endptr, 0);
  if (endptr == buf) {
    errno = EINVAL;
    return (-1);
  }
  switch (tolower((unsigned char)*endptr)) {
  case 'e':
    shift = 60;
    break;
  case 'p':
    shift = 50;
    break;
  case 't':
    shift = 40;
    break;
  case 'g':
    shift = 30;
    break;
  case 'm':
    shift = 20;
    break;
  case 'k':
    shift = 10;
    break;
  case 'b':
  case '\0':
    *num = number;
    return (0);
  default:
    errno = EINVAL;
    return (-1);
  }
  if ((number << shift) >> shift != number) {
    errno = ERANGE;
    return (-1);
  }
  *num = number << shift;
  return (0);
}


x86 assembly (clang, -O0):
<expand_number>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x28(%rbp),%rsi
xor    %edx,%edx
call   1040 <strtoumax@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x10(%rbp),%rax
jne    116e <expand_number+0x4e>
mov    0x2e79(%rip),%rax
mov    (%rax),%ecx
mov    0x2e78(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    12c9 <expand_number+0x1a9>
mov    -0x28(%rbp),%rax
movzbl (%rax),%edi
call   1030 <tolower@plt>
mov    %eax,%ecx
mov    %ecx,-0x2c(%rbp)
test   %eax,%eax
je     124b <expand_number+0x12b>
jmp    118c <expand_number+0x6c>
mov    -0x2c(%rbp),%eax
sub    $0x62,%eax
je     124b <expand_number+0x12b>
jmp    119d <expand_number+0x7d>
mov    -0x2c(%rbp),%eax
sub    $0x65,%eax
je     1203 <expand_number+0xe3>
jmp    11ae <expand_number+0x8e>
mov    -0x2c(%rbp),%eax
sub    $0x67,%eax
je     1227 <expand_number+0x107>
jmp    11bf <expand_number+0x9f>
mov    -0x2c(%rbp),%eax
sub    $0x6b,%eax
je     123f <expand_number+0x11f>
jmp    11d0 <expand_number+0xb0>
mov    -0x2c(%rbp),%eax
sub    $0x6d,%eax
je     1233 <expand_number+0x113>
jmp    11e1 <expand_number+0xc1>
mov    -0x2c(%rbp),%eax
sub    $0x70,%eax
je     120f <expand_number+0xef>
jmp    11f2 <expand_number+0xd2>
mov    -0x2c(%rbp),%eax
sub    $0x74,%eax
je     121b <expand_number+0xfb>
jmp    1260 <expand_number+0x140>
movl   $0x3c,-0x20(%rbp)
jmp    127e <expand_number+0x15e>
movl   $0x32,-0x20(%rbp)
jmp    127e <expand_number+0x15e>
movl   $0x28,-0x20(%rbp)
jmp    127e <expand_number+0x15e>
movl   $0x1e,-0x20(%rbp)
jmp    127e <expand_number+0x15e>
movl   $0x14,-0x20(%rbp)
jmp    127e <expand_number+0x15e>
movl   $0xa,-0x20(%rbp)
jmp    127e <expand_number+0x15e>
mov    -0x1c(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x0,-0x4(%rbp)
jmp    12c9 <expand_number+0x1a9>
mov    0x2d69(%rip),%rax
mov    (%rax),%ecx
mov    0x2d68(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    12c9 <expand_number+0x1a9>
mov    -0x1c(%rbp),%eax
mov    -0x20(%rbp),%ecx
shl    %cl,%eax
mov    -0x20(%rbp),%ecx
shr    %cl,%eax
cmp    -0x1c(%rbp),%eax
je     12b2 <expand_number+0x192>
mov    0x2d4d(%rip),%rax
mov    (%rax),%ecx
mov    0x2d34(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    12c9 <expand_number+0x1a9>
mov    -0x1c(%rbp),%eax
mov    -0x20(%rbp),%ecx
shl    %cl,%eax
mov    %eax,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<expand_number>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r14
mov    %rdi,%rbx
lea    0x8(%rsp),%rsi
xor    %edx,%edx
call   1040 <strtoumax@plt>
mov    %eax,%ebp
mov    0x8(%rsp),%rax
cmp    %rbx,%rax
je     117b <expand_number+0x5b>
movzbl (%rax),%edi
call   1030 <tolower@plt>
lea    -0x62(%rax),%edx
cmp    $0x12,%edx
ja     1172 <expand_number+0x52>
mov    $0x3c,%ecx
lea    0xe9e(%rip),%rax
movslq (%rax,%rdx,4),%rdx
add    %rax,%rdx
jmp    *%rdx
mov    $0x1e,%ecx
jmp    119e <expand_number+0x7e>
test   %eax,%eax
jne    117b <expand_number+0x5b>
mov    %ebp,(%r14)
jmp    11ad <expand_number+0x8d>
mov    0x2e4e(%rip),%rax
jmp    11b8 <expand_number+0x98>
mov    $0xa,%ecx
jmp    119e <expand_number+0x7e>
mov    $0x14,%ecx
jmp    119e <expand_number+0x7e>
mov    $0x32,%ecx
jmp    119e <expand_number+0x7e>
mov    $0x28,%ecx
mov    %ebp,%eax
shl    %cl,%eax
mov    %eax,%edx
shr    %cl,%edx
cmp    %ebp,%edx
jne    11b1 <expand_number+0x91>
mov    %eax,(%r14)
xor    %eax,%eax
jmp    11c8 <expand_number+0xa8>
mov    0x2e30(%rip),%rax
mov    (%rax),%eax
mov    0x2e17(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@EINVAL = global i32 0
@errno = global i32 0
@ERANGE = global i32 0
define i32 @expand_number(i8* %0, i32* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
store i8* %0, i8** %4
store i32* %1, i32** %5
%9 = load i8*, i8** %4
%10 = call i32 @strtoumax(i8* %9, i8** %8, i32 0)
store i32 %10, i32* %6
%11 = load i8*, i8** %8
%12 = load i8*, i8** %4
%13 = icmp eq i8* %11, %12
br i1 %13, label %14, label %16
14: ; preds = %2
%15 = load i32, i32* @EINVAL
store i32 %15, i32* @errno
store i32 -1, i32* %3
br label %46
16: ; preds = %2
%17 = load i8*, i8** %8
%18 = load i8, i8* %17
%19 = call i32 @tolower(i8 zeroext %18)
switch i32 %19, label %29 [
i32 101, label %20
i32 112, label %21
i32 116, label %22
i32 103, label %23
i32 109, label %24
i32 107, label %25
i32 98, label %26
i32 0, label %26
]
20: ; preds = %16
store i32 60, i32* %7
br label %31
21: ; preds = %16
store i32 50, i32* %7
br label %31
22: ; preds = %16
store i32 40, i32* %7
br label %31
23: ; preds = %16
store i32 30, i32* %7
br label %31
24: ; preds = %16
store i32 20, i32* %7
br label %31
25: ; preds = %16
store i32 10, i32* %7
br label %31
26: ; preds = %16, %16
%27 = load i32, i32* %6
%28 = load i32*, i32** %5
store i32 %27, i32* %28
store i32 0, i32* %3
br label %46
29: ; preds = %16
%30 = load i32, i32* @EINVAL
store i32 %30, i32* @errno
store i32 -1, i32* %3
br label %46
31: ; preds = %25, %24, %23, %22, %21, %20
%32 = load i32, i32* %6
%33 = load i32, i32* %7
%34 = shl i32 %32, %33
%35 = load i32, i32* %7
%36 = lshr i32 %34, %35
%37 = load i32, i32* %6
%38 = icmp ne i32 %36, %37
br i1 %38, label %39, label %41
39: ; preds = %31
%40 = load i32, i32* @ERANGE
store i32 %40, i32* @errno
store i32 -1, i32* %3
br label %46
41: ; preds = %31
%42 = load i32, i32* %6
%43 = load i32, i32* %7
%44 = shl i32 %42, %43
%45 = load i32*, i32** %5
store i32 %44, i32* %45
store i32 0, i32* %3
br label %46
46: ; preds = %41, %39, %29, %26, %14
%47 = load i32, i32* %3
ret i32 %47
}

LLVM IR (-O2):
@EINVAL = global i32 0
@errno = global i32 0
@ERANGE = global i32 0
define i32 @expand_number(i8* %0, i32* nocapture writeonly %1) {
%3 = alloca i8*
%4 = bitcast i8** %3 to i8*
%5 = call i32 @strtoumax(i8* %0, i8** %3, i32 0)
%6 = load i8*, i8** %3
%7 = icmp eq i8* %6, %0
br i1 %7, label %8, label %10
8: ; preds = %2
%9 = load i32, i32* @EINVAL
store i32 %9, i32* @errno
br label %29
10: ; preds = %2
%11 = load i8, i8* %6
%12 = call i32 @tolower(i8 zeroext %11)
switch i32 %12, label %19 [
i32 101, label %21
i32 112, label %13
i32 116, label %14
i32 103, label %15
i32 109, label %16
i32 107, label %17
i32 98, label %18
i32 0, label %18
]
13: ; preds = %10
br label %21
14: ; preds = %10
br label %21
15: ; preds = %10
br label %21
16: ; preds = %10
br label %21
17: ; preds = %10
br label %21
18: ; preds = %10, %10
store i32 %5, i32* %1
br label %29
19: ; preds = %10
%20 = load i32, i32* @EINVAL
store i32 %20, i32* @errno
br label %29
21: ; preds = %10, %17, %16, %15, %14, %13
%22 = phi i32 [ 10, %17 ], [ 20, %16 ], [ 30, %15 ], [ 40, %14 ], [ 50, %13 ], [ 60, %10 ]
%23 = shl i32 %5, %22
%24 = lshr i32 %23, %22
%25 = icmp eq i32 %24, %5
br i1 %25, label %28, label %26
26: ; preds = %21
%27 = load i32, i32* @ERANGE
store i32 %27, i32* @errno
br label %29
28: ; preds = %21
store i32 %23, i32* %1
br label %29
29: ; preds = %28, %26, %19, %18, %8
%30 = phi i32 [ -1, %8 ], [ -1, %19 ], [ 0, %18 ], [ -1, %26 ], [ 0, %28 ]
ret i32 %30
}