#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 jam_jtag_io(int, char, int);
int jam_jtag_drscan(int start_state, int count, char *tdi, char *tdo) {
  int i = 0;
  int tdo_bit = 0;
  int status = 1;
  switch (start_state) {
  case 0:
    jam_jtag_io(1, 0, 0);
    jam_jtag_io(0, 0, 0);
    jam_jtag_io(0, 0, 0);
    break;
  case 1:
    jam_jtag_io(1, 0, 0);
    jam_jtag_io(0, 0, 0);
    break;
  case 2:
    jam_jtag_io(1, 0, 0);
    jam_jtag_io(1, 0, 0);
    jam_jtag_io(1, 0, 0);
    jam_jtag_io(0, 0, 0);
    jam_jtag_io(0, 0, 0);
    break;
  default:
    status = 0;
  }
  if (status) {
    for (i = 0; i < count; i++) {
      tdo_bit = jam_jtag_io((i == count - 1), tdi[i >> 3] & (1 << (i & 7)),
                            (tdo != (0)));
      if (tdo != (0)) {
        if (tdo_bit) {
          tdo[i >> 3] |= (1 << (i & 7));
        } else {
          tdo[i >> 3] &= ~(unsigned int)(1 << (i & 7));
        }
      }
    }
    jam_jtag_io(0, 0, 0);
  }
  return (status);
}


x86 assembly (clang, -O0):
<jam_jtag_drscan>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %rdx,-0x10(%rbp)
mov    %rcx,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
movl   $0x1,-0x24(%rbp)
mov    -0x4(%rbp),%eax
mov    %eax,-0x28(%rbp)
test   %eax,%eax
je     1170 <jam_jtag_drscan+0x60>
jmp    114e <jam_jtag_drscan+0x3e>
mov    -0x28(%rbp),%eax
sub    $0x1,%eax
je     1199 <jam_jtag_drscan+0x89>
jmp    115f <jam_jtag_drscan+0x4f>
mov    -0x28(%rbp),%eax
sub    $0x2,%eax
je     11b7 <jam_jtag_drscan+0xa7>
jmp    11fc <jam_jtag_drscan+0xec>
mov    $0x1,%edi
xor    %edx,%edx
mov    %edx,%esi
call   1030 <jam_jtag_io@plt>
xor    %edx,%edx
mov    %edx,%edi
mov    %edx,%esi
call   1030 <jam_jtag_io@plt>
xor    %edx,%edx
mov    %edx,%edi
mov    %edx,%esi
call   1030 <jam_jtag_io@plt>
jmp    1203 <jam_jtag_drscan+0xf3>
mov    $0x1,%edi
xor    %edx,%edx
mov    %edx,%esi
call   1030 <jam_jtag_io@plt>
xor    %edx,%edx
mov    %edx,%edi
mov    %edx,%esi
call   1030 <jam_jtag_io@plt>
jmp    1203 <jam_jtag_drscan+0xf3>
mov    $0x1,%edi
xor    %edx,%edx
mov    %edx,%esi
call   1030 <jam_jtag_io@plt>
mov    $0x1,%edi
xor    %edx,%edx
mov    %edx,%esi
call   1030 <jam_jtag_io@plt>
mov    $0x1,%edi
xor    %edx,%edx
mov    %edx,%esi
call   1030 <jam_jtag_io@plt>
xor    %edx,%edx
mov    %edx,%edi
mov    %edx,%esi
call   1030 <jam_jtag_io@plt>
xor    %edx,%edx
mov    %edx,%edi
mov    %edx,%esi
call   1030 <jam_jtag_io@plt>
jmp    1203 <jam_jtag_drscan+0xf3>
movl   $0x0,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
je     12f4 <jam_jtag_drscan+0x1e4>
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    12e9 <jam_jtag_drscan+0x1d9>
mov    -0x1c(%rbp),%eax
mov    -0x8(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
sete   %al
and    $0x1,%al
movzbl %al,%edi
mov    -0x10(%rbp),%rax
mov    -0x1c(%rbp),%ecx
sar    $0x3,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
mov    -0x1c(%rbp),%ecx
and    $0x7,%ecx
mov    $0x1,%edx
shl    %cl,%edx
mov    %edx,%ecx
and    %ecx,%eax
cmpq   $0x0,-0x18(%rbp)
setne  %cl
and    $0x1,%cl
movzbl %cl,%edx
movsbl %al,%esi
call   1030 <jam_jtag_io@plt>
mov    %eax,-0x20(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     12d6 <jam_jtag_drscan+0x1c6>
cmpl   $0x0,-0x20(%rbp)
je     12ab <jam_jtag_drscan+0x19b>
mov    -0x1c(%rbp),%ecx
and    $0x7,%ecx
mov    $0x1,%esi
shl    %cl,%esi
mov    -0x18(%rbp),%rax
mov    -0x1c(%rbp),%ecx
sar    $0x3,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%edx
or     %esi,%edx
mov    %dl,(%rax,%rcx,1)
jmp    12d1 <jam_jtag_drscan+0x1c1>
mov    -0x1c(%rbp),%ecx
and    $0x7,%ecx
mov    $0x1,%esi
shl    %cl,%esi
xor    $0xffffffff,%esi
mov    -0x18(%rbp),%rax
mov    -0x1c(%rbp),%ecx
sar    $0x3,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%edx
and    %esi,%edx
mov    %dl,(%rax,%rcx,1)
jmp    12d6 <jam_jtag_drscan+0x1c6>
jmp    12db <jam_jtag_drscan+0x1cb>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1214 <jam_jtag_drscan+0x104>
xor    %edx,%edx
mov    %edx,%edi
mov    %edx,%esi
call   1030 <jam_jtag_io@plt>
mov    -0x24(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<jam_jtag_drscan>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rcx,%r13
mov    %rdx,(%rsp)
mov    %esi,%r15d
test   %edi,%edi
je     1159 <jam_jtag_drscan+0x49>
mov    %edi,%r12d
cmp    $0x1,%edi
je     116a <jam_jtag_drscan+0x5a>
xor    %eax,%eax
cmp    $0x2,%r12d
jne    1218 <jam_jtag_drscan+0x108>
mov    $0x1,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1030 <jam_jtag_io@plt>
mov    $0x1,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1030 <jam_jtag_io@plt>
xor    %r12d,%r12d
mov    $0x1,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1030 <jam_jtag_io@plt>
xor    %ebp,%ebp
mov    %r12d,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1030 <jam_jtag_io@plt>
xor    %edi,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1030 <jam_jtag_io@plt>
test   %r15d,%r15d
jle    1208 <jam_jtag_drscan+0xf8>
xor    %r12d,%r12d
test   %r13,%r13
setne  %r12b
neg    %r15d
jmp    11aa <jam_jtag_drscan+0x9a>
nopl   0x0(%rax,%rax,1)
add    $0x1,%ebp
mov    %r15d,%eax
add    %ebp,%eax
je     1208 <jam_jtag_drscan+0xf8>
lea    (%r15,%rbp,1),%eax
xor    %edi,%edi
cmp    $0xffffffff,%eax
sete   %dil
mov    %ebp,%ebx
mov    %ebp,%ecx
and    $0x7,%cl
mov    $0x1,%r14d
shl    %cl,%r14d
shr    $0x3,%rbx
mov    (%rsp),%rax
movzbl (%rax,%rbx,1),%eax
and    %r14b,%al
movsbl %al,%esi
mov    %r12d,%edx
call   1030 <jam_jtag_io@plt>
test   %r13,%r13
je     11a0 <jam_jtag_drscan+0x90>
movzbl 0x0(%r13,%rbx,1),%ecx
mov    %r14d,%edx
not    %dl
and    %cl,%dl
or     %r14b,%cl
test   %eax,%eax
movzbl %dl,%eax
movzbl %cl,%ecx
cmove  %eax,%ecx
mov    %cl,0x0(%r13,%rbx,1)
jmp    11a0 <jam_jtag_drscan+0x90>
xor    %edi,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1030 <jam_jtag_io@plt>
mov    $0x1,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @jam_jtag_drscan(i32 %0, i32 %1, i8* %2, i8* %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
store i32 %0, i32* %5
store i32 %1, i32* %6
store i8* %2, i8** %7
store i8* %3, i8** %8
store i32 0, i32* %9
store i32 0, i32* %10
store i32 1, i32* %11
%12 = load i32, i32* %5
switch i32 %12, label %26 [
i32 0, label %13
i32 1, label %17
i32 2, label %20
]
13: ; preds = %4
%14 = call i32 @jam_jtag_io(i32 1, i8 signext 0, i32 0)
%15 = call i32 @jam_jtag_io(i32 0, i8 signext 0, i32 0)
%16 = call i32 @jam_jtag_io(i32 0, i8 signext 0, i32 0)
br label %27
17: ; preds = %4
%18 = call i32 @jam_jtag_io(i32 1, i8 signext 0, i32 0)
%19 = call i32 @jam_jtag_io(i32 0, i8 signext 0, i32 0)
br label %27
20: ; preds = %4
%21 = call i32 @jam_jtag_io(i32 1, i8 signext 0, i32 0)
%22 = call i32 @jam_jtag_io(i32 1, i8 signext 0, i32 0)
%23 = call i32 @jam_jtag_io(i32 1, i8 signext 0, i32 0)
%24 = call i32 @jam_jtag_io(i32 0, i8 signext 0, i32 0)
%25 = call i32 @jam_jtag_io(i32 0, i8 signext 0, i32 0)
br label %27
26: ; preds = %4
store i32 0, i32* %11
br label %27
27: ; preds = %26, %20, %17, %13
%28 = load i32, i32* %11
%29 = icmp ne i32 %28, 0
br i1 %29, label %30, label %96
30: ; preds = %27
store i32 0, i32* %9
br label %31
31: ; preds = %91, %30
%32 = load i32, i32* %9
%33 = load i32, i32* %6
%34 = icmp slt i32 %32, %33
br i1 %34, label %35, label %94
35: ; preds = %31
%36 = load i32, i32* %9
%37 = load i32, i32* %6
%38 = sub nsw i32 %37, 1
%39 = icmp eq i32 %36, %38
%40 = zext i1 %39 to i32
%41 = load i8*, i8** %7
%42 = load i32, i32* %9
%43 = ashr i32 %42, 3
%44 = sext i32 %43 to i64
%45 = getelementptr inbounds i8, i8* %41, i64 %44
%46 = load i8, i8* %45
%47 = sext i8 %46 to i32
%48 = load i32, i32* %9
%49 = and i32 %48, 7
%50 = shl i32 1, %49
%51 = and i32 %47, %50
%52 = trunc i32 %51 to i8
%53 = load i8*, i8** %8
%54 = icmp ne i8* %53, null
%55 = zext i1 %54 to i32
%56 = call i32 @jam_jtag_io(i32 %40, i8 signext %52, i32 %55)
store i32 %56, i32* %10
%57 = load i8*, i8** %8
%58 = icmp ne i8* %57, null
br i1 %58, label %59, label %90
59: ; preds = %35
%60 = load i32, i32* %10
%61 = icmp ne i32 %60, 0
br i1 %61, label %62, label %75
62: ; preds = %59
%63 = load i32, i32* %9
%64 = and i32 %63, 7
%65 = shl i32 1, %64
%66 = load i8*, i8** %8
%67 = load i32, i32* %9
%68 = ashr i32 %67, 3
%69 = sext i32 %68 to i64
%70 = getelementptr inbounds i8, i8* %66, i64 %69
%71 = load i8, i8* %70
%72 = sext i8 %71 to i32
%73 = or i32 %72, %65
%74 = trunc i32 %73 to i8
store i8 %74, i8* %70
br label %89
75: ; preds = %59
%76 = load i32, i32* %9
%77 = and i32 %76, 7
%78 = shl i32 1, %77
%79 = xor i32 %78, -1
%80 = load i8*, i8** %8
%81 = load i32, i32* %9
%82 = ashr i32 %81, 3
%83 = sext i32 %82 to i64
%84 = getelementptr inbounds i8, i8* %80, i64 %83
%85 = load i8, i8* %84
%86 = sext i8 %85 to i32
%87 = and i32 %86, %79
%88 = trunc i32 %87 to i8
store i8 %88, i8* %84
br label %89
89: ; preds = %75, %62
br label %90
90: ; preds = %89, %35
br label %91
91: ; preds = %90
%92 = load i32, i32* %9
%93 = add nsw i32 %92, 1
store i32 %93, i32* %9
br label %31
94: ; preds = %31
%95 = call i32 @jam_jtag_io(i32 0, i8 signext 0, i32 0)
br label %96
96: ; preds = %94, %27
%97 = load i32, i32* %11
ret i32 %97
}

LLVM IR (-O2):
define i32 @jam_jtag_drscan(i32 %0, i32 %1, i8* nocapture %2, i8* %3) {
switch i32 %0, label %45 [
i32 0, label %8
i32 1, label %10
i32 2, label %5
]
5: ; preds = %4
%6 = tail call i32 @jam_jtag_io(i32 1, i8 signext 0, i32 0)
%7 = tail call i32 @jam_jtag_io(i32 1, i8 signext 0, i32 0)
br label %8
8: ; preds = %4, %5
%9 = tail call i32 @jam_jtag_io(i32 1, i8 signext 0, i32 0)
br label %10
10: ; preds = %8, %4
%11 = phi i32 [ %0, %4 ], [ 0, %8 ]
%12 = tail call i32 @jam_jtag_io(i32 %11, i8 signext 0, i32 0)
%13 = tail call i32 @jam_jtag_io(i32 0, i8 signext 0, i32 0)
%14 = icmp sgt i32 %1, 0
br i1 %14, label %15, label %43
15: ; preds = %10
%16 = add nsw i32 %1, -1
%17 = icmp ne i8* %3, null
%18 = zext i1 %17 to i32
br label %19
19: ; preds = %15, %40
%20 = phi i32 [ 0, %15 ], [ %41, %40 ]
%21 = icmp eq i32 %20, %16
%22 = zext i1 %21 to i32
%23 = lshr i32 %20, 3
%24 = zext i32 %23 to i64
%25 = getelementptr inbounds i8, i8* %2, i64 %24
%26 = load i8, i8* %25
%27 = and i32 %20, 7
%28 = shl nuw nsw i32 1, %27
%29 = trunc i32 %28 to i8
%30 = and i8 %26, %29
%31 = tail call i32 @jam_jtag_io(i32 %22, i8 signext %30, i32 %18)
br i1 %17, label %32, label %40
32: ; preds = %19
%33 = icmp eq i32 %31, 0
%34 = getelementptr inbounds i8, i8* %3, i64 %24
%35 = load i8, i8* %34
%36 = xor i8 %29, -1
%37 = and i8 %35, %36
%38 = or i8 %35, %29
%39 = select i1 %33, i8 %37, i8 %38
store i8 %39, i8* %34
br label %40
40: ; preds = %32, %19
%41 = add nuw nsw i32 %20, 1
%42 = icmp eq i32 %41, %1
br i1 %42, label %43, label %19
43: ; preds = %40, %10
%44 = tail call i32 @jam_jtag_io(i32 0, i8 signext 0, i32 0)
br label %45
45: ; preds = %4, %43
%46 = phi i32 [ 1, %43 ], [ 0, %4 ]
ret i32 %46
}