#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 */
char *strchr(char *, char);
int strlen(char *);
int /*<<< orphan*/ strspn(char *, char *);
char *tgetstr(char *, char *);
int extract(char *cap, char *buf) {
  static char *bp;
  char *d = buf + strlen(buf);
  char *s = tgetstr(cap, (bp = d, &bp));
  int len;
  if (!s)
    return (*bp = 0);
  s += strspn(s, "0123456789*");
  for (len = 0; *s; *d++ = *s++, len++)
    if (*s == '$' && *(s + 1) == '<')
      if (!(s = strchr(s, '>')) || !*++s)
        break;
  *d = 0;
  return len;
}


x86 assembly (clang, -O0):
<extract>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x38(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rax
mov    %rax,0x2ec8(%rip)
lea    0x2ec1(%rip),%rsi
call   1060 <tgetstr@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11b1 <extract+0x71>
mov    0x2ea6(%rip),%rax
movb   $0x0,(%rax)
movl   $0x0,-0x4(%rbp)
jmp    1287 <extract+0x147>
mov    -0x28(%rbp),%rdi
lea    0xe44(%rip),%rsi
call   1050 <strspn@plt>
mov    %eax,%ecx
mov    -0x28(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x28(%rbp),%rax
cmpb   $0x0,(%rax)
je     127a <extract+0x13a>
mov    -0x28(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x24,%eax
jne    1245 <extract+0x105>
mov    -0x28(%rbp),%rax
movsbl 0x1(%rax),%eax
cmp    $0x3c,%eax
jne    1245 <extract+0x105>
mov    -0x28(%rbp),%rdi
mov    $0x3e,%esi
call   1040 <strchr@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
je     123b <extract+0xfb>
mov    -0x28(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x28(%rbp)
cmpb   $0x0,0x1(%rax)
jne    1240 <extract+0x100>
jmp    127a <extract+0x13a>
jmp    1245 <extract+0x105>
jmp    124a <extract+0x10a>
mov    -0x28(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x28(%rbp)
mov    (%rax),%cl
mov    -0x20(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x20(%rbp)
mov    %cl,(%rax)
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    11d8 <extract+0x98>
mov    -0x20(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<extract>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%rbx
mov    %rdi,%r14
mov    %rsi,%rdi
call   1030 <strlen@plt>
movslq %eax,%r15
add    %rbx,%r15
mov    %r15,0x2ee8(%rip)
lea    0x2ee1(%rip),%rsi
mov    %r14,%rdi
call   1060 <tgetstr@plt>
test   %rax,%rax
je     11d5 <extract+0x95>
mov    %rax,%rbx
lea    0xe82(%rip),%rsi
mov    %rax,%rdi
call   1050 <strspn@plt>
cltq
add    %rbx,%rax
xor    %ebx,%ebx
jmp    11a0 <extract+0x60>
nop
test   %cl,%cl
je     11d0 <extract+0x90>
add    $0x1,%rax
mov    %cl,(%r15,%rbx,1)
add    $0x1,%rbx
movzbl (%rax),%ecx
cmp    $0x24,%cl
jne    1190 <extract+0x50>
mov    $0x24,%cl
cmpb   $0x3c,0x1(%rax)
jne    1194 <extract+0x54>
mov    %rax,%rdi
mov    $0x3e,%esi
call   1040 <strchr@plt>
test   %rax,%rax
je     11d0 <extract+0x90>
movzbl 0x1(%rax),%ecx
test   %cl,%cl
je     11d0 <extract+0x90>
add    $0x1,%rax
jmp    1194 <extract+0x54>
add    %rbx,%r15
jmp    11de <extract+0x9e>
xor    %ebx,%ebx
mov    0x2e6a(%rip),%r15
movb   $0x0,(%r15)
mov    %ebx,%eax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@extract.bp = internal global i8* null
@.str = [12 x i8] c"0123456789*\00"
define i32 @extract(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
%9 = load i8*, i8** %5
%10 = load i8*, i8** %5
%11 = call i32 @strlen(i8* %10)
%12 = sext i32 %11 to i64
%13 = getelementptr inbounds i8, i8* %9, i64 %12
store i8* %13, i8** %6
%14 = load i8*, i8** %4
%15 = load i8*, i8** %6
store i8* %15, i8** @extract.bp
%16 = call i8* @tgetstr(i8* %14, i8* bitcast (i8** @extract.bp to i8*))
store i8* %16, i8** %7
%17 = load i8*, i8** %7
%18 = icmp ne i8* %17, null
br i1 %18, label %21, label %19
19: ; preds = %2
%20 = load i8*, i8** @extract.bp
store i8 0, i8* %20
store i32 0, i32* %3
br label %65
21: ; preds = %2
%22 = load i8*, i8** %7
%23 = call i32 @strspn(i8* %22, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%24 = load i8*, i8** %7
%25 = sext i32 %23 to i64
%26 = getelementptr inbounds i8, i8* %24, i64 %25
store i8* %26, i8** %7
store i32 0, i32* %8
br label %27
27: ; preds = %54, %21
%28 = load i8*, i8** %7
%29 = load i8, i8* %28
%30 = icmp ne i8 %29, 0
br i1 %30, label %31, label %62
31: ; preds = %27
%32 = load i8*, i8** %7
%33 = load i8, i8* %32
%34 = sext i8 %33 to i32
%35 = icmp eq i32 %34, 36
br i1 %35, label %36, label %53
36: ; preds = %31
%37 = load i8*, i8** %7
%38 = getelementptr inbounds i8, i8* %37, i64 1
%39 = load i8, i8* %38
%40 = sext i8 %39 to i32
%41 = icmp eq i32 %40, 60
br i1 %41, label %42, label %53
42: ; preds = %36
%43 = load i8*, i8** %7
%44 = call i8* @strchr(i8* %43, i8 signext 62)
store i8* %44, i8** %7
%45 = icmp ne i8* %44, null
br i1 %45, label %46, label %51
46: ; preds = %42
%47 = load i8*, i8** %7
%48 = getelementptr inbounds i8, i8* %47, i32 1
store i8* %48, i8** %7
%49 = load i8, i8* %48
%50 = icmp ne i8 %49, 0
br i1 %50, label %52, label %51
51: ; preds = %46, %42
br label %62
52: ; preds = %46
br label %53
53: ; preds = %52, %36, %31
br label %54
54: ; preds = %53
%55 = load i8*, i8** %7
%56 = getelementptr inbounds i8, i8* %55, i32 1
store i8* %56, i8** %7
%57 = load i8, i8* %55
%58 = load i8*, i8** %6
%59 = getelementptr inbounds i8, i8* %58, i32 1
store i8* %59, i8** %6
store i8 %57, i8* %58
%60 = load i32, i32* %8
%61 = add nsw i32 %60, 1
store i32 %61, i32* %8
br label %27
62: ; preds = %51, %27
%63 = load i8*, i8** %6
store i8 0, i8* %63
%64 = load i32, i32* %8
store i32 %64, i32* %3
br label %65
65: ; preds = %62, %19
%66 = load i32, i32* %3
ret i32 %66
}

LLVM IR (-O2):
@extract.bp = internal global i8* null
@.str = [12 x i8] c"0123456789*\00"
define i32 @extract(i8* %0, i8* %1) {
%3 = tail call i32 @strlen(i8* %1)
%4 = sext i32 %3 to i64
%5 = getelementptr inbounds i8, i8* %1, i64 %4
store i8* %5, i8** @extract.bp
%6 = tail call i8* @tgetstr(i8* %0, i8* bitcast (i8** @extract.bp to i8*))
%7 = icmp eq i8* %6, null
br i1 %7, label %8, label %10
8: ; preds = %2
%9 = load i8*, i8** @extract.bp
br label %36
10: ; preds = %2
%11 = tail call i32 @strspn(i8* %6, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%12 = sext i32 %11 to i64
%13 = getelementptr inbounds i8, i8* %6, i64 %12
br label %14
14: ; preds = %30, %10
%15 = phi i8* [ %5, %10 ], [ %34, %30 ]
%16 = phi i8* [ %13, %10 ], [ %33, %30 ]
%17 = phi i32 [ 0, %10 ], [ %35, %30 ]
%18 = load i8, i8* %16
switch i8 %18, label %30 [
i8 0, label %36
i8 36, label %19
]
19: ; preds = %14
%20 = getelementptr inbounds i8, i8* %16, i64 1
%21 = load i8, i8* %20
%22 = icmp eq i8 %21, 60
br i1 %22, label %23, label %30
23: ; preds = %19
%24 = tail call i8* @strchr(i8* %16, i8 signext 62)
%25 = icmp eq i8* %24, null
br i1 %25, label %36, label %26
26: ; preds = %23
%27 = getelementptr inbounds i8, i8* %24, i64 1
%28 = load i8, i8* %27
%29 = icmp eq i8 %28, 0
br i1 %29, label %36, label %30
30: ; preds = %14, %19, %26
%31 = phi i8 [ %28, %26 ], [ 36, %19 ], [ %18, %14 ]
%32 = phi i8* [ %27, %26 ], [ %16, %19 ], [ %16, %14 ]
%33 = getelementptr inbounds i8, i8* %32, i64 1
%34 = getelementptr inbounds i8, i8* %15, i64 1
store i8 %31, i8* %15
%35 = add nuw nsw i32 %17, 1
br label %14
36: ; preds = %26, %23, %14, %8
%37 = phi i8* [ %9, %8 ], [ %15, %14 ], [ %15, %23 ], [ %15, %26 ]
%38 = phi i32 [ 0, %8 ], [ %17, %14 ], [ %17, %23 ], [ %17, %26 ]
store i8 0, i8* %37
ret i32 %38
}