#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 *alloca(int);
int f(int a) {
  int *a_ref = alloca(sizeof(int));
  int *tmp = alloca(sizeof(int));
  int *count = alloca(sizeof(int));
  *a_ref = a;
  *tmp = 0;
  *count = 0;
  while (*a_ref > 1) {
    *tmp = *a_ref % 2;
    if (*tmp == 0)
      *a_ref = *a_ref / 2;
    else
      *a_ref = *a_ref - 1;
    (*count)++;
  }
  return *count;
}


x86 assembly (clang, -O0):
<f>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    $0x4,%edi
call   1030 <alloca@plt>
mov    %rax,-0x10(%rbp)
mov    $0x4,%edi
call   1030 <alloca@plt>
mov    %rax,-0x18(%rbp)
mov    $0x4,%edi
call   1030 <alloca@plt>
mov    %rax,-0x20(%rbp)
mov    -0x4(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x18(%rbp),%rax
movl   $0x0,(%rax)
mov    -0x20(%rbp),%rax
movl   $0x0,(%rax)
mov    -0x10(%rbp),%rax
cmpl   $0x1,(%rax)
jle    11ca <f+0xba>
mov    -0x10(%rbp),%rax
mov    (%rax),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    -0x18(%rbp),%rax
mov    %edx,(%rax)
mov    -0x18(%rbp),%rax
cmpl   $0x0,(%rax)
jne    11ab <f+0x9b>
mov    -0x10(%rbp),%rax
mov    (%rax),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
jmp    11ba <f+0xaa>
mov    -0x10(%rbp),%rax
mov    (%rax),%ecx
sub    $0x1,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x20(%rbp),%rax
mov    (%rax),%ecx
add    $0x1,%ecx
mov    %ecx,(%rax)
jmp    1162 <f+0x52>
mov    -0x20(%rbp),%rax
mov    (%rax),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<f>:
push   %rbp
push   %r14
push   %rbx
mov    %edi,%ebp
mov    $0x4,%edi
call   1030 <alloca@plt>
mov    %rax,%rbx
mov    $0x4,%edi
call   1030 <alloca@plt>
mov    %rax,%r14
mov    $0x4,%edi
call   1030 <alloca@plt>
mov    %ebp,(%rbx)
movl   $0x0,(%r14)
movl   $0x0,(%rax)
mov    (%rbx),%edx
xor    %ecx,%ecx
cmp    $0x2,%edx
jge    1175 <f+0x65>
mov    %ecx,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
nopl   0x0(%rax)
add    $0xffffffff,%ecx
mov    %ecx,%edx
mov    %edx,(%rbx)
mov    (%rax),%ecx
add    $0x1,%ecx
mov    %ecx,(%rax)
mov    (%rbx),%edx
cmp    $0x1,%edx
jle    1152 <f+0x42>
mov    %edx,%ecx
and    $0x1,%ecx
mov    %ecx,(%r14)
mov    (%rbx),%ecx
test   $0x1,%dl
jne    1160 <f+0x50>
mov    %ecx,%edx
shr    $0x1f,%edx
add    %ecx,%edx
sar    %edx
jmp    1165 <f+0x55>

LLVM IR (-O0):
define i32 @f(i32 %0) {
%2 = alloca i32
%3 = alloca i32*
%4 = alloca i32*
%5 = alloca i32*
store i32 %0, i32* %2
%6 = call i32* @alloca(i32 4)
store i32* %6, i32** %3
%7 = call i32* @alloca(i32 4)
store i32* %7, i32** %4
%8 = call i32* @alloca(i32 4)
store i32* %8, i32** %5
%9 = load i32, i32* %2
%10 = load i32*, i32** %3
store i32 %9, i32* %10
%11 = load i32*, i32** %4
store i32 0, i32* %11
%12 = load i32*, i32** %5
store i32 0, i32* %12
br label %13
13: ; preds = %35, %1
%14 = load i32*, i32** %3
%15 = load i32, i32* %14
%16 = icmp sgt i32 %15, 1
br i1 %16, label %17, label %39
17: ; preds = %13
%18 = load i32*, i32** %3
%19 = load i32, i32* %18
%20 = srem i32 %19, 2
%21 = load i32*, i32** %4
store i32 %20, i32* %21
%22 = load i32*, i32** %4
%23 = load i32, i32* %22
%24 = icmp eq i32 %23, 0
br i1 %24, label %25, label %30
25: ; preds = %17
%26 = load i32*, i32** %3
%27 = load i32, i32* %26
%28 = sdiv i32 %27, 2
%29 = load i32*, i32** %3
store i32 %28, i32* %29
br label %35
30: ; preds = %17
%31 = load i32*, i32** %3
%32 = load i32, i32* %31
%33 = sub nsw i32 %32, 1
%34 = load i32*, i32** %3
store i32 %33, i32* %34
br label %35
35: ; preds = %30, %25
%36 = load i32*, i32** %5
%37 = load i32, i32* %36
%38 = add nsw i32 %37, 1
store i32 %38, i32* %36
br label %13
39: ; preds = %13
%40 = load i32*, i32** %5
%41 = load i32, i32* %40
ret i32 %41
}

LLVM IR (-O2):
define i32 @f(i32 %0) {
%2 = tail call i32* @alloca(i32 4)
%3 = tail call i32* @alloca(i32 4)
%4 = tail call i32* @alloca(i32 4)
store i32 %0, i32* %2
store i32 0, i32* %3
store i32 0, i32* %4
%5 = load i32, i32* %2
%6 = icmp sgt i32 %5, 1
br i1 %6, label %7, label %22
7: ; preds = %1, %16
%8 = phi i32 [ %20, %16 ], [ %5, %1 ]
%9 = and i32 %8, 1
store i32 %9, i32* %3
%10 = icmp eq i32 %9, 0
%11 = load i32, i32* %2
br i1 %10, label %12, label %14
12: ; preds = %7
%13 = sdiv i32 %11, 2
br label %16
14: ; preds = %7
%15 = add nsw i32 %11, -1
br label %16
16: ; preds = %14, %12
%17 = phi i32 [ %15, %14 ], [ %13, %12 ]
store i32 %17, i32* %2
%18 = load i32, i32* %4
%19 = add nsw i32 %18, 1
store i32 %19, i32* %4
%20 = load i32, i32* %2
%21 = icmp sgt i32 %20, 1
br i1 %21, label %7, label %22
22: ; preds = %16, %1
%23 = phi i32 [ 0, %1 ], [ %19, %16 ]
ret i32 %23
}