#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 *);
char *spalloc(size_t);
int /*<<< orphan*/ spfree(char *);
int sp_extract_positional_int(const char *from, size_t size) {
  int result;
  char *buf = spalloc(size + 1);
  char *ptr = buf;
  size_t i = 0;
  while (i < size && *from) {
    *ptr++ = *from++;
    ++i;
  }
  buf[size] = '\0';
  result = atoi(buf);
  spfree(buf);
  return result;
}


x86 assembly (clang, -O0):
<sp_extract_positional_int>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
add    $0x1,%rdi
call   1040 <spalloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
mov    -0x30(%rbp),%rcx
xor    %eax,%eax
cmp    -0x10(%rbp),%rcx
mov    %al,-0x31(%rbp)
jae    1184 <sp_extract_positional_int+0x54>
mov    -0x8(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
setne  %al
mov    %al,-0x31(%rbp)
mov    -0x31(%rbp),%al
test   $0x1,%al
jne    1194 <sp_extract_positional_int+0x64>
jmp    11c7 <sp_extract_positional_int+0x97>
mov    -0x8(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x8(%rbp)
mov    (%rax),%cl
mov    -0x28(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x28(%rbp)
mov    %cl,(%rax)
mov    -0x30(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x30(%rbp)
jmp    1161 <sp_extract_positional_int+0x31>
mov    -0x20(%rbp),%rax
mov    -0x10(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x20(%rbp),%rdi
call   1050 <atoi@plt>
mov    %eax,-0x14(%rbp)
mov    -0x20(%rbp),%rdi
call   1030 <spfree@plt>
mov    -0x14(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sp_extract_positional_int>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%r15
lea    0x1(%rsi),%rdi
call   1040 <spalloc@plt>
mov    %rax,%rbx
test   %r14,%r14
je     1165 <sp_extract_positional_int+0x35>
xor    %eax,%eax
movzbl (%r15,%rax,1),%ecx
test   %cl,%cl
je     1165 <sp_extract_positional_int+0x35>
mov    %cl,(%rbx,%rax,1)
add    $0x1,%rax
cmp    %rax,%r14
jne    1150 <sp_extract_positional_int+0x20>
movb   $0x0,(%rbx,%r14,1)
mov    %rbx,%rdi
call   1050 <atoi@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1030 <spfree@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @sp_extract_positional_int(i8* %0, i64 %1) {
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i64
store i8* %0, i8** %3
store i64 %1, i64* %4
%9 = load i64, i64* %4
%10 = add i64 %9, 1
%11 = call i8* @spalloc(i64 %10)
store i8* %11, i8** %6
%12 = load i8*, i8** %6
store i8* %12, i8** %7
store i64 0, i64* %8
br label %13
13: ; preds = %24, %2
%14 = load i64, i64* %8
%15 = load i64, i64* %4
%16 = icmp ult i64 %14, %15
br i1 %16, label %17, label %22
17: ; preds = %13
%18 = load i8*, i8** %3
%19 = load i8, i8* %18
%20 = sext i8 %19 to i32
%21 = icmp ne i32 %20, 0
br label %22
22: ; preds = %17, %13
%23 = phi i1 [ false, %13 ], [ %21, %17 ]
br i1 %23, label %24, label %32
24: ; preds = %22
%25 = load i8*, i8** %3
%26 = getelementptr inbounds i8, i8* %25, i32 1
store i8* %26, i8** %3
%27 = load i8, i8* %25
%28 = load i8*, i8** %7
%29 = getelementptr inbounds i8, i8* %28, i32 1
store i8* %29, i8** %7
store i8 %27, i8* %28
%30 = load i64, i64* %8
%31 = add i64 %30, 1
store i64 %31, i64* %8
br label %13
32: ; preds = %22
%33 = load i8*, i8** %6
%34 = load i64, i64* %4
%35 = getelementptr inbounds i8, i8* %33, i64 %34
store i8 0, i8* %35
%36 = load i8*, i8** %6
%37 = call i32 @atoi(i8* %36)
store i32 %37, i32* %5
%38 = load i8*, i8** %6
%39 = call i32 @spfree(i8* %38)
%40 = load i32, i32* %5
ret i32 %40
}

LLVM IR (-O2):
define i32 @sp_extract_positional_int(i8* nocapture %0, i64 %1) {
%3 = add i64 %1, 1
%4 = tail call i8* @spalloc(i64 %3)
%5 = icmp eq i64 %1, 0
br i1 %5, label %17, label %6
6: ; preds = %2, %12
%7 = phi i64 [ %15, %12 ], [ 0, %2 ]
%8 = phi i8* [ %14, %12 ], [ %4, %2 ]
%9 = phi i8* [ %13, %12 ], [ %0, %2 ]
%10 = load i8, i8* %9
%11 = icmp eq i8 %10, 0
br i1 %11, label %17, label %12
12: ; preds = %6
%13 = getelementptr inbounds i8, i8* %9, i64 1
%14 = getelementptr inbounds i8, i8* %8, i64 1
store i8 %10, i8* %8
%15 = add nuw i64 %7, 1
%16 = icmp eq i64 %15, %1
br i1 %16, label %17, label %6
17: ; preds = %6, %12, %2
%18 = getelementptr inbounds i8, i8* %4, i64 %1
store i8 0, i8* %18
%19 = tail call i32 @atoi(i8* %4)
%20 = tail call i32 @spfree(i8* %4)
ret i32 %19
}