#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, ...);
int offset(int sec_num, int page_size, int rom) {
  int sec_per_page, page_num, page_offset;
  if (rom)
    sec_per_page = 4;
  else
    sec_per_page = page_size / (512 + 3 + 13);
  page_num = sec_num / sec_per_page;
  page_offset = sec_num % sec_per_page;
  printf("Sec per page: %d\n", sec_per_page);
  printf("Page num: %d\n", page_num);
  printf("Offset in page (sec): %d\n", page_offset);
  printf("Offset in file (bytes): %d (0x%0x)\n",
         (page_num * page_size) + (page_offset * (512 + 3 + 13)),
         (page_num * page_size) + (page_offset * (512 + 3 + 13)));
  return ((page_num * page_size) + (page_offset * (512 + 3 + 13)));
}


x86 assembly (clang, -O0):
<offset>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %edx,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
je     1137 <offset+0x27>
movl   $0x4,-0x10(%rbp)
jmp    1145 <offset+0x35>
mov    -0x8(%rbp),%eax
mov    $0x210,%ecx
cltd
idiv   %ecx
mov    %eax,-0x10(%rbp)
mov    -0x4(%rbp),%eax
cltd
idivl  -0x10(%rbp)
mov    %eax,-0x14(%rbp)
mov    -0x4(%rbp),%eax
cltd
idivl  -0x10(%rbp)
mov    %edx,-0x18(%rbp)
mov    -0x10(%rbp),%esi
lea    0xe9d(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x14(%rbp),%esi
lea    0xe9e(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x18(%rbp),%esi
lea    0xe9b(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x14(%rbp),%esi
imul   -0x8(%rbp),%esi
imul   $0x210,-0x18(%rbp),%eax
add    %eax,%esi
mov    -0x14(%rbp),%edx
imul   -0x8(%rbp),%edx
imul   $0x210,-0x18(%rbp),%eax
add    %eax,%edx
lea    0xe87(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x14(%rbp),%eax
imul   -0x8(%rbp),%eax
imul   $0x210,-0x18(%rbp),%ecx
add    %ecx,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<offset>:
push   %rbp
push   %r14
push   %rbx
mov    %esi,%r14d
mov    %edi,%eax
mov    $0x4,%esi
test   %edx,%edx
jne    1139 <offset+0x29>
movslq %r14d,%rcx
imul   $0x3e0f83e1,%rcx,%rsi
mov    %rsi,%rcx
shr    $0x3f,%rcx
sar    $0x27,%rsi
add    %ecx,%esi
cltd
idiv   %esi
mov    %edx,%ebp
mov    %eax,%ebx
lea    0xeb9(%rip),%rdi
xor    %eax,%eax
call   1030 <printf@plt>
lea    0xebd(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1030 <printf@plt>
lea    0xebb(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1030 <printf@plt>
imul   %ebx,%r14d
imul   $0x210,%ebp,%ebx
add    %r14d,%ebx
lea    0xeb8(%rip),%rdi
mov    %ebx,%esi
mov    %ebx,%edx
xor    %eax,%eax
call   1030 <printf@plt>
mov    %ebx,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [18 x i8] c"Sec per page: %d\0A\00"
@.str.1 = [14 x i8] c"Page num: %d\0A\00"
@.str.2 = [26 x i8] c"Offset in page (sec): %d\0A\00"
@.str.3 = [36 x i8] c"Offset in file (bytes): %d (0x%0x)\0A\00"
define i32 @offset(i32 %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
store i32 %2, i32* %6
%10 = load i32, i32* %6
%11 = icmp ne i32 %10, 0
br i1 %11, label %12, label %13
12: ; preds = %3
store i32 4, i32* %7
br label %16
13: ; preds = %3
%14 = load i32, i32* %5
%15 = sdiv i32 %14, 528
store i32 %15, i32* %7
br label %16
16: ; preds = %13, %12
%17 = load i32, i32* %4
%18 = load i32, i32* %7
%19 = sdiv i32 %17, %18
store i32 %19, i32* %8
%20 = load i32, i32* %4
%21 = load i32, i32* %7
%22 = srem i32 %20, %21
store i32 %22, i32* %9
%23 = load i32, i32* %7
%24 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i32 %23)
%25 = load i32, i32* %8
%26 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i32 %25)
%27 = load i32, i32* %9
%28 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i32 %27)
%29 = load i32, i32* %8
%30 = load i32, i32* %5
%31 = mul nsw i32 %29, %30
%32 = load i32, i32* %9
%33 = mul nsw i32 %32, 528
%34 = add nsw i32 %31, %33
%35 = load i32, i32* %8
%36 = load i32, i32* %5
%37 = mul nsw i32 %35, %36
%38 = load i32, i32* %9
%39 = mul nsw i32 %38, 528
%40 = add nsw i32 %37, %39
%41 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.3, i64 0, i64 0), i32 %34, i32 %40)
%42 = load i32, i32* %8
%43 = load i32, i32* %5
%44 = mul nsw i32 %42, %43
%45 = load i32, i32* %9
%46 = mul nsw i32 %45, 528
%47 = add nsw i32 %44, %46
ret i32 %47
}

LLVM IR (-O2):
@.str = [18 x i8] c"Sec per page: %d\0A\00"
@.str.1 = [14 x i8] c"Page num: %d\0A\00"
@.str.2 = [26 x i8] c"Offset in page (sec): %d\0A\00"
@.str.3 = [36 x i8] c"Offset in file (bytes): %d (0x%0x)\0A\00"
define i32 @offset(i32 %0, i32 %1, i32 %2) {
%4 = icmp eq i32 %2, 0
%5 = sdiv i32 %1, 528
%6 = select i1 %4, i32 %5, i32 4
%7 = sdiv i32 %0, %6
%8 = srem i32 %0, %6
%9 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i32 %6)
%10 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i32 %7)
%11 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i32 %8)
%12 = mul nsw i32 %7, %1
%13 = mul nsw i32 %8, 528
%14 = add nsw i32 %13, %12
%15 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.3, i64 0, i64 0), i32 %14, i32 %14)
ret i32 %14
}