#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*/ printf(char *, ...);
int /*<<< orphan*/ scanf(char *, unsigned int *);
main(void) {
  unsigned int val, bitNum, noOfBits, maskVal = 0x0, exVal;
  int ii;
  printf("Enter an unsingned integer in Hex format\n");
  scanf("%x", &val);
  printf("Enter no of bits to be extracted\n");
  scanf("%d", &noOfBits);
  printf("Enter starting bit number\n");
  scanf("%d", &bitNum);
  for (ii = 0; ii < noOfBits; ii++) {
    if (ii)
      maskVal = maskVal << 1;
    maskVal = maskVal | 0x00000001;
  }
  val = val >> bitNum;
  exVal = val & maskVal;
  printf("value = %x\n", exVal);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x14(%rbp)
lea    0xec3(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xedf(%rip),%rdi
lea    -0x8(%rbp),%rsi
call   1040 <scanf@plt>
lea    0xed2(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xee6(%rip),%rdi
lea    -0x10(%rbp),%rsi
call   1040 <scanf@plt>
lea    0xed9(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xec8(%rip),%rdi
lea    -0xc(%rbp),%rsi
call   1040 <scanf@plt>
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x10(%rbp),%eax
jae    11cd <main+0xad>
cmpl   $0x0,-0x1c(%rbp)
je     11b6 <main+0x96>
mov    -0x14(%rbp),%eax
shl    $0x1,%eax
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
or     $0x1,%eax
mov    %eax,-0x14(%rbp)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1197 <main+0x77>
mov    -0x8(%rbp),%eax
mov    -0xc(%rbp),%ecx
shr    %cl,%eax
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%eax
and    -0x14(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%esi
lea    0xe82(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x10,%rsp
lea    0xed6(%rip),%rdi
call   1030 <puts@plt>
lea    0xeb8(%rip),%rdi
lea    0x4(%rsp),%rsi
call   1050 <scanf@plt>
lea    0xee2(%rip),%rdi
call   1030 <puts@plt>
lea    0xe9e(%rip),%rbx
lea    0x8(%rsp),%rsi
mov    %rbx,%rdi
call   1050 <scanf@plt>
lea    0xee3(%rip),%rdi
call   1030 <puts@plt>
lea    0xc(%rsp),%rsi
mov    %rbx,%rdi
call   1050 <scanf@plt>
mov    0x8(%rsp),%edi
test   %edi,%edi
je     11a6 <main+0x76>
lea    -0x1(%rdi),%ecx
mov    %edi,%eax
and    $0x3,%eax
cmp    $0x3,%ecx
jae    11aa <main+0x7a>
xor    %esi,%esi
xor    %edx,%edx
jmp    11d4 <main+0xa4>
xor    %edx,%edx
jmp    11f1 <main+0xc1>
and    $0xfffffffc,%edi
xor    %esi,%esi
xor    %edx,%edx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
test   %esi,%esi
setne  %cl
shl    %cl,%edx
shl    $0x3,%edx
or     $0xf,%edx
add    $0x4,%esi
cmp    %esi,%edi
jne    11c0 <main+0x90>
test   %eax,%eax
je     11f1 <main+0xc1>
neg    %eax
xor    %edi,%edi
nopl   0x0(%rax)
cmp    %edi,%esi
setne  %cl
shl    %cl,%edx
or     $0x1,%edx
add    $0xffffffff,%edi
cmp    %edi,%eax
jne    11e0 <main+0xb0>
mov    0x4(%rsp),%esi
mov    0xc(%rsp),%cl
shr    %cl,%esi
mov    %esi,0x4(%rsp)
and    %edx,%esi
lea    0xdfe(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@.str = [42 x i8] c"Enter an unsingned integer in Hex format\0A\00"
@.str.1 = [3 x i8] c"%x\00"
@.str.2 = [34 x i8] c"Enter no of bits to be extracted\0A\00"
@.str.3 = [3 x i8] c"%d\00"
@.str.4 = [27 x i8] c"Enter starting bit number\0A\00"
@.str.5 = [12 x i8] c"value = %x\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i32 0, i32* %1
store i32 0, i32* %5
%8 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str, i64 0, i64 0))
%9 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %2)
%10 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0))
%11 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32* %4)
%12 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.4, i64 0, i64 0))
%13 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32* %3)
store i32 0, i32* %7
br label %14
14: ; preds = %27, %0
%15 = load i32, i32* %7
%16 = load i32, i32* %4
%17 = icmp ult i32 %15, %16
br i1 %17, label %18, label %30
18: ; preds = %14
%19 = load i32, i32* %7
%20 = icmp ne i32 %19, 0
br i1 %20, label %21, label %24
21: ; preds = %18
%22 = load i32, i32* %5
%23 = shl i32 %22, 1
store i32 %23, i32* %5
br label %24
24: ; preds = %21, %18
%25 = load i32, i32* %5
%26 = or i32 %25, 1
store i32 %26, i32* %5
br label %27
27: ; preds = %24
%28 = load i32, i32* %7
%29 = add nsw i32 %28, 1
store i32 %29, i32* %7
br label %14
30: ; preds = %14
%31 = load i32, i32* %2
%32 = load i32, i32* %3
%33 = lshr i32 %31, %32
store i32 %33, i32* %2
%34 = load i32, i32* %2
%35 = load i32, i32* %5
%36 = and i32 %34, %35
store i32 %36, i32* %6
%37 = load i32, i32* %6
%38 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.5, i64 0, i64 0), i32 %37)
%39 = load i32, i32* %1
ret i32 %39
}

LLVM IR (-O2):
@.str.1 = [3 x i8] c"%x\00"
@.str.3 = [3 x i8] c"%d\00"
@.str.5 = [12 x i8] c"value = %x\0A\00"
@str = [41 x i8] c"Enter an unsingned integer in Hex format\00"
@str.6 = [33 x i8] c"Enter no of bits to be extracted\00"
@str.7 = [26 x i8] c"Enter starting bit number\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = bitcast i32* %1 to i8*
%5 = bitcast i32* %2 to i8*
%6 = bitcast i32* %3 to i8*
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([41 x i8], [41 x i8]* @str, i64 0, i64 0))
%8 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %1)
%9 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @str.6, i64 0, i64 0))
%10 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32* %3)
%11 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @str.7, i64 0, i64 0))
%12 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32* %2)
%13 = load i32, i32* %3
%14 = icmp eq i32 %13, 0
br i1 %14, label %49, label %15
15: ; preds = %0
%16 = add i32 %13, -1
%17 = and i32 %13, 3
%18 = icmp ult i32 %16, 3
br i1 %18, label %33, label %19
19: ; preds = %15
%20 = and i32 %13, -4
br label %21
21: ; preds = %21, %19
%22 = phi i32 [ 0, %19 ], [ %30, %21 ]
%23 = phi i32 [ 0, %19 ], [ %29, %21 ]
%24 = phi i32 [ 0, %19 ], [ %31, %21 ]
%25 = icmp ne i32 %22, 0
%26 = zext i1 %25 to i32
%27 = shl i32 %23, %26
%28 = shl i32 %27, 3
%29 = or i32 %28, 15
%30 = add nuw nsw i32 %22, 4
%31 = add i32 %24, 4
%32 = icmp eq i32 %31, %20
br i1 %32, label %33, label %21
33: ; preds = %21, %15
%34 = phi i32 [ undef, %15 ], [ %29, %21 ]
%35 = phi i32 [ 0, %15 ], [ %30, %21 ]
%36 = phi i32 [ 0, %15 ], [ %29, %21 ]
%37 = icmp eq i32 %17, 0
br i1 %37, label %49, label %38
38: ; preds = %33, %38
%39 = phi i32 [ %46, %38 ], [ %35, %33 ]
%40 = phi i32 [ %45, %38 ], [ %36, %33 ]
%41 = phi i32 [ %47, %38 ], [ 0, %33 ]
%42 = icmp ne i32 %39, 0
%43 = zext i1 %42 to i32
%44 = shl i32 %40, %43
%45 = or i32 %44, 1
%46 = add nuw nsw i32 %39, 1
%47 = add i32 %41, 1
%48 = icmp eq i32 %47, %17
br i1 %48, label %49, label %38
49: ; preds = %33, %38, %0
%50 = phi i32 [ 0, %0 ], [ %34, %33 ], [ %45, %38 ]
%51 = load i32, i32* %1
%52 = load i32, i32* %2
%53 = lshr i32 %51, %52
store i32 %53, i32* %1
%54 = and i32 %53, %50
%55 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.5, i64 0, i64 0), i32 %54)
ret i32 0
}