#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*/ close_memory(int, void *);
int open_memory(int *, unsigned int, unsigned int *, void **);
int parse_command(int, char **, unsigned int *, int *, int *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ read_gpio(int *, void *, unsigned int);
int /*<<< orphan*/ write_gpio(int, unsigned int, void *, unsigned int);
int main(int argc, char *argv[]) {
  int file_descriptor;
  int r_w = 0;
  int r_or_w_value = 0;
  int ret_parse;
  unsigned phy_addr, page_off;
  void *virt_addr;
  if ((ret_parse = parse_command(argc, argv, &phy_addr, &r_w, &r_or_w_value)) ==
      1)
    return 0;
  else if (ret_parse == -1)
    return -1;
  if (open_memory(&file_descriptor, phy_addr, &page_off, &virt_addr) == -1) {
    printf("nodriver aborted!\n");
    return -1;
  }
  if (r_w == 0)
    read_gpio(&r_or_w_value, virt_addr, page_off);
  else
    write_gpio(r_or_w_value, phy_addr, virt_addr, page_off);
  close_memory(file_descriptor, virt_addr);
  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)
movl   $0x0,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
lea    -0x24(%rbp),%rdx
lea    -0x18(%rbp),%rcx
lea    -0x1c(%rbp),%r8
call   1080 <parse_command@plt>
mov    %eax,-0x20(%rbp)
cmp    $0x1,%eax
jne    11b4 <main+0x54>
movl   $0x0,-0x4(%rbp)
jmp    1248 <main+0xe8>
cmpl   $0xffffffff,-0x20(%rbp)
jne    11ca <main+0x6a>
movl   $0xffffffff,-0x4(%rbp)
jmp    1248 <main+0xe8>
jmp    11cf <main+0x6f>
mov    -0x24(%rbp),%esi
lea    -0x14(%rbp),%rdi
lea    -0x28(%rbp),%rdx
lea    -0x30(%rbp),%rcx
call   1070 <open_memory@plt>
cmp    $0xffffffff,%eax
jne    1204 <main+0xa4>
lea    0xe0d(%rip),%rdi
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1248 <main+0xe8>
cmpl   $0x0,-0x18(%rbp)
jne    1223 <main+0xc3>
mov    -0x30(%rbp),%rsi
mov    -0x28(%rbp),%edx
lea    -0x1c(%rbp),%rdi
call   1060 <read_gpio@plt>
jmp    1235 <main+0xd5>
mov    -0x1c(%rbp),%edi
mov    -0x24(%rbp),%esi
mov    -0x30(%rbp),%rdx
mov    -0x28(%rbp),%ecx
call   1030 <write_gpio@plt>
mov    -0x14(%rbp),%edi
mov    -0x30(%rbp),%rsi
call   1040 <close_memory@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x20,%rsp
movl   $0x0,0xc(%rsp)
movl   $0x0,(%rsp)
lea    0x8(%rsp),%rdx
lea    0xc(%rsp),%rcx
mov    %rsp,%r8
call   1080 <parse_command@plt>
xor    %ebx,%ebx
cmp    $0x1,%eax
je     1206 <main+0xa6>
cmp    $0xffffffff,%eax
jne    1199 <main+0x39>
mov    $0xffffffff,%ebx
jmp    1206 <main+0xa6>
mov    0x8(%rsp),%esi
lea    0x1c(%rsp),%rdi
lea    0x4(%rsp),%rdx
lea    0x10(%rsp),%rcx
call   1070 <open_memory@plt>
cmp    $0xffffffff,%eax
je     11d4 <main+0x74>
cmpl   $0x0,0xc(%rsp)
je     11e7 <main+0x87>
mov    (%rsp),%edi
mov    0x8(%rsp),%esi
mov    0x10(%rsp),%rdx
mov    0x4(%rsp),%ecx
call   1030 <write_gpio@plt>
jmp    11f8 <main+0x98>
lea    0xe25(%rip),%rdi
call   1050 <puts@plt>
mov    $0xffffffff,%ebx
jmp    1206 <main+0xa6>
mov    0x10(%rsp),%rsi
mov    0x4(%rsp),%edx
mov    %rsp,%rdi
call   1060 <read_gpio@plt>
mov    0x1c(%rsp),%edi
mov    0x10(%rsp),%rsi
call   1040 <close_memory@plt>
mov    %ebx,%eax
add    $0x20,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@.str = [19 x i8] c"nodriver aborted!\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
%12 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %7
store i32 0, i32* %8
%13 = load i32, i32* %4
%14 = load i8**, i8*** %5
%15 = call i32 @parse_command(i32 %13, i8** %14, i32* %10, i32* %7, i32* %8)
store i32 %15, i32* %9
%16 = icmp eq i32 %15, 1
br i1 %16, label %17, label %18
17: ; preds = %2
store i32 0, i32* %3
br label %46
18: ; preds = %2
%19 = load i32, i32* %9
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %22
21: ; preds = %18
store i32 -1, i32* %3
br label %46
22: ; preds = %18
br label %23
23: ; preds = %22
%24 = load i32, i32* %10
%25 = call i32 @open_memory(i32* %6, i32 %24, i32* %11, i8** %12)
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %29
27: ; preds = %23
%28 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %46
29: ; preds = %23
%30 = load i32, i32* %7
%31 = icmp eq i32 %30, 0
br i1 %31, label %32, label %36
32: ; preds = %29
%33 = load i8*, i8** %12
%34 = load i32, i32* %11
%35 = call i32 @read_gpio(i32* %8, i8* %33, i32 %34)
br label %42
36: ; preds = %29
%37 = load i32, i32* %8
%38 = load i32, i32* %10
%39 = load i8*, i8** %12
%40 = load i32, i32* %11
%41 = call i32 @write_gpio(i32 %37, i32 %38, i8* %39, i32 %40)
br label %42
42: ; preds = %36, %32
%43 = load i32, i32* %6
%44 = load i8*, i8** %12
%45 = call i32 @close_memory(i32 %43, i8* %44)
store i32 0, i32* %3
br label %46
46: ; preds = %42, %27, %21, %17
%47 = load i32, i32* %3
ret i32 %47
}

LLVM IR (-O2):
@str = [18 x i8] c"nodriver aborted!\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = bitcast i32* %3 to i8*
%10 = bitcast i32* %4 to i8*
store i32 0, i32* %4
%11 = bitcast i32* %5 to i8*
store i32 0, i32* %5
%12 = bitcast i32* %6 to i8*
%13 = bitcast i32* %7 to i8*
%14 = bitcast i8** %8 to i8*
%15 = call i32 @parse_command(i32 %0, i8** %1, i32* %6, i32* %4, i32* %5)
switch i32 %15, label %17 [
i32 1, label %40
i32 -1, label %16
]
16: ; preds = %2
br label %40
17: ; preds = %2
%18 = load i32, i32* %6
%19 = call i32 @open_memory(i32* %3, i32 %18, i32* %7, i8** %8)
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %23
21: ; preds = %17
%22 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str, i64 0, i64 0))
br label %40
23: ; preds = %17
%24 = load i32, i32* %4
%25 = icmp eq i32 %24, 0
br i1 %25, label %26, label %30
26: ; preds = %23
%27 = load i8*, i8** %8
%28 = load i32, i32* %7
%29 = call i32 @read_gpio(i32* %5, i8* %27, i32 %28)
br label %36
30: ; preds = %23
%31 = load i32, i32* %5
%32 = load i32, i32* %6
%33 = load i8*, i8** %8
%34 = load i32, i32* %7
%35 = call i32 @write_gpio(i32 %31, i32 %32, i8* %33, i32 %34)
br label %36
36: ; preds = %30, %26
%37 = load i32, i32* %3
%38 = load i8*, i8** %8
%39 = call i32 @close_memory(i32 %37, i8* %38)
br label %40
40: ; preds = %2, %36, %21, %16
%41 = phi i32 [ -1, %16 ], [ -1, %21 ], [ 0, %36 ], [ 0, %2 ]
ret i32 %41
}