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

/* Variables and functions */
int /*<<< orphan*/ SEEK_CUR;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ cdi_check_footer(int /*<<< orphan*/ *, int *, int *);
int /*<<< orphan*/ cdi_parse_track(int /*<<< orphan*/ *, char const *, int,
                                   unsigned int, unsigned int, int *);
int fread(int /*<<< orphan*/ *, int, int, int /*<<< orphan*/ *);
scalar_t__ fseek(int /*<<< orphan*/ *, int, int /*<<< orphan*/);
unsigned int get_le16(int /*<<< orphan*/ *);
int /*<<< orphan*/ msg_error(char *, ...);
int /*<<< orphan*/ msg_info(char *, unsigned int, ...);
int /*<<< orphan*/ msg_perror(char const *);
bool cdi_parse_and_add_tracks(FILE *f, const char *fn) {
  uint32_t version, hdr_off, offs = 0;
  unsigned nses, ses_no, trk_no = 1;
  uint8_t buf[2];
  if (!cdi_check_footer(f, &version, &hdr_off)) {
    msg_error("Failed to parse CDI footer\n");
    return false;
  }
  if (fseek(f, hdr_off, SEEK_SET) < 0 || fread(buf, 1, 2, f) != 2) {
    msg_perror(fn);
    return false;
  }
  nses = get_le16(buf);
  if (!nses) {
    msg_error("No sessions in image\n");
    return false;
  }
  msg_info("Image has %u session(s)\n", nses);
  for (ses_no = 1; ses_no <= nses; ses_no++) {
    unsigned ntrk;
    if (fread(buf, 1, 2, f) != 2) {
      msg_perror(fn);
      return false;
    }
    ntrk = get_le16(buf);
    if (!ntrk) {
      msg_error("No tracks in session %u\n", ses_no);
      return false;
    }
    msg_info("Session %u has %u track(s)\n", ses_no, ntrk);
    do {
      if (!cdi_parse_track(f, fn, version, trk_no, ses_no, &offs))
        return false;
      trk_no++;
    } while (--ntrk);
    if (fseek(f, (version == 0x80000004UL ? 12 : 13), SEEK_CUR) < 0) {
      msg_perror(fn);
      return false;
    }
  }
  return true;
}


x86 assembly (clang, -O0):
<cdi_parse_and_add_tracks>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movl   $0x0,-0x24(%rbp)
movl   $0x1,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x1c(%rbp),%rsi
lea    -0x20(%rbp),%rdx
call   1080 <cdi_check_footer@plt>
cmp    $0x0,%eax
jne    11d2 <cdi_parse_and_add_tracks+0x52>
lea    0xe41(%rip),%rdi
mov    $0x0,%al
call   1070 <msg_error@plt>
movl   $0x0,-0x4(%rbp)
jmp    13a5 <cdi_parse_and_add_tracks+0x225>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%esi
mov    0x2df8(%rip),%rax
mov    (%rax),%edx
call   1060 <fseek@plt>
cmp    $0x0,%rax
jl     1211 <cdi_parse_and_add_tracks+0x91>
lea    -0x38(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    $0x1,%esi
mov    $0x2,%edx
call   1030 <fread@plt>
cmp    $0x2,%eax
je     1226 <cdi_parse_and_add_tracks+0xa6>
mov    -0x18(%rbp),%rdi
call   10a0 <msg_perror@plt>
movl   $0x0,-0x4(%rbp)
jmp    13a5 <cdi_parse_and_add_tracks+0x225>
lea    -0x38(%rbp),%rdi
call   1040 <get_le16@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jne    1256 <cdi_parse_and_add_tracks+0xd6>
lea    0xdd9(%rip),%rdi
mov    $0x0,%al
call   1070 <msg_error@plt>
movl   $0x0,-0x4(%rbp)
jmp    13a5 <cdi_parse_and_add_tracks+0x225>
mov    -0x28(%rbp),%esi
lea    0xdd2(%rip),%rdi
mov    $0x0,%al
call   1090 <msg_info@plt>
movl   $0x1,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x28(%rbp),%eax
ja     139e <cdi_parse_and_add_tracks+0x21e>
lea    -0x38(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    $0x1,%esi
mov    $0x2,%edx
call   1030 <fread@plt>
cmp    $0x2,%eax
je     12af <cdi_parse_and_add_tracks+0x12f>
mov    -0x18(%rbp),%rdi
call   10a0 <msg_perror@plt>
movl   $0x0,-0x4(%rbp)
jmp    13a5 <cdi_parse_and_add_tracks+0x225>
lea    -0x38(%rbp),%rdi
call   1040 <get_le16@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0x0,-0x3c(%rbp)
jne    12e2 <cdi_parse_and_add_tracks+0x162>
mov    -0x2c(%rbp),%esi
lea    0xd7c(%rip),%rdi
mov    $0x0,%al
call   1070 <msg_error@plt>
movl   $0x0,-0x4(%rbp)
jmp    13a5 <cdi_parse_and_add_tracks+0x225>
mov    -0x2c(%rbp),%esi
mov    -0x3c(%rbp),%edx
lea    0xd75(%rip),%rdi
mov    $0x0,%al
call   1090 <msg_info@plt>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
mov    -0x30(%rbp),%ecx
mov    -0x2c(%rbp),%r8d
lea    -0x24(%rbp),%r9
call   1050 <cdi_parse_track@plt>
cmp    $0x0,%eax
jne    1326 <cdi_parse_and_add_tracks+0x1a6>
movl   $0x0,-0x4(%rbp)
jmp    13a5 <cdi_parse_and_add_tracks+0x225>
mov    -0x30(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x30(%rbp)
mov    -0x3c(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x3c(%rbp)
cmp    $0x0,%eax
jne    12f6 <cdi_parse_and_add_tracks+0x176>
mov    -0x10(%rbp),%rdi
movslq -0x1c(%rbp),%rcx
mov    $0x80000004,%edx
mov    $0xd,%esi
mov    $0xc,%eax
cmp    %rdx,%rcx
cmove  %eax,%esi
mov    0x2c83(%rip),%rax
mov    (%rax),%edx
call   1060 <fseek@plt>
cmp    $0x0,%rax
jge    138b <cdi_parse_and_add_tracks+0x20b>
mov    -0x18(%rbp),%rdi
call   10a0 <msg_perror@plt>
movl   $0x0,-0x4(%rbp)
jmp    13a5 <cdi_parse_and_add_tracks+0x225>
jmp    1390 <cdi_parse_and_add_tracks+0x210>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    126e <cdi_parse_and_add_tracks+0xee>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cdi_parse_and_add_tracks>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%r14
mov    %rdi,%r13
movl   $0x0,0xc(%rsp)
lea    0x14(%rsp),%rsi
lea    0x10(%rsp),%rdx
call   1080 <cdi_check_footer@plt>
test   %eax,%eax
je     12da <cdi_parse_and_add_tracks+0x15a>
mov    0x10(%rsp),%esi
mov    0x2e1a(%rip),%rax
mov    (%rax),%edx
mov    %r13,%rdi
call   1060 <fseek@plt>
test   %rax,%rax
js     12cd <cdi_parse_and_add_tracks+0x14d>
lea    0x4(%rsp),%rdi
mov    $0x1,%esi
mov    $0x2,%edx
mov    %r13,%rcx
call   1030 <fread@plt>
cmp    $0x2,%eax
jne    12cd <cdi_parse_and_add_tracks+0x14d>
lea    0x4(%rsp),%rdi
call   1040 <get_le16@plt>
test   %eax,%eax
je     12fd <cdi_parse_and_add_tracks+0x17d>
lea    0xe28(%rip),%rdi
mov    %eax,(%rsp)
mov    %eax,%esi
xor    %eax,%eax
call   1090 <msg_info@plt>
mov    $0x1,%ebp
lea    0x4(%rsp),%r12
mov    $0x1,%ebx
mov    %r12,%rdi
mov    $0x1,%esi
mov    $0x2,%edx
mov    %r13,%rcx
call   1030 <fread@plt>
cmp    $0x2,%eax
jne    12cd <cdi_parse_and_add_tracks+0x14d>
mov    %r12,%rdi
call   1040 <get_le16@plt>
test   %eax,%eax
je     1306 <cdi_parse_and_add_tracks+0x186>
mov    %eax,%r12d
lea    0xe07(%rip),%rdi
mov    %ebx,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1090 <msg_info@plt>
nopl   0x0(%rax,%rax,1)
mov    0x14(%rsp),%edx
mov    %r13,%rdi
mov    %r14,%rsi
mov    %ebp,%ecx
mov    %ebx,%r8d
lea    0xc(%rsp),%r9
call   1050 <cdi_parse_track@plt>
xor    %r15d,%r15d
test   %eax,%eax
je     12eb <cdi_parse_and_add_tracks+0x16b>
add    $0x1,%ebp
add    $0xffffffff,%r12d
jne    1270 <cdi_parse_and_add_tracks+0xf0>
mov    0x2d48(%rip),%rax
mov    (%rax),%edx
mov    %r13,%rdi
mov    $0xd,%esi
call   1060 <fseek@plt>
test   %rax,%rax
js     131b <cdi_parse_and_add_tracks+0x19b>
add    $0x1,%ebx
mov    $0x1,%r15d
cmp    (%rsp),%ebx
lea    0x4(%rsp),%r12
jbe    1225 <cdi_parse_and_add_tracks+0xa5>
jmp    12eb <cdi_parse_and_add_tracks+0x16b>
mov    %r14,%rdi
call   10a0 <msg_perror@plt>
xor    %r15d,%r15d
jmp    12eb <cdi_parse_and_add_tracks+0x16b>
lea    0xd1f(%rip),%rdi
xor    %r15d,%r15d
xor    %eax,%eax
call   1070 <msg_error@plt>
mov    %r15d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd18(%rip),%rdi
jmp    12e1 <cdi_parse_and_add_tracks+0x161>
lea    0xd3e(%rip),%rdi
xor    %r15d,%r15d
mov    %ebx,%esi
xor    %eax,%eax
call   1070 <msg_error@plt>
jmp    12eb <cdi_parse_and_add_tracks+0x16b>
mov    %r14,%rdi
call   10a0 <msg_perror@plt>
jmp    12eb <cdi_parse_and_add_tracks+0x16b>

LLVM IR (-O0):
@.str = [28 x i8] c"Failed to parse CDI footer\0A\00"
@SEEK_SET = global i32 0
@.str.1 = [22 x i8] c"No sessions in image\0A\00"
@.str.2 = [25 x i8] c"Image has %u session(s)\0A\00"
@.str.3 = [25 x i8] c"No tracks in session %u\0A\00"
@.str.4 = [28 x i8] c"Session %u has %u track(s)\0A\00"
@SEEK_CUR = global i32 0
define i32 @cdi_parse_and_add_tracks(i32* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca [2 x i32]
%13 = alloca i32
store i32* %0, i32** %4
store i8* %1, i8** %5
store i32 0, i32* %8
store i32 1, i32* %11
%14 = load i32*, i32** %4
%15 = call i32 @cdi_check_footer(i32* %14, i32* %6, i32* %7)
%16 = icmp ne i32 %15, 0
br i1 %16, label %19, label %17
17: ; preds = %2
%18 = call i32 (i8*, ...) @msg_error(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %3
br label %101
19: ; preds = %2
%20 = load i32*, i32** %4
%21 = load i32, i32* %7
%22 = load i32, i32* @SEEK_SET
%23 = call i64 @fseek(i32* %20, i32 %21, i32 %22)
%24 = icmp slt i64 %23, 0
br i1 %24, label %30, label %25
25: ; preds = %19
%26 = getelementptr inbounds [2 x i32], [2 x i32]* %12, i64 0, i64 0
%27 = load i32*, i32** %4
%28 = call i32 @fread(i32* %26, i32 1, i32 2, i32* %27)
%29 = icmp ne i32 %28, 2
br i1 %29, label %30, label %33
30: ; preds = %25, %19
%31 = load i8*, i8** %5
%32 = call i32 @msg_perror(i8* %31)
store i32 0, i32* %3
br label %101
33: ; preds = %25
%34 = getelementptr inbounds [2 x i32], [2 x i32]* %12, i64 0, i64 0
%35 = call i32 @get_le16(i32* %34)
store i32 %35, i32* %9
%36 = load i32, i32* %9
%37 = icmp ne i32 %36, 0
br i1 %37, label %40, label %38
38: ; preds = %33
%39 = call i32 (i8*, ...) @msg_error(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
store i32 0, i32* %3
br label %101
40: ; preds = %33
%41 = load i32, i32* %9
%42 = call i32 (i8*, i32, ...) @msg_info(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i32 %41)
store i32 1, i32* %10
br label %43
43: ; preds = %97, %40
%44 = load i32, i32* %10
%45 = load i32, i32* %9
%46 = icmp ule i32 %44, %45
br i1 %46, label %47, label %100
47: ; preds = %43
%48 = getelementptr inbounds [2 x i32], [2 x i32]* %12, i64 0, i64 0
%49 = load i32*, i32** %4
%50 = call i32 @fread(i32* %48, i32 1, i32 2, i32* %49)
%51 = icmp ne i32 %50, 2
br i1 %51, label %52, label %55
52: ; preds = %47
%53 = load i8*, i8** %5
%54 = call i32 @msg_perror(i8* %53)
store i32 0, i32* %3
br label %101
55: ; preds = %47
%56 = getelementptr inbounds [2 x i32], [2 x i32]* %12, i64 0, i64 0
%57 = call i32 @get_le16(i32* %56)
store i32 %57, i32* %13
%58 = load i32, i32* %13
%59 = icmp ne i32 %58, 0
br i1 %59, label %63, label %60
60: ; preds = %55
%61 = load i32, i32* %10
%62 = call i32 (i8*, ...) @msg_error(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0), i32 %61)
store i32 0, i32* %3
br label %101
63: ; preds = %55
%64 = load i32, i32* %10
%65 = load i32, i32* %13
%66 = call i32 (i8*, i32, ...) @msg_info(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.4, i64 0, i64 0), i32 %64, i32 %65)
br label %67
67: ; preds = %79, %63
%68 = load i32*, i32** %4
%69 = load i8*, i8** %5
%70 = load i32, i32* %6
%71 = load i32, i32* %11
%72 = load i32, i32* %10
%73 = call i32 @cdi_parse_track(i32* %68, i8* %69, i32 %70, i32 %71, i32 %72, i32* %8)
%74 = icmp ne i32 %73, 0
br i1 %74, label %76, label %75
75: ; preds = %67
store i32 0, i32* %3
br label %101
76: ; preds = %67
%77 = load i32, i32* %11
%78 = add i32 %77, 1
store i32 %78, i32* %11
br label %79
79: ; preds = %76
%80 = load i32, i32* %13
%81 = add i32 %80, -1
store i32 %81, i32* %13
%82 = icmp ne i32 %81, 0
br i1 %82, label %67, label %83
83: ; preds = %79
%84 = load i32*, i32** %4
%85 = load i32, i32* %6
%86 = sext i32 %85 to i64
%87 = icmp eq i64 %86, 2147483652
%88 = zext i1 %87 to i64
%89 = select i1 %87, i32 12, i32 13
%90 = load i32, i32* @SEEK_CUR
%91 = call i64 @fseek(i32* %84, i32 %89, i32 %90)
%92 = icmp slt i64 %91, 0
br i1 %92, label %93, label %96
93: ; preds = %83
%94 = load i8*, i8** %5
%95 = call i32 @msg_perror(i8* %94)
store i32 0, i32* %3
br label %101
96: ; preds = %83
br label %97
97: ; preds = %96
%98 = load i32, i32* %10
%99 = add i32 %98, 1
store i32 %99, i32* %10
br label %43
100: ; preds = %43
store i32 1, i32* %3
br label %101
101: ; preds = %100, %93, %75, %60, %52, %38, %30, %17
%102 = load i32, i32* %3
ret i32 %102
}

LLVM IR (-O2):
@.str = [28 x i8] c"Failed to parse CDI footer\0A\00"
@SEEK_SET = global i32 0
@.str.1 = [22 x i8] c"No sessions in image\0A\00"
@.str.2 = [25 x i8] c"Image has %u session(s)\0A\00"
@.str.3 = [25 x i8] c"No tracks in session %u\0A\00"
@.str.4 = [28 x i8] c"Session %u has %u track(s)\0A\00"
@SEEK_CUR = global i32 0
define i32 @cdi_parse_and_add_tracks(i32* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca [2 x i32]
%7 = bitcast i32* %3 to i8*
%8 = bitcast i32* %4 to i8*
%9 = bitcast i32* %5 to i8*
store i32 0, i32* %5
%10 = bitcast [2 x i32]* %6 to i8*
%11 = call i32 @cdi_check_footer(i32* %0, i32* %3, i32* %4)
%12 = icmp eq i32 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %2
%14 = call i32 (i8*, ...) @msg_error(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
br label %66
15: ; preds = %2
%16 = load i32, i32* %4
%17 = load i32, i32* @SEEK_SET
%18 = call i64 @fseek(i32* %0, i32 %16, i32 %17)
%19 = icmp slt i64 %18, 0
br i1 %19, label %24, label %20
20: ; preds = %15
%21 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%22 = call i32 @fread(i32* %21, i32 1, i32 2, i32* %0)
%23 = icmp eq i32 %22, 2
br i1 %23, label %26, label %24
24: ; preds = %20, %15
%25 = call i32 @msg_perror(i8* %1)
br label %66
26: ; preds = %20
%27 = call i32 @get_le16(i32* %21)
%28 = icmp eq i32 %27, 0
br i1 %28, label %29, label %31
29: ; preds = %26
%30 = call i32 (i8*, ...) @msg_error(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
br label %66
31: ; preds = %26
%32 = call i32 (i8*, i32, ...) @msg_info(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i32 %27)
br label %33
33: ; preds = %31, %63
%34 = phi i32 [ %54, %63 ], [ 1, %31 ]
%35 = phi i32 [ %64, %63 ], [ 1, %31 ]
%36 = call i32 @fread(i32* %21, i32 1, i32 2, i32* %0)
%37 = icmp eq i32 %36, 2
br i1 %37, label %40, label %38
38: ; preds = %33
%39 = call i32 @msg_perror(i8* %1)
br label %66
40: ; preds = %33
%41 = call i32 @get_le16(i32* %21)
%42 = icmp eq i32 %41, 0
br i1 %42, label %43, label %45
43: ; preds = %40
%44 = call i32 (i8*, ...) @msg_error(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0), i32 %35)
br label %66
45: ; preds = %40
%46 = call i32 (i8*, i32, ...) @msg_info(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.4, i64 0, i64 0), i32 %35, i32 %41)
br label %47
47: ; preds = %53, %45
%48 = phi i32 [ %34, %45 ], [ %54, %53 ]
%49 = phi i32 [ %41, %45 ], [ %55, %53 ]
%50 = load i32, i32* %3
%51 = call i32 @cdi_parse_track(i32* %0, i8* %1, i32 %50, i32 %48, i32 %35, i32* %5)
%52 = icmp eq i32 %51, 0
br i1 %52, label %66, label %53
53: ; preds = %47
%54 = add i32 %48, 1
%55 = add i32 %49, -1
%56 = icmp eq i32 %55, 0
br i1 %56, label %57, label %47
57: ; preds = %53
%58 = load i32, i32* @SEEK_CUR
%59 = call i64 @fseek(i32* %0, i32 13, i32 %58)
%60 = icmp slt i64 %59, 0
br i1 %60, label %61, label %63
61: ; preds = %57
%62 = call i32 @msg_perror(i8* %1)
br label %66
63: ; preds = %57
%64 = add i32 %35, 1
%65 = icmp ugt i32 %64, %27
br i1 %65, label %66, label %33
66: ; preds = %63, %47, %43, %61, %38, %29, %24, %13
%67 = phi i32 [ 0, %24 ], [ 0, %29 ], [ 0, %13 ], [ 0, %38 ], [ 0, %61 ], [ 0, %43 ], [ 0, %47 ], [ 1, %63 ]
ret i32 %67
}