void fn3(char *str) {
  char buf = {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);
}


x86 assembly (clang, -O0):
<fn3>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
movb   $0x0,-0x9(%rbp)
movq   $0x0,-0x20(%rbp)
mov    %rsp,%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rsi
lea    0xea6(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x28(%rbp),%rsi
mov    %rax,%rdx
lea    -0x9(%rbp),%rdi
call   1050 <memcpy@plt>
lea    -0x9(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
cmpb   $0x0,(%rax)
je     124a <fn3+0x11a>
mov    -0x18(%rbp),%rax
movzbl (%rax),%esi
lea    0xe61(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x18(%rbp),%rax
add    $0x1,%rax
and    $0xf,%rax
cmp    $0x0,%rax
jne    11dd <fn3+0xad>
lea    0xe3b(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1234 <fn3+0x104>
mov    -0x18(%rbp),%rax
add    $0x1,%rax
and    $0x7,%rax
cmp    $0x0,%rax
jne    1206 <fn3+0xd6>
lea    0xe19(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    122f <fn3+0xff>
mov    -0x18(%rbp),%rax
add    $0x1,%rax
and    $0x3,%rax
cmp    $0x0,%rax
jne    122a <fn3+0xfa>
lea    0xdf2(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    122f <fn3+0xff>
jmp    1234 <fn3+0x104>
jmp    1239 <fn3+0x109>
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
jmp    1192 <fn3+0x62>
lea    0xdbb(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x9(%rbp),%rsi
lea    0xda3(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fn3>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdi,%rbx
movb   $0x0,(%rsp)
mov    %rsp,%rax
lea    0xea7(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %rsp,%rdi
mov    %rbx,%rsi
mov    %rax,%rdx
call   1060 <memcpy@plt>
mov    (%rsp),%al
test   %al,%al
je     11ec <fn3+0xac>
lea    0x1(%rsp),%r12
lea    0xe82(%rip),%r15
lea    0xe80(%rip),%r14
mov    %r12,%rbx
jmp    11bb <fn3+0x7b>
nopl   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     11ec <fn3+0xac>
movzbl %al,%esi
mov    %r15,%rdi
xor    %eax,%eax
call   1050 <printf@plt>
test   $0xf,%bl
je     11e0 <fn3+0xa0>
test   $0x7,%bl
je     11a0 <fn3+0x60>
test   $0x3,%bl
jne    11aa <fn3+0x6a>
mov    $0x20,%edi
jmp    11e5 <fn3+0xa5>
xchg   %ax,%ax
mov    $0xa,%edi
call   1030 <putchar@plt>
jmp    11aa <fn3+0x6a>
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    (%rsp),%rsi
lea    0xe05(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
add    $0x8,%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 @fn3(i8* %0) {
%2 = alloca i8*
%3 = alloca i8
%4 = alloca i8*
%5 = alloca i64
store i8* %0, i8** %2
store i8 0, i8* %3
store i64 0, i64* %5
%6 = call i64 asm "\09mov %rsp, %rax\0A", "={ax},~{dirflag},~{fpsr},~{flags}"()
store i64 %6, i64* %5
%7 = load i64, i64* %5
%8 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i64 %7)
%9 = load i64, i64* %5
%10 = inttoptr i64 %9 to i8*
store i8* %10, i8** %4
%11 = load i8*, i8** %2
%12 = load i8*, i8** %2
%13 = call i64 @strlen(i8* %12)
store i8* %3, i8** %4
br label %14
14: ; preds = %49, %1
%15 = load i8*, i8** %4
%16 = load i8, i8* %15
%17 = icmp ne i8 %16, 0
br i1 %17, label %18, label %52
18: ; preds = %14
%19 = load i8*, i8** %4
%20 = load i8, i8* %19
%21 = zext i8 %20 to i32
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 %21)
%23 = load i8*, i8** %4
%24 = ptrtoint i8* %23 to i64
%25 = add i64 %24, 1
%26 = urem i64 %25, 16
%27 = icmp eq i64 %26, 0
br i1 %27, label %28, label %30
28: ; preds = %18
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
br label %48
30: ; preds = %18
%31 = load i8*, i8** %4
%32 = ptrtoint i8* %31 to i64
%33 = add i64 %32, 1
%34 = urem i64 %33, 8
%35 = icmp eq i64 %34, 0
br i1 %35, label %36, label %38
36: ; preds = %30
%37 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0))
br label %47
38: ; preds = %30
%39 = load i8*, i8** %4
%40 = ptrtoint i8* %39 to i64
%41 = add i64 %40, 1
%42 = urem i64 %41, 4
%43 = icmp eq i64 %42, 0
br i1 %43, label %44, label %46
44: ; preds = %38
%45 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
br label %46
46: ; preds = %44, %38
br label %47
47: ; preds = %46, %36
br label %48
48: ; preds = %47, %28
br label %49
49: ; preds = %48
%50 = load i8*, i8** %4
%51 = getelementptr inbounds i8, i8* %50, i32 1
store i8* %51, i8** %4
br label %14
52: ; preds = %14
%53 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%54 = bitcast i8* %3 to i64*
%55 = load i64, i64* %54
%56 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.5, i64 0, i64 0), i64 %55)
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 @fn3(i8* nocapture %0) {
%2 = alloca i8
store i8 0, i8* %2
%3 = tail call i64 asm "\09mov %rsp, %rax\0A", "={ax},~{dirflag},~{fpsr},~{flags}"()
%4 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i64 %3)
%5 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%6 = load i8, i8* %2
%7 = icmp eq i8 %6, 0
br i1 %7, label %33, label %8
8: ; preds = %1, %29
%9 = phi i8 [ %31, %29 ], [ %6, %1 ]
%10 = phi i8* [ %30, %29 ], [ %2, %1 ]
%11 = zext i8 %9 to i32
%12 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 %11)
%13 = ptrtoint i8* %10 to i64
%14 = add i64 %13, 1
%15 = and i64 %14, 15
%16 = icmp eq i64 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %8
%18 = call i32 @putchar(i32 10)
br label %29
19: ; preds = %8
%20 = and i64 %14, 7
%21 = icmp eq i64 %20, 0
br i1 %21, label %22, label %24
22: ; preds = %19
%23 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0))
br label %29
24: ; preds = %19
%25 = and i64 %14, 3
%26 = icmp eq i64 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %24
%28 = call i32 @putchar(i32 32)
br label %29
29: ; preds = %17, %24, %27, %22
%30 = getelementptr inbounds i8, i8* %10, i64 1
%31 = load i8, i8* %30
%32 = icmp eq i8 %31, 0
br i1 %32, label %33, label %8
33: ; preds = %29, %1
%34 = call i32 @putchar(i32 10)
%35 = bitcast i8* %2 to i64*
%36 = load i64, i64* %35
%37 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str.5, i64 0, i64 0), i64 %36)
ret void
}