#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 inb(unsigned int);
int get_dma_residue(unsigned int dmanr) {
  unsigned int io_port;
  unsigned short count;
  io_port = (dmanr <= 3) ? ((dmanr & 3) << 1) + 1 + 0x00
                         : ((dmanr & 3) << 2) + 2 + 0xC0;
  count = 1 + inb(io_port);
  count += inb(io_port) << 8;
  return (dmanr <= 3) ? count : (count << 1);
}


x86 assembly (clang, -O0):
<get_dma_residue>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
cmpl   $0x3,-0x4(%rbp)
ja     113c <get_dma_residue+0x2c>
mov    -0x4(%rbp),%eax
and    $0x3,%eax
shl    $0x1,%eax
add    $0x1,%eax
add    $0x0,%eax
mov    %eax,-0x10(%rbp)
jmp    1150 <get_dma_residue+0x40>
mov    -0x4(%rbp),%eax
and    $0x3,%eax
shl    $0x2,%eax
add    $0x2,%eax
add    $0xc0,%eax
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%eax
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%edi
call   1030 <inb@plt>
add    $0x1,%eax
mov    %ax,-0xa(%rbp)
mov    -0x8(%rbp),%edi
call   1030 <inb@plt>
mov    %eax,%ecx
shl    $0x8,%ecx
movzwl -0xa(%rbp),%eax
add    %ecx,%eax
mov    %ax,-0xa(%rbp)
cmpl   $0x3,-0x4(%rbp)
ja     1192 <get_dma_residue+0x82>
movzwl -0xa(%rbp),%eax
mov    %eax,-0x14(%rbp)
jmp    119c <get_dma_residue+0x8c>
movzwl -0xa(%rbp),%eax
shl    $0x1,%eax
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_dma_residue>:
push   %rbp
push   %r14
push   %rbx
mov    %edi,%ebx
cmp    $0x4,%edi
mov    $0x6,%eax
mov    $0xc,%edx
cmovb  %eax,%edx
mov    $0x1,%eax
mov    $0xc2,%esi
cmovb  %eax,%esi
setae  %r14b
mov    $0x2,%cl
sbb    $0x0,%cl
shl    %cl,%ebx
and    %edx,%ebx
or     %esi,%ebx
mov    %ebx,%edi
call   1030 <inb@plt>
mov    %eax,%ebp
mov    %ebx,%edi
call   1030 <inb@plt>
shl    $0x8,%eax
add    %ebp,%eax
add    $0x1,%eax
movzwl %ax,%eax
mov    %r14d,%ecx
shl    %cl,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
define i32 @get_dma_residue(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i16
store i32 %0, i32* %2
%5 = load i32, i32* %2
%6 = icmp ule i32 %5, 3
br i1 %6, label %7, label %13
7: ; preds = %1
%8 = load i32, i32* %2
%9 = and i32 %8, 3
%10 = shl i32 %9, 1
%11 = add i32 %10, 1
%12 = add i32 %11, 0
br label %19
13: ; preds = %1
%14 = load i32, i32* %2
%15 = and i32 %14, 3
%16 = shl i32 %15, 2
%17 = add i32 %16, 2
%18 = add i32 %17, 192
br label %19
19: ; preds = %13, %7
%20 = phi i32 [ %12, %7 ], [ %18, %13 ]
store i32 %20, i32* %3
%21 = load i32, i32* %3
%22 = call i32 @inb(i32 %21)
%23 = add nsw i32 1, %22
%24 = trunc i32 %23 to i16
store i16 %24, i16* %4
%25 = load i32, i32* %3
%26 = call i32 @inb(i32 %25)
%27 = shl i32 %26, 8
%28 = load i16, i16* %4
%29 = zext i16 %28 to i32
%30 = add nsw i32 %29, %27
%31 = trunc i32 %30 to i16
store i16 %31, i16* %4
%32 = load i32, i32* %2
%33 = icmp ule i32 %32, 3
br i1 %33, label %34, label %37
34: ; preds = %19
%35 = load i16, i16* %4
%36 = zext i16 %35 to i32
br label %41
37: ; preds = %19
%38 = load i16, i16* %4
%39 = zext i16 %38 to i32
%40 = shl i32 %39, 1
br label %41
41: ; preds = %37, %34
%42 = phi i32 [ %36, %34 ], [ %40, %37 ]
ret i32 %42
}

LLVM IR (-O2):
define i32 @get_dma_residue(i32 %0) {
%2 = icmp ult i32 %0, 4
%3 = select i1 %2, i32 1, i32 2
%4 = select i1 %2, i32 6, i32 12
%5 = select i1 %2, i32 1, i32 194
%6 = shl i32 %0, %3
%7 = and i32 %6, %4
%8 = or i32 %7, %5
%9 = tail call i32 @inb(i32 %8)
%10 = add i32 %9, 1
%11 = tail call i32 @inb(i32 %8)
%12 = shl i32 %11, 8
%13 = add i32 %10, %12
%14 = and i32 %13, 65535
%15 = xor i1 %2, true
%16 = zext i1 %15 to i32
%17 = shl nuw nsw i32 %14, %16
ret i32 %17
}