#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 fixed_div(int a, int b) {
  int c;
  if (b == 0) {
    printf("Divide by zero!\n");
    return 0;
  }
  c = a << 12;
  if ((c >> 12) != a) {
    c = a << 8;
    c /= b;
    c <<= 4;
  } else {
    c /= b;
  }
  return c;
}


x86 assembly (clang, -O0):
<fixed_div>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jne    1140 <fixed_div+0x30>
lea    0xed1(%rip),%rdi
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    118d <fixed_div+0x7d>
mov    -0x8(%rbp),%eax
shl    $0xc,%eax
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%eax
sar    $0xc,%eax
cmp    -0x8(%rbp),%eax
je     117b <fixed_div+0x6b>
mov    -0x8(%rbp),%eax
shl    $0x8,%eax
mov    %eax,-0x10(%rbp)
mov    -0xc(%rbp),%ecx
mov    -0x10(%rbp),%eax
cltd
idiv   %ecx
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%eax
shl    $0x4,%eax
mov    %eax,-0x10(%rbp)
jmp    1187 <fixed_div+0x77>
mov    -0xc(%rbp),%ecx
mov    -0x10(%rbp),%eax
cltd
idiv   %ecx
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fixed_div>:
test   %esi,%esi
je     112a <fixed_div+0x1a>
lea    0x80000(%rdi),%eax
cmp    $0x100000,%eax
jae    113e <fixed_div+0x2e>
shl    $0xc,%edi
mov    %edi,%eax
cltd
idiv   %esi
ret
push   %rax
lea    0xece(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
add    $0x8,%rsp
ret
shl    $0x8,%edi
mov    %edi,%eax
cltd
idiv   %esi
shl    $0x4,%eax
ret

LLVM IR (-O0):
@.str = [17 x i8] c"Divide by zero!\0A\00"
define i32 @fixed_div(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
%7 = load i32, i32* %5
%8 = icmp eq i32 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %2
%10 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %3
br label %32
11: ; preds = %2
%12 = load i32, i32* %4
%13 = shl i32 %12, 12
store i32 %13, i32* %6
%14 = load i32, i32* %6
%15 = ashr i32 %14, 12
%16 = load i32, i32* %4
%17 = icmp ne i32 %15, %16
br i1 %17, label %18, label %26
18: ; preds = %11
%19 = load i32, i32* %4
%20 = shl i32 %19, 8
store i32 %20, i32* %6
%21 = load i32, i32* %5
%22 = load i32, i32* %6
%23 = sdiv i32 %22, %21
store i32 %23, i32* %6
%24 = load i32, i32* %6
%25 = shl i32 %24, 4
store i32 %25, i32* %6
br label %30
26: ; preds = %11
%27 = load i32, i32* %5
%28 = load i32, i32* %6
%29 = sdiv i32 %28, %27
store i32 %29, i32* %6
br label %30
30: ; preds = %26, %18
%31 = load i32, i32* %6
store i32 %31, i32* %3
br label %32
32: ; preds = %30, %9
%33 = load i32, i32* %3
ret i32 %33
}

LLVM IR (-O2):
@str = [16 x i8] c"Divide by zero!\00"
define i32 @fixed_div(i32 %0, i32 %1) {
%3 = icmp eq i32 %1, 0
br i1 %3, label %4, label %6
4: ; preds = %2
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str, i64 0, i64 0))
br label %16
6: ; preds = %2
%7 = add i32 %0, 524288
%8 = icmp ult i32 %7, 1048576
br i1 %8, label %13, label %9
9: ; preds = %6
%10 = shl i32 %0, 8
%11 = sdiv i32 %10, %1
%12 = shl i32 %11, 4
br label %16
13: ; preds = %6
%14 = shl nsw i32 %0, 12
%15 = sdiv i32 %14, %1
br label %16
16: ; preds = %9, %13, %4
%17 = phi i32 [ 0, %4 ], [ %12, %9 ], [ %15, %13 ]
ret i32 %17
}