#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__ isalpha(char const);
scalar_t__ isdigit(char const);
char *malloc(int);
size_t strlen(char const *);
char *phone_number_clean(const char *input) {
  int cnt = 0;
  bool fail = false;
  char *result = malloc(10);
  for (size_t i = 0; i < strlen(input); ++i) {
    if (isdigit(input[i]))
      result[cnt++] = input[i];
    else {
      if (isalpha(input[i])) {
        fail = true;
        break;
      }
    }
  }
  result[cnt] = '\0';
  int len = strlen(result);
  if (fail || len < 10 || len > 11 || (len > 10 && result[0] != '1')) {
    size_t i;
    for (i = 0; i < 10; ++i)
      result[i] = '0';
    result[i] = '0';
    result[i + 1] = '\0';
  }
  len = strlen(result);
  if (len == 11) {
    size_t i;
    for (i = 1; i < strlen(result); ++i)
      result[i - 1] = result[i];
    result[i - 1] = '\0';
  }
  return result;
}


x86 assembly (clang, -O0):
<phone_number_clean>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
mov    $0xa,%edi
call   1050 <malloc@plt>
mov    %rax,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,%rcx
mov    -0x40(%rbp),%rax
cmp    %rcx,%rax
jae    1213 <phone_number_clean+0xd3>
mov    -0x8(%rbp),%rax
mov    -0x20(%rbp),%rcx
movsbl (%rax,%rcx,1),%edi
call   1030 <isdigit@plt>
cmp    $0x0,%rax
je     11d1 <phone_number_clean+0x91>
mov    -0x8(%rbp),%rax
mov    -0x20(%rbp),%rcx
mov    (%rax,%rcx,1),%dl
mov    -0x18(%rbp),%rax
mov    -0xc(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0xc(%rbp)
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
jmp    11fd <phone_number_clean+0xbd>
mov    -0x8(%rbp),%rax
mov    -0x20(%rbp),%rcx
movsbl (%rax,%rcx,1),%edi
call   1060 <isalpha@plt>
cmp    $0x0,%rax
je     11f8 <phone_number_clean+0xb8>
movl   $0x1,-0x10(%rbp)
jmp    1213 <phone_number_clean+0xd3>
jmp    11fd <phone_number_clean+0xbd>
jmp    1202 <phone_number_clean+0xc2>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    1170 <phone_number_clean+0x30>
mov    -0x18(%rbp),%rax
movslq -0xc(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x10(%rbp)
jne    1263 <phone_number_clean+0x123>
cmpl   $0xa,-0x24(%rbp)
jl     1263 <phone_number_clean+0x123>
cmpl   $0xb,-0x24(%rbp)
jg     1263 <phone_number_clean+0x123>
cmpl   $0xa,-0x24(%rbp)
jle    12ac <phone_number_clean+0x16c>
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x31,%eax
je     12ac <phone_number_clean+0x16c>
movq   $0x0,-0x30(%rbp)
cmpq   $0xa,-0x30(%rbp)
jae    1293 <phone_number_clean+0x153>
mov    -0x18(%rbp),%rax
mov    -0x30(%rbp),%rcx
movb   $0x30,(%rax,%rcx,1)
mov    -0x30(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x30(%rbp)
jmp    126b <phone_number_clean+0x12b>
mov    -0x18(%rbp),%rax
mov    -0x30(%rbp),%rcx
movb   $0x30,(%rax,%rcx,1)
mov    -0x18(%rbp),%rax
mov    -0x30(%rbp),%rcx
movb   $0x0,0x1(%rax,%rcx,1)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xb,-0x24(%rbp)
jne    1326 <phone_number_clean+0x1e6>
movq   $0x1,-0x38(%rbp)
mov    -0x38(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,%rcx
mov    -0x48(%rbp),%rax
cmp    %rcx,%rax
jae    1316 <phone_number_clean+0x1d6>
mov    -0x18(%rbp),%rax
mov    -0x38(%rbp),%rcx
mov    (%rax,%rcx,1),%dl
mov    -0x18(%rbp),%rax
mov    -0x38(%rbp),%rcx
sub    $0x1,%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x38(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x38(%rbp)
jmp    12ca <phone_number_clean+0x18a>
mov    -0x18(%rbp),%rax
mov    -0x38(%rbp),%rcx
sub    $0x1,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<phone_number_clean>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbx
mov    $0xa,%edi
call   1050 <malloc@plt>
mov    %rax,%r14
mov    (%rbx),%al
xor    %r15d,%r15d
test   %al,%al
je     11b8 <phone_number_clean+0x78>
mov    $0x1,%ebp
cs nopw 0x0(%rax,%rax,1)
nop
movsbl %al,%edi
call   1030 <isdigit@plt>
movzbl -0x1(%rbx,%rbp,1),%ecx
test   %rax,%rax
je     1190 <phone_number_clean+0x50>
movslq %r15d,%rax
add    $0x1,%r15d
mov    %cl,(%r14,%rax,1)
jmp    11a1 <phone_number_clean+0x61>
nop
movsbl %cl,%edi
call   1060 <isalpha@plt>
test   %rax,%rax
jne    123f <phone_number_clean+0xff>
mov    %rbx,%rdi
call   1040 <strlen@plt>
cmp    %rax,%rbp
jae    11b8 <phone_number_clean+0x78>
movzbl (%rbx,%rbp,1),%eax
add    $0x1,%rbp
jmp    1170 <phone_number_clean+0x30>
xor    %ebx,%ebx
movslq %r15d,%rax
movb   $0x0,(%r14,%rax,1)
mov    %r14,%rdi
call   1040 <strlen@plt>
test   %bl,%bl
jne    11e0 <phone_number_clean+0xa0>
cmp    $0xa,%eax
jl     11e0 <phone_number_clean+0xa0>
cmp    $0xb,%eax
jg     11e0 <phone_number_clean+0xa0>
jb     11fa <phone_number_clean+0xba>
cmpb   $0x31,(%r14)
je     11fa <phone_number_clean+0xba>
movabs $0x3030303030303030,%rax
30 30 30
mov    %rax,(%r14)
movl   $0x30303030,0x7(%r14)
30
movb   $0x0,0xb(%r14)
mov    %r14,%rdi
call   1040 <strlen@plt>
cmp    $0xb,%eax
jne    1231 <phone_number_clean+0xf1>
mov    $0x1,%ebx
nopl   0x0(%rax)
movzbl (%r14,%rbx,1),%eax
mov    %al,-0x1(%r14,%rbx,1)
add    $0x1,%rbx
mov    %r14,%rdi
call   1040 <strlen@plt>
cmp    %rax,%rbx
jb     1210 <phone_number_clean+0xd0>
movb   $0x0,-0x1(%r14,%rbx,1)
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    $0x1,%bl
jmp    11ba <phone_number_clean+0x7a>

LLVM IR (-O0):
define i8* @phone_number_clean(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i32
%8 = alloca i64
%9 = alloca i64
store i8* %0, i8** %2
store i32 0, i32* %3
store i32 0, i32* %4
%10 = call i8* @malloc(i32 10)
store i8* %10, i8** %5
store i64 0, i64* %6
br label %11
11: ; preds = %43, %1
%12 = load i64, i64* %6
%13 = load i8*, i8** %2
%14 = call i64 @strlen(i8* %13)
%15 = icmp ult i64 %12, %14
br i1 %15, label %16, label %46
16: ; preds = %11
%17 = load i8*, i8** %2
%18 = load i64, i64* %6
%19 = getelementptr inbounds i8, i8* %17, i64 %18
%20 = load i8, i8* %19
%21 = call i64 @isdigit(i8 signext %20)
%22 = icmp ne i64 %21, 0
br i1 %22, label %23, label %33
23: ; preds = %16
%24 = load i8*, i8** %2
%25 = load i64, i64* %6
%26 = getelementptr inbounds i8, i8* %24, i64 %25
%27 = load i8, i8* %26
%28 = load i8*, i8** %5
%29 = load i32, i32* %3
%30 = add nsw i32 %29, 1
store i32 %30, i32* %3
%31 = sext i32 %29 to i64
%32 = getelementptr inbounds i8, i8* %28, i64 %31
store i8 %27, i8* %32
br label %42
33: ; preds = %16
%34 = load i8*, i8** %2
%35 = load i64, i64* %6
%36 = getelementptr inbounds i8, i8* %34, i64 %35
%37 = load i8, i8* %36
%38 = call i64 @isalpha(i8 signext %37)
%39 = icmp ne i64 %38, 0
br i1 %39, label %40, label %41
40: ; preds = %33
store i32 1, i32* %4
br label %46
41: ; preds = %33
br label %42
42: ; preds = %41, %23
br label %43
43: ; preds = %42
%44 = load i64, i64* %6
%45 = add i64 %44, 1
store i64 %45, i64* %6
br label %11
46: ; preds = %40, %11
%47 = load i8*, i8** %5
%48 = load i32, i32* %3
%49 = sext i32 %48 to i64
%50 = getelementptr inbounds i8, i8* %47, i64 %49
store i8 0, i8* %50
%51 = load i8*, i8** %5
%52 = call i64 @strlen(i8* %51)
%53 = trunc i64 %52 to i32
store i32 %53, i32* %7
%54 = load i32, i32* %4
%55 = icmp ne i32 %54, 0
br i1 %55, label %71, label %56
56: ; preds = %46
%57 = load i32, i32* %7
%58 = icmp slt i32 %57, 10
br i1 %58, label %71, label %59
59: ; preds = %56
%60 = load i32, i32* %7
%61 = icmp sgt i32 %60, 11
br i1 %61, label %71, label %62
62: ; preds = %59
%63 = load i32, i32* %7
%64 = icmp sgt i32 %63, 10
br i1 %64, label %65, label %90
65: ; preds = %62
%66 = load i8*, i8** %5
%67 = getelementptr inbounds i8, i8* %66, i64 0
%68 = load i8, i8* %67
%69 = sext i8 %68 to i32
%70 = icmp ne i32 %69, 49
br i1 %70, label %71, label %90
71: ; preds = %65, %59, %56, %46
store i64 0, i64* %8
br label %72
72: ; preds = %79, %71
%73 = load i64, i64* %8
%74 = icmp ult i64 %73, 10
br i1 %74, label %75, label %82
75: ; preds = %72
%76 = load i8*, i8** %5
%77 = load i64, i64* %8
%78 = getelementptr inbounds i8, i8* %76, i64 %77
store i8 48, i8* %78
br label %79
79: ; preds = %75
%80 = load i64, i64* %8
%81 = add i64 %80, 1
store i64 %81, i64* %8
br label %72
82: ; preds = %72
%83 = load i8*, i8** %5
%84 = load i64, i64* %8
%85 = getelementptr inbounds i8, i8* %83, i64 %84
store i8 48, i8* %85
%86 = load i8*, i8** %5
%87 = load i64, i64* %8
%88 = add i64 %87, 1
%89 = getelementptr inbounds i8, i8* %86, i64 %88
store i8 0, i8* %89
br label %90
90: ; preds = %82, %65, %62
%91 = load i8*, i8** %5
%92 = call i64 @strlen(i8* %91)
%93 = trunc i64 %92 to i32
store i32 %93, i32* %7
%94 = load i32, i32* %7
%95 = icmp eq i32 %94, 11
br i1 %95, label %96, label %119
96: ; preds = %90
store i64 1, i64* %9
br label %97
97: ; preds = %111, %96
%98 = load i64, i64* %9
%99 = load i8*, i8** %5
%100 = call i64 @strlen(i8* %99)
%101 = icmp ult i64 %98, %100
br i1 %101, label %102, label %114
102: ; preds = %97
%103 = load i8*, i8** %5
%104 = load i64, i64* %9
%105 = getelementptr inbounds i8, i8* %103, i64 %104
%106 = load i8, i8* %105
%107 = load i8*, i8** %5
%108 = load i64, i64* %9
%109 = sub i64 %108, 1
%110 = getelementptr inbounds i8, i8* %107, i64 %109
store i8 %106, i8* %110
br label %111
111: ; preds = %102
%112 = load i64, i64* %9
%113 = add i64 %112, 1
store i64 %113, i64* %9
br label %97
114: ; preds = %97
%115 = load i8*, i8** %5
%116 = load i64, i64* %9
%117 = sub i64 %116, 1
%118 = getelementptr inbounds i8, i8* %115, i64 %117
store i8 0, i8* %118
br label %119
119: ; preds = %114, %90
%120 = load i8*, i8** %5
ret i8* %120
}

LLVM IR (-O2):
define noalias i8* @phone_number_clean(i8* nocapture %0) {
%2 = tail call dereferenceable_or_null(10) i8* @malloc(i32 10)
%3 = load i8, i8* %0
%4 = icmp eq i8 %3, 0
br i1 %4, label %28, label %5
5: ; preds = %1, %25
%6 = phi i8 [ %27, %25 ], [ %3, %1 ]
%7 = phi i64 [ %22, %25 ], [ 0, %1 ]
%8 = phi i32 [ %21, %25 ], [ 0, %1 ]
%9 = getelementptr inbounds i8, i8* %0, i64 %7
%10 = tail call i64 @isdigit(i8 signext %6)
%11 = icmp eq i64 %10, 0
%12 = load i8, i8* %9
br i1 %11, label %17, label %13
13: ; preds = %5
%14 = add nsw i32 %8, 1
%15 = sext i32 %8 to i64
%16 = getelementptr inbounds i8, i8* %2, i64 %15
store i8 %12, i8* %16
br label %20
17: ; preds = %5
%18 = tail call i64 @isalpha(i8 signext %12)
%19 = icmp eq i64 %18, 0
br i1 %19, label %20, label %28
20: ; preds = %13, %17
%21 = phi i32 [ %14, %13 ], [ %8, %17 ]
%22 = add nuw i64 %7, 1
%23 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%24 = icmp ult i64 %22, %23
br i1 %24, label %25, label %28
25: ; preds = %20
%26 = getelementptr inbounds i8, i8* %0, i64 %22
%27 = load i8, i8* %26
br label %5
28: ; preds = %20, %17, %1
%29 = phi i32 [ 0, %1 ], [ %8, %17 ], [ %21, %20 ]
%30 = phi i1 [ false, %1 ], [ true, %17 ], [ false, %20 ]
%31 = sext i32 %29 to i64
%32 = getelementptr inbounds i8, i8* %2, i64 %31
store i8 0, i8* %32
%33 = tail call i64 @strlen(i8* dereferenceable(1) %2)
%34 = trunc i64 %33 to i32
%35 = icmp slt i32 %34, 10
%36 = select i1 %30, i1 true, i1 %35
%37 = icmp sgt i32 %34, 11
%38 = select i1 %36, i1 true, i1 %37
br i1 %38, label %44, label %39
39: ; preds = %28
%40 = icmp ugt i32 %34, 10
br i1 %40, label %41, label %46
41: ; preds = %39
%42 = load i8, i8* %2
%43 = icmp eq i8 %42, 49
br i1 %43, label %46, label %44
44: ; preds = %41, %28
%45 = getelementptr inbounds i8, i8* %2, i64 11
store i8 0, i8* %45
br label %46
46: ; preds = %44, %41, %39
%47 = tail call i64 @strlen(i8* dereferenceable(1) %2)
%48 = trunc i64 %47 to i32
%49 = icmp eq i32 %48, 11
br i1 %49, label %50, label %61
50: ; preds = %46, %50
%51 = phi i64 [ %56, %50 ], [ 1, %46 ]
%52 = getelementptr inbounds i8, i8* %2, i64 %51
%53 = load i8, i8* %52
%54 = add i64 %51, -1
%55 = getelementptr inbounds i8, i8* %2, i64 %54
store i8 %53, i8* %55
%56 = add nuw i64 %51, 1
%57 = tail call i64 @strlen(i8* dereferenceable(1) %2)
%58 = icmp ult i64 %56, %57
br i1 %58, label %50, label %59
59: ; preds = %50
%60 = getelementptr inbounds i8, i8* %2, i64 %51
store i8 0, i8* %60
br label %61
61: ; preds = %59, %46
ret i8* %2
}