#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*/ free(char *);
int get_numvar(char *);
scalar_t__ isalpha(char);
char *new_string_from(char *, int, int);
int read_var(char *str, int *pos) {
  int start;
  int numchars;
  char *var;
  int result;
  start = *pos;
  numchars = start + 1;
  while (isalpha(str[numchars])) {
    numchars++;
  }
  var = new_string_from(str, start, numchars - start);
  *pos = numchars;
  result = get_numvar(var);
  free(var);
  return result;
}


x86 assembly (clang, -O0):
<read_var>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%rax
movslq -0x18(%rbp),%rcx
movsbl (%rax,%rcx,1),%edi
call   1050 <isalpha@plt>
cmp    $0x0,%rax
je     118b <read_var+0x4b>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1162 <read_var+0x22>
mov    -0x8(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    -0x18(%rbp),%edx
sub    -0x14(%rbp),%edx
call   1060 <new_string_from@plt>
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x20(%rbp),%rdi
call   1040 <get_numvar@plt>
mov    %eax,-0x24(%rbp)
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x24(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_var>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rsi,%r14
mov    %rdi,%r15
movslq (%rsi),%rax
mov    %eax,%r12d
lea    (%rax,%rdi,1),%rbp
add    $0x1,%rbp
xor    %ebx,%ebx
xchg   %ax,%ax
movsbl 0x0(%rbp,%rbx,1),%edi
call   1050 <isalpha@plt>
add    $0x1,%rbx
test   %rax,%rax
jne    1160 <read_var+0x20>
lea    (%r12,%rbx,1),%ebp
mov    %r15,%rdi
mov    %r12d,%esi
mov    %ebx,%edx
call   1060 <new_string_from@plt>
mov    %rax,%rbx
mov    %ebp,(%r14)
mov    %rax,%rdi
call   1040 <get_numvar@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %ebp,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @read_var(i8* %0, i32* %1) {
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
store i8* %0, i8** %3
store i32* %1, i32** %4
%9 = load i32*, i32** %4
%10 = load i32, i32* %9
store i32 %10, i32* %5
%11 = load i32, i32* %5
%12 = add nsw i32 %11, 1
store i32 %12, i32* %6
br label %13
13: ; preds = %21, %2
%14 = load i8*, i8** %3
%15 = load i32, i32* %6
%16 = sext i32 %15 to i64
%17 = getelementptr inbounds i8, i8* %14, i64 %16
%18 = load i8, i8* %17
%19 = call i64 @isalpha(i8 signext %18)
%20 = icmp ne i64 %19, 0
br i1 %20, label %21, label %24
21: ; preds = %13
%22 = load i32, i32* %6
%23 = add nsw i32 %22, 1
store i32 %23, i32* %6
br label %13
24: ; preds = %13
%25 = load i8*, i8** %3
%26 = load i32, i32* %5
%27 = load i32, i32* %6
%28 = load i32, i32* %5
%29 = sub nsw i32 %27, %28
%30 = call i8* @new_string_from(i8* %25, i32 %26, i32 %29)
store i8* %30, i8** %7
%31 = load i32, i32* %6
%32 = load i32*, i32** %4
store i32 %31, i32* %32
%33 = load i8*, i8** %7
%34 = call i32 @get_numvar(i8* %33)
store i32 %34, i32* %8
%35 = load i8*, i8** %7
%36 = call i32 @free(i8* %35)
%37 = load i32, i32* %8
ret i32 %37
}

LLVM IR (-O2):
define i32 @read_var(i8* %0, i32* nocapture %1) {
%3 = load i32, i32* %1
%4 = sext i32 %3 to i64
br label %5
5: ; preds = %5, %2
%6 = phi i64 [ %7, %5 ], [ %4, %2 ]
%7 = add nsw i64 %6, 1
%8 = getelementptr inbounds i8, i8* %0, i64 %7
%9 = load i8, i8* %8
%10 = tail call i64 @isalpha(i8 signext %9)
%11 = icmp eq i64 %10, 0
br i1 %11, label %12, label %5
12: ; preds = %5
%13 = trunc i64 %7 to i32
%14 = sub nsw i32 %13, %3
%15 = tail call i8* @new_string_from(i8* %0, i32 %3, i32 %14)
store i32 %13, i32* %1
%16 = tail call i32 @get_numvar(i8* %15)
%17 = tail call i32 @free(i8* %15)
ret i32 %16
}