#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*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int, char *);
int map_mem(void **, int, int);
int /*<<< orphan*/ printf(char *, ...);
int reg_read(void *, int);
int /*<<< orphan*/ reg_write(void *, int, int);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int /*<<< orphan*/ usleep(int);
int main(int argc, char *argv[]) {
  void *CAR_io_mem_virt;
  int err;
  err = map_mem(&CAR_io_mem_virt, 0x60006000, 0x1000);
  if (err < 0) {
    fprintf(stderr, "mmap failed: %d (%s)\n", err, strerror(errno));
    return err;
  }
  printf("GR3D1 reset = %d\n",
         !!(reg_read(CAR_io_mem_virt, 0x004) & (1 << 24)));
  printf("GR3D2 reset = %d\n", !!(reg_read(CAR_io_mem_virt, 0x058) & (1 << 2)));
  reg_write(CAR_io_mem_virt, 0x300, (1 << 24));
  reg_write(CAR_io_mem_virt, 0x430, (1 << 2));
  usleep(1000);
  reg_write(CAR_io_mem_virt, 0x304, (1 << 24));
  reg_write(CAR_io_mem_virt, 0x434, (1 << 2));
  printf("done\n");
  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    -0x18(%rbp),%rdi
mov    $0x60006000,%esi
mov    $0x1000,%edx
call   1030 <map_mem@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    11e6 <main+0x76>
mov    0x2e33(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    0x2e19(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x24(%rbp),%edi
mov    -0x20(%rbp),%edx
mov    %rax,%rcx
lea    0xe2a(%rip),%rsi
call   1060 <fprintf@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12af <main+0x13f>
mov    -0x18(%rbp),%rdi
mov    $0x4,%esi
call   1050 <reg_read@plt>
and    $0x1000000,%eax
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
xor    $0xff,%al
and    $0x1,%al
movzbl %al,%esi
lea    0xe07(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x18(%rbp),%rdi
mov    $0x58,%esi
call   1050 <reg_read@plt>
and    $0x4,%eax
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
xor    $0xff,%al
and    $0x1,%al
movzbl %al,%esi
lea    0xdeb(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x18(%rbp),%rdi
mov    $0x300,%esi
mov    $0x1000000,%edx
call   1070 <reg_write@plt>
mov    -0x18(%rbp),%rdi
mov    $0x430,%esi
mov    $0x4,%edx
call   1070 <reg_write@plt>
mov    $0x3e8,%edi
call   1090 <usleep@plt>
mov    -0x18(%rbp),%rdi
mov    $0x304,%esi
mov    $0x1000000,%edx
call   1070 <reg_write@plt>
mov    -0x18(%rbp),%rdi
mov    $0x434,%esi
mov    $0x4,%edx
call   1070 <reg_write@plt>
lea    0xd99(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
mov    %rsp,%rdi
mov    $0x60006000,%esi
mov    $0x1000,%edx
call   1030 <map_mem@plt>
test   %eax,%eax
js     124b <main+0xcb>
mov    (%rsp),%rdi
mov    $0x4,%esi
call   1060 <reg_read@plt>
shr    $0x18,%eax
and    $0x1,%eax
lea    0xe5e(%rip),%rdi
xor    %ebx,%ebx
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    (%rsp),%rdi
mov    $0x58,%esi
call   1060 <reg_read@plt>
shr    $0x2,%eax
and    $0x1,%eax
lea    0xe4a(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    (%rsp),%rdi
mov    $0x300,%esi
mov    $0x1000000,%edx
call   1080 <reg_write@plt>
mov    (%rsp),%rdi
mov    $0x430,%esi
mov    $0x4,%edx
call   1080 <reg_write@plt>
mov    $0x3e8,%edi
call   10a0 <usleep@plt>
mov    (%rsp),%rdi
mov    $0x304,%esi
mov    $0x1000000,%edx
call   1080 <reg_write@plt>
mov    (%rsp),%rdi
mov    $0x434,%esi
mov    $0x4,%edx
call   1080 <reg_write@plt>
lea    0xdf6(%rip),%rdi
call   1040 <puts@plt>
jmp    1277 <main+0xf7>
mov    %eax,%ebx
mov    0x2d8c(%rip),%rax
mov    (%rax),%ebp
mov    0x2d7b(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xd95(%rip),%rsi
mov    %ebp,%edi
mov    %ebx,%edx
mov    %rax,%rcx
call   1070 <fprintf@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [22 x i8] c"mmap failed: %d (%s)\0A\00"
@errno = global i32 0
@.str.1 = [18 x i8] c"GR3D1 reset = %d\0A\00"
@.str.2 = [18 x i8] c"GR3D2 reset = %d\0A\00"
@.str.3 = [6 x i8] c"done\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = call i32 @map_mem(i8** %6, i32 1610637312, i32 4096)
store i32 %8, i32* %7
%9 = load i32, i32* %7
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %18
11: ; preds = %2
%12 = load i32, i32* @stderr
%13 = load i32, i32* %7
%14 = load i32, i32* @errno
%15 = call i8* @strerror(i32 %14)
%16 = call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i32 %13, i8* %15)
%17 = load i32, i32* %7
store i32 %17, i32* %3
br label %45
18: ; preds = %2
%19 = load i8*, i8** %6
%20 = call i32 @reg_read(i8* %19, i32 4)
%21 = and i32 %20, 16777216
%22 = icmp ne i32 %21, 0
%23 = xor i1 %22, true
%24 = xor i1 %23, true
%25 = zext i1 %24 to i32
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i32 %25)
%27 = load i8*, i8** %6
%28 = call i32 @reg_read(i8* %27, i32 88)
%29 = and i32 %28, 4
%30 = icmp ne i32 %29, 0
%31 = xor i1 %30, true
%32 = xor i1 %31, true
%33 = zext i1 %32 to i32
%34 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i32 %33)
%35 = load i8*, i8** %6
%36 = call i32 @reg_write(i8* %35, i32 768, i32 16777216)
%37 = load i8*, i8** %6
%38 = call i32 @reg_write(i8* %37, i32 1072, i32 4)
%39 = call i32 @usleep(i32 1000)
%40 = load i8*, i8** %6
%41 = call i32 @reg_write(i8* %40, i32 772, i32 16777216)
%42 = load i8*, i8** %6
%43 = call i32 @reg_write(i8* %42, i32 1076, i32 4)
%44 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
store i32 0, i32* %3
br label %45
45: ; preds = %18, %11
%46 = load i32, i32* %3
ret i32 %46
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [22 x i8] c"mmap failed: %d (%s)\0A\00"
@errno = global i32 0
@.str.1 = [18 x i8] c"GR3D1 reset = %d\0A\00"
@.str.2 = [18 x i8] c"GR3D2 reset = %d\0A\00"
@str = [5 x i8] c"done\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca i8*
%4 = bitcast i8** %3 to i8*
%5 = call i32 @map_mem(i8** %3, i32 1610637312, i32 4096)
%6 = icmp slt i32 %5, 0
br i1 %6, label %7, label %12
7: ; preds = %2
%8 = load i32, i32* @stderr
%9 = load i32, i32* @errno
%10 = call i8* @strerror(i32 %9)
%11 = call i32 @fprintf(i32 %8, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i32 %5, i8* %10)
br label %33
12: ; preds = %2
%13 = load i8*, i8** %3
%14 = call i32 @reg_read(i8* %13, i32 4)
%15 = lshr i32 %14, 24
%16 = and i32 %15, 1
%17 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i32 %16)
%18 = load i8*, i8** %3
%19 = call i32 @reg_read(i8* %18, i32 88)
%20 = lshr i32 %19, 2
%21 = and i32 %20, 1
%22 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i32 %21)
%23 = load i8*, i8** %3
%24 = call i32 @reg_write(i8* %23, i32 768, i32 16777216)
%25 = load i8*, i8** %3
%26 = call i32 @reg_write(i8* %25, i32 1072, i32 4)
%27 = call i32 @usleep(i32 1000)
%28 = load i8*, i8** %3
%29 = call i32 @reg_write(i8* %28, i32 772, i32 16777216)
%30 = load i8*, i8** %3
%31 = call i32 @reg_write(i8* %30, i32 1076, i32 4)
%32 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @str, i64 0, i64 0))
br label %33
33: ; preds = %12, %7
%34 = phi i32 [ %5, %7 ], [ 0, %12 ]
ret i32 %34
}