#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 EOF;
int getchar();
int htoi(char *);
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ puts(char *);
int main() {
  puts("Enter a hexidecimal string and then press the end-of-file key (CTRL-D "
       "on UNIX)");
  char input[1000];
  int current_char = EOF;
  int index = 0;
  for (index; index < 1000 - 1 && (current_char = getchar()) != EOF; ++index) {
    input[index] = current_char;
  }
  input[index] = '\0';
  printf(" => %d\n", htoi(input));
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x400,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xea7(%rip),%rdi
call   1030 <puts@plt>
mov    0x2e83(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3f4(%rbp)
movl   $0x0,-0x3f8(%rbp)
xor    %eax,%eax
cmpl   $0x3e7,-0x3f8(%rbp)
mov    %al,-0x3f9(%rbp)
jge    11ae <main+0x6e>
mov    $0x0,%al
call   1050 <getchar@plt>
mov    %eax,-0x3f4(%rbp)
mov    0x2e45(%rip),%rcx
cmp    (%rcx),%eax
setne  %al
mov    %al,-0x3f9(%rbp)
mov    -0x3f9(%rbp),%al
test   $0x1,%al
jne    11c1 <main+0x81>
jmp    11eb <main+0xab>
mov    -0x3f4(%rbp),%eax
mov    %al,%cl
movslq -0x3f8(%rbp),%rax
mov    %cl,-0x3f0(%rbp,%rax,1)
mov    -0x3f8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x3f8(%rbp)
jmp    1177 <main+0x37>
movslq -0x3f8(%rbp),%rax
movb   $0x0,-0x3f0(%rbp,%rax,1)
lea    -0x3f0(%rbp),%rdi
call   1060 <htoi@plt>
mov    %eax,%esi
lea    0xe40(%rip),%rdi
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x400,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x3e8,%rsp
lea    0xeaf(%rip),%rdi
call   1030 <puts@plt>
mov    0x2e8b(%rip),%r14
xor    %ebx,%ebx
nop
xor    %eax,%eax
call   1050 <getchar@plt>
cmp    (%r14),%eax
je     1181 <main+0x41>
mov    %al,(%rsp,%rbx,1)
add    $0x1,%rbx
cmp    $0x3e7,%rbx
jne    1160 <main+0x20>
mov    $0x3e7,%ebx
mov    %ebx,%eax
movb   $0x0,(%rsp,%rax,1)
mov    %rsp,%rdi
call   1060 <htoi@plt>
lea    0xeb9(%rip),%rdi
mov    %eax,%esi
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x3e8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [79 x i8] c"Enter a hexidecimal string and then press the end-of-file key (CTRL-D on UNIX)\00"
@EOF = global i32 0
@.str.1 = [8 x i8] c" => %d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [1000 x i8]
%3 = alloca i32
%4 = alloca i32
store i32 0, i32* %1
%5 = call i32 @puts(i8* getelementptr inbounds ([79 x i8], [79 x i8]* @.str, i64 0, i64 0))
%6 = load i32, i32* @EOF
store i32 %6, i32* %3
store i32 0, i32* %4
%7 = load i32, i32* %4
br label %8
8: ; preds = %23, %0
%9 = load i32, i32* %4
%10 = icmp slt i32 %9, 999
br i1 %10, label %11, label %15
11: ; preds = %8
%12 = call i32 (...) @getchar()
store i32 %12, i32* %3
%13 = load i32, i32* @EOF
%14 = icmp ne i32 %12, %13
br label %15
15: ; preds = %11, %8
%16 = phi i1 [ false, %8 ], [ %14, %11 ]
br i1 %16, label %17, label %26
17: ; preds = %15
%18 = load i32, i32* %3
%19 = trunc i32 %18 to i8
%20 = load i32, i32* %4
%21 = sext i32 %20 to i64
%22 = getelementptr inbounds [1000 x i8], [1000 x i8]* %2, i64 0, i64 %21
store i8 %19, i8* %22
br label %23
23: ; preds = %17
%24 = load i32, i32* %4
%25 = add nsw i32 %24, 1
store i32 %25, i32* %4
br label %8
26: ; preds = %15
%27 = load i32, i32* %4
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds [1000 x i8], [1000 x i8]* %2, i64 0, i64 %28
store i8 0, i8* %29
%30 = getelementptr inbounds [1000 x i8], [1000 x i8]* %2, i64 0, i64 0
%31 = call i32 @htoi(i8* %30)
%32 = call i32 @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i32 %31)
ret i32 0
}

LLVM IR (-O2):
@.str = [79 x i8] c"Enter a hexidecimal string and then press the end-of-file key (CTRL-D on UNIX)\00"
@EOF = global i32 0
@.str.1 = [8 x i8] c" => %d\0A\00"
define i32 @main() {
%1 = alloca [1000 x i8]
%2 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([79 x i8], [79 x i8]* @.str, i64 0, i64 0))
%3 = getelementptr inbounds [1000 x i8], [1000 x i8]* %1, i64 0, i64 0
br label %4
4: ; preds = %0, %9
%5 = phi i64 [ 0, %0 ], [ %12, %9 ]
%6 = tail call i32 (...) @getchar()
%7 = load i32, i32* @EOF
%8 = icmp eq i32 %6, %7
br i1 %8, label %14, label %9
9: ; preds = %4
%10 = trunc i32 %6 to i8
%11 = getelementptr inbounds [1000 x i8], [1000 x i8]* %1, i64 0, i64 %5
store i8 %10, i8* %11
%12 = add nuw nsw i64 %5, 1
%13 = icmp eq i64 %12, 999
br i1 %13, label %14, label %4
14: ; preds = %9, %4
%15 = phi i64 [ 999, %9 ], [ %5, %4 ]
%16 = and i64 %15, 4294967295
%17 = getelementptr inbounds [1000 x i8], [1000 x i8]* %1, i64 0, i64 %16
store i8 0, i8* %17
%18 = call i32 @htoi(i8* %3)
%19 = call i32 @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i32 %18)
ret i32 0
}