#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*/ printf(char *, ...);
int scanf(char *, int *);
int main() {
  int bin, dec;
  int r;
  int d, p;
  printf("Exercício 3.36. página 76, Deitel e Deitel, 2011.\n");
  printf("Informe o valor do número binário:");
  r = scanf("%d", &bin);
  if (r != 1) {
    printf("Não foi possível ler o número binário.\n");
    printf("**Execução cancelada!**\n");
    return 1;
  }
  printf("O valor binário informado é=%d.\n", bin);
  dec = 0;
  p = 1;
  while (bin > 0) {
    d = bin % 10;
    printf("[%d]\n", d);
    if (d != 0 && d != 1) {
      printf("Dígito inválido. O número não é binário.\n");
      printf("**Execução cancelada!**\n");
      return 2;
    }
    dec = dec + d * p;
    p = p * 2;
    bin = bin / 10;
  }
  printf("O equivalente em decimal é=%d.\n", dec);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xeca(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xef1(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xf08(%rip),%rdi
lea    -0x8(%rbp),%rsi
call   1040 <scanf@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x1,-0x10(%rbp)
je     1190 <main+0x70>
lea    0xeee(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xf0c(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1257 <main+0x137>
mov    -0x8(%rbp),%esi
lea    0xf0a(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0xc(%rbp)
movl   $0x1,-0x18(%rbp)
cmpl   $0x0,-0x8(%rbp)
jle    123f <main+0x11f>
mov    -0x8(%rbp),%eax
mov    $0xa,%ecx
cltd
idiv   %ecx
mov    %edx,-0x14(%rbp)
mov    -0x14(%rbp),%esi
lea    0xef6(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
cmpl   $0x0,-0x14(%rbp)
je     1214 <main+0xf4>
cmpl   $0x1,-0x14(%rbp)
je     1214 <main+0xf4>
lea    0xeda(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xe88(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x2,-0x4(%rbp)
jmp    1257 <main+0x137>
mov    -0xc(%rbp),%eax
mov    -0x14(%rbp),%ecx
imul   -0x18(%rbp),%ecx
add    %ecx,%eax
mov    %eax,-0xc(%rbp)
mov    -0x18(%rbp),%eax
shl    $0x1,%eax
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%eax
mov    $0xa,%ecx
cltd
idiv   %ecx
mov    %eax,-0x8(%rbp)
jmp    11af <main+0x8f>
mov    -0xc(%rbp),%esi
lea    0xeb4(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
lea    0xf2f(%rip),%rdi
call   1030 <puts@plt>
lea    0xeb1(%rip),%rdi
xor    %r15d,%r15d
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xec5(%rip),%rdi
lea    0xc(%rsp),%rsi
call   1050 <scanf@plt>
cmp    $0x1,%eax
jne    1212 <main+0xe2>
mov    0xc(%rsp),%esi
lea    0xeaa(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0xc(%rsp),%ebx
test   %ebx,%ebx
jle    11fd <main+0xcd>
mov    $0x1,%ebp
mov    $0xcccccccd,%r12d
lea    0xeac(%rip),%r14
xor    %r15d,%r15d
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %ebx,%eax
imul   %r12,%rax
shr    $0x23,%rax
add    %eax,%eax
lea    (%rax,%rax,4),%eax
sub    %eax,%ebx
mov    %r14,%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1040 <printf@plt>
cmp    $0x2,%ebx
jae    1231 <main+0x101>
imul   %ebp,%ebx
add    %ebx,%r15d
add    %ebp,%ebp
movslq 0xc(%rsp),%rax
imul   $0x66666667,%rax,%rbx
mov    %rbx,%rcx
shr    $0x3f,%rcx
sar    $0x22,%rbx
add    %ecx,%ebx
mov    %ebx,0xc(%rsp)
cmp    $0x9,%rax
jg     11b0 <main+0x80>
lea    0xe4d(%rip),%rdi
xor    %ebx,%ebx
mov    %r15d,%esi
xor    %eax,%eax
call   1040 <printf@plt>
jmp    124e <main+0x11e>
lea    0xebc(%rip),%rdi
call   1030 <puts@plt>
lea    0xedb(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%ebx
jmp    124e <main+0x11e>
lea    0xe6e(%rip),%rdi
call   1030 <puts@plt>
lea    0xebc(%rip),%rdi
call   1030 <puts@plt>
mov    $0x2,%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [53 x i8] c"Exerc\C3\ADcio 3.36. p\C3\A1gina 76, Deitel e Deitel, 2011.\0A\00"
@.str.1 = [37 x i8] c"Informe o valor do n\C3\BAmero bin\C3\A1rio:\00"
@.str.2 = [3 x i8] c"%d\00"
@.str.3 = [44 x i8] c"N\C3\A3o foi poss\C3\ADvel ler o n\C3\BAmero bin\C3\A1rio.\0A\00"
@.str.4 = [27 x i8] c"**Execu\C3\A7\C3\A3o cancelada!**\0A\00"
@.str.5 = [35 x i8] c"O valor bin\C3\A1rio informado \C3\A9=%d.\0A\00"
@.str.6 = [6 x i8] c"[%d]\0A\00"
@.str.7 = [48 x i8] c"D\C3\ADgito inv\C3\A1lido. O n\C3\BAmero n\C3\A3o \C3\A9 bin\C3\A1rio.\0A\00"
@.str.8 = [33 x i8] c"O equivalente em decimal \C3\A9=%d.\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i32 0, i32* %1
%7 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str, i64 0, i64 0))
%8 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0))
%9 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %2)
store i32 %9, i32* %4
%10 = load i32, i32* %4
%11 = icmp ne i32 %10, 1
br i1 %11, label %12, label %15
12: ; preds = %0
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.3, i64 0, i64 0))
%14 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.4, i64 0, i64 0))
store i32 1, i32* %1
br label %47
15: ; preds = %0
%16 = load i32, i32* %2
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.5, i64 0, i64 0), i32 %16)
store i32 0, i32* %3
store i32 1, i32* %6
br label %18
18: ; preds = %34, %15
%19 = load i32, i32* %2
%20 = icmp sgt i32 %19, 0
br i1 %20, label %21, label %44
21: ; preds = %18
%22 = load i32, i32* %2
%23 = srem i32 %22, 10
store i32 %23, i32* %5
%24 = load i32, i32* %5
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.6, i64 0, i64 0), i32 %24)
%26 = load i32, i32* %5
%27 = icmp ne i32 %26, 0
br i1 %27, label %28, label %34
28: ; preds = %21
%29 = load i32, i32* %5
%30 = icmp ne i32 %29, 1
br i1 %30, label %31, label %34
31: ; preds = %28
%32 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.7, i64 0, i64 0))
%33 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.4, i64 0, i64 0))
store i32 2, i32* %1
br label %47
34: ; preds = %28, %21
%35 = load i32, i32* %3
%36 = load i32, i32* %5
%37 = load i32, i32* %6
%38 = mul nsw i32 %36, %37
%39 = add nsw i32 %35, %38
store i32 %39, i32* %3
%40 = load i32, i32* %6
%41 = mul nsw i32 %40, 2
store i32 %41, i32* %6
%42 = load i32, i32* %2
%43 = sdiv i32 %42, 10
store i32 %43, i32* %2
br label %18
44: ; preds = %18
%45 = load i32, i32* %3
%46 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.8, i64 0, i64 0), i32 %45)
store i32 0, i32* %1
br label %47
47: ; preds = %44, %31, %12
%48 = load i32, i32* %1
ret i32 %48
}

LLVM IR (-O2):
@.str.1 = [37 x i8] c"Informe o valor do n\C3\BAmero bin\C3\A1rio:\00"
@.str.2 = [3 x i8] c"%d\00"
@.str.5 = [35 x i8] c"O valor bin\C3\A1rio informado \C3\A9=%d.\0A\00"
@.str.6 = [6 x i8] c"[%d]\0A\00"
@.str.8 = [33 x i8] c"O equivalente em decimal \C3\A9=%d.\0A\00"
@str = [52 x i8] c"Exerc\C3\ADcio 3.36. p\C3\A1gina 76, Deitel e Deitel, 2011.\00"
@str.9 = [47 x i8] c"D\C3\ADgito inv\C3\A1lido. O n\C3\BAmero n\C3\A3o \C3\A9 bin\C3\A1rio.\00"
@str.11 = [43 x i8] c"N\C3\A3o foi poss\C3\ADvel ler o n\C3\BAmero bin\C3\A1rio.\00"
@str.12 = [26 x i8] c"**Execu\C3\A7\C3\A3o cancelada!**\00"
define i32 @main() {
%1 = alloca i32
%2 = bitcast i32* %1 to i8*
%3 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([52 x i8], [52 x i8]* @str, i64 0, i64 0))
%4 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0))
%5 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %1)
%6 = icmp eq i32 %5, 1
br i1 %6, label %10, label %7
7: ; preds = %0
%8 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([43 x i8], [43 x i8]* @str.11, i64 0, i64 0))
%9 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @str.12, i64 0, i64 0))
br label %35
10: ; preds = %0
%11 = load i32, i32* %1
%12 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @.str.5, i64 0, i64 0), i32 %11)
%13 = load i32, i32* %1
%14 = icmp sgt i32 %13, 0
br i1 %14, label %15, label %32
15: ; preds = %10, %25
%16 = phi i32 [ %28, %25 ], [ 1, %10 ]
%17 = phi i32 [ %27, %25 ], [ 0, %10 ]
%18 = phi i32 [ %30, %25 ], [ %13, %10 ]
%19 = urem i32 %18, 10
%20 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.6, i64 0, i64 0), i32 %19)
%21 = icmp ult i32 %19, 2
br i1 %21, label %25, label %22
22: ; preds = %15
%23 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([47 x i8], [47 x i8]* @str.9, i64 0, i64 0))
%24 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @str.12, i64 0, i64 0))
br label %35
25: ; preds = %15
%26 = mul nuw nsw i32 %16, %19
%27 = add nsw i32 %26, %17
%28 = shl nsw i32 %16, 1
%29 = load i32, i32* %1
%30 = sdiv i32 %29, 10
store i32 %30, i32* %1
%31 = icmp sgt i32 %29, 9
br i1 %31, label %15, label %32
32: ; preds = %25, %10
%33 = phi i32 [ 0, %10 ], [ %27, %25 ]
%34 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @.str.8, i64 0, i64 0), i32 %33)
br label %35
35: ; preds = %32, %22, %7
%36 = phi i32 [ 1, %7 ], [ 2, %22 ], [ 0, %32 ]
ret i32 %36
}