void zero_fill_bss(void) {
  extern char __START_BSS[];
  extern char __END_BSS[];
  unsigned long len = __END_BSS - __START_BSS;
  unsigned long dst = (unsigned long)__START_BSS;
  const unsigned int mask_int = sizeof(int) - 1;
  const unsigned int mask_short = sizeof(short) - 1;
  if (len == 0) {
    return;
  }
  while (len > 0) {
    if (!(dst & mask_int) && len >= sizeof(int)) {
      *((int *)dst) = 0;
      dst += sizeof(int);
      len -= sizeof(int);
    } else if (!(dst & mask_short) && len >= sizeof(short)) {
      *((short *)dst) = 0;
      dst += sizeof(short);
      len -= sizeof(short);
    } else {
      *((char *)dst) = 0;
      dst += sizeof(char);
      len -= sizeof(char);
    }
  }
}


x86 assembly (clang, -O0):
<zero_fill_bss>:
push   %rbp
mov    %rsp,%rbp
mov    0x2eed(%rip),%rax
mov    0x2ece(%rip),%rcx
sub    %rcx,%rax
mov    %rax,-0x8(%rbp)
mov    0x2ec0(%rip),%rax
mov    %rax,-0x10(%rbp)
movl   $0x3,-0x14(%rbp)
movl   $0x1,-0x18(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    1142 <zero_fill_bss+0x42>
jmp    1202 <zero_fill_bss+0x102>
jmp    1147 <zero_fill_bss+0x47>
cmpq   $0x0,-0x8(%rbp)
jbe    1202 <zero_fill_bss+0x102>
mov    -0x10(%rbp),%rax
and    $0x3,%rax
cmp    $0x0,%rax
jne    1196 <zero_fill_bss+0x96>
cmpq   $0x4,-0x8(%rbp)
jb     1196 <zero_fill_bss+0x96>
mov    -0x10(%rbp),%rax
movl   $0x0,(%rax)
mov    -0x10(%rbp),%rax
add    $0x4,%rax
mov    %rax,-0x10(%rbp)
mov    -0x8(%rbp),%rax
sub    $0x4,%rax
mov    %rax,-0x8(%rbp)
jmp    11fd <zero_fill_bss+0xfd>
mov    -0x10(%rbp),%rax
and    $0x1,%rax
cmp    $0x0,%rax
jne    11d9 <zero_fill_bss+0xd9>
cmpq   $0x2,-0x8(%rbp)
jb     11d9 <zero_fill_bss+0xd9>
mov    -0x10(%rbp),%rax
movw   $0x0,(%rax)
mov    -0x10(%rbp),%rax
add    $0x2,%rax
mov    %rax,-0x10(%rbp)
mov    -0x8(%rbp),%rax
sub    $0x2,%rax
mov    %rax,-0x8(%rbp)
jmp    11f8 <zero_fill_bss+0xf8>
mov    -0x10(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
mov    -0x8(%rbp),%rax
sub    $0x1,%rax
mov    %rax,-0x8(%rbp)
jmp    11fd <zero_fill_bss+0xfd>
jmp    1147 <zero_fill_bss+0x47>
pop    %rbp
ret

x86 assembly (clang, -O2):
<zero_fill_bss>:
mov    0x2ef1(%rip),%rax
cmp    0x2ed2(%rip),%rax
jne    1111 <zero_fill_bss+0x11>
ret
mov    0x2ec8(%rip),%rcx
sub    %rcx,%rax
jmp    1137 <zero_fill_bss+0x37>
nopl   (%rax)
movb   $0x0,(%rcx)
mov    $0xffffffffffffffff,%rdx
mov    $0x1,%esi
add    %rsi,%rcx
add    %rdx,%rax
je     1110 <zero_fill_bss+0x10>
test   $0x3,%cl
jne    1160 <zero_fill_bss+0x60>
cmp    $0x4,%rax
jb     1160 <zero_fill_bss+0x60>
movl   $0x0,(%rcx)
mov    $0xfffffffffffffffc,%rdx
mov    $0x4,%esi
jmp    112f <zero_fill_bss+0x2f>
cs nopw 0x0(%rax,%rax,1)
test   $0x1,%cl
jne    1120 <zero_fill_bss+0x20>
cmp    $0x2,%rax
jb     1120 <zero_fill_bss+0x20>
movw   $0x0,(%rcx)
mov    $0xfffffffffffffffe,%rdx
mov    $0x2,%esi
jmp    112f <zero_fill_bss+0x2f>

LLVM IR (-O0):
@__END_BSS = external global [0 x i8]
@__START_BSS = external global [0 x i8]
define void @zero_fill_bss() {
%1 = alloca i64
%2 = alloca i64
%3 = alloca i32
%4 = alloca i32
store i64 sub (i64 ptrtoint ([0 x i8]* @__END_BSS to i64), i64 ptrtoint ([0 x i8]* @__START_BSS to i64)), i64* %1
store i64 ptrtoint ([0 x i8]* @__START_BSS to i64), i64* %2
store i32 3, i32* %3
store i32 1, i32* %4
%5 = load i64, i64* %1
%6 = icmp eq i64 %5, 0
br i1 %6, label %7, label %8
7: ; preds = %0
br label %49
8: ; preds = %0
br label %9
9: ; preds = %48, %8
%10 = load i64, i64* %1
%11 = icmp ugt i64 %10, 0
br i1 %11, label %12, label %49
12: ; preds = %9
%13 = load i64, i64* %2
%14 = and i64 %13, 3
%15 = icmp ne i64 %14, 0
br i1 %15, label %26, label %16
16: ; preds = %12
%17 = load i64, i64* %1
%18 = icmp uge i64 %17, 4
br i1 %18, label %19, label %26
19: ; preds = %16
%20 = load i64, i64* %2
%21 = inttoptr i64 %20 to i32*
store i32 0, i32* %21
%22 = load i64, i64* %2
%23 = add i64 %22, 4
store i64 %23, i64* %2
%24 = load i64, i64* %1
%25 = sub i64 %24, 4
store i64 %25, i64* %1
br label %48
26: ; preds = %16, %12
%27 = load i64, i64* %2
%28 = and i64 %27, 1
%29 = icmp ne i64 %28, 0
br i1 %29, label %40, label %30
30: ; preds = %26
%31 = load i64, i64* %1
%32 = icmp uge i64 %31, 2
br i1 %32, label %33, label %40
33: ; preds = %30
%34 = load i64, i64* %2
%35 = inttoptr i64 %34 to i16*
store i16 0, i16* %35
%36 = load i64, i64* %2
%37 = add i64 %36, 2
store i64 %37, i64* %2
%38 = load i64, i64* %1
%39 = sub i64 %38, 2
store i64 %39, i64* %1
br label %47
40: ; preds = %30, %26
%41 = load i64, i64* %2
%42 = inttoptr i64 %41 to i8*
store i8 0, i8* %42
%43 = load i64, i64* %2
%44 = add i64 %43, 1
store i64 %44, i64* %2
%45 = load i64, i64* %1
%46 = sub i64 %45, 1
store i64 %46, i64* %1
br label %47
47: ; preds = %40, %33
br label %48
48: ; preds = %47, %19
br label %9
49: ; preds = %7, %9
ret void
}

LLVM IR (-O2):
@__END_BSS = external global [0 x i8]
@__START_BSS = external global [0 x i8]
define void @zero_fill_bss() {
br i1 icmp eq (i64 sub (i64 ptrtoint ([0 x i8]* @__END_BSS to i64), i64 ptrtoint ([0 x i8]* @__START_BSS to i64)), i64 0), label %25, label %1
1: ; preds = %0, %19
%2 = phi i64 [ %23, %19 ], [ sub (i64 ptrtoint ([0 x i8]* @__END_BSS to i64), i64 ptrtoint ([0 x i8]* @__START_BSS to i64)), %0 ]
%3 = phi i64 [ %22, %19 ], [ ptrtoint ([0 x i8]* @__START_BSS to i64), %0 ]
%4 = and i64 %3, 3
%5 = icmp eq i64 %4, 0
%6 = icmp ugt i64 %2, 3
%7 = select i1 %5, i1 %6, i1 false
br i1 %7, label %8, label %10
8: ; preds = %1
%9 = inttoptr i64 %3 to i32*
store i32 0, i32* %9
br label %19
10: ; preds = %1
%11 = and i64 %3, 1
%12 = icmp eq i64 %11, 0
%13 = icmp ugt i64 %2, 1
%14 = select i1 %12, i1 %13, i1 false
br i1 %14, label %15, label %17
15: ; preds = %10
%16 = inttoptr i64 %3 to i16*
store i16 0, i16* %16
br label %19
17: ; preds = %10
%18 = inttoptr i64 %3 to i8*
store i8 0, i8* %18
br label %19
19: ; preds = %15, %17, %8
%20 = phi i64 [ 2, %15 ], [ 1, %17 ], [ 4, %8 ]
%21 = phi i64 [ -2, %15 ], [ -1, %17 ], [ -4, %8 ]
%22 = add i64 %3, %20
%23 = add i64 %2, %21
%24 = icmp eq i64 %23, 0
br i1 %24, label %25, label %1
25: ; preds = %19, %0
ret void
}