#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;
char *MAP_FAILED;
int /*<<< orphan*/ MAP_SHARED;
int /*<<< orphan*/ O_RDWR;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int 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*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int main(int argc, char *argv[]) {
  int fbfd = 0;
  struct fb_var_screeninfo vinfo;
  struct fb_fix_screeninfo finfo;
  long int screensize = 0;
  char *fbp = 0;
  char *fbName = "/dev/fb0";
  if (argc > 2) {
    printf("usage: checkfb [framebuffer device] \n");
    exit(1);
  }
  if (argc == 2) {
    fbName = argv[1];
  }
  fbfd = open(fbName, O_RDWR);
  if (fbfd == -1) {
    perror("Error: cannot open framebuffer device");
    exit(1);
  }
  printf("The framebuffer device (%s) was opened successfully.\n", fbName);
  if (ioctl(fbfd, FBIOGET_FSCREENINFO, &finfo) == -1) {
    perror("Error reading fixed information");
    exit(2);
  }
  if (ioctl(fbfd, FBIOGET_VSCREENINFO, &vinfo) == -1) {
    perror("Error reading variable information");
    exit(3);
  }
  printf("%dx%d, %dbpp\n", vinfo.xres, vinfo.yres, vinfo.bits_per_pixel);
  screensize = vinfo.xres * vinfo.yres * vinfo.bits_per_pixel / 8;
  fbp =
      (char *)mmap(0, screensize, PROT_READ | PROT_WRITE, MAP_SHARED, fbfd, 0);
  if (fbp == MAP_FAILED) {
    perror("Error: failed to map framebuffer device to memory");
    exit(4);
  }
  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    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
movq   $0x0,-0x38(%rbp)
movq   $0x0,-0x40(%rbp)
lea    0xe4c(%rip),%rax
mov    %rax,-0x48(%rbp)
cmpl   $0x2,-0x8(%rbp)
jle    11da <main+0x5a>
lea    0xe40(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
cmpl   $0x2,-0x8(%rbp)
jne    11f0 <main+0x70>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x48(%rbp)
mov    -0x48(%rbp),%rdi
mov    0x2ddd(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    1225 <main+0xa5>
lea    0xe19(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x48(%rbp),%rsi
lea    0xe25(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x14(%rbp),%edi
mov    0x2d87(%rip),%rax
mov    (%rax),%esi
lea    -0x30(%rbp),%rdx
call   1050 <ioctl@plt>
cmp    $0xffffffff,%eax
jne    126b <main+0xeb>
lea    0xe2f(%rip),%rdi
call   1090 <perror@plt>
mov    $0x2,%edi
call   10a0 <exit@plt>
mov    -0x14(%rbp),%edi
mov    0x2d43(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1050 <ioctl@plt>
cmp    $0xffffffff,%eax
jne    129f <main+0x11f>
lea    0xe1b(%rip),%rdi
call   1090 <perror@plt>
mov    $0x3,%edi
call   10a0 <exit@plt>
mov    -0x20(%rbp),%esi
mov    -0x1c(%rbp),%edx
mov    -0x18(%rbp),%ecx
lea    0xe1f(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x20(%rbp),%eax
imul   -0x1c(%rbp),%eax
imul   -0x18(%rbp),%eax
mov    $0x8,%ecx
cltd
idiv   %ecx
cltq
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rsi
mov    0x2d1e(%rip),%rax
mov    (%rax),%edx
mov    0x2cdd(%rip),%rax
or     (%rax),%edx
mov    0x2cfc(%rip),%rax
mov    (%rax),%ecx
mov    -0x14(%rbp),%r8d
xor    %r9d,%r9d
mov    %r9d,%edi
call   1030 <mmap@plt>
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rax
mov    0x2ca4(%rip),%rcx
cmp    (%rcx),%rax
jne    132b <main+0x1ab>
lea    0xdc0(%rip),%rdi
call   1090 <perror@plt>
mov    $0x4,%edi
call   10a0 <exit@plt>
lea    0xddc(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x40(%rbp),%rdi
mov    -0x38(%rbp),%rsi
call   1070 <munmap@plt>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
xor    %eax,%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x20,%rsp
cmp    $0x3,%edi
jge    12b9 <main+0x129>
cmp    $0x2,%edi
jne    11ac <main+0x1c>
mov    0x8(%rsi),%rbx
jmp    11b3 <main+0x23>
lea    0xe4d(%rip),%rbx
mov    0x2e1e(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1090 <open@plt>
cmp    $0xffffffff,%eax
je     12cf <main+0x13f>
mov    %eax,%r14d
lea    0xe58(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x2de0(%rip),%rax
mov    (%rax),%esi
lea    0x10(%rsp),%rdx
mov    %r14d,%edi
call   1060 <ioctl@plt>
cmp    $0xffffffff,%eax
je     12e5 <main+0x155>
mov    0x2db1(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
mov    %r14d,%edi
call   1060 <ioctl@plt>
cmp    $0xffffffff,%eax
je     12fb <main+0x16b>
mov    (%rsp),%esi
mov    0x4(%rsp),%edx
mov    0x8(%rsp),%ecx
lea    0xe79(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
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    0x2da0(%rip),%rax
mov    0x2d61(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2d7e(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    %rbx,%rsi
mov    %r14d,%r8d
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    %rax,%rbp
mov    0x2d2a(%rip),%rax
cmp    %rbp,(%rax)
je     1311 <main+0x181>
lea    0xe52(%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    0xe62(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
lea    0xd33(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
lea    0xd79(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x2,%edi
call   10b0 <exit@plt>
lea    0xd83(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x3,%edi
call   10b0 <exit@plt>
lea    0xd9e(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x4,%edi
call   10b0 <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"
@.str.1 = [38 x i8] c"usage: checkfb [framebuffer device] \0A\00"
@O_RDWR = global i32 0
@.str.2 = [38 x i8] c"Error: cannot open framebuffer device\00"
@.str.3 = [54 x i8] c"The framebuffer device (%s) was opened successfully.\0A\00"
@FBIOGET_FSCREENINFO = global i32 0
@.str.4 = [32 x i8] c"Error reading fixed information\00"
@FBIOGET_VSCREENINFO = global i32 0
@.str.5 = [35 x i8] c"Error reading variable information\00"
@.str.6 = [14 x i8] c"%dx%d, %dbpp\0A\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str.7 = [50 x i8] c"Error: failed to map framebuffer device to memory\00"
@.str.8 = [59 x i8] c"The framebuffer device was mapped to memory successfully.\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca %struct.fb_var_screeninfo
%8 = alloca %struct.fb_fix_screeninfo
%9 = alloca i64
%10 = alloca i8*
%11 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %6
store i64 0, i64* %9
store i8* null, i8** %10
store i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8** %11
%12 = load i32, i32* %4
%13 = icmp sgt i32 %12, 2
br i1 %13, label %14, label %17
14: ; preds = %2
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.1, i64 0, i64 0))
%16 = call i32 @exit(i32 1)
unreachable
17: ; preds = %2
%18 = load i32, i32* %4
%19 = icmp eq i32 %18, 2
br i1 %19, label %20, label %24
20: ; preds = %17
%21 = load i8**, i8*** %5
%22 = getelementptr inbounds i8*, i8** %21, i64 1
%23 = load i8*, i8** %22
store i8* %23, i8** %11
br label %24
24: ; preds = %20, %17
%25 = load i8*, i8** %11
%26 = load i32, i32* @O_RDWR
%27 = call i32 @open(i8* %25, i32 %26)
store i32 %27, i32* %6
%28 = load i32, i32* %6
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %33
30: ; preds = %24
%31 = call i32 @perror(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.2, i64 0, i64 0))
%32 = call i32 @exit(i32 1)
unreachable
33: ; preds = %24
%34 = load i8*, i8** %11
%35 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str.3, i64 0, i64 0), i8* %34)
%36 = load i32, i32* %6
%37 = load i32, i32* @FBIOGET_FSCREENINFO
%38 = bitcast %struct.fb_fix_screeninfo* %8 to %struct.fb_var_screeninfo*
%39 = call i32 @ioctl(i32 %36, i32 %37, %struct.fb_var_screeninfo* %38)
%40 = icmp eq i32 %39, -1
br i1 %40, label %41, label %44
41: ; preds = %33
%42 = call i32 @perror(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.4, i64 0, i64 0))
%43 = call i32 @exit(i32 2)
unreachable
44: ; preds = %33
%45 = load i32, i32* %6
%46 = load i32, i32* @FBIOGET_VSCREENINFO
%47 = call i32 @ioctl(i32 %45, i32 %46, %struct.fb_var_screeninfo* %7)
%48 = icmp eq i32 %47, -1
br i1 %48, label %49, label %52
49: ; preds = %44
%50 = call i32 @perror(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.5, i64 0, i64 0))
%51 = call i32 @exit(i32 3)
unreachable
52: ; preds = %44
%53 = getelementptr inbounds %struct.fb_var_screeninfo, %struct.fb_var_screeninfo* %7, i32 0, i32 0
%54 = load i32, i32* %53
%55 = getelementptr inbounds %struct.fb_var_screeninfo, %struct.fb_var_screeninfo* %7, i32 0, i32 1
%56 = load i32, i32* %55
%57 = getelementptr inbounds %struct.fb_var_screeninfo, %struct.fb_var_screeninfo* %7, i32 0, i32 2
%58 = load i32, i32* %57
%59 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.6, i64 0, i64 0), i32 %54, i32 %56, i32 %58)
%60 = getelementptr inbounds %struct.fb_var_screeninfo, %struct.fb_var_screeninfo* %7, i32 0, i32 0
%61 = load i32, i32* %60
%62 = getelementptr inbounds %struct.fb_var_screeninfo, %struct.fb_var_screeninfo* %7, i32 0, i32 1
%63 = load i32, i32* %62
%64 = mul nsw i32 %61, %63
%65 = getelementptr inbounds %struct.fb_var_screeninfo, %struct.fb_var_screeninfo* %7, i32 0, i32 2
%66 = load i32, i32* %65
%67 = mul nsw i32 %64, %66
%68 = sdiv i32 %67, 8
%69 = sext i32 %68 to i64
store i64 %69, i64* %9
%70 = load i64, i64* %9
%71 = load i32, i32* @PROT_READ
%72 = load i32, i32* @PROT_WRITE
%73 = or i32 %71, %72
%74 = load i32, i32* @MAP_SHARED
%75 = load i32, i32* %6
%76 = call i64 @mmap(i32 0, i64 %70, i32 %73, i32 %74, i32 %75, i32 0)
%77 = inttoptr i64 %76 to i8*
store i8* %77, i8** %10
%78 = load i8*, i8** %10
%79 = load i8*, i8** @MAP_FAILED
%80 = icmp eq i8* %78, %79
br i1 %80, label %81, label %84
81: ; preds = %52
%82 = call i32 @perror(i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.7, i64 0, i64 0))
%83 = call i32 @exit(i32 4)
unreachable
84: ; preds = %52
%85 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([59 x i8], [59 x i8]* @.str.8, i64 0, i64 0))
%86 = load i8*, i8** %10
%87 = load i64, i64* %9
%88 = call i32 @munmap(i8* %86, i64 %87)
%89 = load i32, i32* %6
%90 = call i32 @close(i32 %89)
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
@.str.2 = [38 x i8] c"Error: cannot open framebuffer device\00"
@.str.3 = [54 x i8] c"The framebuffer device (%s) was opened successfully.\0A\00"
@FBIOGET_FSCREENINFO = global i32 0
@.str.4 = [32 x i8] c"Error reading fixed information\00"
@FBIOGET_VSCREENINFO = global i32 0
@.str.5 = [35 x i8] c"Error reading variable information\00"
@.str.6 = [14 x i8] c"%dx%d, %dbpp\0A\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str.7 = [50 x i8] c"Error: failed to map framebuffer device to memory\00"
@str = [58 x i8] c"The framebuffer device was mapped to memory successfully.\00"
@str.9 = [37 x i8] c"usage: checkfb [framebuffer device] \00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.fb_var_screeninfo
%4 = alloca %struct.fb_fix_screeninfo
%5 = bitcast %struct.fb_var_screeninfo* %3 to i8*
%6 = bitcast %struct.fb_fix_screeninfo* %4 to i8*
%7 = icmp sgt i32 %0, 2
br i1 %7, label %8, label %11
8: ; preds = %2
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @str.9, i64 0, i64 0))
%10 = tail call i32 @exit(i32 1)
unreachable
11: ; preds = %2
%12 = icmp eq i32 %0, 2
br i1 %12, label %13, label %16
13: ; preds = %11
%14 = getelementptr inbounds i8*, i8** %1, i64 1
%15 = load i8*, i8** %14
br label %16
16: ; preds = %13, %11
%17 = phi i8* [ %15, %13 ], [ getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), %11 ]
%18 = load i32, i32* @O_RDWR
%19 = tail call i32 @open(i8* %17, i32 %18)
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %24
21: ; preds = %16
%22 = tail call i32 @perror(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.2, i64 0, i64 0))
%23 = tail call i32 @exit(i32 1)
unreachable
24: ; preds = %16
%25 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([54 x i8], [54 x i8]* @.str.3, i64 0, i64 0), i8* %17)
%26 = load i32, i32* @FBIOGET_FSCREENINFO
%27 = bitcast %struct.fb_fix_screeninfo* %4 to %struct.fb_var_screeninfo*
%28 = call i32 @ioctl(i32 %19, i32 %26, %struct.fb_var_screeninfo* %27)
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %33
30: ; preds = %24
%31 = call i32 @perror(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.4, i64 0, i64 0))
%32 = call i32 @exit(i32 2)
unreachable
33: ; preds = %24
%34 = load i32, i32* @FBIOGET_VSCREENINFO
%35 = call i32 @ioctl(i32 %19, i32 %34, %struct.fb_var_screeninfo* %3)
%36 = icmp eq i32 %35, -1
br i1 %36, label %37, label %40
37: ; preds = %33
%38 = call i32 @perror(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.5, i64 0, i64 0))
%39 = call i32 @exit(i32 3)
unreachable
40: ; preds = %33
%41 = getelementptr inbounds %struct.fb_var_screeninfo, %struct.fb_var_screeninfo* %3, i64 0, i32 0
%42 = load i32, i32* %41
%43 = getelementptr inbounds %struct.fb_var_screeninfo, %struct.fb_var_screeninfo* %3, i64 0, i32 1
%44 = load i32, i32* %43
%45 = getelementptr inbounds %struct.fb_var_screeninfo, %struct.fb_var_screeninfo* %3, i64 0, i32 2
%46 = load i32, i32* %45
%47 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.6, i64 0, i64 0), i32 %42, i32 %44, i32 %46)
%48 = load i32, i32* %41
%49 = load i32, i32* %43
%50 = mul nsw i32 %49, %48
%51 = load i32, i32* %45
%52 = mul nsw i32 %50, %51
%53 = sdiv i32 %52, 8
%54 = sext i32 %53 to i64
%55 = load i32, i32* @PROT_READ
%56 = load i32, i32* @PROT_WRITE
%57 = or i32 %56, %55
%58 = load i32, i32* @MAP_SHARED
%59 = call i64 @mmap(i32 0, i64 %54, i32 %57, i32 %58, i32 %19, i32 0)
%60 = inttoptr i64 %59 to i8*
%61 = load i8*, i8** @MAP_FAILED
%62 = icmp eq i8* %61, %60
br i1 %62, label %63, label %66
63: ; preds = %40
%64 = call i32 @perror(i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.7, i64 0, i64 0))
%65 = call i32 @exit(i32 4)
unreachable
66: ; preds = %40
%67 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([58 x i8], [58 x i8]* @str, i64 0, i64 0))
%68 = call i32 @munmap(i8* %60, i64 %54)
%69 = call i32 @close(i32 %19)
ret i32 0
}