#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 */
typedef struct TYPE_3__ TYPE_1__;

/* Type definitions */
struct TYPE_3__ {
  scalar_t__ rem;
};
typedef TYPE_1__ div_t;

/* Variables and functions */
TYPE_1__ div(int, int);
int INT_REM(int a, int b) {
  div_t d = div(a, b);
  if (a < 0 && d.rem != 0) {
    d.rem += b < 0 ? -b : b;
  }
  return d.rem;
}


x86 assembly (clang, -O0):
<INT_REM>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x8(%rbp),%esi
call   1030 <div@plt>
mov    %rax,-0x10(%rbp)
cmpl   $0x0,-0x4(%rbp)
jge    116c <INT_REM+0x5c>
cmpq   $0x0,-0x10(%rbp)
je     116c <INT_REM+0x5c>
cmpl   $0x0,-0x8(%rbp)
jge    1159 <INT_REM+0x49>
xor    %eax,%eax
sub    -0x8(%rbp),%eax
mov    %eax,-0x14(%rbp)
jmp    115f <INT_REM+0x4f>
mov    -0x8(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cltq
add    -0x10(%rbp),%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<INT_REM>:
push   %rbp
push   %rbx
push   %rax
mov    %esi,%ebx
mov    %edi,%ebp
call   1030 <div@plt>
mov    %ebx,%ecx
neg    %ecx
cmovs  %ebx,%ecx
xor    %edx,%edx
test   %rax,%rax
cmovne %ecx,%edx
sar    $0x1f,%ebp
and    %edx,%ebp
add    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.TYPE_3__ = type { i64 }
define i32 @INT_REM(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.TYPE_3__
store i32 %0, i32* %3
store i32 %1, i32* %4
%6 = load i32, i32* %3
%7 = load i32, i32* %4
%8 = call i64 @div(i32 %6, i32 %7)
%9 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %5, i32 0, i32 0
store i64 %8, i64* %9
%10 = load i32, i32* %3
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %30
12: ; preds = %2
%13 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %5, i32 0, i32 0
%14 = load i64, i64* %13
%15 = icmp ne i64 %14, 0
br i1 %15, label %16, label %30
16: ; preds = %12
%17 = load i32, i32* %4
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %16
%20 = load i32, i32* %4
%21 = sub nsw i32 0, %20
br label %24
22: ; preds = %16
%23 = load i32, i32* %4
br label %24
24: ; preds = %22, %19
%25 = phi i32 [ %21, %19 ], [ %23, %22 ]
%26 = sext i32 %25 to i64
%27 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %5, i32 0, i32 0
%28 = load i64, i64* %27
%29 = add nsw i64 %28, %26
store i64 %29, i64* %27
br label %30
30: ; preds = %24, %12, %2
%31 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %5, i32 0, i32 0
%32 = load i64, i64* %31
%33 = trunc i64 %32 to i32
ret i32 %33
}

LLVM IR (-O2):
define i32 @INT_REM(i32 %0, i32 %1) {
%3 = tail call i64 @div(i32 %0, i32 %1)
%4 = icmp slt i32 %0, 0
%5 = icmp ne i64 %3, 0
%6 = select i1 %4, i1 %5, i1 false
%7 = tail call i32 @llvm.abs.i32(i32 %1, i1 true)
%8 = zext i32 %7 to i64
%9 = select i1 %6, i64 %8, i64 0
%10 = add nsw i64 %9, %3
%11 = trunc i64 %10 to i32
ret i32 %11
}