#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*/ EXIT_FAILURE;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, long, long);
int /*<<< orphan*/ printf(char *, char);
int /*<<< orphan*/ stderr;
void causeSigbus(char *mem, long filesize, long pagesize) {
  int pages, sigbusBytes;
  pages = filesize / pagesize;
  sigbusBytes = filesize % pagesize;
  if (sigbusBytes == 0) {
    fprintf(stderr, "File size (%ldb) is a multiple of the page size (%ldb)\n",
            filesize, pagesize);
    exit(EXIT_FAILURE);
  }
  char x = mem[(pages + 1) * pagesize + 1];
  printf("x: %c\n", x);
}


x86 assembly (clang, -O0):
<causeSigbus>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x10(%rbp),%rax
cqto
idivq  -0x18(%rbp)
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rax
cqto
idivq  -0x18(%rbp)
mov    %edx,%eax
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jne    1195 <causeSigbus+0x65>
mov    0x2e6f(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
mov    -0x18(%rbp),%rcx
lea    0xe7e(%rip),%rsi
call   1040 <fprintf@plt>
mov    0x2e4a(%rip),%rax
mov    (%rax),%edi
call   1050 <exit@plt>
mov    -0x8(%rbp),%rax
mov    -0x1c(%rbp),%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
imul   -0x18(%rbp),%rcx
mov    0x1(%rax,%rcx,1),%al
mov    %al,-0x21(%rbp)
lea    0xe83(%rip),%rdi
movsbl -0x21(%rbp),%esi
call   1030 <printf@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<causeSigbus>:
push   %rax
mov    %rdx,%rcx
mov    %rsi,%r8
mov    %rsi,%rax
or     %rdx,%rax
shr    $0x20,%rax
je     116a <causeSigbus+0x3a>
mov    %r8,%rax
cqto
idiv   %rcx
test   %edx,%edx
je     1175 <causeSigbus+0x45>
add    $0x1,%eax
cltq
imul   %rcx,%rax
movsbl 0x1(%rax,%rdi,1),%esi
lea    0xed4(%rip),%rdi
pop    %rax
jmp    1030 <printf@plt>
mov    %r8d,%eax
xor    %edx,%edx
div    %ecx
test   %edx,%edx
jne    114f <causeSigbus+0x1f>
mov    0x2e64(%rip),%rax
mov    (%rax),%edi
lea    0xe7b(%rip),%rsi
mov    %r8,%rdx
call   1040 <fprintf@plt>
mov    0x2e44(%rip),%rax
mov    (%rax),%edi
call   1050 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [56 x i8] c"File size (%ldb) is a multiple of the page size (%ldb)\0A\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [7 x i8] c"x: %c\0A\00"
define void @causeSigbus(i8* %0, i64 %1, i64 %2) {
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i64
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8
store i8* %0, i8** %4
store i64 %1, i64* %5
store i64 %2, i64* %6
%10 = load i64, i64* %5
%11 = load i64, i64* %6
%12 = sdiv i64 %10, %11
%13 = trunc i64 %12 to i32
store i32 %13, i32* %7
%14 = load i64, i64* %5
%15 = load i64, i64* %6
%16 = srem i64 %14, %15
%17 = trunc i64 %16 to i32
store i32 %17, i32* %8
%18 = load i32, i32* %8
%19 = icmp eq i32 %18, 0
br i1 %19, label %20, label %27
20: ; preds = %3
%21 = load i32, i32* @stderr
%22 = load i64, i64* %5
%23 = load i64, i64* %6
%24 = call i32 @fprintf(i32 %21, i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str, i64 0, i64 0), i64 %22, i64 %23)
%25 = load i32, i32* @EXIT_FAILURE
%26 = call i32 @exit(i32 %25)
unreachable
27: ; preds = %3
%28 = load i8*, i8** %4
%29 = load i32, i32* %7
%30 = add nsw i32 %29, 1
%31 = sext i32 %30 to i64
%32 = load i64, i64* %6
%33 = mul nsw i64 %31, %32
%34 = add nsw i64 %33, 1
%35 = getelementptr inbounds i8, i8* %28, i64 %34
%36 = load i8, i8* %35
store i8 %36, i8* %9
%37 = load i8, i8* %9
%38 = call i32 @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i8 signext %37)
ret void
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [56 x i8] c"File size (%ldb) is a multiple of the page size (%ldb)\0A\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [7 x i8] c"x: %c\0A\00"
define void @causeSigbus(i8* nocapture %0, i64 %1, i64 %2) {
%4 = srem i64 %1, %2
%5 = sdiv i64 %1, %2
%6 = trunc i64 %4 to i32
%7 = icmp eq i32 %6, 0
br i1 %7, label %8, label %13
8: ; preds = %3
%9 = load i32, i32* @stderr
%10 = tail call i32 @fprintf(i32 %9, i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str, i64 0, i64 0), i64 %1, i64 %2)
%11 = load i32, i32* @EXIT_FAILURE
%12 = tail call i32 @exit(i32 %11)
unreachable
13: ; preds = %3
%14 = shl i64 %5, 32
%15 = add i64 %14, 4294967296
%16 = ashr exact i64 %15, 32
%17 = mul nsw i64 %16, %2
%18 = add nsw i64 %17, 1
%19 = getelementptr inbounds i8, i8* %0, i64 %18
%20 = load i8, i8* %19
%21 = tail call i32 @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i8 signext %20)
ret void
}