void fn2(char *str) {
  char buf[32] = {0};
  char *cp;
  {
    unsigned long a = 0;
    __asm__("\tmov %%rsp, %%rax\n" : "=a"(a));
    printf("%%rsp %016lx\n", a);
    cp = (void *)a;
  };
  memcpy(buf, str, strlen(str));
  for (cp = buf; *cp; cp++) {
    printf("%02x", (unsigned char)*cp);
    if (((unsigned long)cp + 1) % 16 == 0) {
      printf("\n");
    } else if (((unsigned long)cp + 1) % 8 == 0) {
      printf("   ");
    } else if (((unsigned long)cp + 1) % 4 == 0) {
      printf(" ");
    }
  }
  printf("\n");
  printf("%016lx\n", *(unsigned long *)&buf[40]);
}


x86 assembly (clang, -O0):
<fn2>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1050 <memset@plt>
movq   $0x0,-0x40(%rbp)
mov    %rsp,%rax
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rsi
lea    0xe8a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x40(%rbp),%rax
mov    %rax,-0x38(%rbp)
lea    -0x30(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x50(%rbp),%rdi
mov    -0x48(%rbp),%rsi
mov    %rax,%rdx
call   1060 <memcpy@plt>
lea    -0x30(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
cmpb   $0x0,(%rax)
je     126e <fn2+0x12e>
mov    -0x38(%rbp),%rax
movzbl (%rax),%esi
lea    0xe3d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x38(%rbp),%rax
add    $0x1,%rax
and    $0xf,%rax
cmp    $0x0,%rax
jne    1201 <fn2+0xc1>
lea    0xe17(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1258 <fn2+0x118>
mov    -0x38(%rbp),%rax
add    $0x1,%rax
and    $0x7,%rax
cmp    $0x0,%rax
jne    122a <fn2+0xea>
lea    0xdf5(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1253 <fn2+0x113>
mov    -0x38(%rbp),%rax
add    $0x1,%rax
and    $0x3,%rax
cmp    $0x0,%rax
jne    124e <fn2+0x10e>
lea    0xdce(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1253 <fn2+0x113>
jmp    1258 <fn2+0x118>
jmp    125d <fn2+0x11d>
mov    -0x38(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x38(%rbp)
jmp    11b6 <fn2+0x76>
lea    0xd97(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x8(%rbp),%rsi
lea    0xd7f(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fn2>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rdi,%r14
mov    %rsp,%rax
lea    0xe98(%rip),%rdi
xor    %r15d,%r15d
mov    %rax,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %r14,%rdi
call   1040 <strlen@plt>
mov    %rax,%rbx
add    $0xffffffffffffffe0,%rax
neg    %rax
cmp    $0x20,%rbx
cmovb  %rax,%r15
lea    (%rsp,%rbx,1),%rdi
xor    %esi,%esi
mov    %r15,%rdx
call   1060 <memset@plt>
mov    %rsp,%rdi
mov    %r14,%rsi
mov    %rbx,%rdx
call   1070 <memcpy@plt>
mov    (%rsp),%al
test   %al,%al
je     121c <fn2+0xcc>
lea    0x1(%rsp),%r12
lea    0xe50(%rip),%r15
lea    0xe4e(%rip),%r14
mov    %r12,%rbx
jmp    11eb <fn2+0x9b>
nopw   0x0(%rax,%rax,1)
mov    %r14,%rdi
xor    %eax,%eax
call   1050 <printf@plt>
movzbl (%r12),%eax
add    $0x1,%rbx
add    $0x1,%r12
test   %al,%al
je     121c <fn2+0xcc>
movzbl %al,%esi
mov    %r15,%rdi
xor    %eax,%eax
call   1050 <printf@plt>
test   $0xf,%bl
je     1210 <fn2+0xc0>
test   $0x7,%bl
je     11d0 <fn2+0x80>
test   $0x3,%bl
jne    11da <fn2+0x8a>
mov    $0x20,%edi
jmp    1215 <fn2+0xc5>
xchg   %ax,%ax
mov    $0xa,%edi
call   1030 <putchar@plt>
jmp    11da <fn2+0x8a>
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    0x28(%rsp),%rsi
lea    0xdd4(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [14 x i8] c"%%rsp %016lx\0A\00"
@.str.1 = [5 x i8] c"%02x\00"
@.str.2 = [2 x i8] c"\0A\00"
@.str.3 = [4 x i8] c" \00"
@.str.4 = [2 x i8] c" \00"
@.str.5 = [8 x i8] c"%016lx\0A\00"
define void @fn2(i8* %0) {
%2 = alloca i8*
%3 = alloca [32 x i8]
%4 = alloca i8*
%5 = alloca i64
store i8* %0, i8** %2
%6 = bitcast [32 x i8]* %3 to i8*
store i64 0, i64* %5
%7 = call i64 asm "\09mov %rsp, %rax\0A", "={ax},~{dirflag},~{fpsr},~{flags}"()
store i64 %7, i64* %5
%8 = load i64, i64* %5
%9 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i64 %8)
%10 = load i64, i64* %5
%11 = inttoptr i64 %10 to i8*
store i8* %11, i8** %4
%12 = getelementptr inbounds [32 x i8], [32 x i8]* %3, i64 0, i64 0
%13 = load i8*, i8** %2
%14 = load i8*, i8** %2
%15 = call i64 @strlen(i8* %14)
%16 = getelementptr inbounds [32 x i8], [32 x i8]* %3, i64 0, i64 0
store i8* %16, i8** %4
br label %17
17: ; preds = %52, %1
%18 = load i8*, i8** %4
%19 = load i8, i8* %18
%20 = icmp ne i8 %19, 0
br i1 %20, label %21, label %55
21: ; preds = %17
%22 = load i8*, i8** %4
%23 = load i8, i8* %22
%24 = zext i8 %23 to i32
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 %24)
%26 = load i8*, i8** %4
%27 = ptrtoint i8* %26 to i64
%28 = add i64 %27, 1
%29 = urem i64 %28, 16
%30 = icmp eq i64 %29, 0
br i1 %30, label %31, label %33
31: ; preds = %21
%32 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
br label %51
33: ; preds = %21
%34 = load i8*, i8** %4
%35 = ptrtoint i8* %34 to i64
%36 = add i64 %35, 1
%37 = urem i64 %36, 8
%38 = icmp eq i64 %37, 0
br i1 %38, label %39, label %41
39: ; preds = %33
%40 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0))
br label %50
41: ; preds = %33
%42 = load i8*, i8** %4
%43 = ptrtoint i8* %42 to i64
%44 = add i64 %43, 1
%45 = urem i64 %44, 4
%46 = icmp eq i64 %45, 0
br i1 %46, label %47, label %49
47: ; preds = %41
%48 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
br label %49
49: ; preds = %47, %41
br label %50
50: ; preds = %49, %39
br label %51
51: ; preds = %50, %31
br label %52
52: ; preds = %51
%53 = load i8*, i8** %4
%54 = getelementptr inbounds i8, i8* %53, i32 1
store i8* %54, i8** %4
br label %17
55: ; preds = %17
%56 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%57 = getelementptr inbounds [32 x i8], [32 x i8]* %3, i64 0, i64 40
%58 = bitcast i8* %57 to i64*
%59 = load i64, i64* %58
%60 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.5, i64 0, i64 0), i64 %59)
ret void
}

LLVM IR (-O2):
@.str = [14 x i8] c"%%rsp %016lx\0A\00"
@.str.1 = [5 x i8] c"%02x\00"
@.str.3 = [4 x i8] c" \00"
@.str.5 = [8 x i8] c"%016lx\0A\00"
define void @fn2(i8* nocapture %0) {
%2 = alloca [32 x i8]
%3 = getelementptr inbounds [32 x i8], [32 x i8]* %2, i64 0, i64 0
%4 = tail call i64 asm "\09mov %rsp, %rax\0A", "={ax},~{dirflag},~{fpsr},~{flags}"()
%5 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i64 %4)
%6 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%7 = icmp ugt i64 %6, 31
%8 = sub i64 32, %6
%9 = select i1 %7, i64 0, i64 %8
%10 = getelementptr [32 x i8], [32 x i8]* %2, i64 0, i64 %6
%11 = load i8, i8* %3
%12 = icmp eq i8 %11, 0
br i1 %12, label %38, label %13
13: ; preds = %1, %34
%14 = phi i8 [ %36, %34 ], [ %11, %1 ]
%15 = phi i8* [ %35, %34 ], [ %3, %1 ]
%16 = zext i8 %14 to i32
%17 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 %16)
%18 = ptrtoint i8* %15 to i64
%19 = add i64 %18, 1
%20 = and i64 %19, 15
%21 = icmp eq i64 %20, 0
br i1 %21, label %22, label %24
22: ; preds = %13
%23 = call i32 @putchar(i32 10)
br label %34
24: ; preds = %13
%25 = and i64 %19, 7
%26 = icmp eq i64 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %24
%28 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0))
br label %34
29: ; preds = %24
%30 = and i64 %19, 3
%31 = icmp eq i64 %30, 0
br i1 %31, label %32, label %34
32: ; preds = %29
%33 = call i32 @putchar(i32 32)
br label %34
34: ; preds = %22, %29, %32, %27
%35 = getelementptr inbounds i8, i8* %15, i64 1
%36 = load i8, i8* %35
%37 = icmp eq i8 %36, 0
br i1 %37, label %38, label %13
38: ; preds = %34, %1
%39 = call i32 @putchar(i32 10)
%40 = getelementptr inbounds [32 x i8], [32 x i8]* %2, i64 0, i64 40
%41 = bitcast i8* %40 to i64*
%42 = load i64, i64* %41
%43 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str.5, i64 0, i64 0), i64 %42)
ret void
}