#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 */
scalar_t__ pow(int, int);
int /*<<< orphan*/ printf(char *, int, ...);
void half(int n) {
  int q = 0, i;
  printf("%d/2\n", n);
  while (n >= 2) {
    for (i = 1; ((int)pow(2, i)) <= n; i++)
      ;
    printf("i = %d, d = %d\n", i - 1, (int)pow(2, i - 1));
    n -= (int)pow(2, i - 1);
    q += i;
  }
  printf("quotient = %d, remainder = %d\n", q, n);
}


x86 assembly (clang, -O0):
<half>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
movl   $0x0,-0x8(%rbp)
mov    -0x4(%rbp),%esi
lea    0xec4(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
cmpl   $0x2,-0x4(%rbp)
jl     11d1 <half+0xb1>
movl   $0x1,-0xc(%rbp)
mov    -0xc(%rbp),%esi
mov    $0x2,%edi
call   1040 <pow@plt>
cmp    -0x4(%rbp),%eax
jg     117d <half+0x5d>
jmp    116f <half+0x4f>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    1154 <half+0x34>
mov    -0xc(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x10(%rbp)
mov    -0xc(%rbp),%esi
sub    $0x1,%esi
mov    $0x2,%edi
call   1040 <pow@plt>
mov    -0x10(%rbp),%esi
mov    %eax,%edx
lea    0xe64(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0xc(%rbp),%esi
sub    $0x1,%esi
mov    $0x2,%edi
call   1040 <pow@plt>
mov    %eax,%ecx
mov    -0x4(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x4(%rbp)
mov    -0xc(%rbp),%eax
add    -0x8(%rbp),%eax
mov    %eax,-0x8(%rbp)
jmp    1143 <half+0x23>
mov    -0x8(%rbp),%esi
mov    -0x4(%rbp),%edx
lea    0xe38(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<half>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edi,%ebp
lea    0xed0(%rip),%rdi
xor    %ebx,%ebx
mov    %ebp,%esi
xor    %eax,%eax
call   1030 <printf@plt>
cmp    $0x2,%ebp
jl     11ae <half+0x8e>
lea    0xebf(%rip),%r14
xor    %eax,%eax
nopl   0x0(%rax)
mov    %eax,%r15d
mov    $0xffffffff,%ebx
nopl   0x0(%rax,%rax,1)
lea    0x2(%rbx),%esi
mov    $0x2,%edi
call   1040 <pow@plt>
add    $0x1,%ebx
cmp    %eax,%ebp
jge    1160 <half+0x40>
mov    $0x2,%edi
mov    %ebx,%esi
call   1040 <pow@plt>
mov    %r14,%rdi
mov    %ebx,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1030 <printf@plt>
mov    $0x2,%edi
mov    %ebx,%esi
call   1040 <pow@plt>
sub    %eax,%ebp
lea    (%r15,%rbx,1),%eax
add    $0x1,%eax
cmp    $0x1,%ebp
jg     1150 <half+0x30>
add    %r15d,%ebx
add    $0x1,%ebx
lea    0xe61(%rip),%rdi
mov    %ebx,%esi
mov    %ebp,%edx
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <printf@plt>

LLVM IR (-O0):
@.str = [6 x i8] c"%d/2\0A\00"
@.str.1 = [16 x i8] c"i = %d, d = %d\0A\00"
@.str.2 = [31 x i8] c"quotient = %d, remainder = %d\0A\00"
define void @half(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
store i32 %0, i32* %2
store i32 0, i32* %3
%5 = load i32, i32* %2
%6 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32 %5)
br label %7
7: ; preds = %21, %1
%8 = load i32, i32* %2
%9 = icmp sge i32 %8, 2
br i1 %9, label %10, label %38
10: ; preds = %7
store i32 1, i32* %4
br label %11
11: ; preds = %18, %10
%12 = load i32, i32* %4
%13 = call i64 @pow(i32 2, i32 %12)
%14 = trunc i64 %13 to i32
%15 = load i32, i32* %2
%16 = icmp sle i32 %14, %15
br i1 %16, label %17, label %21
17: ; preds = %11
br label %18
18: ; preds = %17
%19 = load i32, i32* %4
%20 = add nsw i32 %19, 1
store i32 %20, i32* %4
br label %11
21: ; preds = %11
%22 = load i32, i32* %4
%23 = sub nsw i32 %22, 1
%24 = load i32, i32* %4
%25 = sub nsw i32 %24, 1
%26 = call i64 @pow(i32 2, i32 %25)
%27 = trunc i64 %26 to i32
%28 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i32 %23, i32 %27)
%29 = load i32, i32* %4
%30 = sub nsw i32 %29, 1
%31 = call i64 @pow(i32 2, i32 %30)
%32 = trunc i64 %31 to i32
%33 = load i32, i32* %2
%34 = sub nsw i32 %33, %32
store i32 %34, i32* %2
%35 = load i32, i32* %4
%36 = load i32, i32* %3
%37 = add nsw i32 %36, %35
store i32 %37, i32* %3
br label %7
38: ; preds = %7
%39 = load i32, i32* %3
%40 = load i32, i32* %2
%41 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0), i32 %39, i32 %40)
ret void
}

LLVM IR (-O2):
@.str = [6 x i8] c"%d/2\0A\00"
@.str.1 = [16 x i8] c"i = %d, d = %d\0A\00"
@.str.2 = [31 x i8] c"quotient = %d, remainder = %d\0A\00"
define void @half(i32 %0) {
%2 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32 %0)
%3 = icmp sgt i32 %0, 1
br i1 %3, label %4, label %23
4: ; preds = %1, %13
%5 = phi i32 [ %21, %13 ], [ 0, %1 ]
%6 = phi i32 [ %20, %13 ], [ %0, %1 ]
br label %7
7: ; preds = %7, %4
%8 = phi i32 [ %12, %7 ], [ 1, %4 ]
%9 = tail call i64 @pow(i32 2, i32 %8)
%10 = trunc i64 %9 to i32
%11 = icmp slt i32 %6, %10
%12 = add nuw nsw i32 %8, 1
br i1 %11, label %13, label %7
13: ; preds = %7
%14 = add nsw i32 %8, -1
%15 = tail call i64 @pow(i32 2, i32 %14)
%16 = trunc i64 %15 to i32
%17 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i32 %14, i32 %16)
%18 = tail call i64 @pow(i32 2, i32 %14)
%19 = trunc i64 %18 to i32
%20 = sub nsw i32 %6, %19
%21 = add nuw nsw i32 %8, %5
%22 = icmp sgt i32 %20, 1
br i1 %22, label %4, label %23
23: ; preds = %13, %1
%24 = phi i32 [ %0, %1 ], [ %20, %13 ]
%25 = phi i32 [ 0, %1 ], [ %21, %13 ]
%26 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0), i32 %25, i32 %24)
ret void
}