void dump_registers() {
  const char *reg_names[] = {"eax", "ebx", "ecx", "edx",
                             "esp", "ebp", "esi", "edi"};
  int reg_val[8];
  asm("movl %%eax, %0" : "=r"(reg_val[0]));
  asm("movl %%ebx, %0" : "=r"(reg_val[1]));
  asm("movl %%ecx, %0" : "=r"(reg_val[2]));
  asm("movl %%edx, %0" : "=r"(reg_val[3]));
  asm("movl %%esp, %0" : "=r"(reg_val[4]));
  asm("movl %%ebp, %0" : "=r"(reg_val[5]));
  asm("movl %%esi, %0" : "=r"(reg_val[6]));
  asm("movl %%edi, %0" : "=r"(reg_val[7]));
  for (int i = 0; i < 8; i++) {
    if (i != 7) {
      kprint("%s %x\n", reg_names[i], reg_val[i]);
    } else {
      kprint("%s %x", reg_names[i], reg_val[i]);
    }
  }
}


x86 assembly (clang, -O0):
<dump_registers>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
lea    -0x40(%rbp),%rdi
lea    0x2cad(%rip),%rsi
mov    $0x40,%edx
call   1030 <memcpy@plt>
mov    %eax,%eax
mov    %eax,-0x60(%rbp)
mov    %ebx,%eax
mov    %eax,-0x5c(%rbp)
mov    %ecx,%eax
mov    %eax,-0x58(%rbp)
mov    %edx,%eax
mov    %eax,-0x54(%rbp)
mov    %esp,%eax
mov    %eax,-0x50(%rbp)
mov    %ebp,%eax
mov    %eax,-0x4c(%rbp)
mov    %esi,%eax
mov    %eax,-0x48(%rbp)
mov    %edi,%eax
mov    %eax,-0x44(%rbp)
movl   $0x0,-0x64(%rbp)
cmpl   $0x8,-0x64(%rbp)
jge    11d6 <dump_registers+0xb6>
cmpl   $0x7,-0x64(%rbp)
je     11a4 <dump_registers+0x84>
movslq -0x64(%rbp),%rax
mov    -0x40(%rbp,%rax,8),%rsi
movslq -0x64(%rbp),%rax
mov    -0x60(%rbp,%rax,4),%edx
lea    0xe88(%rip),%rdi
mov    $0x0,%al
call   1040 <kprint@plt>
jmp    11c3 <dump_registers+0xa3>
movslq -0x64(%rbp),%rax
mov    -0x40(%rbp,%rax,8),%rsi
movslq -0x64(%rbp),%rax
mov    -0x60(%rbp,%rax,4),%edx
lea    0xe6b(%rip),%rdi
mov    $0x0,%al
call   1040 <kprint@plt>
jmp    11c8 <dump_registers+0xa8>
mov    -0x64(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x64(%rbp)
jmp    116c <dump_registers+0x4c>
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dump_registers>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %eax,%edx
mov    %ebx,%r14d
mov    %ecx,%r15d
mov    %edx,%ebp
mov    %esp,%r13d
mov    %ebp,%r12d
mov    %esi,%eax
mov    %eax,(%rsp)
mov    %edi,%eax
mov    %eax,0x4(%rsp)
lea    0xee3(%rip),%rbx
lea    0xebc(%rip),%rsi
mov    %rbx,%rdi
xor    %eax,%eax
call   1030 <kprint@plt>
lea    0xeaf(%rip),%rsi
mov    %rbx,%rdi
mov    %r14d,%edx
xor    %eax,%eax
call   1030 <kprint@plt>
lea    0xe9f(%rip),%rsi
mov    %rbx,%rdi
mov    %r15d,%edx
xor    %eax,%eax
call   1030 <kprint@plt>
lea    0xe8f(%rip),%rsi
mov    %rbx,%rdi
mov    %ebp,%edx
xor    %eax,%eax
call   1030 <kprint@plt>
lea    0xe80(%rip),%rsi
mov    %rbx,%rdi
mov    %r13d,%edx
xor    %eax,%eax
call   1030 <kprint@plt>
lea    0xe70(%rip),%rsi
mov    %rbx,%rdi
mov    %r12d,%edx
xor    %eax,%eax
call   1030 <kprint@plt>
lea    0xe60(%rip),%rsi
mov    %rbx,%rdi
mov    (%rsp),%edx
xor    %eax,%eax
call   1030 <kprint@plt>
lea    0xe5b(%rip),%rdi
lea    0xe49(%rip),%rsi
mov    0x4(%rsp),%edx
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <kprint@plt>

LLVM IR (-O0):
@.str = [4 x i8] c"eax\00"
@.str.1 = [4 x i8] c"ebx\00"
@.str.2 = [4 x i8] c"ecx\00"
@.str.3 = [4 x i8] c"edx\00"
@.str.4 = [4 x i8] c"esp\00"
@.str.5 = [4 x i8] c"ebp\00"
@.str.6 = [4 x i8] c"esi\00"
@.str.7 = [4 x i8] c"edi\00"
@__const.dump_registers.reg_names = [8 x i8*] [i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i32 0, i32 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i32 0, i32 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i32 0, i32 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i32 0, i32 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i32 0, i32 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i32 0, i32 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.6, i32 0, i32 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.7, i32 0, i32 0)]
@.str.8 = [7 x i8] c"%s %x\0A\00"
@.str.9 = [6 x i8] c"%s %x\00"
define void @dump_registers() {
%1 = alloca [8 x i8*]
%2 = alloca [8 x i32]
%3 = alloca i32
%4 = bitcast [8 x i8*]* %1 to i8*
%5 = getelementptr inbounds [8 x i32], [8 x i32]* %2, i64 0, i64 0
%6 = call i32 asm "movl %eax, $0", "=r,~{dirflag},~{fpsr},~{flags}"()
store i32 %6, i32* %5
%7 = getelementptr inbounds [8 x i32], [8 x i32]* %2, i64 0, i64 1
%8 = call i32 asm "movl %ebx, $0", "=r,~{dirflag},~{fpsr},~{flags}"()
store i32 %8, i32* %7
%9 = getelementptr inbounds [8 x i32], [8 x i32]* %2, i64 0, i64 2
%10 = call i32 asm "movl %ecx, $0", "=r,~{dirflag},~{fpsr},~{flags}"()
store i32 %10, i32* %9
%11 = getelementptr inbounds [8 x i32], [8 x i32]* %2, i64 0, i64 3
%12 = call i32 asm "movl %edx, $0", "=r,~{dirflag},~{fpsr},~{flags}"()
store i32 %12, i32* %11
%13 = getelementptr inbounds [8 x i32], [8 x i32]* %2, i64 0, i64 4
%14 = call i32 asm "movl %esp, $0", "=r,~{dirflag},~{fpsr},~{flags}"()
store i32 %14, i32* %13
%15 = getelementptr inbounds [8 x i32], [8 x i32]* %2, i64 0, i64 5
%16 = call i32 asm "movl %ebp, $0", "=r,~{dirflag},~{fpsr},~{flags}"()
store i32 %16, i32* %15
%17 = getelementptr inbounds [8 x i32], [8 x i32]* %2, i64 0, i64 6
%18 = call i32 asm "movl %esi, $0", "=r,~{dirflag},~{fpsr},~{flags}"()
store i32 %18, i32* %17
%19 = getelementptr inbounds [8 x i32], [8 x i32]* %2, i64 0, i64 7
%20 = call i32 asm "movl %edi, $0", "=r,~{dirflag},~{fpsr},~{flags}"()
store i32 %20, i32* %19
store i32 0, i32* %3
br label %21
21: ; preds = %48, %0
%22 = load i32, i32* %3
%23 = icmp slt i32 %22, 8
br i1 %23, label %24, label %51
24: ; preds = %21
%25 = load i32, i32* %3
%26 = icmp ne i32 %25, 7
br i1 %26, label %27, label %37
27: ; preds = %24
%28 = load i32, i32* %3
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds [8 x i8*], [8 x i8*]* %1, i64 0, i64 %29
%31 = load i8*, i8** %30
%32 = load i32, i32* %3
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds [8 x i32], [8 x i32]* %2, i64 0, i64 %33
%35 = load i32, i32* %34
%36 = call i32 (i8*, i8*, i32, ...) bitcast (i32 (...)* @kprint to i32 (i8*, i8*, i32, ...)*)(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.8, i64 0, i64 0), i8* %31, i32 %35)
br label %47
37: ; preds = %24
%38 = load i32, i32* %3
%39 = sext i32 %38 to i64
%40 = getelementptr inbounds [8 x i8*], [8 x i8*]* %1, i64 0, i64 %39
%41 = load i8*, i8** %40
%42 = load i32, i32* %3
%43 = sext i32 %42 to i64
%44 = getelementptr inbounds [8 x i32], [8 x i32]* %2, i64 0, i64 %43
%45 = load i32, i32* %44
%46 = call i32 (i8*, i8*, i32, ...) bitcast (i32 (...)* @kprint to i32 (i8*, i8*, i32, ...)*)(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.9, i64 0, i64 0), i8* %41, i32 %45)
br label %47
47: ; preds = %37, %27
br label %48
48: ; preds = %47
%49 = load i32, i32* %3
%50 = add nsw i32 %49, 1
store i32 %50, i32* %3
br label %21
51: ; preds = %21
ret void
}

LLVM IR (-O2):
@.str = [4 x i8] c"eax\00"
@.str.1 = [4 x i8] c"ebx\00"
@.str.2 = [4 x i8] c"ecx\00"
@.str.3 = [4 x i8] c"edx\00"
@.str.4 = [4 x i8] c"esp\00"
@.str.5 = [4 x i8] c"ebp\00"
@.str.6 = [4 x i8] c"esi\00"
@.str.7 = [4 x i8] c"edi\00"
@.str.8 = [7 x i8] c"%s %x\0A\00"
@.str.9 = [6 x i8] c"%s %x\00"
define void @dump_registers() {
%1 = tail call i32 asm "movl %eax, $0", "=r,~{dirflag},~{fpsr},~{flags}"()
%2 = tail call i32 asm "movl %ebx, $0", "=r,~{dirflag},~{fpsr},~{flags}"()
%3 = tail call i32 asm "movl %ecx, $0", "=r,~{dirflag},~{fpsr},~{flags}"()
%4 = tail call i32 asm "movl %edx, $0", "=r,~{dirflag},~{fpsr},~{flags}"()
%5 = tail call i32 asm "movl %esp, $0", "=r,~{dirflag},~{fpsr},~{flags}"()
%6 = tail call i32 asm "movl %ebp, $0", "=r,~{dirflag},~{fpsr},~{flags}"()
%7 = tail call i32 asm "movl %esi, $0", "=r,~{dirflag},~{fpsr},~{flags}"()
%8 = tail call i32 asm "movl %edi, $0", "=r,~{dirflag},~{fpsr},~{flags}"()
%9 = tail call i32 (i8*, i8*, i32, ...) bitcast (i32 (...)* @kprint to i32 (i8*, i8*, i32, ...)*)(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.8, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %1)
%10 = tail call i32 (i8*, i8*, i32, ...) bitcast (i32 (...)* @kprint to i32 (i8*, i8*, i32, ...)*)(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.8, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %2)
%11 = tail call i32 (i8*, i8*, i32, ...) bitcast (i32 (...)* @kprint to i32 (i8*, i8*, i32, ...)*)(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.8, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 %3)
%12 = tail call i32 (i8*, i8*, i32, ...) bitcast (i32 (...)* @kprint to i32 (i8*, i8*, i32, ...)*)(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.8, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i32 %4)
%13 = tail call i32 (i8*, i8*, i32, ...) bitcast (i32 (...)* @kprint to i32 (i8*, i8*, i32, ...)*)(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.8, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i32 %5)
%14 = tail call i32 (i8*, i8*, i32, ...) bitcast (i32 (...)* @kprint to i32 (i8*, i8*, i32, ...)*)(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.8, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0), i32 %6)
%15 = tail call i32 (i8*, i8*, i32, ...) bitcast (i32 (...)* @kprint to i32 (i8*, i8*, i32, ...)*)(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.8, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.6, i64 0, i64 0), i32 %7)
%16 = tail call i32 (i8*, i8*, i32, ...) bitcast (i32 (...)* @kprint to i32 (i8*, i8*, i32, ...)*)(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.9, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.7, i64 0, i64 0), i32 %8)
ret void
}