short int ipv4_integer2bin(unsigned long ip_int, char *ip_bin) {
  int i = 31;
  for (; i >= 0; i--) {
    if (i == 23 || i == 15 || i == 7)
      *ip_bin++ = '.';
    unsigned long bit = (ip_int & (1 << i));
    *ip_bin = bit ? '1' : '0';
    ip_bin++;
  }
  *ip_bin = 0;
  return 0;
}


x86 assembly (clang, -O0):
<ipv4_integer2bin>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x1f,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jl     119e <ipv4_integer2bin+0x9e>
cmpl   $0x17,-0x14(%rbp)
je     113b <ipv4_integer2bin+0x3b>
cmpl   $0xf,-0x14(%rbp)
je     113b <ipv4_integer2bin+0x3b>
cmpl   $0x7,-0x14(%rbp)
jne    114d <ipv4_integer2bin+0x4d>
mov    -0x10(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x10(%rbp)
movb   $0x2e,(%rax)
mov    -0x8(%rbp),%rax
mov    -0x14(%rbp),%ecx
mov    $0x1,%edx
shl    %cl,%edx
mov    %edx,%ecx
movslq %ecx,%rcx
and    %rcx,%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdx
mov    $0x30,%eax
mov    $0x31,%ecx
cmp    $0x0,%rdx
cmovne %ecx,%eax
mov    %al,%cl
mov    -0x10(%rbp),%rax
mov    %cl,(%rax)
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
mov    -0x14(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x14(%rbp)
jmp    1113 <ipv4_integer2bin+0x13>
mov    -0x10(%rbp),%rax
movb   $0x0,(%rax)
xor    %eax,%eax
cwtl
pop    %rbp
ret

x86 assembly (clang, -O2):
<ipv4_integer2bin>:
mov    $0x1f,%ecx
mov    $0x1010100,%r8d
mov    $0x808080,%r9d
jmp    1147 <ipv4_integer2bin+0x47>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
add    $0xffffffff,%ecx
mov    $0x1,%esi
shl    %cl,%esi
movslq %esi,%rsi
and    %rdi,%rsi
cmp    $0x1,%rsi
mov    $0x31,%dl
sbb    $0x0,%dl
mov    %dl,(%rax)
mov    %rax,%rsi
add    $0x1,%rsi
sub    $0x1,%ecx
jb     118b <ipv4_integer2bin+0x8b>
cmp    $0x17,%ecx
ja     1159 <ipv4_integer2bin+0x59>
bt     %ecx,%r9d
jae    1159 <ipv4_integer2bin+0x59>
movb   $0x2e,(%rsi)
add    $0x1,%rsi
mov    $0x1,%eax
shl    %cl,%eax
cltq
and    %rdi,%rax
cmp    $0x1,%rax
mov    $0x31,%al
sbb    $0x0,%al
mov    %al,(%rsi)
lea    0x1(%rsi),%rax
cmp    $0x18,%ecx
ja     1120 <ipv4_integer2bin+0x20>
bt     %ecx,%r8d
jae    1120 <ipv4_integer2bin+0x20>
movb   $0x2e,0x1(%rsi)
add    $0x2,%rsi
mov    %rsi,%rax
jmp    1120 <ipv4_integer2bin+0x20>
movb   $0x0,(%rsi)
xor    %eax,%eax
ret

LLVM IR (-O0):
define signext i16 @ipv4_integer2bin(i64 %0, i8* %1) {
%3 = alloca i64
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i64
store i64 %0, i64* %3
store i8* %1, i8** %4
store i32 31, i32* %5
br label %7
7: ; preds = %36, %2
%8 = load i32, i32* %5
%9 = icmp sge i32 %8, 0
br i1 %9, label %10, label %39
10: ; preds = %7
%11 = load i32, i32* %5
%12 = icmp eq i32 %11, 23
br i1 %12, label %19, label %13
13: ; preds = %10
%14 = load i32, i32* %5
%15 = icmp eq i32 %14, 15
br i1 %15, label %19, label %16
16: ; preds = %13
%17 = load i32, i32* %5
%18 = icmp eq i32 %17, 7
br i1 %18, label %19, label %22
19: ; preds = %16, %13, %10
%20 = load i8*, i8** %4
%21 = getelementptr inbounds i8, i8* %20, i32 1
store i8* %21, i8** %4
store i8 46, i8* %20
br label %22
22: ; preds = %19, %16
%23 = load i64, i64* %3
%24 = load i32, i32* %5
%25 = shl i32 1, %24
%26 = sext i32 %25 to i64
%27 = and i64 %23, %26
store i64 %27, i64* %6
%28 = load i64, i64* %6
%29 = icmp ne i64 %28, 0
%30 = zext i1 %29 to i64
%31 = select i1 %29, i32 49, i32 48
%32 = trunc i32 %31 to i8
%33 = load i8*, i8** %4
store i8 %32, i8* %33
%34 = load i8*, i8** %4
%35 = getelementptr inbounds i8, i8* %34, i32 1
store i8* %35, i8** %4
br label %36
36: ; preds = %22
%37 = load i32, i32* %5
%38 = add nsw i32 %37, -1
store i32 %38, i32* %5
br label %7
39: ; preds = %7
%40 = load i8*, i8** %4
store i8 0, i8* %40
ret i16 0
}

LLVM IR (-O2):
define signext i16 @ipv4_integer2bin(i64 %0, i8* nocapture writeonly %1) {
br label %3
3: ; preds = %19, %2
%4 = phi i8* [ %1, %2 ], [ %26, %19 ]
%5 = phi i32 [ 31, %2 ], [ %27, %19 ]
switch i32 %5, label %8 [
i32 23, label %6
i32 15, label %6
i32 7, label %6
]
6: ; preds = %3, %3, %3
%7 = getelementptr inbounds i8, i8* %4, i64 1
store i8 46, i8* %4
br label %8
8: ; preds = %3, %6
%9 = phi i8* [ %7, %6 ], [ %4, %3 ]
%10 = shl nuw i32 1, %5
%11 = sext i32 %10 to i64
%12 = and i64 %11, %0
%13 = icmp eq i64 %12, 0
%14 = select i1 %13, i8 48, i8 49
store i8 %14, i8* %9
%15 = getelementptr inbounds i8, i8* %9, i64 1
%16 = add nsw i32 %5, -1
switch i32 %5, label %19 [
i32 24, label %17
i32 16, label %17
i32 8, label %17
]
17: ; preds = %8, %8, %8
%18 = getelementptr inbounds i8, i8* %9, i64 2
store i8 46, i8* %15
br label %19
19: ; preds = %17, %8
%20 = phi i8* [ %18, %17 ], [ %15, %8 ]
%21 = shl nuw i32 1, %16
%22 = sext i32 %21 to i64
%23 = and i64 %22, %0
%24 = icmp eq i64 %23, 0
%25 = select i1 %24, i8 48, i8 49
store i8 %25, i8* %20
%26 = getelementptr inbounds i8, i8* %20, i64 1
%27 = add nsw i32 %5, -2
%28 = icmp eq i32 %16, 0
br i1 %28, label %29, label %3
29: ; preds = %19
store i8 0, i8* %26
ret i16 0
}