#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 */
char *mem_begin(char *);
int /*<<< orphan*/ memmove(char *, char *, size_t);
size_t str_len(char *);
char *str_reserve(char *, size_t);
char *str_assign(char *dst, char *src) {
  char *dst_start = NULL;
  size_t src_len = 0;
  size_t dst_offset = 0;
  if (src == NULL)
    return (NULL);
  src_len = str_len(src);
  dst_start = mem_begin(dst);
  dst_offset = dst - dst_start;
  dst_start = str_reserve(dst_start, dst_offset + src_len + 1);
  if (dst_start == NULL)
    return (NULL);
  dst = dst_start + dst_offset;
  memmove(dst, src, src_len + 1);
  return (dst_start);
}


x86 assembly (clang, -O0):
<str_assign>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1180 <str_assign+0x40>
movq   $0x0,-0x8(%rbp)
jmp    1203 <str_assign+0xc3>
mov    -0x18(%rbp),%rdi
call   1060 <str_len@plt>
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <mem_begin@plt>
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    -0x20(%rbp),%rcx
sub    %rcx,%rax
mov    %rax,-0x30(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x30(%rbp),%rsi
add    -0x28(%rbp),%rsi
add    $0x1,%rsi
call   1030 <str_reserve@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11da <str_assign+0x9a>
movq   $0x0,-0x8(%rbp)
jmp    1203 <str_assign+0xc3>
mov    -0x20(%rbp),%rax
add    -0x30(%rbp),%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x28(%rbp),%rdx
add    $0x1,%rdx
call   1050 <memmove@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<str_assign>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
test   %rsi,%rsi
je     1198 <str_assign+0x58>
mov    %rsi,%r14
mov    %rdi,%rbx
mov    %rsi,%rdi
call   1060 <str_len@plt>
mov    %rax,%r15
mov    %rbx,%rdi
call   1040 <mem_begin@plt>
sub    %rax,%rbx
lea    (%r15,%rbx,1),%rsi
add    $0x1,%rsi
mov    %rax,%rdi
call   1030 <str_reserve@plt>
test   %rax,%rax
je     1198 <str_assign+0x58>
mov    %rax,%r12
add    $0x1,%r15
add    %rax,%rbx
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %r15,%rdx
call   1050 <memmove@plt>
jmp    119b <str_assign+0x5b>
xor    %r12d,%r12d
mov    %r12,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i8* @str_assign(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i64
store i8* %0, i8** %4
store i8* %1, i8** %5
store i8* null, i8** %6
store i64 0, i64* %7
store i64 0, i64* %8
%9 = load i8*, i8** %5
%10 = icmp eq i8* %9, null
br i1 %10, label %11, label %12
11: ; preds = %2
store i8* null, i8** %3
br label %41
12: ; preds = %2
%13 = load i8*, i8** %5
%14 = call i64 @str_len(i8* %13)
store i64 %14, i64* %7
%15 = load i8*, i8** %4
%16 = call i8* @mem_begin(i8* %15)
store i8* %16, i8** %6
%17 = load i8*, i8** %4
%18 = load i8*, i8** %6
%19 = ptrtoint i8* %17 to i64
%20 = ptrtoint i8* %18 to i64
%21 = sub i64 %19, %20
store i64 %21, i64* %8
%22 = load i8*, i8** %6
%23 = load i64, i64* %8
%24 = load i64, i64* %7
%25 = add i64 %23, %24
%26 = add i64 %25, 1
%27 = call i8* @str_reserve(i8* %22, i64 %26)
store i8* %27, i8** %6
%28 = load i8*, i8** %6
%29 = icmp eq i8* %28, null
br i1 %29, label %30, label %31
30: ; preds = %12
store i8* null, i8** %3
br label %41
31: ; preds = %12
%32 = load i8*, i8** %6
%33 = load i64, i64* %8
%34 = getelementptr inbounds i8, i8* %32, i64 %33
store i8* %34, i8** %4
%35 = load i8*, i8** %4
%36 = load i8*, i8** %5
%37 = load i64, i64* %7
%38 = add i64 %37, 1
%39 = call i32 @memmove(i8* %35, i8* %36, i64 %38)
%40 = load i8*, i8** %6
store i8* %40, i8** %3
br label %41
41: ; preds = %31, %30, %11
%42 = load i8*, i8** %3
ret i8* %42
}

LLVM IR (-O2):
define i8* @str_assign(i8* %0, i8* %1) {
%3 = icmp eq i8* %1, null
br i1 %3, label %17, label %4
4: ; preds = %2
%5 = tail call i64 @str_len(i8* %1)
%6 = tail call i8* @mem_begin(i8* %0)
%7 = ptrtoint i8* %0 to i64
%8 = ptrtoint i8* %6 to i64
%9 = sub i64 %7, %8
%10 = add i64 %5, 1
%11 = add i64 %10, %9
%12 = tail call i8* @str_reserve(i8* %6, i64 %11)
%13 = icmp eq i8* %12, null
br i1 %13, label %17, label %14
14: ; preds = %4
%15 = getelementptr inbounds i8, i8* %12, i64 %9
%16 = tail call i32 @memmove(i8* %15, i8* %1, i64 %10)
br label %17
17: ; preds = %4, %2, %14
%18 = phi i8* [ %12, %14 ], [ null, %2 ], [ null, %4 ]
ret i8* %18
}