#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 /*<<< orphan*/ free(char *);
char *malloc(int);
char *phone_number_clean(char const *);
int /*<<< orphan*/ strcat(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
int strlen(char *);
int /*<<< orphan*/ strncat(char *, char *, int);
char *phone_number_format(const char *input) {
  char *clean_input = phone_number_clean(input);
  char *result = malloc(15);
  int len = strlen(clean_input);
  if (len == 10) {
    strcpy(result, "(");
    strncat(result, clean_input, 3);
    strcat(result, ") ");
    strncat(result, clean_input + 3, 3);
    strcat(result, "-");
    strcat(result, clean_input + 6);
  }
  if (len == 11) {
    strcpy(result, "(");
    strncat(result, clean_input + 1, 4);
    strcat(result, ") ");
    strncat(result, clean_input + 4, 3);
    strcat(result, "-");
    strcat(result, clean_input + 7);
  }
  free(clean_input);
  return result;
}


x86 assembly (clang, -O0):
<phone_number_format>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <phone_number_clean@plt>
mov    %rax,-0x10(%rbp)
mov    $0xf,%edi
call   1080 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <strlen@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xa,-0x1c(%rbp)
jne    1216 <phone_number_format+0xa6>
mov    -0x18(%rbp),%rdi
lea    0xe48(%rip),%rsi
call   1050 <strcpy@plt>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    $0x3,%edx
call   1070 <strncat@plt>
mov    -0x18(%rbp),%rdi
lea    0xe28(%rip),%rsi
call   1090 <strcat@plt>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
add    $0x3,%rsi
mov    $0x3,%edx
call   1070 <strncat@plt>
mov    -0x18(%rbp),%rdi
lea    0xe05(%rip),%rsi
call   1090 <strcat@plt>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
add    $0x6,%rsi
call   1090 <strcat@plt>
cmpl   $0xb,-0x1c(%rbp)
jne    128d <phone_number_format+0x11d>
mov    -0x18(%rbp),%rdi
lea    0xdd5(%rip),%rsi
call   1050 <strcpy@plt>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
add    $0x1,%rsi
mov    $0x4,%edx
call   1070 <strncat@plt>
mov    -0x18(%rbp),%rdi
lea    0xdb1(%rip),%rsi
call   1090 <strcat@plt>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
add    $0x4,%rsi
mov    $0x3,%edx
call   1070 <strncat@plt>
mov    -0x18(%rbp),%rdi
lea    0xd8e(%rip),%rsi
call   1090 <strcat@plt>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
add    $0x7,%rsi
call   1090 <strcat@plt>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<phone_number_format>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
call   1040 <phone_number_clean@plt>
mov    %rax,%r14
mov    $0xf,%edi
call   1080 <malloc@plt>
mov    %rax,%r12
mov    %r14,%rdi
call   1060 <strlen@plt>
cmp    $0xb,%eax
je     11cf <phone_number_format+0x5f>
cmp    $0xa,%eax
jne    1239 <phone_number_format+0xc9>
lea    0xe56(%rip),%rsi
mov    %r12,%rdi
call   1050 <strcpy@plt>
mov    %r12,%rdi
mov    %r14,%rsi
mov    $0x3,%edx
call   1070 <strncat@plt>
mov    $0x6,%r15d
mov    $0x3,%ebx
jmp    11fa <phone_number_format+0x8a>
lea    0xe2a(%rip),%rsi
mov    %r12,%rdi
call   1050 <strcpy@plt>
lea    0x1(%r14),%rsi
mov    %r12,%rdi
mov    $0x4,%edx
call   1070 <strncat@plt>
mov    $0x7,%r15d
mov    $0x4,%ebx
lea    0xe01(%rip),%rsi
mov    %r12,%rdi
call   1090 <strcat@plt>
add    %r14,%rbx
mov    %r12,%rdi
mov    %rbx,%rsi
mov    $0x3,%edx
call   1070 <strncat@plt>
lea    0xde2(%rip),%rsi
mov    %r12,%rdi
call   1090 <strcat@plt>
add    %r14,%r15
mov    %r12,%rdi
mov    %r15,%rsi
call   1090 <strcat@plt>
mov    %r14,%rdi
call   1030 <free@plt>
mov    %r12,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [2 x i8] c"(\00"
@.str.1 = [3 x i8] c") \00"
@.str.2 = [2 x i8] c"-\00"
define i8* @phone_number_format(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
store i8* %0, i8** %2
%6 = load i8*, i8** %2
%7 = call i8* @phone_number_clean(i8* %6)
store i8* %7, i8** %3
%8 = call i8* @malloc(i32 15)
store i8* %8, i8** %4
%9 = load i8*, i8** %3
%10 = call i32 @strlen(i8* %9)
store i32 %10, i32* %5
%11 = load i32, i32* %5
%12 = icmp eq i32 %11, 10
br i1 %12, label %13, label %31
13: ; preds = %1
%14 = load i8*, i8** %4
%15 = call i32 @strcpy(i8* %14, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%16 = load i8*, i8** %4
%17 = load i8*, i8** %3
%18 = call i32 @strncat(i8* %16, i8* %17, i32 3)
%19 = load i8*, i8** %4
%20 = call i32 @strcat(i8* %19, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%21 = load i8*, i8** %4
%22 = load i8*, i8** %3
%23 = getelementptr inbounds i8, i8* %22, i64 3
%24 = call i32 @strncat(i8* %21, i8* %23, i32 3)
%25 = load i8*, i8** %4
%26 = call i32 @strcat(i8* %25, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%27 = load i8*, i8** %4
%28 = load i8*, i8** %3
%29 = getelementptr inbounds i8, i8* %28, i64 6
%30 = call i32 @strcat(i8* %27, i8* %29)
br label %31
31: ; preds = %13, %1
%32 = load i32, i32* %5
%33 = icmp eq i32 %32, 11
br i1 %33, label %34, label %53
34: ; preds = %31
%35 = load i8*, i8** %4
%36 = call i32 @strcpy(i8* %35, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%37 = load i8*, i8** %4
%38 = load i8*, i8** %3
%39 = getelementptr inbounds i8, i8* %38, i64 1
%40 = call i32 @strncat(i8* %37, i8* %39, i32 4)
%41 = load i8*, i8** %4
%42 = call i32 @strcat(i8* %41, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%43 = load i8*, i8** %4
%44 = load i8*, i8** %3
%45 = getelementptr inbounds i8, i8* %44, i64 4
%46 = call i32 @strncat(i8* %43, i8* %45, i32 3)
%47 = load i8*, i8** %4
%48 = call i32 @strcat(i8* %47, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%49 = load i8*, i8** %4
%50 = load i8*, i8** %3
%51 = getelementptr inbounds i8, i8* %50, i64 7
%52 = call i32 @strcat(i8* %49, i8* %51)
br label %53
53: ; preds = %34, %31
%54 = load i8*, i8** %3
%55 = call i32 @free(i8* %54)
%56 = load i8*, i8** %4
ret i8* %56
}

LLVM IR (-O2):
@.str = [2 x i8] c"(\00"
@.str.1 = [3 x i8] c") \00"
@.str.2 = [2 x i8] c"-\00"
define i8* @phone_number_format(i8* %0) {
%2 = tail call i8* @phone_number_clean(i8* %0)
%3 = tail call dereferenceable_or_null(15) i8* @malloc(i32 15)
%4 = tail call i32 @strlen(i8* %2)
switch i32 %4, label %21 [
i32 10, label %5
i32 11, label %8
]
5: ; preds = %1
%6 = tail call i32 @strcpy(i8* %3, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%7 = tail call i32 @strncat(i8* %3, i8* %2, i32 3)
br label %12
8: ; preds = %1
%9 = tail call i32 @strcpy(i8* %3, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%10 = getelementptr inbounds i8, i8* %2, i64 1
%11 = tail call i32 @strncat(i8* %3, i8* %10, i32 4)
br label %12
12: ; preds = %8, %5
%13 = phi i64 [ 3, %5 ], [ 4, %8 ]
%14 = phi i64 [ 6, %5 ], [ 7, %8 ]
%15 = tail call i32 @strcat(i8* %3, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%16 = getelementptr inbounds i8, i8* %2, i64 %13
%17 = tail call i32 @strncat(i8* %3, i8* %16, i32 3)
%18 = tail call i32 @strcat(i8* %3, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%19 = getelementptr inbounds i8, i8* %2, i64 %14
%20 = tail call i32 @strcat(i8* %3, i8* %19)
br label %21
21: ; preds = %12, %1
%22 = tail call i32 @free(i8* %2)
ret i8* %3
}