#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 htoi(char *);
int /*<<< orphan*/ memcpy(char *, char *, int);
int /*<<< orphan*/ printf(char *, int);
int strlen_(char *);
scalar_t__ strncmp(char *, char *, int);
unsigned short parse_idx(char *str) {
  int i, n;
  char istr[5];
  if (strncmp(str, "0x", 2) == 0)
    str += 2;
  n = strlen_(str);
  if (n == 4) {
    i = (htoi(str) << 8) + htoi(&str[2]);
  } else if (n == 3) {
    istr[0] = '0';
    memcpy(&istr[1], str, 3);
    i = (htoi(istr) << 8) + htoi(&istr[2]);
  } else
    i = htoi(str);
  printf("idx = 0x%x\n", i);
  return ((unsigned short)i);
}


x86 assembly (clang, -O0):
<parse_idx>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe99(%rip),%rsi
mov    $0x2,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%rax
jne    1187 <parse_idx+0x37>
mov    -0x8(%rbp),%rax
add    $0x2,%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1070 <strlen_@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x4,-0x10(%rbp)
jne    11c8 <parse_idx+0x78>
mov    -0x8(%rbp),%rdi
call   1060 <htoi@plt>
shl    $0x8,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x8(%rbp),%rdi
add    $0x2,%rdi
call   1060 <htoi@plt>
mov    %eax,%ecx
mov    -0x1c(%rbp),%eax
add    %ecx,%eax
mov    %eax,-0xc(%rbp)
jmp    1228 <parse_idx+0xd8>
cmpl   $0x3,-0x10(%rbp)
jne    1217 <parse_idx+0xc7>
movb   $0x30,-0x15(%rbp)
lea    -0x15(%rbp),%rdi
add    $0x1,%rdi
mov    -0x8(%rbp),%rsi
mov    $0x3,%edx
call   1050 <memcpy@plt>
lea    -0x15(%rbp),%rdi
call   1060 <htoi@plt>
shl    $0x8,%eax
mov    %eax,-0x20(%rbp)
lea    -0x15(%rbp),%rdi
add    $0x2,%rdi
call   1060 <htoi@plt>
mov    %eax,%ecx
mov    -0x20(%rbp),%eax
add    %ecx,%eax
mov    %eax,-0xc(%rbp)
jmp    1223 <parse_idx+0xd3>
mov    -0x8(%rbp),%rdi
call   1060 <htoi@plt>
mov    %eax,-0xc(%rbp)
jmp    1228 <parse_idx+0xd8>
mov    -0xc(%rbp),%esi
lea    0xdd1(%rip),%rdi
call   1040 <printf@plt>
mov    -0xc(%rbp),%eax
movzwl %ax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<parse_idx>:
push   %rbp
push   %rbx
push   %rax
mov    %rdi,%rbx
lea    0xea3(%rip),%rsi
mov    $0x2,%edx
call   1030 <strncmp@plt>
xor    %ecx,%ecx
test   %rax,%rax
sete   %cl
lea    (%rbx,%rcx,2),%rbx
mov    %rbx,%rdi
call   1070 <strlen_@plt>
cmp    $0x3,%eax
je     119b <parse_idx+0x4b>
cmp    $0x4,%eax
jne    11d1 <parse_idx+0x81>
mov    %rbx,%rdi
call   1060 <htoi@plt>
mov    %eax,%ebp
shl    $0x8,%ebp
add    $0x2,%rbx
mov    %rbx,%rdi
jmp    11c6 <parse_idx+0x76>
movb   $0x30,0x3(%rsp)
lea    0x4(%rsp),%rdi
mov    %rbx,%rsi
mov    $0x3,%edx
call   1050 <memcpy@plt>
lea    0x3(%rsp),%rdi
call   1060 <htoi@plt>
mov    %eax,%ebp
shl    $0x8,%ebp
lea    0x5(%rsp),%rdi
call   1060 <htoi@plt>
mov    %eax,%ebx
add    %ebp,%ebx
jmp    11db <parse_idx+0x8b>
mov    %rbx,%rdi
call   1060 <htoi@plt>
mov    %eax,%ebx
lea    0xe21(%rip),%rdi
mov    %ebx,%esi
call   1040 <printf@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"0x\00"
@.str.1 = [12 x i8] c"idx = 0x%x\0A\00"
define zeroext i16 @parse_idx(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
%5 = alloca [5 x i8]
store i8* %0, i8** %2
%6 = load i8*, i8** %2
%7 = call i64 @strncmp(i8* %6, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 2)
%8 = icmp eq i64 %7, 0
br i1 %8, label %9, label %12
9: ; preds = %1
%10 = load i8*, i8** %2
%11 = getelementptr inbounds i8, i8* %10, i64 2
store i8* %11, i8** %2
br label %12
12: ; preds = %9, %1
%13 = load i8*, i8** %2
%14 = call i32 @strlen_(i8* %13)
store i32 %14, i32* %4
%15 = load i32, i32* %4
%16 = icmp eq i32 %15, 4
br i1 %16, label %17, label %25
17: ; preds = %12
%18 = load i8*, i8** %2
%19 = call i32 @htoi(i8* %18)
%20 = shl i32 %19, 8
%21 = load i8*, i8** %2
%22 = getelementptr inbounds i8, i8* %21, i64 2
%23 = call i32 @htoi(i8* %22)
%24 = add nsw i32 %20, %23
store i32 %24, i32* %3
br label %43
25: ; preds = %12
%26 = load i32, i32* %4
%27 = icmp eq i32 %26, 3
br i1 %27, label %28, label %39
28: ; preds = %25
%29 = getelementptr inbounds [5 x i8], [5 x i8]* %5, i64 0, i64 0
store i8 48, i8* %29
%30 = getelementptr inbounds [5 x i8], [5 x i8]* %5, i64 0, i64 1
%31 = load i8*, i8** %2
%32 = call i32 @memcpy(i8* %30, i8* %31, i32 3)
%33 = getelementptr inbounds [5 x i8], [5 x i8]* %5, i64 0, i64 0
%34 = call i32 @htoi(i8* %33)
%35 = shl i32 %34, 8
%36 = getelementptr inbounds [5 x i8], [5 x i8]* %5, i64 0, i64 2
%37 = call i32 @htoi(i8* %36)
%38 = add nsw i32 %35, %37
store i32 %38, i32* %3
br label %42
39: ; preds = %25
%40 = load i8*, i8** %2
%41 = call i32 @htoi(i8* %40)
store i32 %41, i32* %3
br label %42
42: ; preds = %39, %28
br label %43
43: ; preds = %42, %17
%44 = load i32, i32* %3
%45 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 %44)
%46 = load i32, i32* %3
%47 = trunc i32 %46 to i16
ret i16 %47
}

LLVM IR (-O2):
@.str = [3 x i8] c"0x\00"
@.str.1 = [12 x i8] c"idx = 0x%x\0A\00"
define zeroext i16 @parse_idx(i8* %0) {
%2 = alloca [5 x i8]
%3 = getelementptr inbounds [5 x i8], [5 x i8]* %2, i64 0, i64 0
%4 = tail call i64 @strncmp(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 2)
%5 = icmp eq i64 %4, 0
%6 = select i1 %5, i64 2, i64 0
%7 = getelementptr i8, i8* %0, i64 %6
%8 = tail call i32 @strlen_(i8* %7)
switch i32 %8, label %23 [
i32 4, label %9
i32 3, label %15
]
9: ; preds = %1
%10 = tail call i32 @htoi(i8* %7)
%11 = shl i32 %10, 8
%12 = getelementptr inbounds i8, i8* %7, i64 2
%13 = tail call i32 @htoi(i8* %12)
%14 = add nsw i32 %11, %13
br label %25
15: ; preds = %1
store i8 48, i8* %3
%16 = getelementptr inbounds [5 x i8], [5 x i8]* %2, i64 0, i64 1
%17 = call i32 @memcpy(i8* %16, i8* %7, i32 3)
%18 = call i32 @htoi(i8* %3)
%19 = shl i32 %18, 8
%20 = getelementptr inbounds [5 x i8], [5 x i8]* %2, i64 0, i64 2
%21 = call i32 @htoi(i8* %20)
%22 = add nsw i32 %19, %21
br label %25
23: ; preds = %1
%24 = tail call i32 @htoi(i8* %7)
br label %25
25: ; preds = %15, %23, %9
%26 = phi i32 [ %14, %9 ], [ %22, %15 ], [ %24, %23 ]
%27 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 %26)
%28 = trunc i32 %26 to i16
ret i16 %28
}