#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 atoi(char *);
int hex2int(char *, int);
scalar_t__ strncmp(char *, char *, int);
unsigned short parse_address(char *str) {
  int res = 0xffff;
  if ((strncmp(str, "0x", 2) == 0) || (strncmp(str, "0X", 2) == 0)) {
    res = hex2int(&str[2], 0xffff);
  } else if (strncmp(str, "$", 1) == 0) {
    res = hex2int(&str[1], 0xffff);
  } else {
    res = atoi(str);
  }
  return res;
}


x86 assembly (clang, -O0):
<parse_address>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0xffff,-0xc(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xeb2(%rip),%rsi
mov    $0x2,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%rax
je     1181 <parse_address+0x51>
mov    -0x8(%rbp),%rdi
lea    0xe96(%rip),%rsi
mov    $0x2,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%rax
jne    119b <parse_address+0x6b>
mov    -0x8(%rbp),%rdi
add    $0x2,%rdi
mov    $0xffff,%esi
call   1040 <hex2int@plt>
mov    %eax,-0xc(%rbp)
jmp    11e5 <parse_address+0xb5>
mov    -0x8(%rbp),%rdi
lea    0xe60(%rip),%rsi
mov    $0x1,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%rax
jne    11d4 <parse_address+0xa4>
mov    -0x8(%rbp),%rdi
add    $0x1,%rdi
mov    $0xffff,%esi
call   1040 <hex2int@plt>
mov    %eax,-0xc(%rbp)
jmp    11e0 <parse_address+0xb0>
mov    -0x8(%rbp),%rdi
call   1050 <atoi@plt>
mov    %eax,-0xc(%rbp)
jmp    11e5 <parse_address+0xb5>
mov    -0xc(%rbp),%eax
movzwl %ax,%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<parse_address>:
push   %rbx
mov    %rdi,%rbx
lea    0xec5(%rip),%rsi
mov    $0x2,%edx
call   1030 <strncmp@plt>
test   %rax,%rax
je     1186 <parse_address+0x56>
lea    0xeb2(%rip),%rsi
mov    %rbx,%rdi
mov    $0x2,%edx
call   1030 <strncmp@plt>
test   %rax,%rax
je     1186 <parse_address+0x56>
lea    0xe9c(%rip),%rsi
mov    %rbx,%rdi
mov    $0x1,%edx
call   1030 <strncmp@plt>
test   %rax,%rax
je     118c <parse_address+0x5c>
mov    %rbx,%rdi
call   1050 <atoi@plt>
pop    %rbx
ret
add    $0x2,%rbx
jmp    1190 <parse_address+0x60>
add    $0x1,%rbx
mov    %rbx,%rdi
mov    $0xffff,%esi
call   1040 <hex2int@plt>
pop    %rbx
ret

LLVM IR (-O0):
@.str = [3 x i8] c"0x\00"
@.str.1 = [3 x i8] c"0X\00"
@.str.2 = [2 x i8] c"$\00"
define zeroext i16 @parse_address(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
store i8* %0, i8** %2
store i32 65535, i32* %3
%4 = load i8*, i8** %2
%5 = call i64 @strncmp(i8* %4, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 2)
%6 = icmp eq i64 %5, 0
br i1 %6, label %11, label %7
7: ; preds = %1
%8 = load i8*, i8** %2
%9 = call i64 @strncmp(i8* %8, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 2)
%10 = icmp eq i64 %9, 0
br i1 %10, label %11, label %15
11: ; preds = %7, %1
%12 = load i8*, i8** %2
%13 = getelementptr inbounds i8, i8* %12, i64 2
%14 = call i32 @hex2int(i8* %13, i32 65535)
store i32 %14, i32* %3
br label %27
15: ; preds = %7
%16 = load i8*, i8** %2
%17 = call i64 @strncmp(i8* %16, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i32 1)
%18 = icmp eq i64 %17, 0
br i1 %18, label %19, label %23
19: ; preds = %15
%20 = load i8*, i8** %2
%21 = getelementptr inbounds i8, i8* %20, i64 1
%22 = call i32 @hex2int(i8* %21, i32 65535)
store i32 %22, i32* %3
br label %26
23: ; preds = %15
%24 = load i8*, i8** %2
%25 = call i32 @atoi(i8* %24)
store i32 %25, i32* %3
br label %26
26: ; preds = %23, %19
br label %27
27: ; preds = %26, %11
%28 = load i32, i32* %3
%29 = trunc i32 %28 to i16
ret i16 %29
}

LLVM IR (-O2):
@.str = [3 x i8] c"0x\00"
@.str.1 = [3 x i8] c"0X\00"
@.str.2 = [2 x i8] c"$\00"
define zeroext i16 @parse_address(i8* %0) {
%2 = tail call i64 @strncmp(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 2)
%3 = icmp eq i64 %2, 0
br i1 %3, label %7, label %4
4: ; preds = %1
%5 = tail call i64 @strncmp(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 2)
%6 = icmp eq i64 %5, 0
br i1 %6, label %7, label %10
7: ; preds = %4, %1
%8 = getelementptr inbounds i8, i8* %0, i64 2
%9 = tail call i32 @hex2int(i8* %8, i32 65535)
br label %18
10: ; preds = %4
%11 = tail call i64 @strncmp(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i32 1)
%12 = icmp eq i64 %11, 0
br i1 %12, label %13, label %16
13: ; preds = %10
%14 = getelementptr inbounds i8, i8* %0, i64 1
%15 = tail call i32 @hex2int(i8* %14, i32 65535)
br label %18
16: ; preds = %10
%17 = tail call i32 @atoi(i8* %0)
br label %18
18: ; preds = %13, %16, %7
%19 = phi i32 [ %9, %7 ], [ %15, %13 ], [ %17, %16 ]
%20 = trunc i32 %19 to i16
ret i16 %20
}