#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 long off_t;

/* Variables and functions */
scalar_t__ ERANGE;
int /*<<< orphan*/ EXIT_FAILURE;
scalar_t__ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fputs(char *, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
unsigned long strtoul(char const *, char **, int);
int tolower(unsigned char);
int /*<<< orphan*/ usage(int /*<<< orphan*/);
off_t get_size(const char str[]) {
  unsigned long val;
  int suffix;
  char *end;
  errno = 0;
  val = strtoul(str, &end, 10);
  if (end == str) {
    fputs("trunc"
          ": size is not a number.\n",
          stderr);
    usage(stderr);
    exit(EXIT_FAILURE);
  } else if (errno == ERANGE || (unsigned long)(off_t)val != val) {
    fputs("trunc"
          ": size is out of range.\n",
          stderr);
    exit(EXIT_FAILURE);
  }
  suffix = tolower((unsigned char)end[0]);
  if (suffix == 'k') {
    val *= 1024;
  } else if (suffix == 'm') {
    val *= 1024 * 1024;
  }
  return val;
}


x86 assembly (clang, -O0):
<get_size>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    0x2e6d(%rip),%rax
movq   $0x0,(%rax)
mov    -0x8(%rbp),%rdi
lea    -0x20(%rbp),%rsi
mov    $0xa,%edx
call   1060 <strtoul@plt>
mov    %rax,-0x10(%rbp)
mov    -0x20(%rbp),%rax
cmp    -0x8(%rbp),%rax
jne    11bf <get_size+0x6f>
mov    0x2e43(%rip),%rax
mov    (%rax),%esi
lea    0xe62(%rip),%rdi
call   1030 <fputs@plt>
mov    0x2e2e(%rip),%rax
mov    (%rax),%edi
call   1050 <usage@plt>
mov    0x2e10(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
mov    0x2e0a(%rip),%rax
mov    (%rax),%rax
mov    0x2e18(%rip),%rcx
cmp    (%rcx),%rax
je     11e7 <get_size+0x97>
mov    -0x10(%rbp),%rax
cmp    -0x10(%rbp),%rax
je     120a <get_size+0xba>
mov    0x2dea(%rip),%rax
mov    (%rax),%esi
lea    0xe27(%rip),%rdi
call   1030 <fputs@plt>
mov    0x2dc5(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
jmp    120f <get_size+0xbf>
mov    -0x20(%rbp),%rax
movzbl (%rax),%edi
call   1040 <tolower@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x6b,-0x14(%rbp)
jne    1239 <get_size+0xe9>
mov    -0x10(%rbp),%rax
shl    $0xa,%rax
mov    %rax,-0x10(%rbp)
jmp    1254 <get_size+0x104>
cmpl   $0x6d,-0x14(%rbp)
jne    124f <get_size+0xff>
mov    -0x10(%rbp),%rax
shl    $0x14,%rax
mov    %rax,-0x10(%rbp)
jmp    1254 <get_size+0x104>
mov    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_size>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%rbx
mov    0x2e6d(%rip),%r15
movq   $0x0,(%r15)
lea    0x8(%rsp),%rsi
mov    $0xa,%edx
call   1060 <strtoul@plt>
mov    %rax,%r14
mov    0x8(%rsp),%rax
cmp    %rbx,%rax
je     11be <get_size+0x6e>
mov    (%r15),%rcx
mov    0x2e58(%rip),%rdx
cmp    (%rdx),%rcx
je     11dc <get_size+0x8c>
movzbl (%rax),%edi
call   1040 <tolower@plt>
cmp    $0x6d,%eax
je     11ad <get_size+0x5d>
cmp    $0x6b,%eax
jne    11b1 <get_size+0x61>
shl    $0xa,%r14
jmp    11b1 <get_size+0x61>
shl    $0x14,%r14
mov    %r14,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
mov    0x2e13(%rip),%rbx
mov    (%rbx),%esi
lea    0xe32(%rip),%rdi
call   1030 <fputs@plt>
mov    (%rbx),%edi
call   1050 <usage@plt>
jmp    11f1 <get_size+0xa1>
mov    0x2df5(%rip),%rax
mov    (%rax),%esi
lea    0xe32(%rip),%rdi
call   1030 <fputs@plt>
mov    0x2dd0(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@errno = global i64 0
@.str = [30 x i8] c"trunc: size is not a number.\0A\00"
@stderr = global i32 0
@EXIT_FAILURE = global i32 0
@ERANGE = global i64 0
@.str.1 = [30 x i8] c"trunc: size is out of range.\0A\00"
define i64 @get_size(i8* %0) {
%2 = alloca i8*
%3 = alloca i64
%4 = alloca i32
%5 = alloca i8*
store i8* %0, i8** %2
store i64 0, i64* @errno
%6 = load i8*, i8** %2
%7 = call i64 @strtoul(i8* %6, i8** %5, i32 10)
store i64 %7, i64* %3
%8 = load i8*, i8** %5
%9 = load i8*, i8** %2
%10 = icmp eq i8* %8, %9
br i1 %10, label %11, label %18
11: ; preds = %1
%12 = load i32, i32* @stderr
%13 = call i32 @fputs(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i32 %12)
%14 = load i32, i32* @stderr
%15 = call i32 @usage(i32 %14)
%16 = load i32, i32* @EXIT_FAILURE
%17 = call i32 @exit(i32 %16)
unreachable
18: ; preds = %1
%19 = load i64, i64* @errno
%20 = load i64, i64* @ERANGE
%21 = icmp eq i64 %19, %20
br i1 %21, label %26, label %22
22: ; preds = %18
%23 = load i64, i64* %3
%24 = load i64, i64* %3
%25 = icmp ne i64 %23, %24
br i1 %25, label %26, label %31
26: ; preds = %22, %18
%27 = load i32, i32* @stderr
%28 = call i32 @fputs(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), i32 %27)
%29 = load i32, i32* @EXIT_FAILURE
%30 = call i32 @exit(i32 %29)
unreachable
31: ; preds = %22
br label %32
32: ; preds = %31
%33 = load i8*, i8** %5
%34 = getelementptr inbounds i8, i8* %33, i64 0
%35 = load i8, i8* %34
%36 = call i32 @tolower(i8 zeroext %35)
store i32 %36, i32* %4
%37 = load i32, i32* %4
%38 = icmp eq i32 %37, 107
br i1 %38, label %39, label %42
39: ; preds = %32
%40 = load i64, i64* %3
%41 = mul i64 %40, 1024
store i64 %41, i64* %3
br label %49
42: ; preds = %32
%43 = load i32, i32* %4
%44 = icmp eq i32 %43, 109
br i1 %44, label %45, label %48
45: ; preds = %42
%46 = load i64, i64* %3
%47 = mul i64 %46, 1048576
store i64 %47, i64* %3
br label %48
48: ; preds = %45, %42
br label %49
49: ; preds = %48, %39
%50 = load i64, i64* %3
ret i64 %50
}

LLVM IR (-O2):
@errno = global i64 0
@.str = [30 x i8] c"trunc: size is not a number.\0A\00"
@stderr = global i32 0
@EXIT_FAILURE = global i32 0
@ERANGE = global i64 0
@.str.1 = [30 x i8] c"trunc: size is out of range.\0A\00"
define i64 @get_size(i8* %0) {
%2 = alloca i8*
%3 = bitcast i8** %2 to i8*
store i64 0, i64* @errno
%4 = call i64 @strtoul(i8* %0, i8** %2, i32 10)
%5 = load i8*, i8** %2
%6 = icmp eq i8* %5, %0
br i1 %6, label %7, label %14
7: ; preds = %1
%8 = load i32, i32* @stderr
%9 = tail call i32 @fputs(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i32 %8)
%10 = load i32, i32* @stderr
%11 = tail call i32 @usage(i32 %10)
%12 = load i32, i32* @EXIT_FAILURE
%13 = tail call i32 @exit(i32 %12)
unreachable
14: ; preds = %1
%15 = load i64, i64* @errno
%16 = load i64, i64* @ERANGE
%17 = icmp eq i64 %15, %16
br i1 %17, label %18, label %23
18: ; preds = %14
%19 = load i32, i32* @stderr
%20 = tail call i32 @fputs(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), i32 %19)
%21 = load i32, i32* @EXIT_FAILURE
%22 = tail call i32 @exit(i32 %21)
unreachable
23: ; preds = %14
%24 = load i8, i8* %5
%25 = tail call i32 @tolower(i8 zeroext %24)
switch i32 %25, label %30 [
i32 107, label %26
i32 109, label %28
]
26: ; preds = %23
%27 = shl i64 %4, 10
br label %30
28: ; preds = %23
%29 = shl i64 %4, 20
br label %30
30: ; preds = %23, %28, %26
%31 = phi i64 [ %27, %26 ], [ %29, %28 ], [ %4, %23 ]
ret i64 %31
}