#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */
typedef size_t uint8_t;
typedef int int32_t;
typedef int /*<<< orphan*/ bin_array;

/* Variables and functions */
int /*<<< orphan*/ complement_code(size_t *, int);
int /*<<< orphan*/ memset(size_t *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, ...);
void dec2bin(int32_t num) {
  uint8_t bin_array[32];
  uint8_t i, j, len = 0;
  uint8_t negetive_num = 0;
  memset(bin_array, 0, sizeof(bin_array));
  printf("decimal to binary %d=0b", num);
  if (num == 0) {
    printf("0\n");
    return;
  }
  if (num < 0) {
    negetive_num = 1;
    num = -num;
  }
  do {
    bin_array[len] = num % 2;
    num /= 2;
    len += 1;
  } while (num > 0);
  if (negetive_num == 1) {
    complement_code(bin_array, 32);
    len = 32;
  } else {
    if (len % 4 != 0) {
      len = (1 + len / 4) * 4;
    }
  }
  j = 0;
  for (i = len; i > 0; i -= 1) {
    printf("%d", bin_array[i - 1]);
    j += 1;
    if (j >= 4) {
      j = 0;
      printf(" ");
    }
  }
  printf("\n");
}


x86 assembly (clang, -O0):
<dec2bin>:
push   %rbp
mov    %rsp,%rbp
sub    $0x130,%rsp
mov    %edi,-0x4(%rbp)
movq   $0x0,-0x128(%rbp)
movq   $0x0,-0x130(%rbp)
lea    -0x110(%rbp),%rdi
xor    %esi,%esi
mov    $0x100,%edx
call   1040 <memset@plt>
mov    -0x4(%rbp),%esi
lea    0xe8f(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
cmpl   $0x0,-0x4(%rbp)
jne    1195 <dec2bin+0x65>
lea    0xe8f(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    130c <dec2bin+0x1dc>
cmpl   $0x0,-0x4(%rbp)
jge    11b2 <dec2bin+0x82>
movq   $0x1,-0x130(%rbp)
xor    %eax,%eax
sub    -0x4(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11b7 <dec2bin+0x87>
mov    -0x4(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
movslq %edx,%rcx
mov    -0x128(%rbp),%rax
mov    %rcx,-0x110(%rbp,%rax,8)
ff
mov    -0x4(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,-0x4(%rbp)
mov    -0x128(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x128(%rbp)
cmpl   $0x0,-0x4(%rbp)
jg     11b7 <dec2bin+0x87>
cmpq   $0x1,-0x130(%rbp)
01
jne    122d <dec2bin+0xfd>
lea    -0x110(%rbp),%rdi
mov    $0x20,%esi
call   1050 <complement_code@plt>
movq   $0x20,-0x128(%rbp)
jmp    1261 <dec2bin+0x131>
mov    -0x128(%rbp),%rax
and    $0x3,%rax
cmp    $0x0,%rax
je     125c <dec2bin+0x12c>
mov    -0x128(%rbp),%rax
shr    $0x2,%rax
add    $0x1,%rax
shl    $0x2,%rax
mov    %rax,-0x128(%rbp)
jmp    1261 <dec2bin+0x131>
movq   $0x0,-0x120(%rbp)
mov    -0x128(%rbp),%rax
mov    %rax,-0x118(%rbp)
cmpq   $0x0,-0x118(%rbp)
jbe    12fe <dec2bin+0x1ce>
mov    -0x118(%rbp),%rax
sub    $0x1,%rax
mov    -0x110(%rbp,%rax,8),%rsi
ff
lea    0xd79(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x120(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x120(%rbp)
cmpq   $0x4,-0x120(%rbp)
04
jb     12e2 <dec2bin+0x1b2>
movq   $0x0,-0x120(%rbp)
lea    0xd43(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    12e7 <dec2bin+0x1b7>
mov    -0x118(%rbp),%rax
sub    $0x1,%rax
mov    %rax,-0x118(%rbp)
jmp    127a <dec2bin+0x14a>
lea    0xd14(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
add    $0x130,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dec2bin>:
push   %r15
push   %r14
push   %rbx
sub    $0x100,%rsp
mov    %edi,%ebx
mov    %rsp,%rdi
xor    %esi,%esi
mov    $0x100,%edx
call   1060 <memset@plt>
lea    0xe8c(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1050 <printf@plt>
test   %ebx,%ebx
je     11d5 <dec2bin+0x85>
mov    %ebx,%ecx
neg    %ecx
cmovs  %ebx,%ecx
xor    %eax,%eax
mov    %ecx,%edx
nopl   0x0(%rax)
shr    $0x1f,%edx
add    %ecx,%edx
mov    %edx,%esi
and    $0xfffffffe,%esi
mov    %ecx,%edi
sub    %esi,%edi
movslq %edi,%rsi
mov    %rsi,(%rsp,%rax,8)
sar    %edx
add    $0x1,%rax
cmp    $0x1,%ecx
mov    %edx,%ecx
jg     1190 <dec2bin+0x40>
test   %ebx,%ebx
js     11ee <dec2bin+0x9e>
lea    0x4(%rax),%r15
and    $0xfffffffffffffffc,%r15
test   $0x3,%al
cmove  %rax,%r15
test   %r15,%r15
jne    1201 <dec2bin+0xb1>
mov    $0xa,%edi
call   1030 <putchar@plt>
jmp    11e1 <dec2bin+0x91>
lea    0xe3f(%rip),%rdi
call   1040 <puts@plt>
add    $0x100,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
mov    %rsp,%rdi
mov    $0x20,%esi
call   1070 <complement_code@plt>
mov    $0x20,%r15d
lea    0xe10(%rip),%r14
xor    %ebx,%ebx
jmp    1215 <dec2bin+0xc5>
nopl   0x0(%rax)
test   %r15,%r15
je     11c9 <dec2bin+0x79>
mov    -0x8(%rsp,%r15,8),%rsi
add    $0xffffffffffffffff,%r15
mov    %r14,%rdi
xor    %eax,%eax
call   1050 <printf@plt>
add    $0x1,%rbx
cmp    $0x4,%rbx
jb     1210 <dec2bin+0xc0>
mov    $0x20,%edi
call   1030 <putchar@plt>
xor    %ebx,%ebx
jmp    1210 <dec2bin+0xc0>

LLVM IR (-O0):
@.str = [24 x i8] c"decimal to binary %d=0b\00"
@.str.1 = [3 x i8] c"0\0A\00"
@.str.2 = [3 x i8] c"%d\00"
@.str.3 = [2 x i8] c" \00"
@.str.4 = [2 x i8] c"\0A\00"
define void @dec2bin(i32 %0) {
%2 = alloca i32
%3 = alloca [32 x i64]
%4 = alloca i64
%5 = alloca i64
%6 = alloca i64
%7 = alloca i64
store i32 %0, i32* %2
store i64 0, i64* %6
store i64 0, i64* %7
%8 = getelementptr inbounds [32 x i64], [32 x i64]* %3, i64 0, i64 0
%9 = call i32 @memset(i64* %8, i32 0, i32 256)
%10 = load i32, i32* %2
%11 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i32 %10)
%12 = load i32, i32* %2
%13 = icmp eq i32 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %1
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
br label %75
16: ; preds = %1
%17 = load i32, i32* %2
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %16
store i64 1, i64* %7
%20 = load i32, i32* %2
%21 = sub nsw i32 0, %20
store i32 %21, i32* %2
br label %22
22: ; preds = %19, %16
br label %23
23: ; preds = %33, %22
%24 = load i32, i32* %2
%25 = srem i32 %24, 2
%26 = sext i32 %25 to i64
%27 = load i64, i64* %6
%28 = getelementptr inbounds [32 x i64], [32 x i64]* %3, i64 0, i64 %27
store i64 %26, i64* %28
%29 = load i32, i32* %2
%30 = sdiv i32 %29, 2
store i32 %30, i32* %2
%31 = load i64, i64* %6
%32 = add i64 %31, 1
store i64 %32, i64* %6
br label %33
33: ; preds = %23
%34 = load i32, i32* %2
%35 = icmp sgt i32 %34, 0
br i1 %35, label %23, label %36
36: ; preds = %33
%37 = load i64, i64* %7
%38 = icmp eq i64 %37, 1
br i1 %38, label %39, label %42
39: ; preds = %36
%40 = getelementptr inbounds [32 x i64], [32 x i64]* %3, i64 0, i64 0
%41 = call i32 @complement_code(i64* %40, i32 32)
store i64 32, i64* %6
br label %52
42: ; preds = %36
%43 = load i64, i64* %6
%44 = urem i64 %43, 4
%45 = icmp ne i64 %44, 0
br i1 %45, label %46, label %51
46: ; preds = %42
%47 = load i64, i64* %6
%48 = udiv i64 %47, 4
%49 = add i64 1, %48
%50 = mul i64 %49, 4
store i64 %50, i64* %6
br label %51
51: ; preds = %46, %42
br label %52
52: ; preds = %51, %39
store i64 0, i64* %5
%53 = load i64, i64* %6
store i64 %53, i64* %4
br label %54
54: ; preds = %70, %52
%55 = load i64, i64* %4
%56 = icmp ugt i64 %55, 0
br i1 %56, label %57, label %73
57: ; preds = %54
%58 = load i64, i64* %4
%59 = sub i64 %58, 1
%60 = getelementptr inbounds [32 x i64], [32 x i64]* %3, i64 0, i64 %59
%61 = load i64, i64* %60
%62 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i64 %61)
%63 = load i64, i64* %5
%64 = add i64 %63, 1
store i64 %64, i64* %5
%65 = load i64, i64* %5
%66 = icmp uge i64 %65, 4
br i1 %66, label %67, label %69
67: ; preds = %57
store i64 0, i64* %5
%68 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
br label %69
69: ; preds = %67, %57
br label %70
70: ; preds = %69
%71 = load i64, i64* %4
%72 = sub i64 %71, 1
store i64 %72, i64* %4
br label %54
73: ; preds = %54
%74 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
br label %75
75: ; preds = %73, %14
ret void
}

LLVM IR (-O2):
@.str = [24 x i8] c"decimal to binary %d=0b\00"
@.str.2 = [3 x i8] c"%d\00"
@str = [2 x i8] c"0\00"
define void @dec2bin(i32 %0) {
%2 = alloca [32 x i64]
%3 = bitcast [32 x i64]* %2 to i8*
%4 = getelementptr inbounds [32 x i64], [32 x i64]* %2, i64 0, i64 0
%5 = call i32 @memset(i64* %4, i32 0, i32 256)
%6 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i32 %0)
%7 = icmp eq i32 %0, 0
br i1 %7, label %8, label %10
8: ; preds = %1
%9 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([2 x i8], [2 x i8]* @str, i64 0, i64 0))
br label %51
10: ; preds = %1
%11 = icmp slt i32 %0, 0
%12 = sub nsw i32 0, %0
%13 = select i1 %11, i32 %12, i32 %0
br label %14
14: ; preds = %14, %10
%15 = phi i32 [ %13, %10 ], [ %20, %14 ]
%16 = phi i64 [ 0, %10 ], [ %21, %14 ]
%17 = srem i32 %15, 2
%18 = sext i32 %17 to i64
%19 = getelementptr inbounds [32 x i64], [32 x i64]* %2, i64 0, i64 %16
store i64 %18, i64* %19
%20 = sdiv i32 %15, 2
%21 = add i64 %16, 1
%22 = icmp sgt i32 %15, 1
br i1 %22, label %14, label %23
23: ; preds = %14
br i1 %11, label %24, label %26
24: ; preds = %23
%25 = call i32 @complement_code(i64* %4, i32 32)
br label %33
26: ; preds = %23
%27 = and i64 %21, 3
%28 = icmp eq i64 %27, 0
%29 = add i64 %16, 5
%30 = and i64 %29, -4
%31 = select i1 %28, i64 %21, i64 %30
%32 = icmp eq i64 %31, 0
br i1 %32, label %49, label %33
33: ; preds = %24, %26
%34 = phi i64 [ %31, %26 ], [ 32, %24 ]
br label %35
35: ; preds = %33, %46
%36 = phi i64 [ %47, %46 ], [ 0, %33 ]
%37 = phi i64 [ %38, %46 ], [ %34, %33 ]
%38 = add i64 %37, -1
%39 = getelementptr inbounds [32 x i64], [32 x i64]* %2, i64 0, i64 %38
%40 = load i64, i64* %39
%41 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i64 %40)
%42 = add i64 %36, 1
%43 = icmp ugt i64 %42, 3
br i1 %43, label %44, label %46
44: ; preds = %35
%45 = call i32 @putchar(i32 32)
br label %46
46: ; preds = %35, %44
%47 = phi i64 [ 0, %44 ], [ %42, %35 ]
%48 = icmp eq i64 %38, 0
br i1 %48, label %49, label %35
49: ; preds = %46, %26
%50 = call i32 @putchar(i32 10)
br label %51
51: ; preds = %49, %8
ret void
}