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 int size_int = sizeof(int);
  const int mask_int = sizeof(int) - 1;
  const int size_short = sizeof(short);
  const int mask_short = sizeof(short) - 1;
  const int size_char = sizeof(char);
  if (len == 0) {
    return;
  }
  while (len > 0) {
    if (!(dst & mask_int) && len >= size_int) {
      *((int *)dst) = 0;
      dst += size_int;
      len -= size_int;
    } else if (!(dst & mask_short) && len >= size_short) {
      *((short *)dst) = 0;
      dst += size_short;
      len -= size_short;
    } else {
      *((char *)dst) = 0;
      dst += size_char;
      len -= size_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   $0x4,-0x14(%rbp)
movl   $0x3,-0x18(%rbp)
movl   $0x2,-0x1c(%rbp)
movl   $0x1,-0x20(%rbp)
movl   $0x1,-0x24(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    1157 <zero_fill_bss+0x57>
jmp    1217 <zero_fill_bss+0x117>
jmp    115c <zero_fill_bss+0x5c>
cmpq   $0x0,-0x8(%rbp)
jbe    1217 <zero_fill_bss+0x117>
mov    -0x10(%rbp),%rax
and    $0x3,%rax
cmp    $0x0,%rax
jne    11ab <zero_fill_bss+0xab>
cmpq   $0x4,-0x8(%rbp)
jb     11ab <zero_fill_bss+0xab>
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    1212 <zero_fill_bss+0x112>
mov    -0x10(%rbp),%rax
and    $0x1,%rax
cmp    $0x0,%rax
jne    11ee <zero_fill_bss+0xee>
cmpq   $0x2,-0x8(%rbp)
jb     11ee <zero_fill_bss+0xee>
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    120d <zero_fill_bss+0x10d>
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    1212 <zero_fill_bss+0x112>
jmp    115c <zero_fill_bss+0x5c>
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
%5 = alloca i32
%6 = alloca i32
%7 = 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 4, i32* %3
store i32 3, i32* %4
store i32 2, i32* %5
store i32 1, i32* %6
store i32 1, i32* %7
%8 = load i64, i64* %1
%9 = icmp eq i64 %8, 0
br i1 %9, label %10, label %11
10: ; preds = %0
br label %52
11: ; preds = %0
br label %12
12: ; preds = %51, %11
%13 = load i64, i64* %1
%14 = icmp ugt i64 %13, 0
br i1 %14, label %15, label %52
15: ; preds = %12
%16 = load i64, i64* %2
%17 = and i64 %16, 3
%18 = icmp ne i64 %17, 0
br i1 %18, label %29, label %19
19: ; preds = %15
%20 = load i64, i64* %1
%21 = icmp uge i64 %20, 4
br i1 %21, label %22, label %29
22: ; preds = %19
%23 = load i64, i64* %2
%24 = inttoptr i64 %23 to i32*
store i32 0, i32* %24
%25 = load i64, i64* %2
%26 = add i64 %25, 4
store i64 %26, i64* %2
%27 = load i64, i64* %1
%28 = sub i64 %27, 4
store i64 %28, i64* %1
br label %51
29: ; preds = %19, %15
%30 = load i64, i64* %2
%31 = and i64 %30, 1
%32 = icmp ne i64 %31, 0
br i1 %32, label %43, label %33
33: ; preds = %29
%34 = load i64, i64* %1
%35 = icmp uge i64 %34, 2
br i1 %35, label %36, label %43
36: ; preds = %33
%37 = load i64, i64* %2
%38 = inttoptr i64 %37 to i16*
store i16 0, i16* %38
%39 = load i64, i64* %2
%40 = add i64 %39, 2
store i64 %40, i64* %2
%41 = load i64, i64* %1
%42 = sub i64 %41, 2
store i64 %42, i64* %1
br label %50
43: ; preds = %33, %29
%44 = load i64, i64* %2
%45 = inttoptr i64 %44 to i8*
store i8 0, i8* %45
%46 = load i64, i64* %2
%47 = add i64 %46, 1
store i64 %47, i64* %2
%48 = load i64, i64* %1
%49 = sub i64 %48, 1
store i64 %49, i64* %1
br label %50
50: ; preds = %43, %36
br label %51
51: ; preds = %50, %22
br label %12
52: ; preds = %10, %12
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
}