#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*/ log_error(char *, int);
scalar_t__ seq_ctrl_get_track_select(int);
int /*<<< orphan*/ seq_engine_stop_notes(int);
int /*<<< orphan*/ seq_utils_clamp(int /*<<< orphan*/, int /*<<< orphan*/, int);
int /*<<< orphan*/ song_get_midi_channel_map(int, int);
int song_get_midi_port_map(int, int);
int /*<<< orphan*/ song_set_midi_channel_map(int, int, int /*<<< orphan*/);
void seq_ctrl_adjust_midi_channel(int mapnum, int change) {
  int track;
  if (mapnum < 0 || mapnum >= 2) {
    log_error("scamc - mapnum invalid: %d", mapnum);
    return;
  }
  for (track = 0; track < 6; track++) {
    if (seq_ctrl_get_track_select(track)) {
      switch (song_get_midi_port_map(track, mapnum)) {
      case 0:
      case 1:
      case 4:
      case 3:
        seq_engine_stop_notes(track);
        song_set_midi_channel_map(
            track, mapnum,
            seq_utils_clamp(song_get_midi_channel_map(track, mapnum) + change,
                            0, (16 - 1)));
        break;
      case 2:
        seq_engine_stop_notes(track);
        song_set_midi_channel_map(
            track, mapnum,
            seq_utils_clamp(song_get_midi_channel_map(track, mapnum) + change,
                            0, (4 - 1)));
        break;
      default:
        return;
      }
    }
  }
}


x86 assembly (clang, -O0):
<seq_ctrl_adjust_midi_channel>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
cmpl   $0x0,-0x4(%rbp)
jl     1192 <seq_ctrl_adjust_midi_channel+0x22>
cmpl   $0x2,-0x4(%rbp)
jl     11a6 <seq_ctrl_adjust_midi_channel+0x36>
mov    -0x4(%rbp),%esi
lea    0xe64(%rip),%rdi
call   1050 <log_error@plt>
jmp    12ad <seq_ctrl_adjust_midi_channel+0x13d>
movl   $0x0,-0xc(%rbp)
cmpl   $0x6,-0xc(%rbp)
jge    12ad <seq_ctrl_adjust_midi_channel+0x13d>
mov    -0xc(%rbp),%edi
call   1080 <seq_ctrl_get_track_select@plt>
cmp    $0x0,%rax
je     129a <seq_ctrl_adjust_midi_channel+0x12a>
mov    -0xc(%rbp),%edi
mov    -0x4(%rbp),%esi
call   1030 <song_get_midi_port_map@plt>
mov    %eax,%ecx
mov    %ecx,-0x10(%rbp)
sub    $0x2,%eax
jb     120c <seq_ctrl_adjust_midi_channel+0x9c>
jmp    11e7 <seq_ctrl_adjust_midi_channel+0x77>
mov    -0x10(%rbp),%eax
sub    $0x2,%eax
je     124e <seq_ctrl_adjust_midi_channel+0xde>
jmp    11f8 <seq_ctrl_adjust_midi_channel+0x88>
mov    -0x10(%rbp),%eax
add    $0xfffffffd,%eax
sub    $0x1,%eax
ja     1290 <seq_ctrl_adjust_midi_channel+0x120>
jmp    120c <seq_ctrl_adjust_midi_channel+0x9c>
mov    -0xc(%rbp),%edi
call   1060 <seq_engine_stop_notes@plt>
mov    -0xc(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x4(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0xc(%rbp),%edi
mov    -0x4(%rbp),%esi
call   1070 <song_get_midi_channel_map@plt>
mov    %eax,%edi
add    -0x8(%rbp),%edi
xor    %esi,%esi
mov    $0xf,%edx
call   1090 <seq_utils_clamp@plt>
mov    -0x18(%rbp),%edi
mov    -0x14(%rbp),%esi
mov    %eax,%edx
call   1040 <song_set_midi_channel_map@plt>
jmp    1295 <seq_ctrl_adjust_midi_channel+0x125>
mov    -0xc(%rbp),%edi
call   1060 <seq_engine_stop_notes@plt>
mov    -0xc(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x4(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0xc(%rbp),%edi
mov    -0x4(%rbp),%esi
call   1070 <song_get_midi_channel_map@plt>
mov    %eax,%edi
add    -0x8(%rbp),%edi
xor    %esi,%esi
mov    $0x3,%edx
call   1090 <seq_utils_clamp@plt>
mov    -0x20(%rbp),%edi
mov    -0x1c(%rbp),%esi
mov    %eax,%edx
call   1040 <song_set_midi_channel_map@plt>
jmp    1295 <seq_ctrl_adjust_midi_channel+0x125>
jmp    12ad <seq_ctrl_adjust_midi_channel+0x13d>
jmp    129a <seq_ctrl_adjust_midi_channel+0x12a>
jmp    129f <seq_ctrl_adjust_midi_channel+0x12f>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    11ad <seq_ctrl_adjust_midi_channel+0x3d>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<seq_ctrl_adjust_midi_channel>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %edi,%r15d
cmp    $0x1,%edi
jbe    1197 <seq_ctrl_adjust_midi_channel+0x27>
lea    0xe79(%rip),%rdi
mov    %r15d,%esi
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1050 <log_error@plt>
mov    %esi,%r14d
xor    %ebp,%ebp
lea    0xe79(%rip),%r12
jmp    11e8 <seq_ctrl_adjust_midi_channel+0x78>
cs nopw 0x0(%rax,%rax,1)
nop
cltq
mov    (%r12,%rax,4),%ebx
mov    %ebp,%edi
call   1060 <seq_engine_stop_notes@plt>
mov    %ebp,%edi
mov    %r15d,%esi
call   1070 <song_get_midi_channel_map@plt>
lea    (%rax,%r14,1),%edi
xor    %esi,%esi
mov    %ebx,%edx
call   1090 <seq_utils_clamp@plt>
mov    %ebp,%edi
mov    %r15d,%esi
mov    %eax,%edx
call   1040 <song_set_midi_channel_map@plt>
add    $0x1,%ebp
cmp    $0x6,%ebp
je     1203 <seq_ctrl_adjust_midi_channel+0x93>
mov    %ebp,%edi
call   1080 <seq_ctrl_get_track_select@plt>
test   %rax,%rax
je     11e0 <seq_ctrl_adjust_midi_channel+0x70>
mov    %ebp,%edi
mov    %r15d,%esi
call   1030 <song_get_midi_port_map@plt>
cmp    $0x4,%eax
jbe    11b0 <seq_ctrl_adjust_midi_channel+0x40>
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [27 x i8] c"scamc - mapnum invalid: %d\00"
define void @seq_ctrl_adjust_midi_channel(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
store i32 %0, i32* %3
store i32 %1, i32* %4
%6 = load i32, i32* %3
%7 = icmp slt i32 %6, 0
br i1 %7, label %11, label %8
8: ; preds = %2
%9 = load i32, i32* %3
%10 = icmp sge i32 %9, 2
br i1 %10, label %11, label %14
11: ; preds = %8, %2
%12 = load i32, i32* %3
%13 = call i32 @log_error(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i32 %12)
br label %56
14: ; preds = %8
store i32 0, i32* %5
br label %15
15: ; preds = %53, %14
%16 = load i32, i32* %5
%17 = icmp slt i32 %16, 6
br i1 %17, label %18, label %56
18: ; preds = %15
%19 = load i32, i32* %5
%20 = call i64 @seq_ctrl_get_track_select(i32 %19)
%21 = icmp ne i64 %20, 0
br i1 %21, label %22, label %52
22: ; preds = %18
%23 = load i32, i32* %5
%24 = load i32, i32* %3
%25 = call i32 @song_get_midi_port_map(i32 %23, i32 %24)
switch i32 %25, label %50 [
i32 0, label %26
i32 1, label %26
i32 4, label %26
i32 3, label %26
i32 2, label %38
]
26: ; preds = %22, %22, %22, %22
%27 = load i32, i32* %5
%28 = call i32 @seq_engine_stop_notes(i32 %27)
%29 = load i32, i32* %5
%30 = load i32, i32* %3
%31 = load i32, i32* %5
%32 = load i32, i32* %3
%33 = call i32 @song_get_midi_channel_map(i32 %31, i32 %32)
%34 = load i32, i32* %4
%35 = add nsw i32 %33, %34
%36 = call i32 @seq_utils_clamp(i32 %35, i32 0, i32 15)
%37 = call i32 @song_set_midi_channel_map(i32 %29, i32 %30, i32 %36)
br label %51
38: ; preds = %22
%39 = load i32, i32* %5
%40 = call i32 @seq_engine_stop_notes(i32 %39)
%41 = load i32, i32* %5
%42 = load i32, i32* %3
%43 = load i32, i32* %5
%44 = load i32, i32* %3
%45 = call i32 @song_get_midi_channel_map(i32 %43, i32 %44)
%46 = load i32, i32* %4
%47 = add nsw i32 %45, %46
%48 = call i32 @seq_utils_clamp(i32 %47, i32 0, i32 3)
%49 = call i32 @song_set_midi_channel_map(i32 %41, i32 %42, i32 %48)
br label %51
50: ; preds = %22
br label %56
51: ; preds = %38, %26
br label %52
52: ; preds = %51, %18
br label %53
53: ; preds = %52
%54 = load i32, i32* %5
%55 = add nsw i32 %54, 1
store i32 %55, i32* %5
br label %15
56: ; preds = %11, %50, %15
ret void
}

LLVM IR (-O2):
@.str = [27 x i8] c"scamc - mapnum invalid: %d\00"
@switch.table.seq_ctrl_adjust_midi_channel = [5 x i32] [i32 15, i32 15, i32 3, i32 15, i32 15]
define void @seq_ctrl_adjust_midi_channel(i32 %0, i32 %1) {
%3 = icmp ugt i32 %0, 1
br i1 %3, label %4, label %6
4: ; preds = %2
%5 = tail call i32 @log_error(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i32 %0)
br label %25
6: ; preds = %2, %22
%7 = phi i32 [ %23, %22 ], [ 0, %2 ]
%8 = tail call i64 @seq_ctrl_get_track_select(i32 %7)
%9 = icmp eq i64 %8, 0
br i1 %9, label %22, label %10
10: ; preds = %6
%11 = tail call i32 @song_get_midi_port_map(i32 %7, i32 %0)
%12 = icmp ult i32 %11, 5
br i1 %12, label %13, label %25
13: ; preds = %10
%14 = sext i32 %11 to i64
%15 = getelementptr inbounds [5 x i32], [5 x i32]* @switch.table.seq_ctrl_adjust_midi_channel, i64 0, i64 %14
%16 = load i32, i32* %15
%17 = tail call i32 @seq_engine_stop_notes(i32 %7)
%18 = tail call i32 @song_get_midi_channel_map(i32 %7, i32 %0)
%19 = add nsw i32 %18, %1
%20 = tail call i32 @seq_utils_clamp(i32 %19, i32 0, i32 %16)
%21 = tail call i32 @song_set_midi_channel_map(i32 %7, i32 %0, i32 %20)
br label %22
22: ; preds = %13, %6
%23 = add nuw nsw i32 %7, 1
%24 = icmp eq i32 %23, 6
br i1 %24, label %25, label %6
25: ; preds = %22, %10, %4
ret void
}