int addr_copy(const char *list, const char *word, char *buf, int buf_size) {
  int word_len;
  assert(word);
  assert(list > word);
  word_len = list - word;
  if (word_len >= buf_size)
    return -1;
  memcpy(buf, word, word_len);
  buf[word_len] = '\0';
  return 0;
}


x86 assembly (clang, -O0):
<addr_copy>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %ecx,-0x24(%rbp)
mov    -0x18(%rbp),%rdi
mov    $0x0,%al
call   1030 <assert@plt>
mov    -0x10(%rbp),%rax
cmp    -0x18(%rbp),%rax
seta   %al
and    $0x1,%al
movzbl %al,%edi
mov    $0x0,%al
call   1030 <assert@plt>
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,-0x28(%rbp)
mov    -0x28(%rbp),%eax
cmp    -0x24(%rbp),%eax
jl     117f <addr_copy+0x5f>
movl   $0xffffffff,-0x4(%rbp)
jmp    11a3 <addr_copy+0x83>
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rsi
movslq -0x28(%rbp),%rdx
call   1040 <memcpy@plt>
mov    -0x20(%rbp),%rax
movslq -0x28(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<addr_copy>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %ecx,%ebp
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%rbx
mov    %rsi,%rdi
xor    %eax,%eax
call   1030 <assert@plt>
xor    %edi,%edi
sub    %r15,%rbx
seta   %dil
xor    %eax,%eax
call   1030 <assert@plt>
mov    $0xffffffff,%eax
cmp    %ebp,%ebx
jge    116d <addr_copy+0x4d>
movslq %ebx,%rbx
mov    %r14,%rdi
mov    %r15,%rsi
mov    %rbx,%rdx
call   1040 <memcpy@plt>
movb   $0x0,(%r14,%rbx,1)
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @addr_copy(i8* %0, i8* %1, i8* %2, i32 %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32
store i8* %0, i8** %6
store i8* %1, i8** %7
store i8* %2, i8** %8
store i32 %3, i32* %9
%11 = load i8*, i8** %7
%12 = call i32 (i8*, ...) bitcast (i32 (...)* @assert to i32 (i8*, ...)*)(i8* %11)
%13 = load i8*, i8** %6
%14 = load i8*, i8** %7
%15 = icmp ugt i8* %13, %14
%16 = zext i1 %15 to i32
%17 = call i32 (i32, ...) bitcast (i32 (...)* @assert to i32 (i32, ...)*)(i32 %16)
%18 = load i8*, i8** %6
%19 = load i8*, i8** %7
%20 = ptrtoint i8* %18 to i64
%21 = ptrtoint i8* %19 to i64
%22 = sub i64 %20, %21
%23 = trunc i64 %22 to i32
store i32 %23, i32* %10
%24 = load i32, i32* %10
%25 = load i32, i32* %9
%26 = icmp sge i32 %24, %25
br i1 %26, label %27, label %28
27: ; preds = %4
store i32 -1, i32* %5
br label %37
28: ; preds = %4
%29 = load i8*, i8** %8
%30 = load i8*, i8** %7
%31 = load i32, i32* %10
%32 = sext i32 %31 to i64
%33 = load i8*, i8** %8
%34 = load i32, i32* %10
%35 = sext i32 %34 to i64
%36 = getelementptr inbounds i8, i8* %33, i64 %35
store i8 0, i8* %36
store i32 0, i32* %5
br label %37
37: ; preds = %28, %27
%38 = load i32, i32* %5
ret i32 %38
}

LLVM IR (-O2):
define i32 @addr_copy(i8* %0, i8* %1, i8* nocapture writeonly %2, i32 %3) {
%5 = tail call i32 (i8*, ...) bitcast (i32 (...)* @assert to i32 (i8*, ...)*)(i8* %1)
%6 = icmp ugt i8* %0, %1
%7 = zext i1 %6 to i32
%8 = tail call i32 (i32, ...) bitcast (i32 (...)* @assert to i32 (i32, ...)*)(i32 %7)
%9 = ptrtoint i8* %0 to i64
%10 = ptrtoint i8* %1 to i64
%11 = sub i64 %9, %10
%12 = trunc i64 %11 to i32
%13 = icmp slt i32 %12, %3
br i1 %13, label %14, label %18
14: ; preds = %4
%15 = shl i64 %11, 32
%16 = ashr exact i64 %15, 32
tail call void @llvm.memcpy.p0i8.p0i8.i64(i8* align 1 %2, i8* align 1 %1, i64 %16, i1 false)
%17 = getelementptr inbounds i8, i8* %2, i64 %16
store i8 0, i8* %17
br label %18
18: ; preds = %4, %14
%19 = phi i32 [ 0, %14 ], [ -1, %4 ]
ret i32 %19
}