#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 int32_t;
typedef scalar_t__ int16_t;

/* Variables and functions */
int L_deposit_l(scalar_t__);
int L_sub(int, int);
scalar_t__ add(scalar_t__, int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int16_t div_s(int16_t var1, int16_t var2) {
  int16_t var_out = 0;
  int16_t iteration;
  int32_t L_num;
  int32_t L_denom;
  if ((var1 > var2) || (var1 < 0) || (var2 < 0)) {
    printf("Division Error var1=%d  var2=%d\n", var1, var2);
    exit(0);
  }
  if (var2 == 0) {
    printf("Division by 0, Fatal error \n");
    exit(0);
  }
  if (var1 == 0) {
    var_out = 0;
  } else {
    if (var1 == var2) {
      var_out = (int16_t)0x7fff;
    } else {
      L_num = L_deposit_l(var1);
      L_denom = L_deposit_l(var2);
      for (iteration = 0; iteration < 15; iteration++) {
        var_out <<= 1;
        L_num <<= 1;
        if (L_num >= L_denom) {
          L_num = L_sub(L_num, L_denom);
          var_out = add(var_out, 1);
        }
      }
    }
  }
  return (var_out);
}


x86 assembly (clang, -O0):
<div_s>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
mov    -0x8(%rbp),%rax
cmp    -0x10(%rbp),%rax
jg     118c <div_s+0x3c>
cmpq   $0x0,-0x8(%rbp)
jl     118c <div_s+0x3c>
cmpq   $0x0,-0x10(%rbp)
jge    11a9 <div_s+0x59>
mov    -0x8(%rbp),%rsi
mov    -0x10(%rbp),%rdx
lea    0xe65(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
xor    %edi,%edi
call   1070 <exit@plt>
cmpq   $0x0,-0x10(%rbp)
jne    11c9 <div_s+0x79>
lea    0xe66(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
xor    %edi,%edi
call   1070 <exit@plt>
cmpq   $0x0,-0x8(%rbp)
jne    11e1 <div_s+0x91>
movq   $0x0,-0x18(%rbp)
jmp    1288 <div_s+0x138>
mov    -0x8(%rbp),%rax
cmp    -0x10(%rbp),%rax
jne    11fc <div_s+0xac>
movq   $0x7fff,-0x18(%rbp)
jmp    1283 <div_s+0x133>
mov    -0x8(%rbp),%rdi
call   1060 <L_deposit_l@plt>
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <L_deposit_l@plt>
mov    %eax,-0x28(%rbp)
movq   $0x0,-0x20(%rbp)
cmpq   $0xf,-0x20(%rbp)
jge    127e <div_s+0x12e>
mov    -0x18(%rbp),%rax
shl    $0x1,%rax
mov    %rax,-0x18(%rbp)
mov    -0x24(%rbp),%eax
shl    $0x1,%eax
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x28(%rbp),%eax
jl     1268 <div_s+0x118>
mov    -0x24(%rbp),%edi
mov    -0x28(%rbp),%esi
call   1040 <L_sub@plt>
mov    %eax,-0x24(%rbp)
mov    -0x18(%rbp),%rdi
mov    $0x1,%esi
call   1030 <add@plt>
mov    %rax,-0x18(%rbp)
jmp    126d <div_s+0x11d>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    121c <div_s+0xcc>
jmp    1283 <div_s+0x133>
jmp    1288 <div_s+0x138>
mov    -0x18(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<div_s>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rsi
cmp    %r14,%rdi
jg     11ff <div_s+0x9f>
mov    %r14,%rax
or     %rsi,%rax
js     11ff <div_s+0x9f>
test   %r14,%r14
je     1217 <div_s+0xb7>
test   %rsi,%rsi
je     11ee <div_s+0x8e>
mov    $0x7fff,%r15d
cmp    %r14,%rsi
je     11f1 <div_s+0x91>
mov    %rsi,%rdi
call   1070 <L_deposit_l@plt>
mov    %eax,%ebp
mov    %r14,%rdi
call   1070 <L_deposit_l@plt>
mov    %eax,%r14d
mov    $0xf,%ebx
xor    %r15d,%r15d
jmp    11c6 <div_s+0x66>
nopw   0x0(%rax,%rax,1)
add    $0xffffffffffffffff,%rbx
je     11f1 <div_s+0x91>
add    %r15,%r15
add    %ebp,%ebp
cmp    %r14d,%ebp
jl     11c0 <div_s+0x60>
mov    %ebp,%edi
mov    %r14d,%esi
call   1050 <L_sub@plt>
mov    %eax,%ebp
mov    %r15,%rdi
mov    $0x1,%esi
call   1030 <add@plt>
mov    %rax,%r15
jmp    11c0 <div_s+0x60>
xor    %r15d,%r15d
mov    %r15,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdfa(%rip),%rdi
mov    %r14,%rdx
xor    %eax,%eax
call   1060 <printf@plt>
xor    %edi,%edi
call   1080 <exit@plt>
lea    0xe03(%rip),%rdi
call   1040 <puts@plt>
xor    %edi,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [33 x i8] c"Division Error var1=%d var2=%d\0A\00"
@.str.1 = [29 x i8] c"Division by 0, Fatal error \0A\00"
define i64 @div_s(i64 %0, i64 %1) {
%3 = alloca i64
%4 = alloca i64
%5 = alloca i64
%6 = alloca i64
%7 = alloca i32
%8 = alloca i32
store i64 %0, i64* %3
store i64 %1, i64* %4
store i64 0, i64* %5
%9 = load i64, i64* %3
%10 = load i64, i64* %4
%11 = icmp sgt i64 %9, %10
br i1 %11, label %18, label %12
12: ; preds = %2
%13 = load i64, i64* %3
%14 = icmp slt i64 %13, 0
br i1 %14, label %18, label %15
15: ; preds = %12
%16 = load i64, i64* %4
%17 = icmp slt i64 %16, 0
br i1 %17, label %18, label %23
18: ; preds = %15, %12, %2
%19 = load i64, i64* %3
%20 = load i64, i64* %4
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0), i64 %19, i64 %20)
%22 = call i32 @exit(i32 0)
unreachable
23: ; preds = %15
%24 = load i64, i64* %4
%25 = icmp eq i64 %24, 0
br i1 %25, label %26, label %29
26: ; preds = %23
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0))
%28 = call i32 @exit(i32 0)
unreachable
29: ; preds = %23
%30 = load i64, i64* %3
%31 = icmp eq i64 %30, 0
br i1 %31, label %32, label %33
32: ; preds = %29
store i64 0, i64* %5
br label %66
33: ; preds = %29
%34 = load i64, i64* %3
%35 = load i64, i64* %4
%36 = icmp eq i64 %34, %35
br i1 %36, label %37, label %38
37: ; preds = %33
store i64 32767, i64* %5
br label %65
38: ; preds = %33
%39 = load i64, i64* %3
%40 = call i32 @L_deposit_l(i64 %39)
store i32 %40, i32* %7
%41 = load i64, i64* %4
%42 = call i32 @L_deposit_l(i64 %41)
store i32 %42, i32* %8
store i64 0, i64* %6
br label %43
43: ; preds = %61, %38
%44 = load i64, i64* %6
%45 = icmp slt i64 %44, 15
br i1 %45, label %46, label %64
46: ; preds = %43
%47 = load i64, i64* %5
%48 = shl i64 %47, 1
store i64 %48, i64* %5
%49 = load i32, i32* %7
%50 = shl i32 %49, 1
store i32 %50, i32* %7
%51 = load i32, i32* %7
%52 = load i32, i32* %8
%53 = icmp sge i32 %51, %52
br i1 %53, label %54, label %60
54: ; preds = %46
%55 = load i32, i32* %7
%56 = load i32, i32* %8
%57 = call i32 @L_sub(i32 %55, i32 %56)
store i32 %57, i32* %7
%58 = load i64, i64* %5
%59 = call i64 @add(i64 %58, i32 1)
store i64 %59, i64* %5
br label %60
60: ; preds = %54, %46
br label %61
61: ; preds = %60
%62 = load i64, i64* %6
%63 = add nsw i64 %62, 1
store i64 %63, i64* %6
br label %43
64: ; preds = %43
br label %65
65: ; preds = %64, %37
br label %66
66: ; preds = %65, %32
%67 = load i64, i64* %5
ret i64 %67
}

LLVM IR (-O2):
@.str = [33 x i8] c"Division Error var1=%d var2=%d\0A\00"
@str = [28 x i8] c"Division by 0, Fatal error \00"
define i64 @div_s(i64 %0, i64 %1) {
%3 = icmp sgt i64 %0, %1
%4 = or i64 %1, %0
%5 = icmp slt i64 %4, 0
%6 = or i1 %3, %5
br i1 %6, label %7, label %10
7: ; preds = %2
%8 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0), i64 %0, i64 %1)
%9 = tail call i32 @exit(i32 0)
unreachable
10: ; preds = %2
%11 = icmp eq i64 %1, 0
br i1 %11, label %12, label %15
12: ; preds = %10
%13 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @str, i64 0, i64 0))
%14 = tail call i32 @exit(i32 0)
unreachable
15: ; preds = %10
%16 = icmp eq i64 %0, 0
br i1 %16, label %37, label %17
17: ; preds = %15
%18 = icmp eq i64 %0, %1
br i1 %18, label %37, label %19
19: ; preds = %17
%20 = tail call i32 @L_deposit_l(i64 %0)
%21 = tail call i32 @L_deposit_l(i64 %1)
br label %22
22: ; preds = %19, %32
%23 = phi i64 [ 0, %19 ], [ %34, %32 ]
%24 = phi i32 [ %20, %19 ], [ %33, %32 ]
%25 = phi i64 [ 0, %19 ], [ %35, %32 ]
%26 = shl i64 %23, 1
%27 = shl i32 %24, 1
%28 = icmp slt i32 %27, %21
br i1 %28, label %32, label %29
29: ; preds = %22
%30 = tail call i32 @L_sub(i32 %27, i32 %21)
%31 = tail call i64 @add(i64 %26, i32 1)
br label %32
32: ; preds = %22, %29
%33 = phi i32 [ %30, %29 ], [ %27, %22 ]
%34 = phi i64 [ %31, %29 ], [ %26, %22 ]
%35 = add nuw nsw i64 %25, 1
%36 = icmp eq i64 %35, 15
br i1 %36, label %37, label %22
37: ; preds = %32, %17, %15
%38 = phi i64 [ 0, %15 ], [ 32767, %17 ], [ %34, %32 ]
ret i64 %38
}