#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__ malloc(int);
int strlen(char *);
char *str_append_char(char *s, char c) {
  int size_res = strlen(s) + 1;
  char *res = (char *)malloc(sizeof(char) * size_res + 1);
  char *pt_r = res;
  char *pt_s = s;
  for (int i = 0; i < strlen(s); i++) {
    *pt_r = *pt_s;
    pt_r++;
    pt_s++;
  }
  *pt_r = c;
  *(++pt_r) = '\0';
  return res;
}


x86 assembly (clang, -O0):
<str_append_char>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %sil,%al
mov    %rdi,-0x8(%rbp)
mov    %al,-0x9(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
movslq -0x10(%rbp),%rax
shl    $0x0,%rax
add    $0x1,%rax
mov    %eax,%edi
call   1040 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x30(%rbp),%eax
cmp    %ecx,%eax
jge    11bd <str_append_char+0x9d>
mov    -0x28(%rbp),%rax
mov    (%rax),%cl
mov    -0x20(%rbp),%rax
mov    %cl,(%rax)
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    116f <str_append_char+0x4f>
mov    -0x9(%rbp),%cl
mov    -0x20(%rbp),%rax
mov    %cl,(%rax)
mov    -0x20(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x20(%rbp)
movb   $0x0,0x1(%rax)
mov    -0x18(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<str_append_char>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %esi,%r14d
mov    %rdi,%rbx
call   1030 <strlen@plt>
lea    0x2(%rax),%edi
call   1040 <malloc@plt>
mov    %rax,%r15
mov    %rbx,%rdi
call   1030 <strlen@plt>
test   %eax,%eax
jle    1170 <str_append_char+0x50>
xor    %ebp,%ebp
nopl   0x0(%rax,%rax,1)
movzbl (%rbx,%rbp,1),%eax
mov    %al,(%r15,%rbp,1)
mov    %rbx,%rdi
call   1030 <strlen@plt>
add    $0x1,%rbp
cmp    %eax,%ebp
jl     1150 <str_append_char+0x30>
mov    %r15,%rax
add    %rbp,%rax
jmp    1173 <str_append_char+0x53>
mov    %r15,%rax
mov    %r14b,(%rax)
movb   $0x0,0x1(%rax)
mov    %r15,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define i8* @str_append_char(i8* %0, i8 signext %1) {
%3 = tail call i32 @strlen(i8* %0)
%4 = add i32 %3, 2
%5 = tail call i64 @malloc(i32 %4)
%6 = inttoptr i64 %5 to i8*
%7 = tail call i32 @strlen(i8* %0)
%8 = icmp sgt i32 %7, 0
br i1 %8, label %12, label %9
9: ; preds = %12, %2
%10 = phi i8* [ %6, %2 ], [ %17, %12 ]
store i8 %1, i8* %10
%11 = getelementptr inbounds i8, i8* %10, i64 1
store i8 0, i8* %11
ret i8* %6
12: ; preds = %2, %12
%13 = phi i32 [ %19, %12 ], [ 0, %2 ]
%14 = phi i8* [ %18, %12 ], [ %0, %2 ]
%15 = phi i8* [ %17, %12 ], [ %6, %2 ]
%16 = load i8, i8* %14
store i8 %16, i8* %15
%17 = getelementptr inbounds i8, i8* %15, i64 1
%18 = getelementptr inbounds i8, i8* %14, i64 1
%19 = add nuw nsw i32 %13, 1
%20 = tail call i32 @strlen(i8* %0)
%21 = icmp slt i32 %19, %20
br i1 %21, label %12, label %9
}