#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 */
typedef int uint;
struct fb_var_screeninfo {
  int xres;
  int yres;
  int bits_per_pixel;
  long smem_len;
};
struct fb_fix_screeninfo {
  int xres;
  int yres;
  int bits_per_pixel;
  long smem_len;
};

/* 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);
scalar_t__ ioctl(int, int /*<<< orphan*/, struct fb_var_screeninfo *);
int /*<<< orphan*/ memset(char *, int, long);
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 argc, char *argv[]) {
  int fbfd = 0;
  struct fb_var_screeninfo vinfo;
  struct fb_fix_screeninfo finfo;
  long int screensize = 0;
  char *fbp = 0;
  uint i;
  fbfd = open("/dev/fb1", O_RDWR);
  if (!fbfd) {
    printf("Error: cannot open framebuffer device.\n");
    return (1);
  }
  printf("The framebuffer device was opened successfully.\n");
  if (ioctl(fbfd, FBIOGET_FSCREENINFO, &finfo)) {
    printf("Error reading fixed information.\n");
  }
  if (ioctl(fbfd, FBIOGET_VSCREENINFO, &vinfo)) {
    printf("Error reading variable information.\n");
  }
  printf("%dx%d, %d bpp\n", vinfo.xres, vinfo.yres, vinfo.bits_per_pixel);
  screensize = finfo.smem_len;
  fbp =
      (char *)mmap(0, screensize, PROT_READ | PROT_WRITE, MAP_SHARED, fbfd, 0);
  if ((int)fbp == -1) {
    printf("Failed to mmap.\n");
  } else {
    for (i = 0; i < 0xff; i++) {
      memset(fbp, i, screensize / 2);
      memset(fbp + screensize / 2, 0xff - i, screensize / 2);
    }
  }
  munmap(fbp, screensize);
  close(fbfd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
movq   $0x0,-0x50(%rbp)
movq   $0x0,-0x58(%rbp)
mov    0x2e34(%rip),%rax
mov    (%rax),%esi
lea    0xe53(%rip),%rdi
call   1090 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jne    11d9 <main+0x69>
lea    0xe43(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1345 <main+0x1d5>
lea    0xe51(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x14(%rbp),%edi
mov    0x2dd7(%rip),%rax
mov    (%rax),%esi
lea    -0x48(%rbp),%rdx
call   1060 <ioctl@plt>
cmp    $0x0,%rax
je     1214 <main+0xa4>
lea    0xe55(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x14(%rbp),%edi
mov    0x2d9a(%rip),%rax
mov    (%rax),%esi
lea    -0x30(%rbp),%rdx
call   1060 <ioctl@plt>
cmp    $0x0,%rax
je     1241 <main+0xd1>
lea    0xe4a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x30(%rbp),%esi
mov    -0x2c(%rbp),%edx
mov    -0x28(%rbp),%ecx
lea    0xe58(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x38(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x50(%rbp),%rsi
mov    0x2d8d(%rip),%rax
mov    (%rax),%edx
mov    0x2d4c(%rip),%rax
or     (%rax),%edx
mov    0x2d6b(%rip),%rax
mov    (%rax),%ecx
mov    -0x14(%rbp),%r8d
xor    %r9d,%r9d
mov    %r9d,%edi
call   1030 <mmap@plt>
mov    %rax,-0x58(%rbp)
mov    -0x58(%rbp),%rax
cmp    $0xffffffff,%eax
jne    12b2 <main+0x142>
lea    0xe12(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1329 <main+0x1b9>
movl   $0x0,-0x5c(%rbp)
cmpl   $0xff,-0x5c(%rbp)
jge    1324 <main+0x1b4>
mov    -0x58(%rbp),%rdi
mov    -0x5c(%rbp),%esi
mov    -0x50(%rbp),%rax
mov    $0x2,%ecx
cqto
idiv   %rcx
mov    %rax,%rdx
call   1050 <memset@plt>
mov    -0x58(%rbp),%rdi
mov    -0x50(%rbp),%rax
mov    $0x2,%ecx
cqto
idiv   %rcx
add    %rax,%rdi
mov    $0xff,%esi
sub    -0x5c(%rbp),%esi
mov    -0x50(%rbp),%rax
mov    $0x2,%ecx
cqto
idiv   %rcx
mov    %rax,%rdx
call   1050 <memset@plt>
mov    -0x5c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x5c(%rbp)
jmp    12b9 <main+0x149>
jmp    1329 <main+0x1b9>
mov    -0x58(%rbp),%rdi
mov    -0x50(%rbp),%rsi
call   1080 <munmap@plt>
mov    -0x14(%rbp),%edi
call   1070 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    0x2e43(%rip),%rax
mov    (%rax),%esi
lea    0xe62(%rip),%rdi
call   10a0 <open@plt>
test   %eax,%eax
je     12a6 <main+0x126>
mov    %eax,%ebx
lea    0xe8b(%rip),%rdi
call   1030 <puts@plt>
mov    0x2e08(%rip),%rax
mov    (%rax),%esi
lea    0x20(%rsp),%rdx
mov    %ebx,%edi
call   1070 <ioctl@plt>
test   %rax,%rax
je     11df <main+0x5f>
lea    0xec9(%rip),%rdi
call   1030 <puts@plt>
mov    0x2dd2(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
mov    %ebx,%edi
call   1070 <ioctl@plt>
test   %rax,%rax
je     1205 <main+0x85>
lea    0xe7f(%rip),%rdi
call   1030 <puts@plt>
mov    0x8(%rsp),%esi
mov    0xc(%rsp),%edx
mov    0x10(%rsp),%ecx
lea    0xdf1(%rip),%rdi
xor    %ebp,%ebp
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x30(%rsp),%r15
mov    0x2dcb(%rip),%rax
mov    0x2d8c(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2da9(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    %r15,%rsi
mov    %ebx,0x4(%rsp)
mov    %ebx,%r8d
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    %rax,%r12
cmp    $0xffffffff,%r12d
je     12b9 <main+0x139>
mov    %r15,%rbx
shr    $0x3f,%rbx
add    %r15,%rbx
sar    %rbx
mov    %r12,%r13
add    %rbx,%r13
mov    $0xff,%r14d
nopw   0x0(%rax,%rax,1)
mov    %r12,%rdi
mov    %ebp,%esi
mov    %rbx,%rdx
call   1060 <memset@plt>
mov    %r13,%rdi
mov    %r14d,%esi
mov    %rbx,%rdx
call   1060 <memset@plt>
add    $0x1,%ebp
add    $0xffffffff,%r14d
jne    1280 <main+0x100>
jmp    12c5 <main+0x145>
lea    0xd6b(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%eax
jmp    12db <main+0x15b>
lea    0xdaf(%rip),%rdi
call   1030 <puts@plt>
mov    %r12,%rdi
mov    %r15,%rsi
call   1090 <munmap@plt>
mov    0x4(%rsp),%edi
call   1080 <close@plt>
xor    %eax,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.fb_var_screeninfo = type { i32, i32, i32, i64 }
%struct.fb_fix_screeninfo = type { i32, i32, i32, i64 }
@.str = [9 x i8] c"/dev/fb1\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"
@.str.5 = [15 x i8] c"%dx%d, %d bpp\0A\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@.str.6 = [17 x i8] c"Failed to mmap.\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 i32
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
%12 = load i32, i32* @O_RDWR
%13 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %12)
store i32 %13, i32* %6
%14 = load i32, i32* %6
%15 = icmp ne i32 %14, 0
br i1 %15, label %18, label %16
16: ; preds = %2
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %3
br label %86
18: ; preds = %2
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.2, i64 0, i64 0))
%20 = load i32, i32* %6
%21 = load i32, i32* @FBIOGET_FSCREENINFO
%22 = bitcast %struct.fb_fix_screeninfo* %8 to %struct.fb_var_screeninfo*
%23 = call i64 @ioctl(i32 %20, i32 %21, %struct.fb_var_screeninfo* %22)
%24 = icmp ne i64 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %18
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.3, i64 0, i64 0))
br label %27
27: ; preds = %25, %18
%28 = load i32, i32* %6
%29 = load i32, i32* @FBIOGET_VSCREENINFO
%30 = call i64 @ioctl(i32 %28, i32 %29, %struct.fb_var_screeninfo* %7)
%31 = icmp ne i64 %30, 0
br i1 %31, label %32, label %34
32: ; preds = %27
%33 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.4, i64 0, i64 0))
br label %34
34: ; preds = %32, %27
%35 = getelementptr inbounds %struct.fb_var_screeninfo, %struct.fb_var_screeninfo* %7, i32 0, i32 0
%36 = load i32, i32* %35
%37 = getelementptr inbounds %struct.fb_var_screeninfo, %struct.fb_var_screeninfo* %7, i32 0, i32 1
%38 = load i32, i32* %37
%39 = getelementptr inbounds %struct.fb_var_screeninfo, %struct.fb_var_screeninfo* %7, i32 0, i32 2
%40 = load i32, i32* %39
%41 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.5, i64 0, i64 0), i32 %36, i32 %38, i32 %40)
%42 = getelementptr inbounds %struct.fb_fix_screeninfo, %struct.fb_fix_screeninfo* %8, i32 0, i32 3
%43 = load i64, i64* %42
store i64 %43, i64* %9
%44 = load i64, i64* %9
%45 = load i32, i32* @PROT_READ
%46 = load i32, i32* @PROT_WRITE
%47 = or i32 %45, %46
%48 = load i32, i32* @MAP_SHARED
%49 = load i32, i32* %6
%50 = call i64 @mmap(i32 0, i64 %44, i32 %47, i32 %48, i32 %49, i32 0)
%51 = inttoptr i64 %50 to i8*
store i8* %51, i8** %10
%52 = load i8*, i8** %10
%53 = ptrtoint i8* %52 to i32
%54 = icmp eq i32 %53, -1
br i1 %54, label %55, label %57
55: ; preds = %34
%56 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.6, i64 0, i64 0))
br label %80
57: ; preds = %34
store i32 0, i32* %11
br label %58
58: ; preds = %76, %57
%59 = load i32, i32* %11
%60 = icmp slt i32 %59, 255
br i1 %60, label %61, label %79
61: ; preds = %58
%62 = load i8*, i8** %10
%63 = load i32, i32* %11
%64 = load i64, i64* %9
%65 = sdiv i64 %64, 2
%66 = call i32 @memset(i8* %62, i32 %63, i64 %65)
%67 = load i8*, i8** %10
%68 = load i64, i64* %9
%69 = sdiv i64 %68, 2
%70 = getelementptr inbounds i8, i8* %67, i64 %69
%71 = load i32, i32* %11
%72 = sub nsw i32 255, %71
%73 = load i64, i64* %9
%74 = sdiv i64 %73, 2
%75 = call i32 @memset(i8* %70, i32 %72, i64 %74)
br label %76
76: ; preds = %61
%77 = load i32, i32* %11
%78 = add nsw i32 %77, 1
store i32 %78, i32* %11
br label %58
79: ; preds = %58
br label %80
80: ; preds = %79, %55
%81 = load i8*, i8** %10
%82 = load i64, i64* %9
%83 = call i32 @munmap(i8* %81, i64 %82)
%84 = load i32, i32* %6
%85 = call i32 @close(i32 %84)
store i32 0, i32* %3
br label %86
86: ; preds = %80, %16
%87 = load i32, i32* %3
ret i32 %87
}

LLVM IR (-O2):
%struct.fb_var_screeninfo = type { i32, i32, i32, i64 }
%struct.fb_fix_screeninfo = type { i32, i32, i32, i64 }
@.str = [9 x i8] c"/dev/fb1\00"
@O_RDWR = global i32 0
@FBIOGET_FSCREENINFO = global i32 0
@FBIOGET_VSCREENINFO = global i32 0
@.str.5 = [15 x i8] c"%dx%d, %d bpp\0A\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@str = [39 x i8] c"Error: cannot open framebuffer device.\00"
@str.7 = [48 x i8] c"The framebuffer device was opened successfully.\00"
@str.8 = [16 x i8] c"Failed to mmap.\00"
@str.9 = [36 x i8] c"Error reading variable information.\00"
@str.10 = [33 x i8] c"Error reading fixed information.\00"
define i32 @main(i32 %0, i8** nocapture readnone %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 = load i32, i32* @O_RDWR
%8 = tail call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %7)
%9 = icmp eq i32 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %2
%11 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([39 x i8], [39 x i8]* @str, i64 0, i64 0))
br label %59
12: ; preds = %2
%13 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([48 x i8], [48 x i8]* @str.7, i64 0, i64 0))
%14 = load i32, i32* @FBIOGET_FSCREENINFO
%15 = bitcast %struct.fb_fix_screeninfo* %4 to %struct.fb_var_screeninfo*
%16 = call i64 @ioctl(i32 %8, i32 %14, %struct.fb_var_screeninfo* %15)
%17 = icmp eq i64 %16, 0
br i1 %17, label %20, label %18
18: ; preds = %12
%19 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @str.10, i64 0, i64 0))
br label %20
20: ; preds = %18, %12
%21 = load i32, i32* @FBIOGET_VSCREENINFO
%22 = call i64 @ioctl(i32 %8, i32 %21, %struct.fb_var_screeninfo* %3)
%23 = icmp eq i64 %22, 0
br i1 %23, label %26, label %24
24: ; preds = %20
%25 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @str.9, i64 0, i64 0))
br label %26
26: ; preds = %24, %20
%27 = getelementptr inbounds %struct.fb_var_screeninfo, %struct.fb_var_screeninfo* %3, i64 0, i32 0
%28 = load i32, i32* %27
%29 = getelementptr inbounds %struct.fb_var_screeninfo, %struct.fb_var_screeninfo* %3, i64 0, i32 1
%30 = load i32, i32* %29
%31 = getelementptr inbounds %struct.fb_var_screeninfo, %struct.fb_var_screeninfo* %3, i64 0, i32 2
%32 = load i32, i32* %31
%33 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.5, i64 0, i64 0), i32 %28, i32 %30, i32 %32)
%34 = getelementptr inbounds %struct.fb_fix_screeninfo, %struct.fb_fix_screeninfo* %4, i64 0, i32 3
%35 = load i64, i64* %34
%36 = load i32, i32* @PROT_READ
%37 = load i32, i32* @PROT_WRITE
%38 = or i32 %37, %36
%39 = load i32, i32* @MAP_SHARED
%40 = call i64 @mmap(i32 0, i64 %35, i32 %38, i32 %39, i32 %8, i32 0)
%41 = inttoptr i64 %40 to i8*
%42 = trunc i64 %40 to i32
%43 = icmp eq i32 %42, -1
br i1 %43, label %47, label %44
44: ; preds = %26
%45 = sdiv i64 %35, 2
%46 = getelementptr inbounds i8, i8* %41, i64 %45
br label %49
47: ; preds = %26
%48 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str.8, i64 0, i64 0))
br label %56
49: ; preds = %44, %49
%50 = phi i32 [ 0, %44 ], [ %54, %49 ]
%51 = call i32 @memset(i8* %41, i32 %50, i64 %45)
%52 = sub nuw nsw i32 255, %50
%53 = call i32 @memset(i8* %46, i32 %52, i64 %45)
%54 = add nuw nsw i32 %50, 1
%55 = icmp eq i32 %54, 255
br i1 %55, label %56, label %49
56: ; preds = %49, %47
%57 = call i32 @munmap(i8* %41, i64 %35)
%58 = call i32 @close(i32 %8)
br label %59
59: ; preds = %56, %10
%60 = phi i32 [ 0, %56 ], [ 1, %10 ]
ret i32 %60
}