#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*/ assert(int /*<<< orphan*/);
int /*<<< orphan*/ log_debug(char *, ...);
int strtoint(char *, int);
short readaddress(const char *line, int *error) {
  assert(line != NULL);
  char hexaddr[4] = {line[0], line[1], line[2], '\0'};
  *error = 0;
  int memaddr = strtoint(hexaddr, 16);
  if (memaddr == -1) {
    log_debug("error converting string '%s' to an address", hexaddr);
    *error = 1;
    return -1;
  }
  if (memaddr > 65535) {
    log_debug("address is too large: %d", memaddr);
    *error = 1;
    return -1;
  }
  return (short)memaddr;
}


x86 assembly (clang, -O0):
<readaddress>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%al
mov    %al,-0x1c(%rbp)
mov    -0x10(%rbp),%rax
mov    0x1(%rax),%al
mov    %al,-0x1b(%rbp)
mov    -0x10(%rbp),%rax
mov    0x2(%rax),%al
mov    %al,-0x1a(%rbp)
movb   $0x0,-0x19(%rbp)
mov    -0x18(%rbp),%rax
movl   $0x0,(%rax)
lea    -0x1c(%rbp),%rdi
mov    $0x10,%esi
call   1030 <strtoint@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0xffffffff,-0x20(%rbp)
jne    11bf <readaddress+0x8f>
lea    -0x1c(%rbp),%rsi
lea    0xe5d(%rip),%rdi
mov    $0x0,%al
call   1040 <log_debug@plt>
mov    -0x18(%rbp),%rax
movl   $0x1,(%rax)
movw   $0xffff,-0x2(%rbp)
jmp    11f9 <readaddress+0xc9>
cmpl   $0xffff,-0x20(%rbp)
jle    11f2 <readaddress+0xc2>
mov    -0x20(%rbp),%esi
lea    0xe55(%rip),%rdi
mov    $0x0,%al
call   1040 <log_debug@plt>
mov    -0x18(%rbp),%rax
movl   $0x1,(%rax)
movw   $0xffff,-0x2(%rbp)
jmp    11f9 <readaddress+0xc9>
mov    -0x20(%rbp),%eax
mov    %ax,-0x2(%rbp)
movswl -0x2(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readaddress>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbx
xor    %edi,%edi
test   %rbx,%rbx
setne  %dil
call   1050 <assert@plt>
mov    (%rbx),%al
mov    %al,0x4(%rsp)
mov    0x1(%rbx),%al
mov    %al,0x5(%rsp)
mov    0x2(%rbx),%al
mov    %al,0x6(%rsp)
movb   $0x0,0x7(%rsp)
movl   $0x0,(%r14)
lea    0x4(%rsp),%rdi
mov    $0x10,%esi
call   1030 <strtoint@plt>
cmp    $0xffffffff,%eax
je     1195 <readaddress+0x65>
cmp    $0x10000,%eax
jl     11b3 <readaddress+0x83>
lea    0xea1(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <log_debug@plt>
jmp    11a8 <readaddress+0x78>
lea    0xe64(%rip),%rdi
lea    0x4(%rsp),%rsi
xor    %eax,%eax
call   1040 <log_debug@plt>
movl   $0x1,(%r14)
mov    $0xffff,%ax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [43 x i8] c"error converting string '%s' to an address\00"
@.str.1 = [25 x i8] c"address is too large: %d\00"
define signext i16 @readaddress(i8* %0, i32* %1) {
%3 = alloca i16
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca [4 x i8]
%7 = alloca i32
store i8* %0, i8** %4
store i32* %1, i32** %5
%8 = load i8*, i8** %4
%9 = icmp ne i8* %8, null
%10 = zext i1 %9 to i32
%11 = call i32 @assert(i32 %10)
%12 = getelementptr inbounds [4 x i8], [4 x i8]* %6, i64 0, i64 0
%13 = load i8*, i8** %4
%14 = getelementptr inbounds i8, i8* %13, i64 0
%15 = load i8, i8* %14
store i8 %15, i8* %12
%16 = getelementptr inbounds i8, i8* %12, i64 1
%17 = load i8*, i8** %4
%18 = getelementptr inbounds i8, i8* %17, i64 1
%19 = load i8, i8* %18
store i8 %19, i8* %16
%20 = getelementptr inbounds i8, i8* %16, i64 1
%21 = load i8*, i8** %4
%22 = getelementptr inbounds i8, i8* %21, i64 2
%23 = load i8, i8* %22
store i8 %23, i8* %20
%24 = getelementptr inbounds i8, i8* %20, i64 1
store i8 0, i8* %24
%25 = load i32*, i32** %5
store i32 0, i32* %25
%26 = getelementptr inbounds [4 x i8], [4 x i8]* %6, i64 0, i64 0
%27 = call i32 @strtoint(i8* %26, i32 16)
store i32 %27, i32* %7
%28 = load i32, i32* %7
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %34
30: ; preds = %2
%31 = getelementptr inbounds [4 x i8], [4 x i8]* %6, i64 0, i64 0
%32 = call i32 (i8*, ...) @log_debug(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str, i64 0, i64 0), i8* %31)
%33 = load i32*, i32** %5
store i32 1, i32* %33
store i16 -1, i16* %3
br label %44
34: ; preds = %2
%35 = load i32, i32* %7
%36 = icmp sgt i32 %35, 65535
br i1 %36, label %37, label %41
37: ; preds = %34
%38 = load i32, i32* %7
%39 = call i32 (i8*, ...) @log_debug(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i32 %38)
%40 = load i32*, i32** %5
store i32 1, i32* %40
store i16 -1, i16* %3
br label %44
41: ; preds = %34
%42 = load i32, i32* %7
%43 = trunc i32 %42 to i16
store i16 %43, i16* %3
br label %44
44: ; preds = %41, %37, %30
%45 = load i16, i16* %3
ret i16 %45
}

LLVM IR (-O2):
@.str = [43 x i8] c"error converting string '%s' to an address\00"
@.str.1 = [25 x i8] c"address is too large: %d\00"
define signext i16 @readaddress(i8* %0, i32* nocapture writeonly %1) {
%3 = alloca [4 x i8]
%4 = icmp ne i8* %0, null
%5 = zext i1 %4 to i32
%6 = tail call i32 @assert(i32 %5)
%7 = getelementptr inbounds [4 x i8], [4 x i8]* %3, i64 0, i64 0
%8 = load i8, i8* %0
store i8 %8, i8* %7
%9 = getelementptr inbounds [4 x i8], [4 x i8]* %3, i64 0, i64 1
%10 = getelementptr inbounds i8, i8* %0, i64 1
%11 = load i8, i8* %10
store i8 %11, i8* %9
%12 = getelementptr inbounds [4 x i8], [4 x i8]* %3, i64 0, i64 2
%13 = getelementptr inbounds i8, i8* %0, i64 2
%14 = load i8, i8* %13
store i8 %14, i8* %12
%15 = getelementptr inbounds [4 x i8], [4 x i8]* %3, i64 0, i64 3
store i8 0, i8* %15
store i32 0, i32* %1
%16 = call i32 @strtoint(i8* %7, i32 16)
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %20
18: ; preds = %2
%19 = call i32 (i8*, ...) @log_debug(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str, i64 0, i64 0), i8* %7)
store i32 1, i32* %1
br label %26
20: ; preds = %2
%21 = icmp sgt i32 %16, 65535
br i1 %21, label %22, label %24
22: ; preds = %20
%23 = call i32 (i8*, ...) @log_debug(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i32 %16)
store i32 1, i32* %1
br label %26
24: ; preds = %20
%25 = trunc i32 %16 to i16
br label %26
26: ; preds = %24, %22, %18
%27 = phi i16 [ -1, %18 ], [ -1, %22 ], [ %25, %24 ]
ret i16 %27
}