#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 */
void *MAP_FAILED;
int /*<<< orphan*/ MAP_SHARED;
int /*<<< orphan*/ O_RDWR;
int PROT_READ;
int PROT_WRITE;
int get_memory_size(char *);
int /*<<< orphan*/ gpio_write(void *, int, int);
void *mmap(int /*<<< orphan*/ *, int, int, int /*<<< orphan*/, int,
           int /*<<< orphan*/);
int /*<<< orphan*/ munmap(void *, int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ wait_for_interrupt(int, void *);
int main(int argc, char *argv[]) {
  int fd;
  char *uiod = "/dev/uio0";
  void *gpio_ptr;
  int gpio_size;
  int i;
  printf("GPIO UIO test.\n");
  fd = open(uiod, O_RDWR);
  if (fd < 1) {
    printf("Invalid UIO device file:%s.\n", uiod);
    return -1;
  }
  gpio_size = get_memory_size("/sys/class/uio/uio0/maps/map0/size");
  gpio_ptr = mmap(NULL, gpio_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
  if (gpio_ptr == MAP_FAILED) {
    printf("Mmap call failure.\n");
    return -1;
  }
  gpio_write(gpio_ptr, 0x04, 0);
  gpio_write(gpio_ptr, 0x0C, 0xF);
  gpio_write(gpio_ptr, 0x11C, 0x80000000);
  gpio_write(gpio_ptr, 0x128, 2);
  while (1) {
    wait_for_interrupt(fd, gpio_ptr);
  }
  munmap(gpio_ptr, gpio_size);
  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),%rax
mov    %rax,-0x20(%rbp)
lea    0xe82(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x20(%rbp),%rdi
mov    0x2e3e(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x1,-0x14(%rbp)
jge    11cc <main+0x6c>
mov    -0x20(%rbp),%rsi
lea    0xe61(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1295 <main+0x135>
lea    0xe64(%rip),%rdi
call   1080 <get_memory_size@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%esi
mov    0x2e13(%rip),%rax
mov    (%rax),%edx
mov    0x2dda(%rip),%rax
or     (%rax),%edx
mov    0x2df1(%rip),%rax
mov    (%rax),%ecx
mov    -0x14(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    0x2da8(%rip),%rcx
cmp    (%rcx),%rax
jne    123b <main+0xdb>
lea    0xe32(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1295 <main+0x135>
mov    -0x28(%rbp),%rdi
mov    $0x4,%esi
xor    %edx,%edx
call   1050 <gpio_write@plt>
mov    -0x28(%rbp),%rdi
mov    $0xc,%esi
mov    $0xf,%edx
call   1050 <gpio_write@plt>
mov    -0x28(%rbp),%rdi
mov    $0x11c,%esi
mov    $0x80000000,%edx
call   1050 <gpio_write@plt>
mov    -0x28(%rbp),%rdi
mov    $0x128,%esi
mov    $0x2,%edx
call   1050 <gpio_write@plt>
mov    -0x14(%rbp),%edi
mov    -0x28(%rbp),%rsi
call   1070 <wait_for_interrupt@plt>
jmp    1284 <main+0x124>
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
lea    0xed0(%rip),%rdi
call   1030 <puts@plt>
mov    0x2e52(%rip),%rax
mov    (%rax),%esi
lea    0xe71(%rip),%rdi
call   1070 <open@plt>
test   %eax,%eax
jle    123c <main+0xcc>
mov    %eax,%ebp
lea    0xe82(%rip),%rdi
call   1090 <get_memory_size@plt>
mov    0x2e47(%rip),%rcx
mov    0x2e10(%rip),%rdx
mov    (%rdx),%edx
or     (%rcx),%edx
mov    0x2e25(%rip),%rcx
mov    (%rcx),%ecx
xor    %edi,%edi
mov    %eax,%esi
mov    %ebp,%r8d
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    %rax,%rbx
mov    0x2de2(%rip),%rax
cmp    (%rax),%rbx
je     1253 <main+0xe3>
mov    %rbx,%rdi
mov    $0x4,%esi
xor    %edx,%edx
call   1060 <gpio_write@plt>
mov    %rbx,%rdi
mov    $0xc,%esi
mov    $0xf,%edx
call   1060 <gpio_write@plt>
mov    %rbx,%rdi
mov    $0x11c,%esi
mov    $0x80000000,%edx
call   1060 <gpio_write@plt>
mov    %rbx,%rdi
mov    $0x128,%esi
mov    $0x2,%edx
call   1060 <gpio_write@plt>
nopl   0x0(%rax,%rax,1)
mov    %ebp,%edi
mov    %rbx,%rsi
call   1080 <wait_for_interrupt@plt>
jmp    1230 <main+0xc0>
lea    0xdc7(%rip),%rdi
lea    0xdb6(%rip),%rsi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    125f <main+0xef>
lea    0xdff(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [10 x i8] c"/dev/uio0\00"
@.str.1 = [16 x i8] c"GPIO UIO test.\0A\00"
@O_RDWR = global i32 0
@.str.2 = [29 x i8] c"Invalid UIO device file:%s.\0A\00"
@.str.3 = [35 x i8] c"/sys/class/uio/uio0/maps/map0/size\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str.4 = [20 x i8] c"Mmap call failure.\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8** %7
%11 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
%12 = load i8*, i8** %7
%13 = load i32, i32* @O_RDWR
%14 = call i32 @open(i8* %12, i32 %13)
store i32 %14, i32* %6
%15 = load i32, i32* %6
%16 = icmp slt i32 %15, 1
br i1 %16, label %17, label %20
17: ; preds = %2
%18 = load i8*, i8** %7
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0), i8* %18)
store i32 -1, i32* %3
br label %47
20: ; preds = %2
%21 = call i32 @get_memory_size(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.3, i64 0, i64 0))
store i32 %21, i32* %9
%22 = load i32, i32* %9
%23 = load i32, i32* @PROT_READ
%24 = load i32, i32* @PROT_WRITE
%25 = or i32 %23, %24
%26 = load i32, i32* @MAP_SHARED
%27 = load i32, i32* %6
%28 = call i8* @mmap(i32* null, i32 %22, i32 %25, i32 %26, i32 %27, i32 0)
store i8* %28, i8** %8
%29 = load i8*, i8** %8
%30 = load i8*, i8** @MAP_FAILED
%31 = icmp eq i8* %29, %30
br i1 %31, label %32, label %34
32: ; preds = %20
%33 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0))
store i32 -1, i32* %3
br label %47
34: ; preds = %20
%35 = load i8*, i8** %8
%36 = call i32 @gpio_write(i8* %35, i32 4, i32 0)
%37 = load i8*, i8** %8
%38 = call i32 @gpio_write(i8* %37, i32 12, i32 15)
%39 = load i8*, i8** %8
%40 = call i32 @gpio_write(i8* %39, i32 284, i32 -2147483648)
%41 = load i8*, i8** %8
%42 = call i32 @gpio_write(i8* %41, i32 296, i32 2)
br label %43
43: ; preds = %34, %43
%44 = load i32, i32* %6
%45 = load i8*, i8** %8
%46 = call i32 @wait_for_interrupt(i32 %44, i8* %45)
br label %43
47: ; preds = %32, %17
%48 = load i32, i32* %3
ret i32 %48
}

LLVM IR (-O2):
@.str = [10 x i8] c"/dev/uio0\00"
@O_RDWR = global i32 0
@.str.2 = [29 x i8] c"Invalid UIO device file:%s.\0A\00"
@.str.3 = [35 x i8] c"/sys/class/uio/uio0/maps/map0/size\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@str = [15 x i8] c"GPIO UIO test.\00"
@str.5 = [19 x i8] c"Mmap call failure.\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str, i64 0, i64 0))
%4 = load i32, i32* @O_RDWR
%5 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %4)
%6 = icmp slt i32 %5, 1
br i1 %6, label %7, label %9
7: ; preds = %2
%8 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
br label %27
9: ; preds = %2
%10 = tail call i32 @get_memory_size(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.3, i64 0, i64 0))
%11 = load i32, i32* @PROT_READ
%12 = load i32, i32* @PROT_WRITE
%13 = or i32 %12, %11
%14 = load i32, i32* @MAP_SHARED
%15 = tail call i8* @mmap(i32* null, i32 %10, i32 %13, i32 %14, i32 %5, i32 0)
%16 = load i8*, i8** @MAP_FAILED
%17 = icmp eq i8* %15, %16
br i1 %17, label %18, label %20
18: ; preds = %9
%19 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str.5, i64 0, i64 0))
br label %27
20: ; preds = %9
%21 = tail call i32 @gpio_write(i8* %15, i32 4, i32 0)
%22 = tail call i32 @gpio_write(i8* %15, i32 12, i32 15)
%23 = tail call i32 @gpio_write(i8* %15, i32 284, i32 -2147483648)
%24 = tail call i32 @gpio_write(i8* %15, i32 296, i32 2)
br label %25
25: ; preds = %25, %20
%26 = tail call i32 @wait_for_interrupt(i32 %5, i8* %15)
br label %25
27: ; preds = %18, %7
ret i32 -1
}