char *new_string(str)
char *str;
{
  char *result;
  int len;
  assert(str != 0);
  len = strlen(str);
  assert(len >= 0);
  result = (char *)malloc(len + 1);
  assert(result != 0);
  strcpy(result, str);
  return result;
}


x86 assembly (clang, -O0):
<new_string>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
mov    $0x0,%al
call   1050 <assert@plt>
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
setge  %al
and    $0x1,%al
movzbl %al,%edi
mov    $0x0,%al
call   1050 <assert@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
movslq %eax,%rdi
call   1060 <malloc@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
mov    $0x0,%al
call   1050 <assert@plt>
mov    -0x10(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<new_string>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
xor    %edi,%edi
test   %r14,%r14
setne  %dil
xor    %eax,%eax
call   1050 <assert@plt>
mov    %r14,%rdi
call   1040 <strlen@plt>
mov    %rax,%rbx
mov    %ebx,%edi
not    %edi
shr    $0x1f,%edi
xor    %eax,%eax
call   1050 <assert@plt>
add    $0x1,%ebx
movslq %ebx,%rdi
call   1060 <malloc@plt>
mov    %rax,%rbx
xor    %edi,%edi
test   %rax,%rax
setne  %dil
xor    %eax,%eax
call   1050 <assert@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1030 <strcpy@plt>

LLVM IR (-O0):
define i8* @new_string(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32
store i8* %0, i8** %2
%5 = load i8*, i8** %2
%6 = icmp ne i8* %5, null
%7 = zext i1 %6 to i32
%8 = call i32 (i32, ...) bitcast (i32 (...)* @assert to i32 (i32, ...)*)(i32 %7)
%9 = load i8*, i8** %2
%10 = call i64 @strlen(i8* %9)
%11 = trunc i64 %10 to i32
store i32 %11, i32* %4
%12 = load i32, i32* %4
%13 = icmp sge i32 %12, 0
%14 = zext i1 %13 to i32
%15 = call i32 (i32, ...) bitcast (i32 (...)* @assert to i32 (i32, ...)*)(i32 %14)
%16 = load i32, i32* %4
%17 = add nsw i32 %16, 1
%18 = sext i32 %17 to i64
%19 = call i8* @malloc(i64 %18)
store i8* %19, i8** %3
%20 = load i8*, i8** %3
%21 = icmp ne i8* %20, null
%22 = zext i1 %21 to i32
%23 = call i32 (i32, ...) bitcast (i32 (...)* @assert to i32 (i32, ...)*)(i32 %22)
%24 = load i8*, i8** %3
%25 = load i8*, i8** %2
%26 = call i8* @strcpy(i8* %24, i8* %25)
%27 = load i8*, i8** %3
ret i8* %27
}

LLVM IR (-O2):
define i8* @new_string(i8* %0) {
%2 = icmp ne i8* %0, null
%3 = zext i1 %2 to i32
%4 = tail call i32 (i32, ...) bitcast (i32 (...)* @assert to i32 (i32, ...)*)(i32 %3)
%5 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%6 = trunc i64 %5 to i32
%7 = xor i32 %6, -1
%8 = lshr i32 %7, 31
%9 = tail call i32 (i32, ...) bitcast (i32 (...)* @assert to i32 (i32, ...)*)(i32 %8)
%10 = shl i64 %5, 32
%11 = add i64 %10, 4294967296
%12 = ashr exact i64 %11, 32
%13 = tail call i8* @malloc(i64 %12)
%14 = icmp ne i8* %13, null
%15 = zext i1 %14 to i32
%16 = tail call i32 (i32, ...) bitcast (i32 (...)* @assert to i32 (i32, ...)*)(i32 %15)
%17 = tail call i8* @strcpy(i8* dereferenceable(1) %13, i8* dereferenceable(1) %0)
ret i8* %13
}