#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 /*<<< orphan*/ free(char *);
scalar_t__ isdigit(char);
char *new_string_from(char *, int, int);
int wspace(char *, int);
int read_num(char *str, int *pos) {
  int start;
  int numchars;
  char *num;
  int result;
  start = wspace(str, *pos);
  numchars = start;
  while (isdigit(str[numchars])) {
    numchars++;
  }
  num = new_string_from(str, start, numchars - start);
  *pos = numchars;
  result = atoi(num);
  free(num);
  return result;
}


x86 assembly (clang, -O0):
<read_num>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    (%rax),%esi
call   1070 <wspace@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%rax
movslq -0x18(%rbp),%rcx
movsbl (%rax,%rcx,1),%edi
call   1040 <isdigit@plt>
cmp    $0x0,%rax
je     11a1 <read_num+0x51>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1178 <read_num+0x28>
mov    -0x8(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    -0x18(%rbp),%edx
sub    -0x14(%rbp),%edx
call   1050 <new_string_from@plt>
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x20(%rbp),%rdi
call   1060 <atoi@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_num>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rsi,%r14
mov    %rdi,%r15
mov    (%rsi),%esi
call   1070 <wspace@plt>
mov    %eax,%r12d
movslq %eax,%rbp
add    %r15,%rbp
mov    $0xffffffff,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
movsbl 0x0(%rbp),%edi
call   1040 <isdigit@plt>
add    $0x1,%ebx
add    $0x1,%rbp
test   %rax,%rax
jne    1180 <read_num+0x30>
mov    %r12d,%ebp
add    %ebx,%ebp
mov    %r15,%rdi
mov    %r12d,%esi
mov    %ebx,%edx
call   1050 <new_string_from@plt>
mov    %rax,%rbx
mov    %ebp,(%r14)
mov    %rax,%rdi
call   1060 <atoi@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_num(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 i8*, i8** %3
%10 = load i32*, i32** %4
%11 = load i32, i32* %10
%12 = call i32 @wspace(i8* %9, i32 %11)
store i32 %12, i32* %5
%13 = load i32, i32* %5
store i32 %13, i32* %6
br label %14
14: ; preds = %22, %2
%15 = load i8*, i8** %3
%16 = load i32, i32* %6
%17 = sext i32 %16 to i64
%18 = getelementptr inbounds i8, i8* %15, i64 %17
%19 = load i8, i8* %18
%20 = call i64 @isdigit(i8 signext %19)
%21 = icmp ne i64 %20, 0
br i1 %21, label %22, label %25
22: ; preds = %14
%23 = load i32, i32* %6
%24 = add nsw i32 %23, 1
store i32 %24, i32* %6
br label %14
25: ; preds = %14
%26 = load i8*, i8** %3
%27 = load i32, i32* %5
%28 = load i32, i32* %6
%29 = load i32, i32* %5
%30 = sub nsw i32 %28, %29
%31 = call i8* @new_string_from(i8* %26, i32 %27, i32 %30)
store i8* %31, i8** %7
%32 = load i32, i32* %6
%33 = load i32*, i32** %4
store i32 %32, i32* %33
%34 = load i8*, i8** %7
%35 = call i32 @atoi(i8* %34)
store i32 %35, i32* %8
%36 = load i8*, i8** %7
%37 = call i32 @free(i8* %36)
%38 = load i32, i32* %8
ret i32 %38
}

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