#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 */
typedef int uint8_t;
typedef scalar_t__ uint64_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ SEEK_CUR;
int /*<<< orphan*/ exit(int);
int fread(int *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, scalar_t__, int /*<<< orphan*/);
scalar_t__ ftell(int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ read_ID(int /*<<< orphan*/ *, int *);
scalar_t__ read_ckDataSize(int /*<<< orphan*/ *);
scalar_t__ strcmp(int *, char *);
int get_channel_count(FILE *infile, uint64_t datasize) {
  uint64_t n = ftell(infile);
  uint64_t end = n + datasize;
  uint8_t ckID[5], tmp[2];
  uint64_t chunksize;
  int channelcount;
  while (n < end) {
    read_ID(infile, ckID);
    chunksize = read_ckDataSize(infile);
    if (strcmp(ckID, "CHNL") == 0) {
      if (fread(tmp, 1, 2, infile) < 2) {
        printf("file ended unexpectedly\n");
        exit(1);
      }
      channelcount = tmp[0] << 8 | tmp[1];
      printf("channels = %d\n", channelcount);
      fseek(infile, chunksize - 2, SEEK_CUR);
    } else if (strcmp(ckID, "CMPR") == 0) {
      read_ID(infile, ckID);
      printf("compression type = %s\n", ckID);
      if (strcmp(ckID, "DSD ")) {
        printf("compressed DSDIFF is not supported\n");
        exit(1);
      }
      fseek(infile, chunksize - 4, SEEK_CUR);
    } else {
      fseek(infile, chunksize, SEEK_CUR);
    }
    n = ftell(infile);
  }
  return channelcount;
}


x86 assembly (clang, -O0):
<get_channel_count>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
call   1060 <ftell@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
add    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rax
cmp    -0x20(%rbp),%rax
jge    1316 <get_channel_count+0x196>
mov    -0x8(%rbp),%rdi
lea    -0x40(%rbp),%rsi
call   1080 <read_ID@plt>
mov    -0x8(%rbp),%rdi
call   1090 <read_ckDataSize@plt>
mov    %rax,-0x50(%rbp)
lea    -0x40(%rbp),%rdi
lea    0xe24(%rip),%rsi
call   1050 <strcmp@plt>
cmp    $0x0,%rax
jne    125f <get_channel_count+0xdf>
lea    -0x48(%rbp),%rdi
mov    -0x8(%rbp),%rcx
mov    $0x1,%esi
mov    $0x2,%edx
call   1030 <fread@plt>
cmp    $0x2,%eax
jge    1223 <get_channel_count+0xa3>
lea    0xdf3(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x48(%rbp),%eax
shl    $0x8,%eax
or     -0x44(%rbp),%eax
mov    %eax,-0x54(%rbp)
mov    -0x54(%rbp),%esi
lea    0xde5(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x8(%rbp),%rdi
mov    -0x50(%rbp),%rsi
sub    $0x2,%rsi
mov    0x2d95(%rip),%rax
mov    (%rax),%edx
call   1070 <fseek@plt>
jmp    1304 <get_channel_count+0x184>
lea    -0x40(%rbp),%rdi
lea    0xdc3(%rip),%rsi
call   1050 <strcmp@plt>
cmp    $0x0,%rax
jne    12e9 <get_channel_count+0x169>
mov    -0x8(%rbp),%rdi
lea    -0x40(%rbp),%rsi
call   1080 <read_ID@plt>
lea    -0x40(%rbp),%rsi
lea    0xda1(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0x40(%rbp),%rdi
lea    0xda6(%rip),%rsi
call   1050 <strcmp@plt>
cmp    $0x0,%rax
je     12ca <get_channel_count+0x14a>
lea    0xd95(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x8(%rbp),%rdi
mov    -0x50(%rbp),%rsi
sub    $0x4,%rsi
mov    0x2d0b(%rip),%rax
mov    (%rax),%edx
call   1070 <fseek@plt>
jmp    12ff <get_channel_count+0x17f>
mov    -0x8(%rbp),%rdi
mov    -0x50(%rbp),%rsi
mov    0x2cf0(%rip),%rax
mov    (%rax),%edx
call   1070 <fseek@plt>
jmp    1304 <get_channel_count+0x184>
mov    -0x8(%rbp),%rdi
call   1060 <ftell@plt>
mov    %rax,-0x18(%rbp)
jmp    11a9 <get_channel_count+0x29>
mov    -0x54(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_channel_count>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rsi,%rbp
mov    %rdi,%rbx
call   1070 <ftell@plt>
test   %rbp,%rbp
jle    12b0 <get_channel_count+0x120>
mov    %rax,%r14
add    %rbp,%r14
lea    0x10(%rsp),%r15
mov    0x2e24(%rip),%r12
jmp    11f0 <get_channel_count+0x60>
cs nopw 0x0(%rax,%rax,1)
mov    (%r12),%edx
mov    %rbx,%rdi
mov    %r13,%rsi
call   1080 <fseek@plt>
mov    %rbx,%rdi
call   1070 <ftell@plt>
cmp    %r14,%rax
jge    12b0 <get_channel_count+0x120>
mov    %rbx,%rdi
mov    %r15,%rsi
call   1090 <read_ID@plt>
mov    %rbx,%rdi
call   10a0 <read_ckDataSize@plt>
mov    %rax,%r13
mov    %r15,%rdi
lea    0xdf0(%rip),%rsi
call   1060 <strcmp@plt>
test   %rax,%rax
je     1270 <get_channel_count+0xe0>
mov    %r15,%rdi
lea    0xdf0(%rip),%rsi
call   1060 <strcmp@plt>
test   %rax,%rax
jne    11d0 <get_channel_count+0x40>
mov    %rbx,%rdi
mov    %r15,%rsi
call   1090 <read_ID@plt>
lea    0xdd9(%rip),%rdi
mov    %r15,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %r15,%rdi
lea    0xddc(%rip),%rsi
call   1060 <strcmp@plt>
test   %rax,%rax
jne    12ca <get_channel_count+0x13a>
add    $0xfffffffffffffffc,%r13
jmp    11d0 <get_channel_count+0x40>
nopw   0x0(%rax,%rax,1)
lea    0x8(%rsp),%rdi
mov    $0x1,%esi
mov    $0x2,%edx
mov    %rbx,%rcx
call   1040 <fread@plt>
cmp    $0x1,%eax
jle    12c1 <get_channel_count+0x131>
mov    0x8(%rsp),%ebp
shl    $0x8,%ebp
or     0xc(%rsp),%ebp
lea    0xd67(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1050 <printf@plt>
add    $0xfffffffffffffffe,%r13
jmp    11d0 <get_channel_count+0x40>
mov    %ebp,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd90(%rip),%rdi
jmp    12d1 <get_channel_count+0x141>
lea    0xd64(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@.str = [5 x i8] c"CHNL\00"
@.str.1 = [25 x i8] c"file ended unexpectedly\0A\00"
@.str.2 = [15 x i8] c"channels = %d\0A\00"
@SEEK_CUR = global i32 0
@.str.3 = [5 x i8] c"CMPR\00"
@.str.4 = [23 x i8] c"compression type = %s\0A\00"
@.str.5 = [5 x i8] c"DSD \00"
@.str.6 = [36 x i8] c"compressed DSDIFF is not supported\0A\00"
define i32 @get_channel_count(i32* %0, i64 %1) {
%3 = alloca i32*
%4 = alloca i64
%5 = alloca i64
%6 = alloca i64
%7 = alloca [5 x i32]
%8 = alloca [2 x i32]
%9 = alloca i64
%10 = alloca i32
store i32* %0, i32** %3
store i64 %1, i64* %4
%11 = load i32*, i32** %3
%12 = call i64 @ftell(i32* %11)
store i64 %12, i64* %5
%13 = load i64, i64* %5
%14 = load i64, i64* %4
%15 = add nsw i64 %13, %14
store i64 %15, i64* %6
br label %16
16: ; preds = %79, %2
%17 = load i64, i64* %5
%18 = load i64, i64* %6
%19 = icmp slt i64 %17, %18
br i1 %19, label %20, label %82
20: ; preds = %16
%21 = load i32*, i32** %3
%22 = getelementptr inbounds [5 x i32], [5 x i32]* %7, i64 0, i64 0
%23 = call i32 @read_ID(i32* %21, i32* %22)
%24 = load i32*, i32** %3
%25 = call i64 @read_ckDataSize(i32* %24)
store i64 %25, i64* %9
%26 = getelementptr inbounds [5 x i32], [5 x i32]* %7, i64 0, i64 0
%27 = call i64 @strcmp(i32* %26, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%28 = icmp eq i64 %27, 0
br i1 %28, label %29, label %51
29: ; preds = %20
%30 = getelementptr inbounds [2 x i32], [2 x i32]* %8, i64 0, i64 0
%31 = load i32*, i32** %3
%32 = call i32 @fread(i32* %30, i32 1, i32 2, i32* %31)
%33 = icmp slt i32 %32, 2
br i1 %33, label %34, label %37
34: ; preds = %29
%35 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
%36 = call i32 @exit(i32 1)
unreachable
37: ; preds = %29
%38 = getelementptr inbounds [2 x i32], [2 x i32]* %8, i64 0, i64 0
%39 = load i32, i32* %38
%40 = shl i32 %39, 8
%41 = getelementptr inbounds [2 x i32], [2 x i32]* %8, i64 0, i64 1
%42 = load i32, i32* %41
%43 = or i32 %40, %42
store i32 %43, i32* %10
%44 = load i32, i32* %10
%45 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i32 %44)
%46 = load i32*, i32** %3
%47 = load i64, i64* %9
%48 = sub nsw i64 %47, 2
%49 = load i32, i32* @SEEK_CUR
%50 = call i32 @fseek(i32* %46, i64 %48, i32 %49)
br label %79
51: ; preds = %20
%52 = getelementptr inbounds [5 x i32], [5 x i32]* %7, i64 0, i64 0
%53 = call i64 @strcmp(i32* %52, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%54 = icmp eq i64 %53, 0
br i1 %54, label %55, label %73
55: ; preds = %51
%56 = load i32*, i32** %3
%57 = getelementptr inbounds [5 x i32], [5 x i32]* %7, i64 0, i64 0
%58 = call i32 @read_ID(i32* %56, i32* %57)
%59 = getelementptr inbounds [5 x i32], [5 x i32]* %7, i64 0, i64 0
%60 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0), i32* %59)
%61 = getelementptr inbounds [5 x i32], [5 x i32]* %7, i64 0, i64 0
%62 = call i64 @strcmp(i32* %61, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0))
%63 = icmp ne i64 %62, 0
br i1 %63, label %64, label %67
64: ; preds = %55
%65 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.6, i64 0, i64 0))
%66 = call i32 @exit(i32 1)
unreachable
67: ; preds = %55
%68 = load i32*, i32** %3
%69 = load i64, i64* %9
%70 = sub nsw i64 %69, 4
%71 = load i32, i32* @SEEK_CUR
%72 = call i32 @fseek(i32* %68, i64 %70, i32 %71)
br label %78
73: ; preds = %51
%74 = load i32*, i32** %3
%75 = load i64, i64* %9
%76 = load i32, i32* @SEEK_CUR
%77 = call i32 @fseek(i32* %74, i64 %75, i32 %76)
br label %78
78: ; preds = %73, %67
br label %79
79: ; preds = %78, %37
%80 = load i32*, i32** %3
%81 = call i64 @ftell(i32* %80)
store i64 %81, i64* %5
br label %16
82: ; preds = %16
%83 = load i32, i32* %10
ret i32 %83
}

LLVM IR (-O2):
@.str = [5 x i8] c"CHNL\00"
@.str.2 = [15 x i8] c"channels = %d\0A\00"
@SEEK_CUR = global i32 0
@.str.3 = [5 x i8] c"CMPR\00"
@.str.4 = [23 x i8] c"compression type = %s\0A\00"
@.str.5 = [5 x i8] c"DSD \00"
@str = [35 x i8] c"compressed DSDIFF is not supported\00"
@str.7 = [24 x i8] c"file ended unexpectedly\00"
define i32 @get_channel_count(i32* %0, i64 %1) {
%3 = alloca [5 x i32]
%4 = alloca [2 x i32]
%5 = tail call i64 @ftell(i32* %0)
%6 = add nsw i64 %5, %1
%7 = bitcast [5 x i32]* %3 to i8*
%8 = bitcast [2 x i32]* %4 to i8*
%9 = icmp sgt i64 %1, 0
br i1 %9, label %10, label %53
10: ; preds = %2
%11 = getelementptr inbounds [5 x i32], [5 x i32]* %3, i64 0, i64 0
%12 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 0
%13 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 1
br label %14
14: ; preds = %10, %46
%15 = phi i32 [ undef, %10 ], [ %48, %46 ]
%16 = call i32 @read_ID(i32* %0, i32* %11)
%17 = call i64 @read_ckDataSize(i32* %0)
%18 = call i64 @strcmp(i32* %11, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%19 = icmp eq i64 %18, 0
br i1 %19, label %20, label %33
20: ; preds = %14
%21 = call i32 @fread(i32* %12, i32 1, i32 2, i32* %0)
%22 = icmp slt i32 %21, 2
br i1 %22, label %23, label %26
23: ; preds = %20
%24 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @str.7, i64 0, i64 0))
%25 = call i32 @exit(i32 1)
unreachable
26: ; preds = %20
%27 = load i32, i32* %12
%28 = shl i32 %27, 8
%29 = load i32, i32* %13
%30 = or i32 %28, %29
%31 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i32 %30)
%32 = add nsw i64 %17, -2
br label %46
33: ; preds = %14
%34 = call i64 @strcmp(i32* %11, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%35 = icmp eq i64 %34, 0
br i1 %35, label %36, label %46
36: ; preds = %33
%37 = call i32 @read_ID(i32* %0, i32* %11)
%38 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0), i32* %11)
%39 = call i64 @strcmp(i32* %11, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0))
%40 = icmp eq i64 %39, 0
br i1 %40, label %44, label %41
41: ; preds = %36
%42 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @str, i64 0, i64 0))
%43 = call i32 @exit(i32 1)
unreachable
44: ; preds = %36
%45 = add nsw i64 %17, -4
br label %46
46: ; preds = %33, %44, %26
%47 = phi i64 [ %45, %44 ], [ %32, %26 ], [ %17, %33 ]
%48 = phi i32 [ %15, %44 ], [ %30, %26 ], [ %15, %33 ]
%49 = load i32, i32* @SEEK_CUR
%50 = call i32 @fseek(i32* %0, i64 %47, i32 %49)
%51 = call i64 @ftell(i32* %0)
%52 = icmp slt i64 %51, %6
br i1 %52, label %14, label %53
53: ; preds = %46, %2
%54 = phi i32 [ undef, %2 ], [ %48, %46 ]
ret i32 %54
}