#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*/ O_RDWR;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ getchar();
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ sprintf(char *, char *, int);
int /*<<< orphan*/ testKbuf(int);
int /*<<< orphan*/ testKmmap(int);
int /*<<< orphan*/ testMmapArea(int);
int /*<<< orphan*/ testMmapMode(int);
int /*<<< orphan*/ testPCIconfig(int);
int /*<<< orphan*/ testPCIinfo(int);
int /*<<< orphan*/ testPCImmap(int);
int /*<<< orphan*/ testUmap(int);
int main() {
  int handle;
  char temp[50];
  int i, ret;
  for (i = 0; i < 4; i++) {
    printf("Trying device %d...", i);
    sprintf(temp, "/dev/fpga%d", i);
    handle = open(temp, O_RDWR);
    if (handle < 0) {
      printf("failed.\n");
      continue;
    }
    printf(" found.\n");
    printf(" Handle: %x\n", handle);
    testMmapMode(handle);
    testMmapArea(handle);
    testPCIinfo(handle);
    testPCIconfig(handle);
    testPCImmap(handle);
    testKbuf(handle);
    testKmmap(handle);
    testUmap(handle);
    printf("Press any key...\n");
    getchar();
    close(handle);
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x44(%rbp)
cmpl   $0x4,-0x44(%rbp)
jge    12d0 <main+0x100>
mov    -0x44(%rbp),%esi
lea    0xe06(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    -0x40(%rbp),%rdi
mov    -0x44(%rbp),%edx
lea    0xe05(%rip),%rsi
call   10f0 <sprintf@plt>
lea    -0x40(%rbp),%rdi
mov    0x2dc9(%rip),%rax
mov    (%rax),%esi
call   10d0 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    1246 <main+0x76>
lea    0xde6(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    12c2 <main+0xf2>
lea    0xddc(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x8(%rbp),%esi
lea    0xdd4(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x8(%rbp),%edi
call   1040 <testMmapMode@plt>
mov    -0x8(%rbp),%edi
call   10b0 <testMmapArea@plt>
mov    -0x8(%rbp),%edi
call   1090 <testPCIinfo@plt>
mov    -0x8(%rbp),%edi
call   10c0 <testPCIconfig@plt>
mov    -0x8(%rbp),%edi
call   10e0 <testPCImmap@plt>
mov    -0x8(%rbp),%edi
call   1050 <testKbuf@plt>
mov    -0x8(%rbp),%edi
call   10a0 <testKmmap@plt>
mov    -0x8(%rbp),%edi
call   1080 <testUmap@plt>
lea    0xd93(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0x0,%al
call   1070 <getchar@plt>
mov    -0x8(%rbp),%edi
call   1060 <close@plt>
mov    -0x44(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x44(%rbp)
jmp    11e6 <main+0x16>
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
lea    0xe0b(%rip),%r14
lea    0xe18(%rip),%r15
mov    %rsp,%rbx
mov    0x2de2(%rip),%r12
xor    %r13d,%r13d
jmp    128a <main+0xaa>
nopl   0x0(%rax,%rax,1)
mov    %eax,%ebp
lea    0xe14(%rip),%rdi
call   1030 <puts@plt>
lea    0xdfb(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %ebp,%edi
call   1050 <testMmapMode@plt>
mov    %ebp,%edi
call   10c0 <testMmapArea@plt>
mov    %ebp,%edi
call   10a0 <testPCIinfo@plt>
mov    %ebp,%edi
call   10d0 <testPCIconfig@plt>
mov    %ebp,%edi
call   10f0 <testPCImmap@plt>
mov    %ebp,%edi
call   1060 <testKbuf@plt>
mov    %ebp,%edi
call   10b0 <testKmmap@plt>
mov    %ebp,%edi
call   1090 <testUmap@plt>
lea    0xdc8(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
call   1080 <getchar@plt>
mov    %ebp,%edi
call   1070 <close@plt>
add    $0x1,%r13d
cmp    $0x4,%r13d
je     12c7 <main+0xe7>
mov    %r14,%rdi
mov    %r13d,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %r13d,%edx
call   1100 <sprintf@plt>
mov    (%r12),%esi
mov    %rbx,%rdi
call   10e0 <open@plt>
test   %eax,%eax
jns    1210 <main+0x30>
lea    0xd86(%rip),%rdi
call   1030 <puts@plt>
jmp    1280 <main+0xa0>
xor    %eax,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [20 x i8] c"Trying device %d...\00"
@.str.1 = [12 x i8] c"/dev/fpga%d\00"
@O_RDWR = global i32 0
@.str.2 = [9 x i8] c"failed.\0A\00"
@.str.3 = [9 x i8] c" found.\0A\00"
@.str.4 = [13 x i8] c" Handle: %x\0A\00"
@.str.5 = [18 x i8] c"Press any key...\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca [50 x i8]
%4 = alloca i32
%5 = alloca i32
store i32 0, i32* %1
store i32 0, i32* %4
br label %6
6: ; preds = %46, %0
%7 = load i32, i32* %4
%8 = icmp slt i32 %7, 4
br i1 %8, label %9, label %49
9: ; preds = %6
%10 = load i32, i32* %4
%11 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i32 %10)
%12 = getelementptr inbounds [50 x i8], [50 x i8]* %3, i64 0, i64 0
%13 = load i32, i32* %4
%14 = call i32 @sprintf(i8* %12, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 %13)
%15 = getelementptr inbounds [50 x i8], [50 x i8]* %3, i64 0, i64 0
%16 = load i32, i32* @O_RDWR
%17 = call i32 @open(i8* %15, i32 %16)
store i32 %17, i32* %2
%18 = load i32, i32* %2
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %9
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0))
br label %46
22: ; preds = %9
%23 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0))
%24 = load i32, i32* %2
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0), i32 %24)
%26 = load i32, i32* %2
%27 = call i32 @testMmapMode(i32 %26)
%28 = load i32, i32* %2
%29 = call i32 @testMmapArea(i32 %28)
%30 = load i32, i32* %2
%31 = call i32 @testPCIinfo(i32 %30)
%32 = load i32, i32* %2
%33 = call i32 @testPCIconfig(i32 %32)
%34 = load i32, i32* %2
%35 = call i32 @testPCImmap(i32 %34)
%36 = load i32, i32* %2
%37 = call i32 @testKbuf(i32 %36)
%38 = load i32, i32* %2
%39 = call i32 @testKmmap(i32 %38)
%40 = load i32, i32* %2
%41 = call i32 @testUmap(i32 %40)
%42 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.5, i64 0, i64 0))
%43 = call i32 (...) @getchar()
%44 = load i32, i32* %2
%45 = call i32 @close(i32 %44)
br label %46
46: ; preds = %22, %20
%47 = load i32, i32* %4
%48 = add nsw i32 %47, 1
store i32 %48, i32* %4
br label %6
49: ; preds = %6
%50 = load i32, i32* %1
ret i32 %50
}

LLVM IR (-O2):
@.str = [20 x i8] c"Trying device %d...\00"
@.str.1 = [12 x i8] c"/dev/fpga%d\00"
@O_RDWR = global i32 0
@.str.4 = [13 x i8] c" Handle: %x\0A\00"
@str = [8 x i8] c" found.\00"
@str.6 = [17 x i8] c"Press any key...\00"
@str.7 = [8 x i8] c"failed.\00"
define i32 @main() {
%1 = alloca [50 x i8]
%2 = getelementptr inbounds [50 x i8], [50 x i8]* %1, i64 0, i64 0
br label %3
3: ; preds = %0, %26
%4 = phi i32 [ 0, %0 ], [ %27, %26 ]
%5 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i32 %4)
%6 = call i32 @sprintf(i8* %2, i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 %4)
%7 = load i32, i32* @O_RDWR
%8 = call i32 @open(i8* %2, i32 %7)
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %3
%11 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @str.7, i64 0, i64 0))
br label %26
12: ; preds = %3
%13 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @str, i64 0, i64 0))
%14 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0), i32 %8)
%15 = tail call i32 @testMmapMode(i32 %8)
%16 = tail call i32 @testMmapArea(i32 %8)
%17 = tail call i32 @testPCIinfo(i32 %8)
%18 = tail call i32 @testPCIconfig(i32 %8)
%19 = tail call i32 @testPCImmap(i32 %8)
%20 = tail call i32 @testKbuf(i32 %8)
%21 = tail call i32 @testKmmap(i32 %8)
%22 = tail call i32 @testUmap(i32 %8)
%23 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @str.6, i64 0, i64 0))
%24 = tail call i32 (...) @getchar()
%25 = tail call i32 @close(i32 %8)
br label %26
26: ; preds = %12, %10
%27 = add nuw nsw i32 %4, 1
%28 = icmp eq i32 %27, 4
br i1 %28, label %29, label %3
29: ; preds = %26
ret i32 0
}