#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_var_screeninfo {
  int xres;
  int yres;
  int bits_per_pixel;
};
struct fb_fix_screeninfo {
  int xres;
  int yres;
  int bits_per_pixel;
};

/* Variables and functions */
int /*<<< orphan*/ FBIOGET_FSCREENINFO;
int /*<<< orphan*/ FBIOGET_VSCREENINFO;
int /*<<< orphan*/ MAP_SHARED;
int /*<<< orphan*/ O_RDWR;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
scalar_t__ ioctl(int, int /*<<< orphan*/, struct fb_var_screeninfo *);
scalar_t__ mmap(int /*<<< orphan*/, long, int, int /*<<< orphan*/, int,
                int /*<<< orphan*/);
int /*<<< orphan*/ munmap(char *, long);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int main() {
  int fbfd = 0;
  struct fb_var_screeninfo vinfo;
  struct fb_fix_screeninfo finfo;
  long int screensize = 0;
  char *fbp = 0;
  fbfd = open("/dev/fb0", O_RDWR);
  if (!fbfd) {
    printf("Error: cannot open framebuffer device.\n");
    exit(1);
  }
  printf("The framebuffer device was opened successfully.\n");
  if (ioctl(fbfd, FBIOGET_FSCREENINFO, &finfo)) {
    printf("Error reading fixed information.\n");
    exit(2);
  }
  if (ioctl(fbfd, FBIOGET_VSCREENINFO, &vinfo)) {
    printf("Error reading variable information.\n");
    exit(3);
  }
  screensize = vinfo.xres * vinfo.yres * vinfo.bits_per_pixel / 8;
  fbp =
      (char *)mmap(0, screensize, PROT_READ | PROT_WRITE, MAP_SHARED, fbfd, 0);
  if ((int)fbp == -1) {
    printf("Error: failed to map framebuffer device to memory.\n");
    exit(4);
  }
  printf("screen size : %d", screensize);
  printf("The framebuffer device was mapped to memory successfully.\n");
  munmap(fbp, screensize);
  close(fbfd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x8(%rbp)
movq   $0x0,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
mov    0x2e3b(%rip),%rax
mov    (%rax),%esi
lea    0xe5a(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jne    11d0 <main+0x60>
lea    0xe4a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
lea    0xe5a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x8(%rbp),%edi
mov    0x2de0(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdx
call   1050 <ioctl@plt>
cmp    $0x0,%rax
je     1215 <main+0xa5>
lea    0xe5e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x2,%edi
call   1090 <exit@plt>
mov    -0x8(%rbp),%edi
mov    0x2d99(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rdx
call   1050 <ioctl@plt>
cmp    $0x0,%rax
je     124c <main+0xdc>
lea    0xe49(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x3,%edi
call   1090 <exit@plt>
mov    -0x18(%rbp),%eax
imul   -0x14(%rbp),%eax
imul   -0x10(%rbp),%eax
mov    $0x8,%ecx
cltd
idiv   %ecx
cltq
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rsi
mov    0x2d88(%rip),%rax
mov    (%rax),%edx
mov    0x2d47(%rip),%rax
or     (%rax),%edx
mov    0x2d66(%rip),%rax
mov    (%rax),%ecx
mov    -0x8(%rbp),%r8d
xor    %r9d,%r9d
mov    %r9d,%edi
call   1030 <mmap@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
cmp    $0xffffffff,%eax
jne    12bc <main+0x14c>
lea    0xdfe(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x4,%edi
call   1090 <exit@plt>
mov    -0x30(%rbp),%rsi
lea    0xe16(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe19(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x38(%rbp),%rdi
mov    -0x30(%rbp),%rsi
call   1070 <munmap@plt>
mov    -0x8(%rbp),%edi
call   1060 <close@plt>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    0x2e49(%rip),%rax
mov    (%rax),%esi
lea    0xe68(%rip),%rdi
call   1090 <open@plt>
test   %eax,%eax
je     1279 <main+0xf9>
mov    %eax,%r14d
lea    0xe92(%rip),%rdi
call   1030 <puts@plt>
mov    0x2e0d(%rip),%rax
mov    (%rax),%esi
lea    0x10(%rsp),%rdx
mov    %r14d,%edi
call   1060 <ioctl@plt>
test   %rax,%rax
jne    128f <main+0x10f>
mov    0x2dde(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
mov    %r14d,%edi
call   1060 <ioctl@plt>
test   %rax,%rax
jne    12a5 <main+0x125>
mov    0x4(%rsp),%eax
imul   (%rsp),%eax
imul   0x8(%rsp),%eax
lea    0x7(%rax),%ecx
test   %eax,%eax
cmovns %eax,%ecx
sar    $0x3,%ecx
movslq %ecx,%rbx
mov    0x2de6(%rip),%rax
mov    0x2da7(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2dc4(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    %rbx,%rsi
mov    %r14d,%r8d
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    %rax,%rbp
cmp    $0xffffffff,%ebp
je     12bb <main+0x13b>
lea    0xdc4(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xe1b(%rip),%rdi
call   1030 <puts@plt>
mov    %rbp,%rdi
mov    %rbx,%rsi
call   1080 <munmap@plt>
mov    %r14d,%edi
call   1070 <close@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xd9a(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
lea    0xe6c(%rip),%rdi
call   1030 <puts@plt>
mov    $0x2,%edi
call   10a0 <exit@plt>
lea    0xe32(%rip),%rdi
call   1030 <puts@plt>
mov    $0x3,%edi
call   10a0 <exit@plt>
lea    0xde9(%rip),%rdi
call   1030 <puts@plt>
mov    $0x4,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
%struct.fb_var_screeninfo = type { i32, i32, i32 }
%struct.fb_fix_screeninfo = type { i32, i32, i32 }
@.str = [9 x i8] c"/dev/fb0\00"
@O_RDWR = global i32 0
@.str.1 = [40 x i8] c"Error: cannot open framebuffer device.\0A\00"
@.str.2 = [49 x i8] c"The framebuffer device was opened successfully.\0A\00"
@FBIOGET_FSCREENINFO = global i32 0
@.str.3 = [34 x i8] c"Error reading fixed information.\0A\00"
@FBIOGET_VSCREENINFO = global i32 0
@.str.4 = [37 x i8] c"Error reading variable information.\0A\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@.str.5 = [52 x i8] c"Error: failed to map framebuffer device to memory.\0A\00"
@.str.6 = [17 x i8] c"screen size : %d\00"
@.str.7 = [59 x i8] c"The framebuffer device was mapped to memory successfully.\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca %struct.fb_var_screeninfo
%4 = alloca %struct.fb_fix_screeninfo
%5 = alloca i64
%6 = alloca i8*
store i32 0, i32* %1
store i32 0, i32* %2
store i64 0, i64* %5
store i8* null, i8** %6
%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* %2
%9 = load i32, i32* %2
%10 = icmp ne i32 %9, 0
br i1 %10, label %14, label %11
11: ; preds = %0
%12 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.1, i64 0, i64 0))
%13 = call i32 @exit(i32 1)
unreachable
14: ; preds = %0
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.2, i64 0, i64 0))
%16 = load i32, i32* %2
%17 = load i32, i32* @FBIOGET_FSCREENINFO
%18 = bitcast %struct.fb_fix_screeninfo* %4 to %struct.fb_var_screeninfo*
%19 = call i64 @ioctl(i32 %16, i32 %17, %struct.fb_var_screeninfo* %18)
%20 = icmp ne i64 %19, 0
br i1 %20, label %21, label %24
21: ; preds = %14
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.3, i64 0, i64 0))
%23 = call i32 @exit(i32 2)
unreachable
24: ; preds = %14
%25 = load i32, i32* %2
%26 = load i32, i32* @FBIOGET_VSCREENINFO
%27 = call i64 @ioctl(i32 %25, i32 %26, %struct.fb_var_screeninfo* %3)
%28 = icmp ne i64 %27, 0
br i1 %28, label %29, label %32
29: ; preds = %24
%30 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.4, i64 0, i64 0))
%31 = call i32 @exit(i32 3)
unreachable
32: ; preds = %24
%33 = getelementptr inbounds %struct.fb_var_screeninfo, %struct.fb_var_screeninfo* %3, i32 0, i32 0
%34 = load i32, i32* %33
%35 = getelementptr inbounds %struct.fb_var_screeninfo, %struct.fb_var_screeninfo* %3, i32 0, i32 1
%36 = load i32, i32* %35
%37 = mul nsw i32 %34, %36
%38 = getelementptr inbounds %struct.fb_var_screeninfo, %struct.fb_var_screeninfo* %3, i32 0, i32 2
%39 = load i32, i32* %38
%40 = mul nsw i32 %37, %39
%41 = sdiv i32 %40, 8
%42 = sext i32 %41 to i64
store i64 %42, i64* %5
%43 = load i64, i64* %5
%44 = load i32, i32* @PROT_READ
%45 = load i32, i32* @PROT_WRITE
%46 = or i32 %44, %45
%47 = load i32, i32* @MAP_SHARED
%48 = load i32, i32* %2
%49 = call i64 @mmap(i32 0, i64 %43, i32 %46, i32 %47, i32 %48, i32 0)
%50 = inttoptr i64 %49 to i8*
store i8* %50, i8** %6
%51 = load i8*, i8** %6
%52 = ptrtoint i8* %51 to i32
%53 = icmp eq i32 %52, -1
br i1 %53, label %54, label %57
54: ; preds = %32
%55 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str.5, i64 0, i64 0))
%56 = call i32 @exit(i32 4)
unreachable
57: ; preds = %32
%58 = load i64, i64* %5
%59 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.6, i64 0, i64 0), i64 %58)
%60 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([59 x i8], [59 x i8]* @.str.7, i64 0, i64 0))
%61 = load i8*, i8** %6
%62 = load i64, i64* %5
%63 = call i32 @munmap(i8* %61, i64 %62)
%64 = load i32, i32* %2
%65 = call i32 @close(i32 %64)
ret i32 0
}

LLVM IR (-O2):
%struct.fb_var_screeninfo = type { i32, i32, i32 }
%struct.fb_fix_screeninfo = type { i32, i32, i32 }
@.str = [9 x i8] c"/dev/fb0\00"
@O_RDWR = global i32 0
@FBIOGET_FSCREENINFO = global i32 0
@FBIOGET_VSCREENINFO = global i32 0
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@.str.6 = [17 x i8] c"screen size : %d\00"
@str = [39 x i8] c"Error: cannot open framebuffer device.\00"
@str.8 = [48 x i8] c"The framebuffer device was opened successfully.\00"
@str.9 = [58 x i8] c"The framebuffer device was mapped to memory successfully.\00"
@str.10 = [51 x i8] c"Error: failed to map framebuffer device to memory.\00"
@str.11 = [36 x i8] c"Error reading variable information.\00"
@str.12 = [33 x i8] c"Error reading fixed information.\00"
define i32 @main() {
%1 = alloca %struct.fb_var_screeninfo
%2 = alloca %struct.fb_fix_screeninfo
%3 = bitcast %struct.fb_var_screeninfo* %1 to i8*
%4 = bitcast %struct.fb_fix_screeninfo* %2 to i8*
%5 = load i32, i32* @O_RDWR
%6 = tail call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %5)
%7 = icmp eq i32 %6, 0
br i1 %7, label %8, label %11
8: ; preds = %0
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([39 x i8], [39 x i8]* @str, i64 0, i64 0))
%10 = tail call i32 @exit(i32 1)
unreachable
11: ; preds = %0
%12 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([48 x i8], [48 x i8]* @str.8, i64 0, i64 0))
%13 = load i32, i32* @FBIOGET_FSCREENINFO
%14 = bitcast %struct.fb_fix_screeninfo* %2 to %struct.fb_var_screeninfo*
%15 = call i64 @ioctl(i32 %6, i32 %13, %struct.fb_var_screeninfo* %14)
%16 = icmp eq i64 %15, 0
br i1 %16, label %20, label %17
17: ; preds = %11
%18 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @str.12, i64 0, i64 0))
%19 = call i32 @exit(i32 2)
unreachable
20: ; preds = %11
%21 = load i32, i32* @FBIOGET_VSCREENINFO
%22 = call i64 @ioctl(i32 %6, i32 %21, %struct.fb_var_screeninfo* %1)
%23 = icmp eq i64 %22, 0
br i1 %23, label %27, label %24
24: ; preds = %20
%25 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @str.11, i64 0, i64 0))
%26 = call i32 @exit(i32 3)
unreachable
27: ; preds = %20
%28 = getelementptr inbounds %struct.fb_var_screeninfo, %struct.fb_var_screeninfo* %1, i64 0, i32 0
%29 = load i32, i32* %28
%30 = getelementptr inbounds %struct.fb_var_screeninfo, %struct.fb_var_screeninfo* %1, i64 0, i32 1
%31 = load i32, i32* %30
%32 = mul nsw i32 %31, %29
%33 = getelementptr inbounds %struct.fb_var_screeninfo, %struct.fb_var_screeninfo* %1, i64 0, i32 2
%34 = load i32, i32* %33
%35 = mul nsw i32 %32, %34
%36 = sdiv i32 %35, 8
%37 = sext i32 %36 to i64
%38 = load i32, i32* @PROT_READ
%39 = load i32, i32* @PROT_WRITE
%40 = or i32 %39, %38
%41 = load i32, i32* @MAP_SHARED
%42 = call i64 @mmap(i32 0, i64 %37, i32 %40, i32 %41, i32 %6, i32 0)
%43 = trunc i64 %42 to i32
%44 = icmp eq i32 %43, -1
br i1 %44, label %45, label %48
45: ; preds = %27
%46 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([51 x i8], [51 x i8]* @str.10, i64 0, i64 0))
%47 = call i32 @exit(i32 4)
unreachable
48: ; preds = %27
%49 = inttoptr i64 %42 to i8*
%50 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str.6, i64 0, i64 0), i64 %37)
%51 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([58 x i8], [58 x i8]* @str.9, i64 0, i64 0))
%52 = call i32 @munmap(i8* %49, i64 %37)
%53 = call i32 @close(i32 %6)
ret i32 0
}