#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 /*<<< orphan*/ scanf(char *, long *);
int bintooct() {
  long int binum, octnum = 0, j = 1, rem;
  printf("Enter binary value: ");
  scanf("%ld", &binum);
  while (binum != 0) {
    rem = binum % 10;
    octnum += rem * j;
    j *= 2;
    binum /= 10;
  }
  printf("Octal: %lo", octnum);
  return octnum;
}


x86 assembly (clang, -O0):
<bintooct>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movq   $0x0,-0x10(%rbp)
movq   $0x1,-0x18(%rbp)
lea    0xec1(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xec8(%rip),%rdi
lea    -0x8(%rbp),%rsi
call   1040 <scanf@plt>
cmpq   $0x0,-0x8(%rbp)
je     11a7 <bintooct+0x87>
mov    -0x8(%rbp),%rax
mov    $0xa,%ecx
cqto
idiv   %rcx
mov    %rdx,-0x20(%rbp)
mov    -0x20(%rbp),%rax
imul   -0x18(%rbp),%rax
add    -0x10(%rbp),%rax
mov    %rax,-0x10(%rbp)
mov    -0x18(%rbp),%rax
shl    $0x1,%rax
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rax
mov    $0xa,%ecx
cqto
idiv   %rcx
mov    %rax,-0x8(%rbp)
jmp    1156 <bintooct+0x36>
mov    -0x10(%rbp),%rsi
lea    0xe67(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bintooct>:
push   %rbx
sub    $0x10,%rsp
lea    0xed4(%rip),%rdi
xor    %ebx,%ebx
xor    %eax,%eax
call   1030 <printf@plt>
lea    0xed9(%rip),%rdi
lea    0x8(%rsp),%rsi
call   1040 <scanf@plt>
mov    0x8(%rsp),%rdx
test   %rdx,%rdx
je     11ae <bintooct+0x8e>
mov    $0x1,%esi
xor    %ebx,%ebx
movabs $0x6666666666666667,%r8
66 66 66
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %rdx,%rdi
mov    %rdx,%rax
imul   %r8
mov    %rdx,%rax
shr    $0x3f,%rax
sar    $0x2,%rdx
add    %rax,%rdx
lea    (%rdx,%rdx,1),%rax
lea    (%rax,%rax,4),%rax
mov    %rdi,%rcx
sub    %rax,%rcx
imul   %rsi,%rcx
add    %rcx,%rbx
add    %rsi,%rsi
add    $0x9,%rdi
cmp    $0x12,%rdi
ja     1170 <bintooct+0x50>
mov    %rdx,0x8(%rsp)
lea    0xe64(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1030 <printf@plt>
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@.str = [21 x i8] c"Enter binary value: \00"
@.str.1 = [4 x i8] c"%ld\00"
@.str.2 = [11 x i8] c"Octal: %lo\00"
define i32 @bintooct() {
%1 = alloca i64
%2 = alloca i64
%3 = alloca i64
%4 = alloca i64
store i64 0, i64* %2
store i64 1, i64* %3
%5 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
%6 = call i32 @scanf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i64* %1)
br label %7
7: ; preds = %10, %0
%8 = load i64, i64* %1
%9 = icmp ne i64 %8, 0
br i1 %9, label %10, label %22
10: ; preds = %7
%11 = load i64, i64* %1
%12 = srem i64 %11, 10
store i64 %12, i64* %4
%13 = load i64, i64* %4
%14 = load i64, i64* %3
%15 = mul nsw i64 %13, %14
%16 = load i64, i64* %2
%17 = add nsw i64 %16, %15
store i64 %17, i64* %2
%18 = load i64, i64* %3
%19 = mul nsw i64 %18, 2
store i64 %19, i64* %3
%20 = load i64, i64* %1
%21 = sdiv i64 %20, 10
store i64 %21, i64* %1
br label %7
22: ; preds = %7
%23 = load i64, i64* %2
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i64 %23)
%25 = load i64, i64* %2
%26 = trunc i64 %25 to i32
ret i32 %26
}

LLVM IR (-O2):
@.str = [21 x i8] c"Enter binary value: \00"
@.str.1 = [4 x i8] c"%ld\00"
@.str.2 = [11 x i8] c"Octal: %lo\00"
define i32 @bintooct() {
%1 = alloca i64
%2 = bitcast i64* %1 to i8*
%3 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
%4 = call i32 @scanf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i64* %1)
%5 = load i64, i64* %1
%6 = icmp eq i64 %5, 0
br i1 %6, label %19, label %7
7: ; preds = %0, %7
%8 = phi i64 [ %13, %7 ], [ 0, %0 ]
%9 = phi i64 [ %14, %7 ], [ 1, %0 ]
%10 = phi i64 [ %15, %7 ], [ %5, %0 ]
%11 = srem i64 %10, 10
%12 = mul nsw i64 %11, %9
%13 = add nsw i64 %8, %12
%14 = shl nsw i64 %9, 1
%15 = sdiv i64 %10, 10
%16 = add i64 %10, 9
%17 = icmp ult i64 %16, 19
br i1 %17, label %18, label %7
18: ; preds = %7
store i64 %15, i64* %1
br label %19
19: ; preds = %18, %0
%20 = phi i64 [ %13, %18 ], [ 0, %0 ]
%21 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i64 %20)
%22 = trunc i64 %20 to i32
ret i32 %22
}