#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__ calloc(int, int);
int /*<<< orphan*/ free(char *);
int kmpMatch(char *, char *, int);
int /*<<< orphan*/ reverse(char *, int);
char *strdup(char *);
int strlen(char *);
int /*<<< orphan*/ strncpy(char *, char *, int);
char *shortestPalindrome(char *s) {
  char *p, *t;
  int i;
  if (!s)
    return NULL;
  int len = strlen(s);
  t = strdup(s);
  reverse(t, len);
  i = kmpMatch(t, s, len);
  p = (char *)calloc(1, 2 * len + 1);
  strncpy(p, t, len);
  strncpy(p + len, s + i, len - i);
  free(t);
  return p;
}


x86 assembly (clang, -O0):
<shortestPalindrome>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1194 <shortestPalindrome+0x24>
movq   $0x0,-0x8(%rbp)
jmp    1225 <shortestPalindrome+0xb5>
mov    -0x10(%rbp),%rdi
call   1060 <strlen@plt>
mov    %eax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
call   1090 <strdup@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%esi
call   1030 <reverse@plt>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x28(%rbp),%edx
call   1070 <kmpMatch@plt>
mov    %eax,-0x24(%rbp)
mov    -0x28(%rbp),%esi
shl    $0x1,%esi
add    $0x1,%esi
mov    $0x1,%edi
call   1080 <calloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x28(%rbp),%edx
call   1050 <strncpy@plt>
mov    -0x18(%rbp),%rdi
movslq -0x28(%rbp),%rax
add    %rax,%rdi
mov    -0x10(%rbp),%rsi
movslq -0x24(%rbp),%rax
add    %rax,%rsi
mov    -0x28(%rbp),%edx
sub    -0x24(%rbp),%edx
call   1050 <strncpy@plt>
mov    -0x20(%rbp),%rdi
call   1040 <free@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<shortestPalindrome>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
test   %rdi,%rdi
je     11f3 <shortestPalindrome+0x83>
mov    %rdi,%rbx
call   1060 <strlen@plt>
mov    %eax,%r12d
mov    %rbx,%rdi
call   1090 <strdup@plt>
mov    %rax,%r14
mov    %rax,%rdi
mov    %r12d,%esi
call   1030 <reverse@plt>
mov    %r14,%rdi
mov    %rbx,%rsi
mov    %r12d,%edx
call   1070 <kmpMatch@plt>
mov    %eax,%ebp
lea    (%r12,%r12,1),%esi
add    $0x1,%esi
mov    $0x1,%edi
call   1080 <calloc@plt>
mov    %rax,%r15
mov    %rax,%rdi
mov    %r14,%rsi
mov    %r12d,%edx
call   1050 <strncpy@plt>
movslq %r12d,%rdx
mov    %r15,%rdi
add    %rdx,%rdi
movslq %ebp,%rax
add    %rax,%rbx
sub    %eax,%edx
mov    %rbx,%rsi
call   1050 <strncpy@plt>
mov    %r14,%rdi
call   1040 <free@plt>
jmp    11f6 <shortestPalindrome+0x86>
xor    %r15d,%r15d
mov    %r15,%rax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i8* @shortestPalindrome(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
store i8* %0, i8** %3
%8 = load i8*, i8** %3
%9 = icmp ne i8* %8, null
br i1 %9, label %11, label %10
10: ; preds = %1
store i8* null, i8** %2
br label %47
11: ; preds = %1
%12 = load i8*, i8** %3
%13 = call i32 @strlen(i8* %12)
store i32 %13, i32* %7
%14 = load i8*, i8** %3
%15 = call i8* @strdup(i8* %14)
store i8* %15, i8** %5
%16 = load i8*, i8** %5
%17 = load i32, i32* %7
%18 = call i32 @reverse(i8* %16, i32 %17)
%19 = load i8*, i8** %5
%20 = load i8*, i8** %3
%21 = load i32, i32* %7
%22 = call i32 @kmpMatch(i8* %19, i8* %20, i32 %21)
store i32 %22, i32* %6
%23 = load i32, i32* %7
%24 = mul nsw i32 2, %23
%25 = add nsw i32 %24, 1
%26 = call i64 @calloc(i32 1, i32 %25)
%27 = inttoptr i64 %26 to i8*
store i8* %27, i8** %4
%28 = load i8*, i8** %4
%29 = load i8*, i8** %5
%30 = load i32, i32* %7
%31 = call i32 @strncpy(i8* %28, i8* %29, i32 %30)
%32 = load i8*, i8** %4
%33 = load i32, i32* %7
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds i8, i8* %32, i64 %34
%36 = load i8*, i8** %3
%37 = load i32, i32* %6
%38 = sext i32 %37 to i64
%39 = getelementptr inbounds i8, i8* %36, i64 %38
%40 = load i32, i32* %7
%41 = load i32, i32* %6
%42 = sub nsw i32 %40, %41
%43 = call i32 @strncpy(i8* %35, i8* %39, i32 %42)
%44 = load i8*, i8** %5
%45 = call i32 @free(i8* %44)
%46 = load i8*, i8** %4
store i8* %46, i8** %2
br label %47
47: ; preds = %11, %10
%48 = load i8*, i8** %2
ret i8* %48
}

LLVM IR (-O2):
define i8* @shortestPalindrome(i8* %0) {
%2 = icmp eq i8* %0, null
br i1 %2, label %20, label %3
3: ; preds = %1
%4 = tail call i32 @strlen(i8* %0)
%5 = tail call i8* @strdup(i8* %0)
%6 = tail call i32 @reverse(i8* %5, i32 %4)
%7 = tail call i32 @kmpMatch(i8* %5, i8* %0, i32 %4)
%8 = shl nsw i32 %4, 1
%9 = or i32 %8, 1
%10 = tail call i64 @calloc(i32 1, i32 %9)
%11 = inttoptr i64 %10 to i8*
%12 = tail call i32 @strncpy(i8* %11, i8* %5, i32 %4)
%13 = sext i32 %4 to i64
%14 = getelementptr inbounds i8, i8* %11, i64 %13
%15 = sext i32 %7 to i64
%16 = getelementptr inbounds i8, i8* %0, i64 %15
%17 = sub nsw i32 %4, %7
%18 = tail call i32 @strncpy(i8* %14, i8* %16, i32 %17)
%19 = tail call i32 @free(i8* %5)
br label %20
20: ; preds = %1, %3
%21 = phi i8* [ %11, %3 ], [ null, %1 ]
ret i8* %21
}