#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*/ strcpy(char *, char *);
scalar_t__ strlen(char *);
int /*<<< orphan*/ strncat(char *, char const *, size_t const);
int /*<<< orphan*/ xfree(char *);
char *xmalloc(scalar_t__);
char *xstrncat(char *pfx, const char *sfx, const size_t n) {
  char *s;
  s = xmalloc(strlen(pfx) + n + 1);
  if (!s)
    return NULL;
  strcpy(s, pfx);
  strncat(s, sfx, n);
  xfree(pfx);
  return s;
}


x86 assembly (clang, -O0):
<xstrncat>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,%rdi
add    -0x20(%rbp),%rdi
add    $0x1,%rdi
call   1060 <xmalloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1199 <xstrncat+0x49>
movq   $0x0,-0x8(%rbp)
jmp    11c8 <xstrncat+0x78>
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1070 <strncat@plt>
mov    -0x10(%rbp),%rdi
call   1050 <xfree@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<xstrncat>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%r12
call   1040 <strlen@plt>
lea    (%r14,%rax,1),%rdi
add    $0x1,%rdi
call   1060 <xmalloc@plt>
mov    %rax,%rbx
test   %rax,%rax
je     119c <xstrncat+0x4c>
mov    %rbx,%rdi
mov    %r12,%rsi
call   1030 <strcpy@plt>
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %r14,%rdx
call   1070 <strncat@plt>
mov    %r12,%rdi
call   1050 <xfree@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
define i8* @xstrncat(i8* %0, i8* %1, i64 %2) {
%4 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%5 = add i64 %2, 1
%6 = add i64 %5, %4
%7 = tail call i8* @xmalloc(i64 %6)
%8 = icmp eq i8* %7, null
br i1 %8, label %13, label %9
9: ; preds = %3
%10 = tail call i32 @strcpy(i8* %7, i8* %0)
%11 = tail call i32 @strncat(i8* %7, i8* %1, i64 %2)
%12 = tail call i32 @xfree(i8* %0)
br label %13
13: ; preds = %3, %9
ret i8* %7
}