char *strappendfield(char **s1p, const char *s2, char sep) {
  char *s1 = *s1p;
  int len = strlen(s1) + strlen(s2) + 2;
  char *s = xmalloc(len);
  snprintf(s, len, "%s%c%s", s1, sep, s2);
  free(s1);
  *s1p = s;
  return s;
}


x86 assembly (clang, -O0):
<strappendfield>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %dl,%al
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %al,-0x11(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x38(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,%rcx
mov    -0x38(%rbp),%rax
add    %rcx,%rax
add    $0x2,%rax
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%edi
mov    $0x0,%al
call   1060 <xmalloc@plt>
cltq
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
movslq -0x24(%rbp),%rsi
mov    -0x20(%rbp),%rcx
movsbl -0x11(%rbp),%r8d
mov    -0x10(%rbp),%r9
lea    0xe4d(%rip),%rdx
mov    $0x0,%al
call   1050 <snprintf@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x30(%rbp),%rcx
mov    -0x8(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x30(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strappendfield>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,%r12d
mov    %rsi,%r15
mov    %rdi,%r14
mov    (%rdi),%r13
mov    %r13,%rdi
call   1040 <strlen@plt>
mov    %rax,%rbp
mov    %r15,%rdi
call   1040 <strlen@plt>
add    %eax,%ebp
add    $0x2,%ebp
mov    %ebp,%edi
xor    %eax,%eax
call   1060 <xmalloc@plt>
movslq %eax,%rbx
movslq %ebp,%rsi
lea    0xe7b(%rip),%rdx
mov    %rbx,%rdi
mov    %r13,%rcx
mov    %r12d,%r8d
mov    %r15,%r9
xor    %eax,%eax
call   1050 <snprintf@plt>
mov    %r13,%rdi
call   1030 <free@plt>
mov    %rbx,(%r14)
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [7 x i8] c"%s%c%s\00"
define i8* @strappendfield(i8** %0, i8* %1, i8 signext %2) {
%4 = alloca i8**
%5 = alloca i8*
%6 = alloca i8
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i8*
store i8** %0, i8*** %4
store i8* %1, i8** %5
store i8 %2, i8* %6
%10 = load i8**, i8*** %4
%11 = load i8*, i8** %10
store i8* %11, i8** %7
%12 = load i8*, i8** %7
%13 = call i64 @strlen(i8* %12)
%14 = load i8*, i8** %5
%15 = call i64 @strlen(i8* %14)
%16 = add i64 %13, %15
%17 = add i64 %16, 2
%18 = trunc i64 %17 to i32
store i32 %18, i32* %8
%19 = load i32, i32* %8
%20 = call i32 (i32, ...) bitcast (i32 (...)* @xmalloc to i32 (i32, ...)*)(i32 %19)
%21 = sext i32 %20 to i64
%22 = inttoptr i64 %21 to i8*
store i8* %22, i8** %9
%23 = load i8*, i8** %9
%24 = load i32, i32* %8
%25 = sext i32 %24 to i64
%26 = load i8*, i8** %7
%27 = load i8, i8* %6
%28 = sext i8 %27 to i32
%29 = load i8*, i8** %5
%30 = call i32 (i8*, i64, i8*, ...) @snprintf(i8* %23, i64 %25, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8* %26, i32 %28, i8* %29)
%31 = load i8*, i8** %7
%32 = load i8*, i8** %9
%33 = load i8**, i8*** %4
store i8* %32, i8** %33
%34 = load i8*, i8** %9
ret i8* %34
}

LLVM IR (-O2):
@.str = [7 x i8] c"%s%c%s\00"
define i8* @strappendfield(i8** nocapture %0, i8* %1, i8 signext %2) {
%4 = load i8*, i8** %0
%5 = tail call i64 @strlen(i8* dereferenceable(1) %4)
%6 = tail call i64 @strlen(i8* dereferenceable(1) %1)
%7 = add i64 %6, %5
%8 = trunc i64 %7 to i32
%9 = add i32 %8, 2
%10 = tail call i32 (i32, ...) bitcast (i32 (...)* @xmalloc to i32 (i32, ...)*)(i32 %9)
%11 = sext i32 %10 to i64
%12 = inttoptr i64 %11 to i8*
%13 = sext i32 %9 to i64
%14 = sext i8 %2 to i32
%15 = tail call i32 (i8*, i64, i8*, ...) @snprintf(i8* %12, i64 %13, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8* %4, i32 %14, i8* %1)
tail call void @free(i8* %4)
store i8* %12, i8** %0
ret i8* %12
}