#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 */
struct fb_fix_screeninfo {
  size_t smem_len;
};

/* Variables and functions */
int /*<<< orphan*/ FBIOGET_FSCREENINFO;
int /*<<< orphan*/ MAP_SHARED;
int /*<<< orphan*/ O_RDWR;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ close(int);
scalar_t__ ioctl(int, int /*<<< orphan*/, struct fb_fix_screeninfo *);
void *mmap(int /*<<< orphan*/, size_t, int, int /*<<< orphan*/, int,
           int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
void *mmap_framebuffer(size_t *fbsize) {
  int fd;
  void *p;
  struct fb_fix_screeninfo finfo;
  if ((fd = open("/dev/fb0", O_RDWR)) == -1)
    if ((fd = open("/dev/graphics/fb0", O_RDWR)) == -1)
      return NULL;
  if (ioctl(fd, FBIOGET_FSCREENINFO, &finfo)) {
    close(fd);
    return NULL;
  }
  p = mmap(0, finfo.smem_len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
  close(fd);
  if (p == (void *)-1)
    return NULL;
  *fbsize = finfo.smem_len;
  return p;
}


x86 assembly (clang, -O0):
<mmap_framebuffer>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e85(%rip),%rax
mov    (%rax),%esi
lea    0xea4(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    11a0 <mmap_framebuffer+0x60>
mov    0x2e64(%rip),%rax
mov    (%rax),%esi
lea    0xe8c(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    119b <mmap_framebuffer+0x5b>
movq   $0x0,-0x8(%rbp)
jmp    123f <mmap_framebuffer+0xff>
jmp    11a0 <mmap_framebuffer+0x60>
mov    -0x14(%rbp),%edi
mov    0x2e1e(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdx
call   1040 <ioctl@plt>
cmp    $0x0,%rax
je     11d4 <mmap_framebuffer+0x94>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
movq   $0x0,-0x8(%rbp)
jmp    123f <mmap_framebuffer+0xff>
mov    -0x28(%rbp),%rsi
mov    0x2e19(%rip),%rax
mov    (%rax),%edx
mov    0x2dd8(%rip),%rax
or     (%rax),%edx
mov    0x2df7(%rip),%rax
mov    (%rax),%ecx
mov    -0x14(%rbp),%r8d
xor    %r9d,%r9d
mov    %r9d,%edi
call   1030 <mmap@plt>
mov    %rax,-0x20(%rbp)
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
mov    $0xffffffffffffffff,%rax
cmp    %rax,-0x20(%rbp)
jne    122c <mmap_framebuffer+0xec>
movq   $0x0,-0x8(%rbp)
jmp    123f <mmap_framebuffer+0xff>
mov    -0x28(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mmap_framebuffer>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
mov    0x2e87(%rip),%rbx
mov    (%rbx),%esi
lea    0xea6(%rip),%rdi
call   1060 <open@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
jne    117b <mmap_framebuffer+0x3b>
mov    (%rbx),%esi
lea    0xe9a(%rip),%rdi
call   1060 <open@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
je     119a <mmap_framebuffer+0x5a>
mov    0x2e46(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
mov    %ebp,%edi
call   1040 <ioctl@plt>
test   %rax,%rax
je     119e <mmap_framebuffer+0x5e>
mov    %ebp,%edi
call   1050 <close@plt>
xor    %ebx,%ebx
jmp    11e6 <mmap_framebuffer+0xa6>
mov    (%rsp),%rsi
mov    0x2e4f(%rip),%rax
mov    0x2e10(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e2d(%rip),%rax
mov    (%rax),%ecx
xor    %ebx,%ebx
xor    %edi,%edi
mov    %ebp,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,%r15
mov    %ebp,%edi
call   1050 <close@plt>
cmp    $0xffffffffffffffff,%r15
je     11e6 <mmap_framebuffer+0xa6>
mov    (%rsp),%rax
mov    %rax,(%r14)
mov    %r15,%rbx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.fb_fix_screeninfo = type { i64 }
@.str = [9 x i8] c"/dev/fb0\00"
@O_RDWR = global i32 0
@.str.1 = [18 x i8] c"/dev/graphics/fb0\00"
@FBIOGET_FSCREENINFO = global i32 0
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
define i8* @mmap_framebuffer(i64* %0) {
%2 = alloca i8*
%3 = alloca i64*
%4 = alloca i32
%5 = alloca i8*
%6 = alloca %struct.fb_fix_screeninfo
store i64* %0, i64** %3
%7 = load i32, i32* @O_RDWR
%8 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %7)
store i32 %8, i32* %4
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %16
10: ; preds = %1
%11 = load i32, i32* @O_RDWR
%12 = call i32 @open(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i32 %11)
store i32 %12, i32* %4
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %15
14: ; preds = %10
store i8* null, i8** %2
br label %43
15: ; preds = %10
br label %16
16: ; preds = %15, %1
%17 = load i32, i32* %4
%18 = load i32, i32* @FBIOGET_FSCREENINFO
%19 = call i64 @ioctl(i32 %17, i32 %18, %struct.fb_fix_screeninfo* %6)
%20 = icmp ne i64 %19, 0
br i1 %20, label %21, label %24
21: ; preds = %16
%22 = load i32, i32* %4
%23 = call i32 @close(i32 %22)
store i8* null, i8** %2
br label %43
24: ; preds = %16
%25 = getelementptr inbounds %struct.fb_fix_screeninfo, %struct.fb_fix_screeninfo* %6, i32 0, i32 0
%26 = load i64, i64* %25
%27 = load i32, i32* @PROT_READ
%28 = load i32, i32* @PROT_WRITE
%29 = or i32 %27, %28
%30 = load i32, i32* @MAP_SHARED
%31 = load i32, i32* %4
%32 = call i8* @mmap(i32 0, i64 %26, i32 %29, i32 %30, i32 %31, i32 0)
store i8* %32, i8** %5
%33 = load i32, i32* %4
%34 = call i32 @close(i32 %33)
%35 = load i8*, i8** %5
%36 = icmp eq i8* %35, inttoptr (i64 -1 to i8*)
br i1 %36, label %37, label %38
37: ; preds = %24
store i8* null, i8** %2
br label %43
38: ; preds = %24
%39 = getelementptr inbounds %struct.fb_fix_screeninfo, %struct.fb_fix_screeninfo* %6, i32 0, i32 0
%40 = load i64, i64* %39
%41 = load i64*, i64** %3
store i64 %40, i64* %41
%42 = load i8*, i8** %5
store i8* %42, i8** %2
br label %43
43: ; preds = %38, %37, %21, %14
%44 = load i8*, i8** %2
ret i8* %44
}

LLVM IR (-O2):
%struct.fb_fix_screeninfo = type { i64 }
@.str = [9 x i8] c"/dev/fb0\00"
@O_RDWR = global i32 0
@.str.1 = [18 x i8] c"/dev/graphics/fb0\00"
@FBIOGET_FSCREENINFO = global i32 0
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
define i8* @mmap_framebuffer(i64* nocapture writeonly %0) {
%2 = alloca %struct.fb_fix_screeninfo
%3 = bitcast %struct.fb_fix_screeninfo* %2 to i8*
%4 = load i32, i32* @O_RDWR
%5 = tail call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %4)
%6 = icmp eq i32 %5, -1
br i1 %6, label %7, label %11
7: ; preds = %1
%8 = load i32, i32* @O_RDWR
%9 = tail call i32 @open(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i32 %8)
%10 = icmp eq i32 %9, -1
br i1 %10, label %30, label %11
11: ; preds = %7, %1
%12 = phi i32 [ %9, %7 ], [ %5, %1 ]
%13 = load i32, i32* @FBIOGET_FSCREENINFO
%14 = call i64 @ioctl(i32 %12, i32 %13, %struct.fb_fix_screeninfo* %2)
%15 = icmp eq i64 %14, 0
br i1 %15, label %18, label %16
16: ; preds = %11
%17 = call i32 @close(i32 %12)
br label %30
18: ; preds = %11
%19 = getelementptr inbounds %struct.fb_fix_screeninfo, %struct.fb_fix_screeninfo* %2, i64 0, i32 0
%20 = load i64, i64* %19
%21 = load i32, i32* @PROT_READ
%22 = load i32, i32* @PROT_WRITE
%23 = or i32 %22, %21
%24 = load i32, i32* @MAP_SHARED
%25 = call i8* @mmap(i32 0, i64 %20, i32 %23, i32 %24, i32 %12, i32 0)
%26 = call i32 @close(i32 %12)
%27 = icmp eq i8* %25, inttoptr (i64 -1 to i8*)
br i1 %27, label %30, label %28
28: ; preds = %18
%29 = load i64, i64* %19
store i64 %29, i64* %0
br label %30
30: ; preds = %18, %7, %28, %16
%31 = phi i8* [ null, %16 ], [ %25, %28 ], [ null, %7 ], [ null, %18 ]
ret i8* %31
}