void dbg_seg(void) {
  unsigned short cs, ds, ss;
  unsigned int esp;
  __asm__ __volatile__("movw %%cs, %0" : "=m"(cs));
  __asm__ __volatile__("movw %%ds, %0" : "=m"(ds));
  __asm__ __volatile__("movw %%ss, %0" : "=m"(ss));
  __asm__ __volatile__("movl %%esp, %0" : "=m"(esp));
  dbgf("CS = %d * 8 + %d", cs >> 3, cs & 0x07);
  dbgf(", DS = %d * 8 + %d", ds >> 3, ds & 0x07);
  dbgf(", SS = %d * 8 + %d", ss >> 3, ss & 0x07);
  dbgf(", ESP = %X\n", esp);
}


x86 assembly (clang, -O0):
<dbg_seg>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %cs,-0x2(%rbp)
mov    %ds,-0x4(%rbp)
mov    %ss,-0x6(%rbp)
mov    %esp,-0xc(%rbp)
movzwl -0x2(%rbp),%esi
sar    $0x3,%esi
movzwl -0x2(%rbp),%edx
and    $0x7,%edx
lea    0xec7(%rip),%rdi
mov    $0x0,%al
call   1030 <dbgf@plt>
movzwl -0x4(%rbp),%esi
sar    $0x3,%esi
movzwl -0x4(%rbp),%edx
and    $0x7,%edx
lea    0xebc(%rip),%rdi
mov    $0x0,%al
call   1030 <dbgf@plt>
movzwl -0x6(%rbp),%esi
sar    $0x3,%esi
movzwl -0x6(%rbp),%edx
and    $0x7,%edx
lea    0xeb3(%rip),%rdi
mov    $0x0,%al
call   1030 <dbgf@plt>
mov    -0xc(%rbp),%esi
lea    0xeb5(%rip),%rdi
mov    $0x0,%al
call   1030 <dbgf@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dbg_seg>:
sub    $0x18,%rsp
mov    %cs,0x12(%rsp)
mov    %ds,0x10(%rsp)
mov    %ss,0xe(%rsp)
mov    %esp,0x14(%rsp)
movzwl 0x12(%rsp),%edx
mov    %edx,%esi
shr    $0x3,%esi
and    $0x7,%edx
lea    0xec8(%rip),%rdi
xor    %eax,%eax
call   1030 <dbgf@plt>
movzwl 0x10(%rsp),%edx
mov    %edx,%esi
shr    $0x3,%esi
and    $0x7,%edx
lea    0xebe(%rip),%rdi
xor    %eax,%eax
call   1030 <dbgf@plt>
movzwl 0xe(%rsp),%edx
mov    %edx,%esi
shr    $0x3,%esi
and    $0x7,%edx
lea    0xeb6(%rip),%rdi
xor    %eax,%eax
call   1030 <dbgf@plt>
mov    0x14(%rsp),%esi
lea    0xeb7(%rip),%rdi
xor    %eax,%eax
call   1030 <dbgf@plt>
add    $0x18,%rsp
ret

LLVM IR (-O0):
@.str = [17 x i8] c"CS = %d * 8 + %d\00"
@.str.1 = [19 x i8] c", DS = %d * 8 + %d\00"
@.str.2 = [19 x i8] c", SS = %d * 8 + %d\00"
@.str.3 = [12 x i8] c", ESP = %X\0A\00"
define void @dbg_seg() {
%1 = alloca i16
%2 = alloca i16
%3 = alloca i16
%4 = alloca i32
%5 = load i16, i16* %1
%6 = zext i16 %5 to i32
%7 = ashr i32 %6, 3
%8 = load i16, i16* %1
%9 = zext i16 %8 to i32
%10 = and i32 %9, 7
%11 = call i32 (i8*, i32, i32, ...) bitcast (i32 (...)* @dbgf to i32 (i8*, i32, i32, ...)*)(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %7, i32 %10)
%12 = load i16, i16* %2
%13 = zext i16 %12 to i32
%14 = ashr i32 %13, 3
%15 = load i16, i16* %2
%16 = zext i16 %15 to i32
%17 = and i32 %16, 7
%18 = call i32 (i8*, i32, i32, ...) bitcast (i32 (...)* @dbgf to i32 (i8*, i32, i32, ...)*)(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i32 %14, i32 %17)
%19 = load i16, i16* %3
%20 = zext i16 %19 to i32
%21 = ashr i32 %20, 3
%22 = load i16, i16* %3
%23 = zext i16 %22 to i32
%24 = and i32 %23, 7
%25 = call i32 (i8*, i32, i32, ...) bitcast (i32 (...)* @dbgf to i32 (i8*, i32, i32, ...)*)(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i32 %21, i32 %24)
%26 = load i32, i32* %4
%27 = call i32 (i8*, i32, ...) bitcast (i32 (...)* @dbgf to i32 (i8*, i32, ...)*)(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), i32 %26)
ret void
}

LLVM IR (-O2):
@.str = [17 x i8] c"CS = %d * 8 + %d\00"
@.str.1 = [19 x i8] c", DS = %d * 8 + %d\00"
@.str.2 = [19 x i8] c", SS = %d * 8 + %d\00"
@.str.3 = [12 x i8] c", ESP = %X\0A\00"
define void @dbg_seg() {
%1 = alloca i16
%2 = alloca i16
%3 = alloca i16
%4 = alloca i32
%5 = bitcast i16* %1 to i8*
%6 = bitcast i16* %2 to i8*
%7 = bitcast i16* %3 to i8*
%8 = bitcast i32* %4 to i8*
%9 = load i16, i16* %1
%10 = zext i16 %9 to i32
%11 = lshr i32 %10, 3
%12 = and i32 %10, 7
%13 = call i32 (i8*, i32, i32, ...) bitcast (i32 (...)* @dbgf to i32 (i8*, i32, i32, ...)*)(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %11, i32 %12)
%14 = load i16, i16* %2
%15 = zext i16 %14 to i32
%16 = lshr i32 %15, 3
%17 = and i32 %15, 7
%18 = call i32 (i8*, i32, i32, ...) bitcast (i32 (...)* @dbgf to i32 (i8*, i32, i32, ...)*)(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i32 %16, i32 %17)
%19 = load i16, i16* %3
%20 = zext i16 %19 to i32
%21 = lshr i32 %20, 3
%22 = and i32 %20, 7
%23 = call i32 (i8*, i32, i32, ...) bitcast (i32 (...)* @dbgf to i32 (i8*, i32, i32, ...)*)(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i32 %21, i32 %22)
%24 = load i32, i32* %4
%25 = call i32 (i8*, i32, ...) bitcast (i32 (...)* @dbgf to i32 (i8*, i32, ...)*)(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), i32 %24)
ret void
}