#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*/ is_suffix(char const *, char const *, size_t *);
char *malloc(scalar_t__);
int /*<<< orphan*/ strcpy(char *, char const *);
scalar_t__ strlen(char const *);
int /*<<< orphan*/ strncpy(char *, char const *, size_t);
char *translate_suffix(const char *string, const char *from, const char *to) {
  size_t prefix_length = 0;
  if (!is_suffix(string, from, &prefix_length)) {
    return NULL;
  }
  char *result = malloc(prefix_length + strlen(to) + 1);
  strncpy(result, string, prefix_length);
  strcpy(result + prefix_length, to);
  return result;
}


x86 assembly (clang, -O0):
<translate_suffix>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
lea    -0x28(%rbp),%rdx
call   1060 <is_suffix@plt>
cmp    $0x0,%eax
jne    1193 <translate_suffix+0x43>
movq   $0x0,-0x8(%rbp)
jmp    11e2 <translate_suffix+0x92>
mov    -0x28(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x20(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x38(%rbp),%rdi
add    %rax,%rdi
add    $0x1,%rdi
call   1070 <malloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1030 <strncpy@plt>
mov    -0x30(%rbp),%rdi
add    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1040 <strcpy@plt>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<translate_suffix>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rdi,%r15
movq   $0x0,(%rsp)
mov    %rsp,%rdx
call   1060 <is_suffix@plt>
test   %eax,%eax
je     11ad <translate_suffix+0x5d>
mov    (%rsp),%r12
mov    %r14,%rdi
call   1050 <strlen@plt>
lea    (%r12,%rax,1),%rdi
add    $0x1,%rdi
call   1070 <malloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r15,%rsi
mov    %r12,%rdx
call   1030 <strncpy@plt>
mov    %rbx,%rdi
add    (%rsp),%rdi
mov    %r14,%rsi
call   1040 <strcpy@plt>
jmp    11af <translate_suffix+0x5f>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
define i8* @translate_suffix(i8* %0, i8* %1, i8* %2) {
%4 = alloca i64
%5 = bitcast i64* %4 to i8*
store i64 0, i64* %4
%6 = call i32 @is_suffix(i8* %0, i8* %1, i64* %4)
%7 = icmp eq i32 %6, 0
br i1 %7, label %18, label %8
8: ; preds = %3
%9 = load i64, i64* %4
%10 = call i64 @strlen(i8* dereferenceable(1) %2)
%11 = add i64 %9, 1
%12 = add i64 %11, %10
%13 = call i8* @malloc(i64 %12)
%14 = call i32 @strncpy(i8* %13, i8* %0, i64 %9)
%15 = load i64, i64* %4
%16 = getelementptr inbounds i8, i8* %13, i64 %15
%17 = call i32 @strcpy(i8* %16, i8* %2)
br label %18
18: ; preds = %3, %8
%19 = phi i8* [ %13, %8 ], [ null, %3 ]
ret i8* %19
}