#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 *malloc(int);
int strlen(char const *);
int /*<<< orphan*/ strncpy(char *, char const *, int);
char *phone_number_get_area_code(const char *input) {
  char *result = malloc(5);
  int len = strlen(input);
  if (len == 10) {
    strncpy(result, input, 3);
    result[3] = '\0';
  }
  if (len == 11) {
    strncpy(result, input + 1, 4);
    result[4] = '\0';
  }
  return result;
}


x86 assembly (clang, -O0):
<phone_number_get_area_code>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    $0x5,%edi
call   1050 <malloc@plt>
mov    %rax,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xa,-0x14(%rbp)
jne    117a <phone_number_get_area_code+0x4a>
mov    -0x10(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    $0x3,%edx
call   1030 <strncpy@plt>
mov    -0x10(%rbp),%rax
movb   $0x0,0x3(%rax)
cmpl   $0xb,-0x14(%rbp)
jne    11a2 <phone_number_get_area_code+0x72>
mov    -0x10(%rbp),%rdi
mov    -0x8(%rbp),%rsi
add    $0x1,%rsi
mov    $0x4,%edx
call   1030 <strncpy@plt>
mov    -0x10(%rbp),%rax
movb   $0x0,0x4(%rax)
mov    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<phone_number_get_area_code>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbx
mov    $0x5,%edi
call   1050 <malloc@plt>
mov    %rax,%r14
mov    %rbx,%rdi
call   1040 <strlen@plt>
cmp    $0xb,%eax
je     116d <phone_number_get_area_code+0x3d>
cmp    $0xa,%eax
jne    118b <phone_number_get_area_code+0x5b>
mov    %r14,%rdi
mov    %rbx,%rsi
mov    $0x3,%edx
call   1030 <strncpy@plt>
mov    $0x3,%eax
jmp    1186 <phone_number_get_area_code+0x56>
add    $0x1,%rbx
mov    %r14,%rdi
mov    %rbx,%rsi
mov    $0x4,%edx
call   1030 <strncpy@plt>
mov    $0x4,%eax
movb   $0x0,(%r14,%rax,1)
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
define i8* @phone_number_get_area_code(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32
store i8* %0, i8** %2
%5 = call i8* @malloc(i32 5)
store i8* %5, i8** %3
%6 = load i8*, i8** %2
%7 = call i32 @strlen(i8* %6)
store i32 %7, i32* %4
%8 = load i32, i32* %4
%9 = icmp eq i32 %8, 10
br i1 %9, label %10, label %16
10: ; preds = %1
%11 = load i8*, i8** %3
%12 = load i8*, i8** %2
%13 = call i32 @strncpy(i8* %11, i8* %12, i32 3)
%14 = load i8*, i8** %3
%15 = getelementptr inbounds i8, i8* %14, i64 3
store i8 0, i8* %15
br label %16
16: ; preds = %10, %1
%17 = load i32, i32* %4
%18 = icmp eq i32 %17, 11
br i1 %18, label %19, label %26
19: ; preds = %16
%20 = load i8*, i8** %3
%21 = load i8*, i8** %2
%22 = getelementptr inbounds i8, i8* %21, i64 1
%23 = call i32 @strncpy(i8* %20, i8* %22, i32 4)
%24 = load i8*, i8** %3
%25 = getelementptr inbounds i8, i8* %24, i64 4
store i8 0, i8* %25
br label %26
26: ; preds = %19, %16
%27 = load i8*, i8** %3
ret i8* %27
}

LLVM IR (-O2):
define i8* @phone_number_get_area_code(i8* %0) {
%2 = tail call dereferenceable_or_null(5) i8* @malloc(i32 5)
%3 = tail call i32 @strlen(i8* %0)
switch i32 %3, label %12 [
i32 10, label %4
i32 11, label %6
]
4: ; preds = %1
%5 = tail call i32 @strncpy(i8* %2, i8* %0, i32 3)
br label %9
6: ; preds = %1
%7 = getelementptr inbounds i8, i8* %0, i64 1
%8 = tail call i32 @strncpy(i8* %2, i8* %7, i32 4)
br label %9
9: ; preds = %6, %4
%10 = phi i64 [ 3, %4 ], [ 4, %6 ]
%11 = getelementptr inbounds i8, i8* %2, i64 %10
store i8 0, i8* %11
br label %12
12: ; preds = %9, %1
ret i8* %2
}