#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 uint;

/* Variables and functions */
int atoi(char *);
int /*<<< orphan*/ divideby2(int, int, int, int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int);
scalar_t__ powerof2(int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ stderr;
int main(int argc, char *argv[]) {
  if (argc != 2) {
    printf("Usage: divc <n>\n");
    printf("Generates optimal ARM code for divide-by-constant\n");
    printf("where <n> is one of (2^n-2^m) or (2^n+2^m) eg. 10\n");
    printf("Advanced RISC Machines [%s]\n", "27 Jan 94");
  } else {
    int num;
    num = atoi(argv[1]);
    if (num <= 1) {
      fprintf(stderr, "%d is not sensible\n", num);
    } else {
      uint lsb = 1;
      while ((num & lsb) == 0) {
        lsb <<= 1;
      }
      if (powerof2(num)) {
        fprintf(stderr, "%d is an easy case\n", num);
      } else if (powerof2(num + lsb)) {
        divideby2(0, num, lsb, num + lsb);
      } else if (powerof2(num - lsb)) {
        divideby2(1, num, lsb, num - lsb);
      } else {
        fprintf(stderr, "%d is not one of (2^n-2^m) or (2^n+2^m)\n", num);
      }
    }
  }
  return (0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     11b4 <main+0x64>
lea    0xe89(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe8c(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xeb1(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xed6(%rip),%rdi
lea    0xeec(%rip),%rsi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    12c7 <main+0x177>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1070 <atoi@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x1,-0x14(%rbp)
jg     11eb <main+0x9b>
mov    0x2e0b(%rip),%rax
mov    (%rax),%edi
mov    -0x14(%rbp),%edx
lea    0xebd(%rip),%rsi
call   1050 <fprintf@plt>
jmp    12c2 <main+0x172>
movl   $0x1,-0x18(%rbp)
mov    -0x14(%rbp),%eax
and    -0x18(%rbp),%eax
cmp    $0x0,%eax
jne    120f <main+0xbf>
mov    -0x18(%rbp),%eax
shl    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    11f2 <main+0xa2>
mov    -0x14(%rbp),%edi
call   1060 <powerof2@plt>
cmp    $0x0,%rax
je     123e <main+0xee>
mov    0x2db8(%rip),%rax
mov    (%rax),%edi
mov    -0x14(%rbp),%edx
lea    0xe7e(%rip),%rsi
call   1050 <fprintf@plt>
jmp    12bd <main+0x16d>
mov    -0x14(%rbp),%edi
add    -0x18(%rbp),%edi
call   1060 <powerof2@plt>
cmp    $0x0,%rax
je     126b <main+0x11b>
mov    -0x14(%rbp),%esi
mov    -0x18(%rbp),%edx
mov    -0x14(%rbp),%ecx
add    -0x18(%rbp),%ecx
xor    %edi,%edi
call   1030 <divideby2@plt>
jmp    12b8 <main+0x168>
mov    -0x14(%rbp),%edi
sub    -0x18(%rbp),%edi
call   1060 <powerof2@plt>
cmp    $0x0,%rax
je     129b <main+0x14b>
mov    -0x14(%rbp),%esi
mov    -0x18(%rbp),%edx
mov    -0x14(%rbp),%ecx
sub    -0x18(%rbp),%ecx
mov    $0x1,%edi
call   1030 <divideby2@plt>
jmp    12b3 <main+0x163>
mov    0x2d3e(%rip),%rax
mov    (%rax),%edi
mov    -0x14(%rbp),%edx
lea    0xe18(%rip),%rsi
call   1050 <fprintf@plt>
jmp    12b8 <main+0x168>
jmp    12bd <main+0x16d>
jmp    12c2 <main+0x172>
jmp    12c7 <main+0x177>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
cmp    $0x2,%edi
jne    118c <main+0x2c>
mov    0x8(%rsi),%rdi
call   1080 <atoi@plt>
mov    %eax,%r14d
cmp    $0x2,%eax
jge    11c7 <main+0x67>
mov    0x2e5f(%rip),%rax
mov    (%rax),%edi
lea    0xe9d(%rip),%rsi
jmp    11f7 <main+0x97>
lea    0xee5(%rip),%rdi
call   1030 <puts@plt>
lea    0xee9(%rip),%rdi
call   1030 <puts@plt>
lea    0xf0f(%rip),%rdi
call   1030 <puts@plt>
lea    0xe49(%rip),%rdi
lea    0xe5f(%rip),%rsi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    11ff <main+0x9f>
mov    $0x1,%eax
nopl   0x0(%rax)
mov    %eax,%ebx
lea    (%rbx,%rbx,1),%eax
test   %r14d,%ebx
je     11d0 <main+0x70>
mov    %r14d,%edi
call   1070 <powerof2@plt>
test   %rax,%rax
je     1206 <main+0xa6>
mov    0x2df2(%rip),%rax
mov    (%rax),%edi
lea    0xe44(%rip),%rsi
mov    %r14d,%edx
call   1060 <fprintf@plt>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    (%rbx,%r14,1),%ebp
mov    %ebp,%edi
call   1070 <powerof2@plt>
test   %rax,%rax
je     121a <main+0xba>
xor    %edi,%edi
jmp    1230 <main+0xd0>
mov    %r14d,%ebp
sub    %ebx,%ebp
mov    %ebp,%edi
call   1070 <powerof2@plt>
test   %rax,%rax
je     123e <main+0xde>
mov    $0x1,%edi
mov    %r14d,%esi
mov    %ebx,%edx
mov    %ebp,%ecx
call   1040 <divideby2@plt>
jmp    11ff <main+0x9f>
mov    0x2d9b(%rip),%rax
mov    (%rax),%edi
lea    0xe01(%rip),%rsi
jmp    11f7 <main+0x97>

LLVM IR (-O0):
@.str = [17 x i8] c"Usage: divc <n>\0A\00"
@.str.1 = [51 x i8] c"Generates optimal ARM code for divide-by-constant\0A\00"
@.str.2 = [51 x i8] c"where <n> is one of (2^n-2^m) or (2^n+2^m) eg. 10\0A\00"
@.str.3 = [29 x i8] c"Advanced RISC Machines [%s]\0A\00"
@.str.4 = [10 x i8] c"27 Jan 94\00"
@stderr = global i32 0
@.str.5 = [20 x i8] c"%d is not sensible\0A\00"
@.str.6 = [20 x i8] c"%d is an easy case\0A\00"
@.str.7 = [41 x i8] c"%d is not one of (2^n-2^m) or (2^n+2^m)\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = load i32, i32* %4
%9 = icmp ne i32 %8, 2
br i1 %9, label %10, label %15
10: ; preds = %2
%11 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
%12 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.1, i64 0, i64 0))
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.2, i64 0, i64 0))
%14 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0))
br label %77
15: ; preds = %2
%16 = load i8**, i8*** %5
%17 = getelementptr inbounds i8*, i8** %16, i64 1
%18 = load i8*, i8** %17
%19 = call i32 @atoi(i8* %18)
store i32 %19, i32* %6
%20 = load i32, i32* %6
%21 = icmp sle i32 %20, 1
br i1 %21, label %22, label %26
22: ; preds = %15
%23 = load i32, i32* @stderr
%24 = load i32, i32* %6
%25 = call i32 @fprintf(i32 %23, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.5, i64 0, i64 0), i32 %24)
br label %76
26: ; preds = %15
store i32 1, i32* %7
br label %27
27: ; preds = %32, %26
%28 = load i32, i32* %6
%29 = load i32, i32* %7
%30 = and i32 %28, %29
%31 = icmp eq i32 %30, 0
br i1 %31, label %32, label %35
32: ; preds = %27
%33 = load i32, i32* %7
%34 = shl i32 %33, 1
store i32 %34, i32* %7
br label %27
35: ; preds = %27
%36 = load i32, i32* %6
%37 = call i64 @powerof2(i32 %36)
%38 = icmp ne i64 %37, 0
br i1 %38, label %39, label %43
39: ; preds = %35
%40 = load i32, i32* @stderr
%41 = load i32, i32* %6
%42 = call i32 @fprintf(i32 %40, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.6, i64 0, i64 0), i32 %41)
br label %75
43: ; preds = %35
%44 = load i32, i32* %6
%45 = load i32, i32* %7
%46 = add nsw i32 %44, %45
%47 = call i64 @powerof2(i32 %46)
%48 = icmp ne i64 %47, 0
br i1 %48, label %49, label %56
49: ; preds = %43
%50 = load i32, i32* %6
%51 = load i32, i32* %7
%52 = load i32, i32* %6
%53 = load i32, i32* %7
%54 = add nsw i32 %52, %53
%55 = call i32 @divideby2(i32 0, i32 %50, i32 %51, i32 %54)
br label %74
56: ; preds = %43
%57 = load i32, i32* %6
%58 = load i32, i32* %7
%59 = sub nsw i32 %57, %58
%60 = call i64 @powerof2(i32 %59)
%61 = icmp ne i64 %60, 0
br i1 %61, label %62, label %69
62: ; preds = %56
%63 = load i32, i32* %6
%64 = load i32, i32* %7
%65 = load i32, i32* %6
%66 = load i32, i32* %7
%67 = sub nsw i32 %65, %66
%68 = call i32 @divideby2(i32 1, i32 %63, i32 %64, i32 %67)
br label %73
69: ; preds = %56
%70 = load i32, i32* @stderr
%71 = load i32, i32* %6
%72 = call i32 @fprintf(i32 %70, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.7, i64 0, i64 0), i32 %71)
br label %73
73: ; preds = %69, %62
br label %74
74: ; preds = %73, %49
br label %75
75: ; preds = %74, %39
br label %76
76: ; preds = %75, %22
br label %77
77: ; preds = %76, %10
ret i32 0
}

LLVM IR (-O2):
@.str.3 = [29 x i8] c"Advanced RISC Machines [%s]\0A\00"
@.str.4 = [10 x i8] c"27 Jan 94\00"
@stderr = global i32 0
@.str.5 = [20 x i8] c"%d is not sensible\0A\00"
@.str.6 = [20 x i8] c"%d is an easy case\0A\00"
@.str.7 = [41 x i8] c"%d is not one of (2^n-2^m) or (2^n+2^m)\0A\00"
@str = [16 x i8] c"Usage: divc <n>\00"
@str.8 = [50 x i8] c"Generates optimal ARM code for divide-by-constant\00"
@str.9 = [50 x i8] c"where <n> is one of (2^n-2^m) or (2^n+2^m) eg. 10\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp eq i32 %0, 2
br i1 %3, label %9, label %4
4: ; preds = %2
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str, i64 0, i64 0))
%6 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([50 x i8], [50 x i8]* @str.8, i64 0, i64 0))
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([50 x i8], [50 x i8]* @str.9, i64 0, i64 0))
%8 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0))
br label %43
9: ; preds = %2
%10 = getelementptr inbounds i8*, i8** %1, i64 1
%11 = load i8*, i8** %10
%12 = tail call i32 @atoi(i8* %11)
%13 = icmp slt i32 %12, 2
br i1 %13, label %14, label %17
14: ; preds = %9
%15 = load i32, i32* @stderr
%16 = tail call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.5, i64 0, i64 0), i32 %12)
br label %43
17: ; preds = %9, %17
%18 = phi i32 [ %21, %17 ], [ 1, %9 ]
%19 = and i32 %18, %12
%20 = icmp eq i32 %19, 0
%21 = shl i32 %18, 1
br i1 %20, label %17, label %22
22: ; preds = %17
%23 = tail call i64 @powerof2(i32 %12)
%24 = icmp eq i64 %23, 0
br i1 %24, label %28, label %25
25: ; preds = %22
%26 = load i32, i32* @stderr
%27 = tail call i32 @fprintf(i32 %26, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.6, i64 0, i64 0), i32 %12)
br label %43
28: ; preds = %22
%29 = add nsw i32 %18, %12
%30 = tail call i64 @powerof2(i32 %29)
%31 = icmp eq i64 %30, 0
br i1 %31, label %34, label %32
32: ; preds = %28
%33 = tail call i32 @divideby2(i32 0, i32 %12, i32 %18, i32 %29)
br label %43
34: ; preds = %28
%35 = sub nsw i32 %12, %18
%36 = tail call i64 @powerof2(i32 %35)
%37 = icmp eq i64 %36, 0
br i1 %37, label %40, label %38
38: ; preds = %34
%39 = tail call i32 @divideby2(i32 1, i32 %12, i32 %18, i32 %35)
br label %43
40: ; preds = %34
%41 = load i32, i32* @stderr
%42 = tail call i32 @fprintf(i32 %41, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.7, i64 0, i64 0), i32 %12)
br label %43
43: ; preds = %14, %32, %40, %38, %25, %4
ret i32 0
}