#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 */
scalar_t__ isdigit(char);
scalar_t__ strtol(char *, char **, int);
int str_extract_int(char *str) {
  int num = 0;
  char *p = str;
  if (!p) {
    return 0;
  }
  while (*p) {
    if (isdigit(*p)) {
      num = (int)strtol(p, &p, 10);
    } else {
      p++;
    }
  }
  return num;
}


x86 assembly (clang, -O0):
<str_extract_int>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1152 <str_extract_int+0x32>
movl   $0x0,-0x4(%rbp)
jmp    11ab <str_extract_int+0x8b>
jmp    1157 <str_extract_int+0x37>
mov    -0x20(%rbp),%rax
cmpb   $0x0,(%rax)
je     11a5 <str_extract_int+0x85>
mov    -0x20(%rbp),%rax
movsbl (%rax),%edi
call   1030 <isdigit@plt>
cmp    $0x0,%rax
je     1194 <str_extract_int+0x74>
mov    -0x20(%rbp),%rdi
lea    -0x20(%rbp),%rsi
mov    $0xa,%edx
call   1040 <strtol@plt>
mov    %eax,-0x14(%rbp)
jmp    11a0 <str_extract_int+0x80>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    1157 <str_extract_int+0x37>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<str_extract_int>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdi,0x8(%rsp)
xor    %r14d,%r14d
test   %rdi,%rdi
je     1187 <str_extract_int+0x67>
mov    %rdi,%rbx
mov    (%rdi),%al
test   %al,%al
je     1187 <str_extract_int+0x67>
xor    %r14d,%r14d
lea    0x8(%rsp),%r15
jmp    1160 <str_extract_int+0x40>
nopl   0x0(%rax)
add    $0x1,%rbx
mov    %rbx,0x8(%rsp)
movzbl (%rbx),%eax
test   %al,%al
je     1187 <str_extract_int+0x67>
movsbl %al,%edi
call   1030 <isdigit@plt>
test   %rax,%rax
je     1150 <str_extract_int+0x30>
mov    %rbx,%rdi
mov    %r15,%rsi
mov    $0xa,%edx
call   1040 <strtol@plt>
mov    %rax,%r14
mov    0x8(%rsp),%rbx
jmp    1159 <str_extract_int+0x39>
mov    %r14d,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i32 @str_extract_int(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i8*
store i8* %0, i8** %3
store i32 0, i32* %4
%6 = load i8*, i8** %3
store i8* %6, i8** %5
%7 = load i8*, i8** %5
%8 = icmp ne i8* %7, null
br i1 %8, label %10, label %9
9: ; preds = %1
store i32 0, i32* %2
br label %30
10: ; preds = %1
br label %11
11: ; preds = %27, %10
%12 = load i8*, i8** %5
%13 = load i8, i8* %12
%14 = icmp ne i8 %13, 0
br i1 %14, label %15, label %28
15: ; preds = %11
%16 = load i8*, i8** %5
%17 = load i8, i8* %16
%18 = call i64 @isdigit(i8 signext %17)
%19 = icmp ne i64 %18, 0
br i1 %19, label %20, label %24
20: ; preds = %15
%21 = load i8*, i8** %5
%22 = call i64 @strtol(i8* %21, i8** %5, i32 10)
%23 = trunc i64 %22 to i32
store i32 %23, i32* %4
br label %27
24: ; preds = %15
%25 = load i8*, i8** %5
%26 = getelementptr inbounds i8, i8* %25, i32 1
store i8* %26, i8** %5
br label %27
27: ; preds = %24, %20
br label %11
28: ; preds = %11
%29 = load i32, i32* %4
store i32 %29, i32* %2
br label %30
30: ; preds = %28, %9
%31 = load i32, i32* %2
ret i32 %31
}

LLVM IR (-O2):
define i32 @str_extract_int(i8* %0) {
%2 = alloca i8*
%3 = bitcast i8** %2 to i8*
store i8* %0, i8** %2
%4 = icmp eq i8* %0, null
br i1 %4, label %25, label %5
5: ; preds = %1
%6 = load i8, i8* %0
%7 = icmp eq i8 %6, 0
br i1 %7, label %25, label %8
8: ; preds = %5, %20
%9 = phi i8 [ %23, %20 ], [ %6, %5 ]
%10 = phi i8* [ %21, %20 ], [ %0, %5 ]
%11 = phi i32 [ %22, %20 ], [ 0, %5 ]
%12 = tail call i64 @isdigit(i8 signext %9)
%13 = icmp eq i64 %12, 0
br i1 %13, label %18, label %14
14: ; preds = %8
%15 = call i64 @strtol(i8* %10, i8** %2, i32 10)
%16 = trunc i64 %15 to i32
%17 = load i8*, i8** %2
br label %20
18: ; preds = %8
%19 = getelementptr inbounds i8, i8* %10, i64 1
store i8* %19, i8** %2
br label %20
20: ; preds = %18, %14
%21 = phi i8* [ %17, %14 ], [ %19, %18 ]
%22 = phi i32 [ %16, %14 ], [ %11, %18 ]
%23 = load i8, i8* %21
%24 = icmp eq i8 %23, 0
br i1 %24, label %25, label %8
25: ; preds = %20, %5, %1
%26 = phi i32 [ 0, %1 ], [ 0, %5 ], [ %22, %20 ]
ret i32 %26
}