#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*/ dump_jreg(char *, unsigned char *, int);
int /*<<< orphan*/ free(unsigned char *);
unsigned char *malloc(int);
double or1ksim_jtag_shift_ir(unsigned char *, int);
int /*<<< orphan*/ printf(char *, ...);
unsigned long reverse_bits(unsigned long, int);
unsigned long strtoul(char *, int /*<<< orphan*/ *, int);
int process_instruction(int next_jreg, int argc, char *argv[]) {
  printf("Shifting instruction.\n");
  if (next_jreg >= argc) {
    printf("ERROR: no instruction register value found.\n");
    return 0;
  }
  unsigned long int ival = strtoul(argv[next_jreg], NULL, 16);
  if (ival > 0xf) {
    printf("ERROR: instruction value 0x%lx too large\n", ival);
    return 0;
  }
  ival = reverse_bits(ival, 4);
  unsigned char *jreg = malloc(1);
  if (NULL == jreg) {
    printf("ERROR: malloc for instruction register failed.\n");
    return 0;
  }
  jreg[0] = ival;
  dump_jreg("  shifting in", jreg, 1);
  double t = or1ksim_jtag_shift_ir(jreg, 4);
  dump_jreg("  shifted out", jreg, 1);
  printf("  time taken:   %.12fs\n", t);
  free(jreg);
  return 1;
}


x86 assembly (clang, -O0):
<process_instruction>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
lea    0xe77(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x8(%rbp),%eax
cmp    -0xc(%rbp),%eax
jl     11b6 <process_instruction+0x46>
lea    0xe74(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    12af <process_instruction+0x13f>
mov    -0x18(%rbp),%rax
movslq -0x8(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0x10,%edx
call   1090 <strtoul@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0xf,-0x20(%rbp)
jbe    11fd <process_instruction+0x8d>
mov    -0x20(%rbp),%rsi
lea    0xe5a(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    12af <process_instruction+0x13f>
mov    -0x20(%rbp),%rdi
mov    $0x4,%esi
call   1050 <reverse_bits@plt>
mov    %rax,-0x20(%rbp)
mov    $0x1,%edi
call   1080 <malloc@plt>
mov    %rax,-0x28(%rbp)
xor    %eax,%eax
cmp    -0x28(%rbp),%rax
jne    1243 <process_instruction+0xd3>
lea    0xe3e(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    12af <process_instruction+0x13f>
mov    -0x20(%rbp),%rax
mov    %al,%cl
mov    -0x28(%rbp),%rax
mov    %cl,(%rax)
mov    -0x28(%rbp),%rsi
lea    0xe44(%rip),%rdi
mov    $0x1,%edx
call   1040 <dump_jreg@plt>
mov    -0x28(%rbp),%rdi
mov    $0x4,%esi
call   1070 <or1ksim_jtag_shift_ir@plt>
movsd  %xmm0,-0x30(%rbp)
mov    -0x28(%rbp),%rsi
lea    0xe2a(%rip),%rdi
mov    $0x1,%edx
call   1040 <dump_jreg@plt>
movsd  -0x30(%rbp),%xmm0
lea    0xe22(%rip),%rdi
mov    $0x1,%al
call   1060 <printf@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<process_instruction>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %esi,%ebx
mov    %edi,%ebp
lea    0xec9(%rip),%rdi
call   1040 <puts@plt>
cmp    %ebx,%ebp
jge    11cf <process_instruction+0x4f>
movslq %ebp,%rax
mov    (%r14,%rax,8),%rdi
xor    %ebx,%ebx
xor    %esi,%esi
mov    $0x10,%edx
call   10a0 <strtoul@plt>
cmp    $0x10,%rax
jb     11df <process_instruction+0x5f>
lea    0xe40(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1070 <printf@plt>
jmp    125e <process_instruction+0xde>
lea    0xecd(%rip),%rdi
call   1040 <puts@plt>
xor    %ebx,%ebx
jmp    125e <process_instruction+0xde>
mov    %rax,%rdi
mov    $0x4,%esi
call   1060 <reverse_bits@plt>
mov    %rax,%r15
mov    $0x1,%ebx
mov    $0x1,%edi
call   1090 <malloc@plt>
test   %rax,%rax
je     126b <process_instruction+0xeb>
mov    %rax,%r14
mov    %r15b,(%rax)
lea    0xe1a(%rip),%rdi
mov    %rax,%rsi
mov    $0x1,%edx
call   1050 <dump_jreg@plt>
mov    %r14,%rdi
mov    $0x4,%esi
call   1080 <or1ksim_jtag_shift_ir@plt>
movsd  %xmm0,(%rsp)
lea    0xe02(%rip),%rdi
mov    %r14,%rsi
mov    $0x1,%edx
call   1050 <dump_jreg@plt>
lea    0xdfc(%rip),%rdi
movsd  (%rsp),%xmm0
mov    $0x1,%al
call   1070 <printf@plt>
mov    %r14,%rdi
call   1030 <free@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe02(%rip),%rdi
jmp    11d6 <process_instruction+0x56>

LLVM IR (-O0):
@.str = [23 x i8] c"Shifting instruction.\0A\00"
@.str.1 = [45 x i8] c"ERROR: no instruction register value found.\0A\00"
@.str.2 = [42 x i8] c"ERROR: instruction value 0x%lx too large\0A\00"
@.str.3 = [48 x i8] c"ERROR: malloc for instruction register failed.\0A\00"
@.str.4 = [14 x i8] c" shifting in\00"
@.str.5 = [14 x i8] c" shifted out\00"
@.str.6 = [24 x i8] c" time taken: %.12fs\0A\00"
define i32 @process_instruction(i32 %0, i32 %1, i8** %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8**
%8 = alloca i64
%9 = alloca i8*
%10 = alloca double
store i32 %0, i32* %5
store i32 %1, i32* %6
store i8** %2, i8*** %7
%11 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
%12 = load i32, i32* %5
%13 = load i32, i32* %6
%14 = icmp sge i32 %12, %13
br i1 %14, label %15, label %17
15: ; preds = %3
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.1, i64 0, i64 0))
store i32 0, i32* %4
br label %52
17: ; preds = %3
%18 = load i8**, i8*** %7
%19 = load i32, i32* %5
%20 = sext i32 %19 to i64
%21 = getelementptr inbounds i8*, i8** %18, i64 %20
%22 = load i8*, i8** %21
%23 = call i64 @strtoul(i8* %22, i32* null, i32 16)
store i64 %23, i64* %8
%24 = load i64, i64* %8
%25 = icmp ugt i64 %24, 15
br i1 %25, label %26, label %29
26: ; preds = %17
%27 = load i64, i64* %8
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.2, i64 0, i64 0), i64 %27)
store i32 0, i32* %4
br label %52
29: ; preds = %17
%30 = load i64, i64* %8
%31 = call i64 @reverse_bits(i64 %30, i32 4)
store i64 %31, i64* %8
%32 = call i8* @malloc(i32 1)
store i8* %32, i8** %9
%33 = load i8*, i8** %9
%34 = icmp eq i8* null, %33
br i1 %34, label %35, label %37
35: ; preds = %29
%36 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.3, i64 0, i64 0))
store i32 0, i32* %4
br label %52
37: ; preds = %29
%38 = load i64, i64* %8
%39 = trunc i64 %38 to i8
%40 = load i8*, i8** %9
%41 = getelementptr inbounds i8, i8* %40, i64 0
store i8 %39, i8* %41
%42 = load i8*, i8** %9
%43 = call i32 @dump_jreg(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0), i8* %42, i32 1)
%44 = load i8*, i8** %9
%45 = call double @or1ksim_jtag_shift_ir(i8* %44, i32 4)
store double %45, double* %10
%46 = load i8*, i8** %9
%47 = call i32 @dump_jreg(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.5, i64 0, i64 0), i8* %46, i32 1)
%48 = load double, double* %10
%49 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.6, i64 0, i64 0), double %48)
%50 = load i8*, i8** %9
%51 = call i32 @free(i8* %50)
store i32 1, i32* %4
br label %52
52: ; preds = %37, %35, %26, %15
%53 = load i32, i32* %4
ret i32 %53
}

LLVM IR (-O2):
@.str.2 = [42 x i8] c"ERROR: instruction value 0x%lx too large\0A\00"
@.str.4 = [14 x i8] c" shifting in\00"
@.str.5 = [14 x i8] c" shifted out\00"
@.str.6 = [24 x i8] c" time taken: %.12fs\0A\00"
@str = [22 x i8] c"Shifting instruction.\00"
@str.7 = [47 x i8] c"ERROR: malloc for instruction register failed.\00"
@str.8 = [44 x i8] c"ERROR: no instruction register value found.\00"
define i32 @process_instruction(i32 %0, i32 %1, i8** nocapture %2) {
%4 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @str, i64 0, i64 0))
%5 = icmp slt i32 %0, %1
br i1 %5, label %8, label %6
6: ; preds = %3
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([44 x i8], [44 x i8]* @str.8, i64 0, i64 0))
br label %29
8: ; preds = %3
%9 = sext i32 %0 to i64
%10 = getelementptr inbounds i8*, i8** %2, i64 %9
%11 = load i8*, i8** %10
%12 = tail call i64 @strtoul(i8* nocapture %11, i32* null, i32 16)
%13 = icmp ugt i64 %12, 15
br i1 %13, label %14, label %16
14: ; preds = %8
%15 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([42 x i8], [42 x i8]* @.str.2, i64 0, i64 0), i64 %12)
br label %29
16: ; preds = %8
%17 = tail call i64 @reverse_bits(i64 %12, i32 4)
%18 = tail call dereferenceable_or_null(1) i8* @malloc(i32 1)
%19 = icmp eq i8* %18, null
br i1 %19, label %20, label %22
20: ; preds = %16
%21 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([47 x i8], [47 x i8]* @str.7, i64 0, i64 0))
br label %29
22: ; preds = %16
%23 = trunc i64 %17 to i8
store i8 %23, i8* %18
%24 = tail call i32 @dump_jreg(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0), i8* %18, i32 1)
%25 = tail call double @or1ksim_jtag_shift_ir(i8* %18, i32 4)
%26 = tail call i32 @dump_jreg(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.5, i64 0, i64 0), i8* %18, i32 1)
%27 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.6, i64 0, i64 0), double %25)
%28 = tail call i32 @free(i8* %18)
br label %29
29: ; preds = %14, %22, %20, %6
%30 = phi i32 [ 0, %6 ], [ 0, %14 ], [ 0, %20 ], [ 1, %22 ]
ret i32 %30
}