#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 */
typedef int /*<<< orphan*/ n;

/* Variables and functions */
int /*<<< orphan*/ printf(char *);
void calc(int n) {
  int i, mask;
  if (n == 0) {
    printf("0");
    return;
  }
  if (n == 1) {
    return;
  }
  if (n == 2) {
    printf("2");
    return;
  }
  for (i = sizeof(n) * 8 - 1; i >= 0; i--) {
    mask = 1 << i;
    if (mask & n) {
      printf("2");
      if (i != 1) {
        printf("(");
      }
      calc(i);
      if (i != 1) {
        printf(")");
      }
      if (n % mask) {
        printf("+");
      }
    }
  }
}


x86 assembly (clang, -O0):
<calc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
cmpl   $0x0,-0x4(%rbp)
jne    1146 <calc+0x26>
lea    0xec4(%rip),%rdi
call   1030 <printf@plt>
jmp    1211 <calc+0xf1>
cmpl   $0x1,-0x4(%rbp)
jne    1155 <calc+0x35>
jmp    1211 <calc+0xf1>
cmpl   $0x2,-0x4(%rbp)
jne    1170 <calc+0x50>
lea    0xe9c(%rip),%rdi
call   1030 <printf@plt>
jmp    1211 <calc+0xf1>
movl   $0x1f,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jl     1211 <calc+0xf1>
mov    -0x8(%rbp),%ecx
mov    $0x1,%eax
shl    %cl,%eax
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%eax
and    -0x4(%rbp),%eax
cmp    $0x0,%eax
je     11fe <calc+0xde>
lea    0xe5e(%rip),%rdi
call   1030 <printf@plt>
cmpl   $0x1,-0x8(%rbp)
je     11bf <calc+0x9f>
lea    0xe4a(%rip),%rdi
call   1030 <printf@plt>
mov    -0x8(%rbp),%edi
call   1040 <calc@plt>
cmpl   $0x1,-0x8(%rbp)
je     11dd <calc+0xbd>
lea    0xe2e(%rip),%rdi
call   1030 <printf@plt>
mov    -0x4(%rbp),%eax
cltd
idivl  -0xc(%rbp)
cmp    $0x0,%edx
je     11f9 <calc+0xd9>
lea    0xe14(%rip),%rdi
call   1030 <printf@plt>
jmp    11fe <calc+0xde>
jmp    1203 <calc+0xe3>
mov    -0x8(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x8(%rbp)
jmp    1177 <calc+0x57>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<calc>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
test   %edi,%edi
je     1144 <calc+0x24>
mov    %edi,%r14d
cmp    $0x1,%edi
je     11d0 <calc+0xb0>
cmp    $0x2,%r14d
jne    1158 <calc+0x38>
mov    $0x32,%edi
jmp    1149 <calc+0x29>
mov    $0x30,%edi
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <putchar@plt>
mov    $0x20,%ebx
jmp    1165 <calc+0x45>
nop
add    $0xffffffff,%ebx
je     11d0 <calc+0xb0>
lea    -0x1(%rbx),%r15d
mov    $0x1,%ebp
mov    %r15d,%ecx
shl    %cl,%ebp
bt     %r15d,%r14d
jae    1160 <calc+0x40>
mov    $0x32,%edi
call   1030 <putchar@plt>
cmp    $0x2,%ebx
jne    11a0 <calc+0x80>
mov    $0x1,%edi
call   1040 <calc@plt>
jmp    11bc <calc+0x9c>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    $0x28,%edi
call   1030 <putchar@plt>
mov    %r15d,%edi
call   1040 <calc@plt>
mov    $0x29,%edi
call   1030 <putchar@plt>
add    $0xffffffff,%ebp
test   %r14d,%ebp
je     1160 <calc+0x40>
mov    $0x2b,%edi
call   1030 <putchar@plt>
jmp    1160 <calc+0x40>
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"0\00"
@.str.1 = [2 x i8] c"2\00"
@.str.2 = [2 x i8] c"(\00"
@.str.3 = [2 x i8] c")\00"
@.str.4 = [2 x i8] c"+\00"
define void @calc(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
store i32 %0, i32* %2
%5 = load i32, i32* %2
%6 = icmp eq i32 %5, 0
br i1 %6, label %7, label %9
7: ; preds = %1
%8 = call i32 @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
br label %53
9: ; preds = %1
%10 = load i32, i32* %2
%11 = icmp eq i32 %10, 1
br i1 %11, label %12, label %13
12: ; preds = %9
br label %53
13: ; preds = %9
%14 = load i32, i32* %2
%15 = icmp eq i32 %14, 2
br i1 %15, label %16, label %18
16: ; preds = %13
%17 = call i32 @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
br label %53
18: ; preds = %13
store i32 31, i32* %3
br label %19
19: ; preds = %50, %18
%20 = load i32, i32* %3
%21 = icmp sge i32 %20, 0
br i1 %21, label %22, label %53
22: ; preds = %19
%23 = load i32, i32* %3
%24 = shl i32 1, %23
store i32 %24, i32* %4
%25 = load i32, i32* %4
%26 = load i32, i32* %2
%27 = and i32 %25, %26
%28 = icmp ne i32 %27, 0
br i1 %28, label %29, label %49
29: ; preds = %22
%30 = call i32 @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%31 = load i32, i32* %3
%32 = icmp ne i32 %31, 1
br i1 %32, label %33, label %35
33: ; preds = %29
%34 = call i32 @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
br label %35
35: ; preds = %33, %29
%36 = load i32, i32* %3
%37 = load i32, i32* %3
%38 = icmp ne i32 %37, 1
br i1 %38, label %39, label %41
39: ; preds = %35
%40 = call i32 @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
br label %41
41: ; preds = %39, %35
%42 = load i32, i32* %2
%43 = load i32, i32* %4
%44 = srem i32 %42, %43
%45 = icmp ne i32 %44, 0
br i1 %45, label %46, label %48
46: ; preds = %41
%47 = call i32 @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
br label %48
48: ; preds = %46, %41
br label %49
49: ; preds = %48, %22
br label %50
50: ; preds = %49
%51 = load i32, i32* %3
%52 = add nsw i32 %51, -1
store i32 %52, i32* %3
br label %19
53: ; preds = %7, %12, %16, %19
ret void
}

LLVM IR (-O2):
define void @calc(i32 %0) {
switch i32 %0, label %3 [
i32 0, label %24
i32 1, label %27
i32 2, label %2
]
2: ; preds = %1
br label %24
3: ; preds = %1, %21
%4 = phi i32 [ %22, %21 ], [ 31, %1 ]
%5 = shl nuw i32 1, %4
%6 = and i32 %5, %0
%7 = icmp eq i32 %6, 0
br i1 %7, label %21, label %8
8: ; preds = %3
%9 = tail call i32 @putchar(i32 50)
%10 = icmp eq i32 %4, 1
br i1 %10, label %14, label %11
11: ; preds = %8
%12 = tail call i32 @putchar(i32 40)
tail call void @calc(i32 %4)
%13 = tail call i32 @putchar(i32 41)
br label %15
14: ; preds = %8
tail call void @calc(i32 1)
br label %15
15: ; preds = %14, %11
%16 = add i32 %5, -1
%17 = and i32 %16, %0
%18 = icmp eq i32 %17, 0
br i1 %18, label %21, label %19
19: ; preds = %15
%20 = tail call i32 @putchar(i32 43)
br label %21
21: ; preds = %3, %19, %15
%22 = add nsw i32 %4, -1
%23 = icmp eq i32 %4, 0
br i1 %23, label %27, label %3
24: ; preds = %1, %2
%25 = phi i32 [ 50, %2 ], [ 48, %1 ]
%26 = tail call i32 @putchar(i32 %25)
br label %27
27: ; preds = %21, %24, %1
ret void
}