#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;
  int cdmsf_min1;
  int cdmsf_sec1;
  int cdmsf_frame1;
};
typedef int /*<<< orphan*/ msf;

/* Variables and functions */
int /*<<< orphan*/ CDROMREADRAW;
scalar_t__ ioctl(int, int /*<<< orphan*/, unsigned char *, unsigned char *);
int /*<<< orphan*/ memcpy(unsigned char *, struct cdrom_msf *, int);
int /*<<< orphan*/ perror(char *);
int read_cdrom_frames(int fd, int frame, int num_frames, unsigned char *data) {
  struct cdrom_msf msf;
  while (num_frames) {
    msf.cdmsf_min0 = frame / 60 / 75;
    msf.cdmsf_sec0 = (frame / 75) % 60;
    msf.cdmsf_frame0 = frame % 75;
    msf.cdmsf_min1 = (frame + 1) / 60 / 75;
    msf.cdmsf_sec1 = ((frame + 1) / 75) % 60;
    msf.cdmsf_frame1 = (frame + 1) % 75;
    memcpy(data, &msf, sizeof(msf));
    if (ioctl(fd, CDROMREADRAW, data, data) < 0) {
      perror("CDROMREADRAW");
      return -1;
    }
    data += 2352;
    frame++;
    num_frames--;
  }
  return 0;
}


x86 assembly (clang, -O0):
<read_cdrom_frames>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    %rcx,-0x18(%rbp)
cmpl   $0x0,-0x10(%rbp)
je     123e <read_cdrom_frames+0x10e>
mov    -0xc(%rbp),%eax
mov    $0x3c,%ecx
cltd
idiv   %ecx
mov    $0x4b,%ecx
cltd
idiv   %ecx
mov    %eax,-0x30(%rbp)
mov    -0xc(%rbp),%eax
mov    $0x4b,%ecx
cltd
idiv   %ecx
mov    $0x3c,%ecx
cltd
idiv   %ecx
mov    %edx,-0x2c(%rbp)
mov    -0xc(%rbp),%eax
mov    $0x4b,%ecx
cltd
idiv   %ecx
mov    %edx,-0x28(%rbp)
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    $0x3c,%ecx
cltd
idiv   %ecx
mov    $0x4b,%ecx
cltd
idiv   %ecx
mov    %eax,-0x24(%rbp)
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    $0x4b,%ecx
cltd
idiv   %ecx
mov    $0x3c,%ecx
cltd
idiv   %ecx
mov    %edx,-0x20(%rbp)
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    $0x4b,%ecx
cltd
idiv   %ecx
mov    %edx,-0x1c(%rbp)
mov    -0x18(%rbp),%rdi
lea    -0x30(%rbp),%rsi
mov    $0x18,%edx
call   1040 <memcpy@plt>
mov    -0x8(%rbp),%edi
mov    0x2df8(%rip),%rax
mov    (%rax),%esi
mov    -0x18(%rbp),%rdx
mov    -0x18(%rbp),%rcx
call   1030 <ioctl@plt>
cmp    $0x0,%rax
jge    1219 <read_cdrom_frames+0xe9>
lea    0xdf8(%rip),%rdi
call   1050 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1245 <read_cdrom_frames+0x115>
mov    -0x18(%rbp),%rax
add    $0x930,%rax
mov    %rax,-0x18(%rbp)
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
mov    -0x10(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x10(%rbp)
jmp    1145 <read_cdrom_frames+0x15>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_cdrom_frames>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
xor    %eax,%eax
test   %edx,%edx
je     125f <read_cdrom_frames+0x12f>
mov    %rcx,%rbx
mov    %edx,%r13d
mov    %esi,%ebp
mov    %edi,%r15d
mov    %rsp,%r12
mov    0x2e83(%rip),%r14
nopl   (%rax)
movslq %ebp,%rax
imul   $0x7482296b,%rax,%rcx
mov    %rcx,%rdx
shr    $0x3f,%rdx
shr    $0x20,%rcx
sar    $0xb,%ecx
add    %edx,%ecx
mov    %ecx,(%rsp)
imul   $0x1b4e81b5,%rax,%rcx
mov    %rcx,%rdx
shr    $0x3f,%rdx
shr    $0x20,%rcx
sar    $0x3,%ecx
add    %edx,%ecx
movslq %ecx,%rdx
imul   $0xffffffff88888889,%rdx,%rcx
shr    $0x20,%rcx
add    %edx,%ecx
mov    %ecx,%esi
shr    $0x1f,%esi
sar    $0x5,%ecx
add    %esi,%ecx
imul   $0x3c,%ecx,%esi
imul   $0x4b,%edx,%ecx
sub    %esi,%edx
mov    %edx,0x4(%rsp)
lea    0x1(%rax),%ebp
movslq %ebp,%rdx
imul   $0x1b4e81b5,%rdx,%rsi
mov    %rsi,%rdi
shr    $0x3f,%rdi
sar    $0x23,%rsi
add    %edi,%esi
movslq %esi,%rsi
imul   $0x4b,%esi,%edi
neg    %edi
add    %eax,%edi
add    $0x1,%edi
sub    %ecx,%eax
mov    %eax,0x8(%rsp)
imul   $0x7482296b,%rdx,%rax
mov    %rax,%rcx
shr    $0x3f,%rcx
shr    $0x20,%rax
sar    $0xb,%eax
add    %ecx,%eax
mov    %eax,0xc(%rsp)
imul   $0xffffffff88888889,%rsi,%rax
shr    $0x20,%rax
add    %esi,%eax
mov    %eax,%ecx
shr    $0x1f,%ecx
sar    $0x5,%eax
add    %ecx,%eax
imul   $0x3c,%eax,%eax
sub    %eax,%esi
mov    %esi,0x10(%rsp)
mov    %edi,0x14(%rsp)
mov    %rbx,%rdi
mov    %r12,%rsi
mov    $0x18,%edx
call   1040 <memcpy@plt>
mov    (%r14),%esi
mov    %r15d,%edi
mov    %rbx,%rdx
mov    %rbx,%rcx
call   1030 <ioctl@plt>
test   %rax,%rax
js     126e <read_cdrom_frames+0x13e>
add    $0x930,%rbx
add    $0xffffffff,%r13d
jne    1160 <read_cdrom_frames+0x30>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd8b(%rip),%rdi
call   1050 <perror@plt>
mov    $0xffffffff,%eax
jmp    125f <read_cdrom_frames+0x12f>

LLVM IR (-O0):
%struct.cdrom_msf = type { i32, i32, i32, i32, i32, i32 }
@CDROMREADRAW = global i32 0
@.str = [13 x i8] c"CDROMREADRAW\00"
define i32 @read_cdrom_frames(i32 %0, i32 %1, i32 %2, i8* %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
%10 = alloca %struct.cdrom_msf
store i32 %0, i32* %6
store i32 %1, i32* %7
store i32 %2, i32* %8
store i8* %3, i8** %9
br label %11
11: ; preds = %50, %4
%12 = load i32, i32* %8
%13 = icmp ne i32 %12, 0
br i1 %13, label %14, label %57
14: ; preds = %11
%15 = load i32, i32* %7
%16 = sdiv i32 %15, 60
%17 = sdiv i32 %16, 75
%18 = getelementptr inbounds %struct.cdrom_msf, %struct.cdrom_msf* %10, i32 0, i32 0
store i32 %17, i32* %18
%19 = load i32, i32* %7
%20 = sdiv i32 %19, 75
%21 = srem i32 %20, 60
%22 = getelementptr inbounds %struct.cdrom_msf, %struct.cdrom_msf* %10, i32 0, i32 1
store i32 %21, i32* %22
%23 = load i32, i32* %7
%24 = srem i32 %23, 75
%25 = getelementptr inbounds %struct.cdrom_msf, %struct.cdrom_msf* %10, i32 0, i32 2
store i32 %24, i32* %25
%26 = load i32, i32* %7
%27 = add nsw i32 %26, 1
%28 = sdiv i32 %27, 60
%29 = sdiv i32 %28, 75
%30 = getelementptr inbounds %struct.cdrom_msf, %struct.cdrom_msf* %10, i32 0, i32 3
store i32 %29, i32* %30
%31 = load i32, i32* %7
%32 = add nsw i32 %31, 1
%33 = sdiv i32 %32, 75
%34 = srem i32 %33, 60
%35 = getelementptr inbounds %struct.cdrom_msf, %struct.cdrom_msf* %10, i32 0, i32 4
store i32 %34, i32* %35
%36 = load i32, i32* %7
%37 = add nsw i32 %36, 1
%38 = srem i32 %37, 75
%39 = getelementptr inbounds %struct.cdrom_msf, %struct.cdrom_msf* %10, i32 0, i32 5
store i32 %38, i32* %39
%40 = load i8*, i8** %9
%41 = call i32 @memcpy(i8* %40, %struct.cdrom_msf* %10, i32 24)
%42 = load i32, i32* %6
%43 = load i32, i32* @CDROMREADRAW
%44 = load i8*, i8** %9
%45 = load i8*, i8** %9
%46 = call i64 @ioctl(i32 %42, i32 %43, i8* %44, i8* %45)
%47 = icmp slt i64 %46, 0
br i1 %47, label %48, label %50
48: ; preds = %14
%49 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %5
br label %58
50: ; preds = %14
%51 = load i8*, i8** %9
%52 = getelementptr inbounds i8, i8* %51, i64 2352
store i8* %52, i8** %9
%53 = load i32, i32* %7
%54 = add nsw i32 %53, 1
store i32 %54, i32* %7
%55 = load i32, i32* %8
%56 = add nsw i32 %55, -1
store i32 %56, i32* %8
br label %11
57: ; preds = %11
store i32 0, i32* %5
br label %58
58: ; preds = %57, %48
%59 = load i32, i32* %5
ret i32 %59
}

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