#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 */
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
scalar_t__ EOF;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, int, int, int, int);
scalar_t__ fscanf(int /*<<< orphan*/ *, char *, int *, int *, int *);
int getbits(int, int, int);
int /*<<< orphan*/ perror(char *);
int main(int argc, char *argv[]) {
  FILE *in, *out;
  in = fopen("input.dat", "r");
  if (in == NULL) {
    perror("input.dat");
    return 1;
  }
  out = fopen("output.dat", "w");
  if (out == NULL) {
    perror("output.dat");
    return 1;
  }
  int x, p, n;
  int y;
  while (fscanf(in, "%x %d %d", &x, &p, &n) != EOF) {
    y = getbits(x, p, n);
    fprintf(out, "%d-bit field of 0x%x at %d is %d\n", n, x, p, y);
  }
  fclose(in);
  fclose(out);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xe83(%rip),%rdi
lea    0xe86(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11b0 <main+0x50>
lea    0xe61(%rip),%rdi
call   1080 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1268 <main+0x108>
lea    0xe55(%rip),%rdi
lea    0xe59(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11ea <main+0x8a>
lea    0xe33(%rip),%rdi
call   1080 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1268 <main+0x108>
jmp    11ef <main+0x8f>
mov    -0x18(%rbp),%rdi
lea    0xe1f(%rip),%rsi
lea    -0x24(%rbp),%rdx
lea    -0x28(%rbp),%rcx
lea    -0x2c(%rbp),%r8
call   1040 <fscanf@plt>
mov    0x2dd6(%rip),%rcx
cmp    (%rcx),%rax
je     124f <main+0xef>
mov    -0x24(%rbp),%edi
mov    -0x28(%rbp),%esi
mov    -0x2c(%rbp),%edx
call   1050 <getbits@plt>
mov    %eax,-0x30(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x2c(%rbp),%edx
mov    -0x24(%rbp),%ecx
mov    -0x28(%rbp),%r8d
mov    -0x30(%rbp),%r9d
lea    0xddd(%rip),%rsi
call   1060 <fprintf@plt>
jmp    11ef <main+0x8f>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
lea    0xe8b(%rip),%rdi
lea    0xe8e(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     125f <main+0xff>
mov    %rax,%r14
lea    0xe78(%rip),%rdi
lea    0xe7c(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     1268 <main+0x108>
mov    %rax,%rbx
lea    0xe66(%rip),%rsi
lea    0x14(%rsp),%rdx
lea    0x10(%rsp),%rcx
lea    0xc(%rsp),%r8
mov    %r14,%rdi
call   1040 <fscanf@plt>
mov    0x2e17(%rip),%r12
cmp    (%r12),%rax
je     123e <main+0xde>
lea    0x14(%rsp),%r13
lea    0x10(%rsp),%rbp
lea    0xc(%rsp),%r15
cs nopw 0x0(%rax,%rax,1)
mov    0x14(%rsp),%edi
mov    0x10(%rsp),%esi
mov    0xc(%rsp),%edx
call   1050 <getbits@plt>
mov    0xc(%rsp),%edx
mov    0x14(%rsp),%ecx
mov    0x10(%rsp),%r8d
mov    %rbx,%rdi
lea    0xe0a(%rip),%rsi
mov    %eax,%r9d
call   1060 <fprintf@plt>
mov    %r14,%rdi
lea    0xdef(%rip),%rsi
mov    %r13,%rdx
mov    %rbp,%rcx
mov    %r15,%r8
call   1040 <fscanf@plt>
cmp    (%r12),%rax
jne    11f0 <main+0x90>
mov    %r14,%rdi
call   1030 <fclose@plt>
mov    %rbx,%rdi
call   1030 <fclose@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd9a(%rip),%rdi
jmp    126f <main+0x10f>
lea    0xd9d(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%eax
jmp    1250 <main+0xf0>

LLVM IR (-O0):
@.str = [10 x i8] c"input.dat\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [11 x i8] c"output.dat\00"
@.str.3 = [2 x i8] c"w\00"
@.str.4 = [9 x i8] c"%x %d %d\00"
@EOF = global i64 0
@.str.5 = [34 x i8] c"%d-bit field of 0x%x at %d is %d\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32*
%7 = alloca i32*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = call i32* @fopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %12, i32** %6
%13 = load i32*, i32** %6
%14 = icmp eq i32* %13, null
br i1 %14, label %15, label %17
15: ; preds = %2
%16 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %45
17: ; preds = %2
%18 = call i32* @fopen(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i32* %18, i32** %7
%19 = load i32*, i32** %7
%20 = icmp eq i32* %19, null
br i1 %20, label %21, label %23
21: ; preds = %17
%22 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %3
br label %45
23: ; preds = %17
br label %24
24: ; preds = %29, %23
%25 = load i32*, i32** %6
%26 = call i64 @fscanf(i32* %25, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0), i32* %8, i32* %9, i32* %10)
%27 = load i64, i64* @EOF
%28 = icmp ne i64 %26, %27
br i1 %28, label %29, label %40
29: ; preds = %24
%30 = load i32, i32* %8
%31 = load i32, i32* %9
%32 = load i32, i32* %10
%33 = call i32 @getbits(i32 %30, i32 %31, i32 %32)
store i32 %33, i32* %11
%34 = load i32*, i32** %7
%35 = load i32, i32* %10
%36 = load i32, i32* %8
%37 = load i32, i32* %9
%38 = load i32, i32* %11
%39 = call i32 @fprintf(i32* %34, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.5, i64 0, i64 0), i32 %35, i32 %36, i32 %37, i32 %38)
br label %24
40: ; preds = %24
%41 = load i32*, i32** %6
%42 = call i32 @fclose(i32* %41)
%43 = load i32*, i32** %7
%44 = call i32 @fclose(i32* %43)
store i32 0, i32* %3
br label %45
45: ; preds = %40, %21, %15
%46 = load i32, i32* %3
ret i32 %46
}

LLVM IR (-O2):
@.str = [10 x i8] c"input.dat\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [11 x i8] c"output.dat\00"
@.str.3 = [2 x i8] c"w\00"
@.str.4 = [9 x i8] c"%x %d %d\00"
@EOF = global i64 0
@.str.5 = [34 x i8] c"%d-bit field of 0x%x at %d is %d\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = tail call i32* @fopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%7 = icmp eq i32* %6, null
br i1 %7, label %8, label %10
8: ; preds = %2
%9 = tail call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
br label %37
10: ; preds = %2
%11 = tail call i32* @fopen(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%12 = icmp eq i32* %11, null
br i1 %12, label %13, label %15
13: ; preds = %10
%14 = tail call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
br label %37
15: ; preds = %10
%16 = bitcast i32* %3 to i8*
%17 = bitcast i32* %4 to i8*
%18 = bitcast i32* %5 to i8*
%19 = call i64 @fscanf(i32* %6, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0), i32* %3, i32* %4, i32* %5)
%20 = load i64, i64* @EOF
%21 = icmp eq i64 %19, %20
br i1 %21, label %34, label %22
22: ; preds = %15, %22
%23 = load i32, i32* %3
%24 = load i32, i32* %4
%25 = load i32, i32* %5
%26 = call i32 @getbits(i32 %23, i32 %24, i32 %25)
%27 = load i32, i32* %5
%28 = load i32, i32* %3
%29 = load i32, i32* %4
%30 = call i32 @fprintf(i32* %11, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.5, i64 0, i64 0), i32 %27, i32 %28, i32 %29, i32 %26)
%31 = call i64 @fscanf(i32* %6, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0), i32* %3, i32* %4, i32* %5)
%32 = load i64, i64* @EOF
%33 = icmp eq i64 %31, %32
br i1 %33, label %34, label %22
34: ; preds = %22, %15
%35 = call i32 @fclose(i32* %6)
%36 = call i32 @fclose(i32* %11)
br label %37
37: ; preds = %34, %13, %8
%38 = phi i32 [ 1, %8 ], [ 1, %13 ], [ 0, %34 ]
ret i32 %38
}