#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*/ fflush(int /*<<< orphan*/);
int /*<<< orphan*/ free(void *);
int /*<<< orphan*/ ioctl_dump(int, char *);
int ioctl_reap_inquiry(int, int /*<<< orphan*/);
int /*<<< orphan*/ ioctl_send_identify_cmd(int, void *);
int /*<<< orphan*/ ioctl_tst_ring_dbl(int, int /*<<< orphan*/);
scalar_t__ posix_memalign(void **, int, int);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ scanf(char *, int *);
int /*<<< orphan*/ stdout;
void test_loop_irq(int fd) {
  int i, cmds, num;
  void *rd_buffer;
  if (posix_memalign(&rd_buffer, 4096, (2 * 4096))) {
    printf("Memalign Failed");
    return;
  }
  num = ioctl_reap_inquiry(fd, 0);
  printf("\nEnter no of commands in ACQ:");
  fflush(stdout);
  scanf("%d", &cmds);
  for (i = 0; i < cmds; i++) {
    ioctl_send_identify_cmd(fd, rd_buffer);
  }
  ioctl_tst_ring_dbl(fd, 0);
  while (ioctl_reap_inquiry(fd, 0) != (num + cmds))
    ;
  ioctl_dump(fd, "/tmp/irq_loop_test.txt");
  free(rd_buffer);
}


x86 assembly (clang, -O0):
<test_loop_irq>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
lea    -0x18(%rbp),%rdi
mov    $0x1000,%esi
mov    $0x2000,%edx
call   10b0 <posix_memalign@plt>
cmp    $0x0,%rax
je     11c9 <test_loop_irq+0x39>
lea    0xe41(%rip),%rdi
call   1050 <printf@plt>
jmp    126c <test_loop_irq+0xdc>
mov    -0x4(%rbp),%edi
xor    %esi,%esi
call   1040 <ioctl_reap_inquiry@plt>
mov    %eax,-0x10(%rbp)
lea    0xe33(%rip),%rdi
call   1050 <printf@plt>
mov    0x2df7(%rip),%rax
mov    (%rax),%edi
call   1080 <fflush@plt>
lea    0xe37(%rip),%rdi
lea    -0xc(%rbp),%rsi
call   1090 <scanf@plt>
movl   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    122d <test_loop_irq+0x9d>
mov    -0x4(%rbp),%edi
mov    -0x18(%rbp),%rsi
call   1060 <ioctl_send_identify_cmd@plt>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    1207 <test_loop_irq+0x77>
mov    -0x4(%rbp),%edi
xor    %esi,%esi
call   10a0 <ioctl_tst_ring_dbl@plt>
mov    -0x4(%rbp),%edi
xor    %esi,%esi
call   1040 <ioctl_reap_inquiry@plt>
mov    -0x10(%rbp),%ecx
add    -0xc(%rbp),%ecx
cmp    %ecx,%eax
je     1254 <test_loop_irq+0xc4>
jmp    1237 <test_loop_irq+0xa7>
mov    -0x4(%rbp),%edi
lea    0xdd3(%rip),%rsi
call   1070 <ioctl_dump@plt>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_loop_irq>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %edi,%ebx
lea    0x8(%rsp),%rdi
mov    $0x1000,%esi
mov    $0x2000,%edx
call   10b0 <posix_memalign@plt>
test   %rax,%rax
je     11c4 <test_loop_irq+0x34>
lea    0xe46(%rip),%rdi
call   1050 <printf@plt>
jmp    125c <test_loop_irq+0xcc>
xor    %ebp,%ebp
mov    %ebx,%edi
xor    %esi,%esi
call   1040 <ioctl_reap_inquiry@plt>
mov    %eax,%r14d
lea    0xe37(%rip),%rdi
call   1050 <printf@plt>
mov    0x2dfb(%rip),%rax
mov    (%rax),%edi
call   1080 <fflush@plt>
lea    0xe3b(%rip),%rdi
lea    0x4(%rsp),%rsi
call   1090 <scanf@plt>
cmpl   $0x0,0x4(%rsp)
jle    1225 <test_loop_irq+0x95>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    0x8(%rsp),%rsi
mov    %ebx,%edi
call   1060 <ioctl_send_identify_cmd@plt>
add    $0x1,%ebp
cmp    0x4(%rsp),%ebp
jl     1210 <test_loop_irq+0x80>
mov    %ebx,%edi
xor    %esi,%esi
call   10a0 <ioctl_tst_ring_dbl@plt>
xchg   %ax,%ax
mov    %ebx,%edi
xor    %esi,%esi
call   1040 <ioctl_reap_inquiry@plt>
mov    0x4(%rsp),%ecx
add    %r14d,%ecx
cmp    %ecx,%eax
jne    1230 <test_loop_irq+0xa0>
lea    0xde6(%rip),%rsi
mov    %ebx,%edi
call   1070 <ioctl_dump@plt>
mov    0x8(%rsp),%rdi
call   1030 <free@plt>
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [16 x i8] c"Memalign Failed\00"
@.str.1 = [30 x i8] c"\0AEnter no of commands in ACQ:\00"
@stdout = global i32 0
@.str.2 = [3 x i8] c"%d\00"
@.str.3 = [23 x i8] c"/tmp/irq_loop_test.txt\00"
define void @test_loop_irq(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
store i32 %0, i32* %2
%7 = call i64 @posix_memalign(i8** %6, i32 4096, i32 8192)
%8 = icmp ne i64 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %1
%10 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
br label %45
11: ; preds = %1
%12 = load i32, i32* %2
%13 = call i32 @ioctl_reap_inquiry(i32 %12, i32 0)
store i32 %13, i32* %5
%14 = call i32 @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0))
%15 = load i32, i32* @stdout
%16 = call i32 @fflush(i32 %15)
%17 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %4)
store i32 0, i32* %3
br label %18
18: ; preds = %26, %11
%19 = load i32, i32* %3
%20 = load i32, i32* %4
%21 = icmp slt i32 %19, %20
br i1 %21, label %22, label %29
22: ; preds = %18
%23 = load i32, i32* %2
%24 = load i8*, i8** %6
%25 = call i32 @ioctl_send_identify_cmd(i32 %23, i8* %24)
br label %26
26: ; preds = %22
%27 = load i32, i32* %3
%28 = add nsw i32 %27, 1
store i32 %28, i32* %3
br label %18
29: ; preds = %18
%30 = load i32, i32* %2
%31 = call i32 @ioctl_tst_ring_dbl(i32 %30, i32 0)
br label %32
32: ; preds = %39, %29
%33 = load i32, i32* %2
%34 = call i32 @ioctl_reap_inquiry(i32 %33, i32 0)
%35 = load i32, i32* %5
%36 = load i32, i32* %4
%37 = add nsw i32 %35, %36
%38 = icmp ne i32 %34, %37
br i1 %38, label %39, label %40
39: ; preds = %32
br label %32
40: ; preds = %32
%41 = load i32, i32* %2
%42 = call i32 @ioctl_dump(i32 %41, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0))
%43 = load i8*, i8** %6
%44 = call i32 @free(i8* %43)
br label %45
45: ; preds = %40, %9
ret void
}

LLVM IR (-O2):
@.str = [16 x i8] c"Memalign Failed\00"
@.str.1 = [30 x i8] c"\0AEnter no of commands in ACQ:\00"
@stdout = global i32 0
@.str.2 = [3 x i8] c"%d\00"
@.str.3 = [23 x i8] c"/tmp/irq_loop_test.txt\00"
define void @test_loop_irq(i32 %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = bitcast i32* %2 to i8*
%5 = bitcast i8** %3 to i8*
%6 = call i64 @posix_memalign(i8** %3, i32 4096, i32 8192)
%7 = icmp eq i64 %6, 0
br i1 %7, label %10, label %8
8: ; preds = %1
%9 = call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
br label %36
10: ; preds = %1
%11 = call i32 @ioctl_reap_inquiry(i32 %0, i32 0)
%12 = call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0))
%13 = load i32, i32* @stdout
%14 = call i32 @fflush(i32 %13)
%15 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %2)
%16 = load i32, i32* %2
%17 = icmp sgt i32 %16, 0
br i1 %17, label %18, label %25
18: ; preds = %10, %18
%19 = phi i32 [ %22, %18 ], [ 0, %10 ]
%20 = load i8*, i8** %3
%21 = call i32 @ioctl_send_identify_cmd(i32 %0, i8* %20)
%22 = add nuw nsw i32 %19, 1
%23 = load i32, i32* %2
%24 = icmp slt i32 %22, %23
br i1 %24, label %18, label %25
25: ; preds = %18, %10
%26 = call i32 @ioctl_tst_ring_dbl(i32 %0, i32 0)
br label %27
27: ; preds = %27, %25
%28 = call i32 @ioctl_reap_inquiry(i32 %0, i32 0)
%29 = load i32, i32* %2
%30 = add nsw i32 %29, %11
%31 = icmp eq i32 %28, %30
br i1 %31, label %32, label %27
32: ; preds = %27
%33 = call i32 @ioctl_dump(i32 %0, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0))
%34 = load i8*, i8** %3
%35 = call i32 @free(i8* %34)
br label %36
36: ; preds = %32, %8
ret void
}