#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 */
int /*<<< orphan*/ atasm_error(char *, int);
int /*<<< orphan*/ isdigit(unsigned char);
int /*<<< orphan*/ sscanf(char *, char *, int *);
int /*<<< orphan*/ strlen(char *);
int num_cvt(char *num) {
  int v, i, bit, tp;
  char *txt;
  if (!isdigit((unsigned char)(num[0]))) {
    txt = num + 1;
    if (num[0] == '$')
      tp = 1;
    else if ((num[0] == '~') || (num[0] == '%'))
      tp = 2;
    else {
      tp = 0;
      atasm_error("Malformed numeric constant.", 1);
    }
  } else {
    tp = 0;
    txt = num;
  }
  switch (tp) {
  case 0:
    sscanf(txt, "%d", &v);
    break;
  case 1:
    sscanf(txt, "%x", &v);
    break;
  case 2:
    v = 0;
    bit = 1;
    for (i = strlen(txt) - 1; i >= 0; i--) {
      if (txt[i] == '1')
        v += bit;
      else if (txt[i] != '0') {
        atasm_error("Malformed binary value", 0);
      }
      bit = bit << 1;
    }
    break;
  }
  return v;
}


x86 assembly (clang, -O0):
<num_cvt>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rax
movzbl (%rax),%edi
call   1040 <isdigit@plt>
cmp    $0x0,%eax
jne    11d7 <num_cvt+0x97>
mov    -0x8(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x24,%eax
jne    1189 <num_cvt+0x49>
movl   $0x1,-0x18(%rbp)
jmp    11d2 <num_cvt+0x92>
mov    -0x8(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x7e,%eax
je     11a9 <num_cvt+0x69>
mov    -0x8(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x25,%eax
jne    11b5 <num_cvt+0x75>
movl   $0x2,-0x18(%rbp)
jmp    11cd <num_cvt+0x8d>
movl   $0x0,-0x18(%rbp)
lea    0xe3d(%rip),%rdi
mov    $0x1,%esi
call   1030 <atasm_error@plt>
jmp    11d2 <num_cvt+0x92>
jmp    11e6 <num_cvt+0xa6>
movl   $0x0,-0x18(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%eax
mov    %eax,-0x24(%rbp)
test   %eax,%eax
je     121b <num_cvt+0xdb>
jmp    11f9 <num_cvt+0xb9>
mov    -0x24(%rbp),%eax
sub    $0x1,%eax
je     1234 <num_cvt+0xf4>
jmp    120a <num_cvt+0xca>
mov    -0x24(%rbp),%eax
sub    $0x2,%eax
je     124d <num_cvt+0x10d>
jmp    12db <num_cvt+0x19b>
mov    -0x20(%rbp),%rdi
lea    0xdf6(%rip),%rsi
lea    -0xc(%rbp),%rdx
call   1060 <sscanf@plt>
jmp    12db <num_cvt+0x19b>
mov    -0x20(%rbp),%rdi
lea    0xde0(%rip),%rsi
lea    -0xc(%rbp),%rdx
call   1060 <sscanf@plt>
jmp    12db <num_cvt+0x19b>
movl   $0x0,-0xc(%rbp)
movl   $0x1,-0x14(%rbp)
mov    -0x20(%rbp),%rdi
call   1050 <strlen@plt>
sub    $0x1,%eax
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jl     12d6 <num_cvt+0x196>
mov    -0x20(%rbp),%rax
movslq -0x10(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x31,%eax
jne    1297 <num_cvt+0x157>
mov    -0x14(%rbp),%eax
add    -0xc(%rbp),%eax
mov    %eax,-0xc(%rbp)
jmp    12bf <num_cvt+0x17f>
mov    -0x20(%rbp),%rax
movslq -0x10(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x30,%eax
je     12ba <num_cvt+0x17a>
lea    0xd6f(%rip),%rdi
xor    %esi,%esi
call   1030 <atasm_error@plt>
jmp    12bf <num_cvt+0x17f>
mov    -0x14(%rbp),%eax
shl    $0x1,%eax
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x10(%rbp)
jmp    126a <num_cvt+0x12a>
jmp    12db <num_cvt+0x19b>
mov    -0xc(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<num_cvt>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%r12
movzbl (%rdi),%edi
call   1040 <isdigit@plt>
test   %eax,%eax
je     1180 <num_cvt+0x40>
lea    0xeba(%rip),%rsi
lea    0xc(%rsp),%rdx
mov    %r12,%rdi
call   1060 <sscanf@plt>
mov    0xc(%rsp),%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    (%r12),%al
add    $0x1,%r12
cmp    $0x24,%al
je     11eb <num_cvt+0xab>
cmp    $0x25,%al
je     1194 <num_cvt+0x54>
cmp    $0x7e,%al
jne    11f7 <num_cvt+0xb7>
mov    %r12,%rdi
call   1050 <strlen@plt>
test   %eax,%eax
jle    120d <num_cvt+0xcd>
mov    %eax,%ebp
add    $0x1,%rbp
xor    %r15d,%r15d
mov    $0x1,%ebx
lea    0xe6d(%rip),%r14
jmp    11d6 <num_cvt+0x96>
nopw   0x0(%rax,%rax,1)
mov    %r14,%rdi
xor    %esi,%esi
call   1030 <atasm_error@plt>
add    %ebx,%ebx
add    $0xffffffffffffffff,%rbp
cmp    $0x1,%rbp
jbe    1210 <num_cvt+0xd0>
lea    -0x2(%rbp),%eax
movzbl (%r12,%rax,1),%eax
cmp    $0x30,%al
je     11ca <num_cvt+0x8a>
cmp    $0x31,%al
jne    11c0 <num_cvt+0x80>
add    %ebx,%r15d
jmp    11ca <num_cvt+0x8a>
lea    0xe2d(%rip),%rsi
jmp    1162 <num_cvt+0x22>
lea    0xe02(%rip),%rdi
mov    $0x1,%esi
call   1030 <atasm_error@plt>
jmp    115b <num_cvt+0x1b>
xor    %r15d,%r15d
mov    %r15d,0xc(%rsp)
jmp    116f <num_cvt+0x2f>

LLVM IR (-O0):
@.str = [28 x i8] c"Malformed numeric constant.\00"
@.str.1 = [3 x i8] c"%d\00"
@.str.2 = [3 x i8] c"%x\00"
@.str.3 = [23 x i8] c"Malformed binary value\00"
define i32 @num_cvt(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
store i8* %0, i8** %2
%8 = load i8*, i8** %2
%9 = getelementptr inbounds i8, i8* %8, i64 0
%10 = load i8, i8* %9
%11 = call i32 @isdigit(i8 zeroext %10)
%12 = icmp ne i32 %11, 0
br i1 %12, label %39, label %13
13: ; preds = %1
%14 = load i8*, i8** %2
%15 = getelementptr inbounds i8, i8* %14, i64 1
store i8* %15, i8** %7
%16 = load i8*, i8** %2
%17 = getelementptr inbounds i8, i8* %16, i64 0
%18 = load i8, i8* %17
%19 = sext i8 %18 to i32
%20 = icmp eq i32 %19, 36
br i1 %20, label %21, label %22
21: ; preds = %13
store i32 1, i32* %6
br label %38
22: ; preds = %13
%23 = load i8*, i8** %2
%24 = getelementptr inbounds i8, i8* %23, i64 0
%25 = load i8, i8* %24
%26 = sext i8 %25 to i32
%27 = icmp eq i32 %26, 126
br i1 %27, label %34, label %28
28: ; preds = %22
%29 = load i8*, i8** %2
%30 = getelementptr inbounds i8, i8* %29, i64 0
%31 = load i8, i8* %30
%32 = sext i8 %31 to i32
%33 = icmp eq i32 %32, 37
br i1 %33, label %34, label %35
34: ; preds = %28, %22
store i32 2, i32* %6
br label %37
35: ; preds = %28
store i32 0, i32* %6
%36 = call i32 @atasm_error(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i32 1)
br label %37
37: ; preds = %35, %34
br label %38
38: ; preds = %37, %21
br label %41
39: ; preds = %1
store i32 0, i32* %6
%40 = load i8*, i8** %2
store i8* %40, i8** %7
br label %41
41: ; preds = %39, %38
%42 = load i32, i32* %6
switch i32 %42, label %86 [
i32 0, label %43
i32 1, label %46
i32 2, label %49
]
43: ; preds = %41
%44 = load i8*, i8** %7
%45 = call i32 @sscanf(i8* %44, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %3)
br label %86
46: ; preds = %41
%47 = load i8*, i8** %7
%48 = call i32 @sscanf(i8* %47, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %3)
br label %86
49: ; preds = %41
store i32 0, i32* %3
store i32 1, i32* %5
%50 = load i8*, i8** %7
%51 = call i32 @strlen(i8* %50)
%52 = sub nsw i32 %51, 1
store i32 %52, i32* %4
br label %53
53: ; preds = %82, %49
%54 = load i32, i32* %4
%55 = icmp sge i32 %54, 0
br i1 %55, label %56, label %85
56: ; preds = %53
%57 = load i8*, i8** %7
%58 = load i32, i32* %4
%59 = sext i32 %58 to i64
%60 = getelementptr inbounds i8, i8* %57, i64 %59
%61 = load i8, i8* %60
%62 = sext i8 %61 to i32
%63 = icmp eq i32 %62, 49
br i1 %63, label %64, label %68
64: ; preds = %56
%65 = load i32, i32* %5
%66 = load i32, i32* %3
%67 = add nsw i32 %66, %65
store i32 %67, i32* %3
br label %79
68: ; preds = %56
%69 = load i8*, i8** %7
%70 = load i32, i32* %4
%71 = sext i32 %70 to i64
%72 = getelementptr inbounds i8, i8* %69, i64 %71
%73 = load i8, i8* %72
%74 = sext i8 %73 to i32
%75 = icmp ne i32 %74, 48
br i1 %75, label %76, label %78
76: ; preds = %68
%77 = call i32 @atasm_error(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0), i32 0)
br label %78
78: ; preds = %76, %68
br label %79
79: ; preds = %78, %64
%80 = load i32, i32* %5
%81 = shl i32 %80, 1
store i32 %81, i32* %5
br label %82
82: ; preds = %79
%83 = load i32, i32* %4
%84 = add nsw i32 %83, -1
store i32 %84, i32* %4
br label %53
85: ; preds = %53
br label %86
86: ; preds = %41, %85, %46, %43
%87 = load i32, i32* %3
ret i32 %87
}

LLVM IR (-O2):
@.str = [28 x i8] c"Malformed numeric constant.\00"
@.str.1 = [3 x i8] c"%d\00"
@.str.2 = [3 x i8] c"%x\00"
@.str.3 = [23 x i8] c"Malformed binary value\00"
define i32 @num_cvt(i8* %0) {
%2 = alloca i32
%3 = bitcast i32* %2 to i8*
%4 = load i8, i8* %0
%5 = tail call i32 @isdigit(i8 zeroext %4)
%6 = icmp eq i32 %5, 0
br i1 %6, label %7, label %12
7: ; preds = %1
%8 = getelementptr inbounds i8, i8* %0, i64 1
%9 = load i8, i8* %0
switch i8 %9, label %10 [
i8 36, label %15
i8 126, label %17
i8 37, label %17
]
10: ; preds = %7
%11 = tail call i32 @atasm_error(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i32 1)
br label %12
12: ; preds = %10, %1
%13 = phi i8* [ %0, %1 ], [ %8, %10 ]
%14 = call i32 @sscanf(i8* %13, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %2)
br label %40
15: ; preds = %7
%16 = call i32 @sscanf(i8* %8, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %2)
br label %40
17: ; preds = %7, %7
%18 = tail call i32 @strlen(i8* %8)
%19 = icmp sgt i32 %18, 0
br i1 %19, label %20, label %38
20: ; preds = %17
%21 = zext i32 %18 to i64
br label %22
22: ; preds = %20, %34
%23 = phi i64 [ %21, %20 ], [ %26, %34 ]
%24 = phi i32 [ 1, %20 ], [ %36, %34 ]
%25 = phi i32 [ 0, %20 ], [ %35, %34 ]
%26 = add nsw i64 %23, -1
%27 = and i64 %26, 4294967295
%28 = getelementptr inbounds i8, i8* %8, i64 %27
%29 = load i8, i8* %28
switch i8 %29, label %32 [
i8 49, label %30
i8 48, label %34
]
30: ; preds = %22
%31 = add nsw i32 %25, %24
br label %34
32: ; preds = %22
%33 = tail call i32 @atasm_error(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0), i32 0)
br label %34
34: ; preds = %22, %32, %30
%35 = phi i32 [ %25, %22 ], [ %25, %32 ], [ %31, %30 ]
%36 = shl i32 %24, 1
%37 = icmp ugt i64 %23, 1
br i1 %37, label %22, label %38
38: ; preds = %34, %17
%39 = phi i32 [ 0, %17 ], [ %35, %34 ]
store i32 %39, i32* %2
br label %40
40: ; preds = %38, %15, %12
%41 = load i32, i32* %2
ret i32 %41
}