#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 */
struct cdrom_msf {
  int cdmsf_min0;
  int cdmsf_sec0;
  int cdmsf_frame0;
};

/* Variables and functions */
int /*<<< orphan*/ CDROMREADMODE2;
int CD_FRAMES;
int CD_MSF_OFFSET;
int CD_SECS;
int ioctl(int, int /*<<< orphan*/, unsigned char *);
int /*<<< orphan*/ perror(char *);
int read_raw_frame(int fd, int lba, unsigned char *buf) {
  struct cdrom_msf *msf;
  int rc;
  msf = (struct cdrom_msf *)buf;
  msf->cdmsf_min0 = (lba + CD_MSF_OFFSET) / CD_FRAMES / CD_SECS;
  msf->cdmsf_sec0 = (lba + CD_MSF_OFFSET) / CD_FRAMES % CD_SECS;
  msf->cdmsf_frame0 = (lba + CD_MSF_OFFSET) % CD_FRAMES;
  rc = ioctl(fd, CDROMREADMODE2, buf);
  if (-1 == rc)
    perror("ioctl CDROMREADMODE2");
  return rc;
}


x86 assembly (clang, -O0):
<read_raw_frame>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %rdx,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%eax
mov    0x2e94(%rip),%rcx
add    (%rcx),%eax
mov    0x2e83(%rip),%rcx
cltd
idivl  (%rcx)
mov    0x2e91(%rip),%rcx
cltd
idivl  (%rcx)
mov    %eax,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x8(%rbp),%eax
mov    0x2e6c(%rip),%rcx
add    (%rcx),%eax
mov    0x2e5b(%rip),%rcx
cltd
idivl  (%rcx)
mov    0x2e69(%rip),%rcx
cltd
idivl  (%rcx)
mov    -0x18(%rbp),%rax
mov    %edx,0x4(%rax)
mov    -0x8(%rbp),%eax
mov    0x2e45(%rip),%rcx
add    (%rcx),%eax
mov    0x2e34(%rip),%rcx
cltd
idivl  (%rcx)
mov    -0x18(%rbp),%rax
mov    %edx,0x8(%rax)
mov    -0x4(%rbp),%edi
mov    0x2e10(%rip),%rax
mov    (%rax),%esi
mov    -0x10(%rbp),%rdx
call   1030 <ioctl@plt>
mov    %eax,-0x1c(%rbp)
mov    $0xffffffff,%eax
cmp    -0x1c(%rbp),%eax
jne    11d8 <read_raw_frame+0xb8>
lea    0xe2d(%rip),%rdi
call   1040 <perror@plt>
mov    -0x1c(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_raw_frame>:
push   %rbx
mov    %rdx,%rcx
mov    0x2ead(%rip),%r8
mov    (%r8),%eax
add    %esi,%eax
mov    0x2e99(%rip),%rbx
cltd
idivl  (%rbx)
mov    0x2ea7(%rip),%r9
cltd
idivl  (%r9)
mov    %eax,(%rcx)
add    (%r8),%esi
mov    %esi,%eax
cltd
idivl  (%rbx)
mov    %edx,%esi
cltd
idivl  (%r9)
mov    %edx,0x4(%rcx)
mov    %esi,0x8(%rcx)
mov    0x2e5e(%rip),%rax
mov    (%rax),%esi
mov    %rcx,%rdx
call   1030 <ioctl@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
je     1177 <read_raw_frame+0x57>
mov    %ebx,%eax
pop    %rbx
ret
lea    0xe82(%rip),%rdi
call   1040 <perror@plt>
jmp    1173 <read_raw_frame+0x53>

LLVM IR (-O0):
%struct.cdrom_msf = type { i32, i32, i32 }
@CD_MSF_OFFSET = global i32 0
@CD_FRAMES = global i32 0
@CD_SECS = global i32 0
@CDROMREADMODE2 = global i32 0
@.str = [21 x i8] c"ioctl CDROMREADMODE2\00"
define i32 @read_raw_frame(i32 %0, i32 %1, i8* %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca %struct.cdrom_msf*
%8 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
store i8* %2, i8** %6
%9 = load i8*, i8** %6
%10 = bitcast i8* %9 to %struct.cdrom_msf*
store %struct.cdrom_msf* %10, %struct.cdrom_msf** %7
%11 = load i32, i32* %5
%12 = load i32, i32* @CD_MSF_OFFSET
%13 = add nsw i32 %11, %12
%14 = load i32, i32* @CD_FRAMES
%15 = sdiv i32 %13, %14
%16 = load i32, i32* @CD_SECS
%17 = sdiv i32 %15, %16
%18 = load %struct.cdrom_msf*, %struct.cdrom_msf** %7
%19 = getelementptr inbounds %struct.cdrom_msf, %struct.cdrom_msf* %18, i32 0, i32 0
store i32 %17, i32* %19
%20 = load i32, i32* %5
%21 = load i32, i32* @CD_MSF_OFFSET
%22 = add nsw i32 %20, %21
%23 = load i32, i32* @CD_FRAMES
%24 = sdiv i32 %22, %23
%25 = load i32, i32* @CD_SECS
%26 = srem i32 %24, %25
%27 = load %struct.cdrom_msf*, %struct.cdrom_msf** %7
%28 = getelementptr inbounds %struct.cdrom_msf, %struct.cdrom_msf* %27, i32 0, i32 1
store i32 %26, i32* %28
%29 = load i32, i32* %5
%30 = load i32, i32* @CD_MSF_OFFSET
%31 = add nsw i32 %29, %30
%32 = load i32, i32* @CD_FRAMES
%33 = srem i32 %31, %32
%34 = load %struct.cdrom_msf*, %struct.cdrom_msf** %7
%35 = getelementptr inbounds %struct.cdrom_msf, %struct.cdrom_msf* %34, i32 0, i32 2
store i32 %33, i32* %35
%36 = load i32, i32* %4
%37 = load i32, i32* @CDROMREADMODE2
%38 = load i8*, i8** %6
%39 = call i32 @ioctl(i32 %36, i32 %37, i8* %38)
store i32 %39, i32* %8
%40 = load i32, i32* %8
%41 = icmp eq i32 -1, %40
br i1 %41, label %42, label %44
42: ; preds = %3
%43 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
br label %44
44: ; preds = %42, %3
%45 = load i32, i32* %8
ret i32 %45
}

LLVM IR (-O2):
@CD_MSF_OFFSET = global i32 0
@CD_FRAMES = global i32 0
@CD_SECS = global i32 0
@CDROMREADMODE2 = global i32 0
@.str = [21 x i8] c"ioctl CDROMREADMODE2\00"
define i32 @read_raw_frame(i32 %0, i32 %1, i8* %2) {
%4 = load i32, i32* @CD_MSF_OFFSET
%5 = add nsw i32 %4, %1
%6 = load i32, i32* @CD_FRAMES
%7 = sdiv i32 %5, %6
%8 = load i32, i32* @CD_SECS
%9 = sdiv i32 %7, %8
%10 = bitcast i8* %2 to i32*
store i32 %9, i32* %10
%11 = load i32, i32* @CD_MSF_OFFSET
%12 = add nsw i32 %11, %1
%13 = load i32, i32* @CD_FRAMES
%14 = sdiv i32 %12, %13
%15 = load i32, i32* @CD_SECS
%16 = srem i32 %14, %15
%17 = getelementptr inbounds i8, i8* %2, i64 4
%18 = bitcast i8* %17 to i32*
store i32 %16, i32* %18
%19 = srem i32 %12, %13
%20 = getelementptr inbounds i8, i8* %2, i64 8
%21 = bitcast i8* %20 to i32*
store i32 %19, i32* %21
%22 = load i32, i32* @CDROMREADMODE2
%23 = tail call i32 @ioctl(i32 %0, i32 %22, i8* %2)
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %27
25: ; preds = %3
%26 = tail call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
br label %27
27: ; preds = %25, %3
ret i32 %23
}