#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*/ free(char *);
scalar_t__ malloc(int);
int /*<<< orphan*/ memcpy(char *, char *, int);
int /*<<< orphan*/ printf(char *, ...);
char *realloc(char *, int);
int /*<<< orphan*/ show_int(unsigned char);
int strlen(char *);
void test_byte_copy() {
  unsigned char z = 97;
  char *s = "hello";
  char *ps = (char *)malloc(sizeof(char) * 5 + 1);
  printf("%s\n", s);
  printf("97-->0x%.2x\n", ((unsigned char *)&z)[0]);
  show_int(z);
  memcpy(ps, s, strlen(s));
  ps = realloc(ps, strlen(s) + 5);
  memcpy(ps + 5, (char *)&z, 1);
  ps[6] = '\0';
  printf("ps=%s, len=%zu\n", ps, strlen(ps));
  free(ps);
}


x86 assembly (clang, -O0):
<test_byte_copy>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movb   $0x61,-0x1(%rbp)
lea    0xe7d(%rip),%rax
mov    %rax,-0x10(%rbp)
mov    $0x6,%edi
call   1070 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rsi
lea    0xe66(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movzbl -0x1(%rbp),%esi
lea    0xe58(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movzbl -0x1(%rbp),%edi
call   1090 <show_int@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x38(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    %eax,%edx
call   1060 <memcpy@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x28(%rbp),%rdi
mov    %eax,%esi
add    $0x5,%esi
call   1080 <realloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
add    $0x5,%rdi
lea    -0x1(%rbp),%rsi
mov    $0x1,%edx
call   1060 <memcpy@plt>
mov    -0x18(%rbp),%rax
movb   $0x0,0x6(%rax)
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x20(%rbp),%rsi
mov    %eax,%edx
lea    0xdce(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_byte_copy>:
push   %r14
push   %rbx
push   %rax
movb   $0x61,0x7(%rsp)
mov    $0x6,%edi
call   1080 <malloc@plt>
mov    %rax,%rbx
lea    0xe63(%rip),%r14
mov    %r14,%rdi
call   1040 <puts@plt>
lea    0xe5a(%rip),%rdi
mov    $0x61,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0x61,%edi
call   10a0 <show_int@plt>
mov    %r14,%rdi
call   1050 <strlen@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %eax,%edx
call   1070 <memcpy@plt>
mov    %r14,%rdi
call   1050 <strlen@plt>
lea    0x5(%rax),%esi
mov    %rbx,%rdi
call   1090 <realloc@plt>
mov    %rax,%rbx
lea    0x5(%rax),%rdi
lea    0x7(%rsp),%rsi
mov    $0x1,%edx
call   1070 <memcpy@plt>
movb   $0x0,0x6(%rbx)
mov    %rbx,%rdi
call   1050 <strlen@plt>
lea    0xe00(%rip),%rdi
mov    %rbx,%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1060 <printf@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [6 x i8] c"hello\00"
@.str.1 = [4 x i8] c"%s\0A\00"
@.str.2 = [13 x i8] c"97-->0x%.2x\0A\00"
@.str.3 = [16 x i8] c"ps=%s, len=%zu\0A\00"
define void @test_byte_copy() {
%1 = alloca i8
%2 = alloca i8*
%3 = alloca i8*
store i8 97, i8* %1
store i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8** %2
%4 = call i64 @malloc(i32 6)
%5 = inttoptr i64 %4 to i8*
store i8* %5, i8** %3
%6 = load i8*, i8** %2
%7 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %6)
%8 = getelementptr inbounds i8, i8* %1, i64 0
%9 = load i8, i8* %8
%10 = zext i8 %9 to i32
%11 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i32 %10)
%12 = load i8, i8* %1
%13 = call i32 @show_int(i8 zeroext %12)
%14 = load i8*, i8** %3
%15 = load i8*, i8** %2
%16 = load i8*, i8** %2
%17 = call i32 @strlen(i8* %16)
%18 = call i32 @memcpy(i8* %14, i8* %15, i32 %17)
%19 = load i8*, i8** %3
%20 = load i8*, i8** %2
%21 = call i32 @strlen(i8* %20)
%22 = add nsw i32 %21, 5
%23 = call i8* @realloc(i8* %19, i32 %22)
store i8* %23, i8** %3
%24 = load i8*, i8** %3
%25 = getelementptr inbounds i8, i8* %24, i64 5
%26 = call i32 @memcpy(i8* %25, i8* %1, i32 1)
%27 = load i8*, i8** %3
%28 = getelementptr inbounds i8, i8* %27, i64 6
store i8 0, i8* %28
%29 = load i8*, i8** %3
%30 = load i8*, i8** %3
%31 = call i32 @strlen(i8* %30)
%32 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0), i8* %29, i32 %31)
%33 = load i8*, i8** %3
%34 = call i32 @free(i8* %33)
ret void
}

LLVM IR (-O2):
@.str = [6 x i8] c"hello\00"
@.str.2 = [13 x i8] c"97-->0x%.2x\0A\00"
@.str.3 = [16 x i8] c"ps=%s, len=%zu\0A\00"
define void @test_byte_copy() {
%1 = alloca i8
store i8 97, i8* %1
%2 = tail call i64 @malloc(i32 6)
%3 = inttoptr i64 %2 to i8*
%4 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%5 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i32 97)
%6 = tail call i32 @show_int(i8 zeroext 97)
%7 = tail call i32 @strlen(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%8 = tail call i32 @memcpy(i8* %3, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32 %7)
%9 = tail call i32 @strlen(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%10 = add nsw i32 %9, 5
%11 = tail call i8* @realloc(i8* %3, i32 %10)
%12 = getelementptr inbounds i8, i8* %11, i64 5
%13 = call i32 @memcpy(i8* %12, i8* %1, i32 1)
%14 = getelementptr inbounds i8, i8* %11, i64 6
store i8 0, i8* %14
%15 = call i32 @strlen(i8* %11)
%16 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0), i8* %11, i32 %15)
%17 = call i32 @free(i8* %11)
ret void
}