#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 */

/* Variables and functions */
int /*<<< orphan*/ free(char *);
char *malloc(int);
int /*<<< orphan*/ memcpy(char *, char *, int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ scanf(char *, int *);
int main() {
  int decimal;
  printf("Digite um nÃºmero em base decimal: ");
  scanf("%d", &decimal);
  int counter = 1;
  char *binary = NULL;
  while (decimal >= 1) {
    int q = decimal / 2;
    int r = decimal - (q * 2);
    char *aux = malloc(counter);
    if (binary != NULL) {
      memcpy(aux, binary, counter - 1);
      free(binary);
    }
    binary = aux;
    if (r == 0)
      binary[counter - 1] = '0';
    else
      binary[counter - 1] = '1';
    counter++;
    decimal = q;
  }
  printf("Resultado em binÃ¡rio = ");
  for (int i = counter - 1; i >= 0; i--)
    printf("%c", binary[i]);
  printf("\n");
  free(binary);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe9a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xeb2(%rip),%rdi
lea    -0x8(%rbp),%rsi
call   1070 <scanf@plt>
movl   $0x1,-0xc(%rbp)
movq   $0x0,-0x18(%rbp)
cmpl   $0x1,-0x8(%rbp)
jl     1232 <main+0xe2>
mov    -0x8(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,-0x1c(%rbp)
mov    -0x8(%rbp),%eax
mov    -0x1c(%rbp),%ecx
shl    $0x1,%ecx
sub    %ecx,%eax
mov    %eax,-0x20(%rbp)
mov    -0xc(%rbp),%edi
call   1060 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     11e5 <main+0x95>
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0xc(%rbp),%edx
sub    $0x1,%edx
call   1050 <memcpy@plt>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x18(%rbp)
cmpl   $0x0,-0x20(%rbp)
jne    120d <main+0xbd>
mov    -0x18(%rbp),%rax
mov    -0xc(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x30,(%rax,%rcx,1)
jmp    121e <main+0xce>
mov    -0x18(%rbp),%rax
mov    -0xc(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x31,(%rax,%rcx,1)
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x8(%rbp)
jmp    118c <main+0x3c>
lea    0xdf0(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0xc(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jl     127b <main+0x12b>
mov    -0x18(%rbp),%rax
movslq -0x2c(%rbp),%rcx
movsbl (%rax,%rcx,1),%esi
lea    0xdde(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x2c(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x2c(%rbp)
jmp    1249 <main+0xf9>
lea    0xdc5(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
lea    0xe8e(%rip),%rdi
xor    %r14d,%r14d
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xea3(%rip),%rdi
mov    %rsp,%rsi
call   1080 <scanf@plt>
mov    (%rsp),%eax
mov    $0x1,%ebx
test   %eax,%eax
jle    11fd <main+0x9d>
xor    %r12d,%r12d
mov    %eax,%r13d
jmp    11c7 <main+0x67>
nop
mov    0x4(%rsp),%ecx
add    %ecx,%r15d
sete   %al
xor    $0x31,%al
mov    %al,-0x1(%rbp,%rbx,1)
add    $0x1,%rbx
mov    %r13d,(%rsp)
add    $0x1,%r14d
mov    %rbp,%r12
cmp    $0x1,%ecx
mov    %r13d,%eax
jbe    11ff <main+0x9f>
mov    %eax,0x4(%rsp)
shr    %r13d
lea    0x0(%r13,%r13,1),%r15d
neg    %r15d
mov    %ebx,%edi
call   1070 <malloc@plt>
mov    %rax,%rbp
test   %r12,%r12
je     11a0 <main+0x40>
mov    %rbp,%rdi
mov    %r12,%rsi
mov    %r14d,%edx
call   1060 <memcpy@plt>
mov    %r12,%rdi
call   1030 <free@plt>
jmp    11a0 <main+0x40>
xor    %ebp,%ebp
lea    0xe23(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
add    $0x1,%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
movsbl -0x2(%rbp,%rbx,1),%edi
call   1040 <putchar@plt>
add    $0xffffffffffffffff,%rbx
cmp    $0x1,%rbx
ja     1220 <main+0xc0>
mov    $0xa,%edi
call   1040 <putchar@plt>
mov    %rbp,%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [38 x i8] c"Digite um n\C3\83\C2\BAmero em base decimal: \00"
@.str.1 = [3 x i8] c"%d\00"
@.str.2 = [27 x i8] c"Resultado em bin\C3\83\C2\A1rio = \00"
@.str.3 = [3 x i8] c"%c\00"
@.str.4 = [2 x i8] c"\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
store i32 0, i32* %1
%9 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0))
%10 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %2)
store i32 1, i32* %3
store i8* null, i8** %4
br label %11
11: ; preds = %49, %0
%12 = load i32, i32* %2
%13 = icmp sge i32 %12, 1
br i1 %13, label %14, label %53
14: ; preds = %11
%15 = load i32, i32* %2
%16 = sdiv i32 %15, 2
store i32 %16, i32* %5
%17 = load i32, i32* %2
%18 = load i32, i32* %5
%19 = mul nsw i32 %18, 2
%20 = sub nsw i32 %17, %19
store i32 %20, i32* %6
%21 = load i32, i32* %3
%22 = call i8* @malloc(i32 %21)
store i8* %22, i8** %7
%23 = load i8*, i8** %4
%24 = icmp ne i8* %23, null
br i1 %24, label %25, label %33
25: ; preds = %14
%26 = load i8*, i8** %7
%27 = load i8*, i8** %4
%28 = load i32, i32* %3
%29 = sub nsw i32 %28, 1
%30 = call i32 @memcpy(i8* %26, i8* %27, i32 %29)
%31 = load i8*, i8** %4
%32 = call i32 @free(i8* %31)
br label %33
33: ; preds = %25, %14
%34 = load i8*, i8** %7
store i8* %34, i8** %4
%35 = load i32, i32* %6
%36 = icmp eq i32 %35, 0
br i1 %36, label %37, label %43
37: ; preds = %33
%38 = load i8*, i8** %4
%39 = load i32, i32* %3
%40 = sub nsw i32 %39, 1
%41 = sext i32 %40 to i64
%42 = getelementptr inbounds i8, i8* %38, i64 %41
store i8 48, i8* %42
br label %49
43: ; preds = %33
%44 = load i8*, i8** %4
%45 = load i32, i32* %3
%46 = sub nsw i32 %45, 1
%47 = sext i32 %46 to i64
%48 = getelementptr inbounds i8, i8* %44, i64 %47
store i8 49, i8* %48
br label %49
49: ; preds = %43, %37
%50 = load i32, i32* %3
%51 = add nsw i32 %50, 1
store i32 %51, i32* %3
%52 = load i32, i32* %5
store i32 %52, i32* %2
br label %11
53: ; preds = %11
%54 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0))
%55 = load i32, i32* %3
%56 = sub nsw i32 %55, 1
store i32 %56, i32* %8
br label %57
57: ; preds = %68, %53
%58 = load i32, i32* %8
%59 = icmp sge i32 %58, 0
br i1 %59, label %60, label %71
60: ; preds = %57
%61 = load i8*, i8** %4
%62 = load i32, i32* %8
%63 = sext i32 %62 to i64
%64 = getelementptr inbounds i8, i8* %61, i64 %63
%65 = load i8, i8* %64
%66 = sext i8 %65 to i32
%67 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32 %66)
br label %68
68: ; preds = %60
%69 = load i32, i32* %8
%70 = add nsw i32 %69, -1
store i32 %70, i32* %8
br label %57
71: ; preds = %57
%72 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%73 = load i8*, i8** %4
%74 = call i32 @free(i8* %73)
%75 = load i32, i32* %1
ret i32 %75
}

LLVM IR (-O2):
@.str = [38 x i8] c"Digite um n\C3\83\C2\BAmero em base decimal: \00"
@.str.1 = [3 x i8] c"%d\00"
@.str.2 = [27 x i8] c"Resultado em bin\C3\83\C2\A1rio = \00"
define i32 @main() {
%1 = alloca i32
%2 = bitcast i32* %1 to i8*
%3 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0))
%4 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %1)
%5 = load i32, i32* %1
%6 = icmp sgt i32 %5, 0
br i1 %6, label %7, label %31
7: ; preds = %0, %21
%8 = phi i64 [ %27, %21 ], [ 1, %0 ]
%9 = phi i8* [ %14, %21 ], [ null, %0 ]
%10 = phi i32 [ %11, %21 ], [ %5, %0 ]
%11 = lshr i32 %10, 1
%12 = mul nsw i32 %11, -2
%13 = trunc i64 %8 to i32
%14 = call i8* @malloc(i32 %13)
%15 = icmp eq i8* %9, null
br i1 %15, label %21, label %16
16: ; preds = %7
%17 = trunc i64 %8 to i32
%18 = add i32 %17, -1
%19 = call i32 @memcpy(i8* %14, i8* %9, i32 %18)
%20 = call i32 @free(i8* %9)
br label %21
21: ; preds = %16, %7
%22 = sub nsw i32 0, %10
%23 = icmp eq i32 %12, %22
%24 = add nsw i64 %8, -1
%25 = getelementptr inbounds i8, i8* %14, i64 %24
%26 = select i1 %23, i8 48, i8 49
store i8 %26, i8* %25
%27 = add nuw nsw i64 %8, 1
store i32 %11, i32* %1
%28 = icmp ult i32 %10, 2
br i1 %28, label %29, label %7
29: ; preds = %21
%30 = and i64 %27, 4294967295
br label %31
31: ; preds = %0, %29
%32 = phi i8* [ null, %0 ], [ %14, %29 ]
%33 = phi i64 [ 1, %0 ], [ %30, %29 ]
%34 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0))
br label %38
35: ; preds = %38
%36 = call i32 @putchar(i32 10)
%37 = call i32 @free(i8* %32)
ret i32 0
38: ; preds = %31, %38
%39 = phi i64 [ %33, %31 ], [ %40, %38 ]
%40 = add nsw i64 %39, -1
%41 = getelementptr inbounds i8, i8* %32, i64 %40
%42 = load i8, i8* %41
%43 = sext i8 %42 to i32
%44 = call i32 @putchar(i32 %43)
%45 = icmp ugt i64 %39, 1
br i1 %45, label %38, label %35
}