#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 {
  int len;
  int /*<<< orphan*/ *transp;
  int /*<<< orphan*/ *green;
  int /*<<< orphan*/ *blue;
  int /*<<< orphan*/ *red;
};
struct fb_cmap {
  int len;
  int /*<<< orphan*/ *transp;
  int /*<<< orphan*/ *green;
  int /*<<< orphan*/ *blue;
  int /*<<< orphan*/ *red;
};
typedef int /*<<< orphan*/ cmap;
typedef int /*<<< orphan*/ __u16;

/* Variables and functions */
int /*<<< orphan*/ FBIOGETCMAP;
int /*<<< orphan*/ FBIOGET_FSCREENINFO;
int /*<<< orphan*/ FBIOPUTCMAP;
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dump_color_map(struct fb_fix_screeninfo *);
int /*<<< orphan*/ dump_fixed_screen_info(struct fb_fix_screeninfo *);
int /*<<< orphan*/ free(int /*<<< orphan*/ *);
int ioctl(int, int /*<<< orphan*/, struct fb_fix_screeninfo *);
scalar_t__ malloc(int);
int /*<<< orphan*/ memset(struct fb_fix_screeninfo *, int /*<<< orphan*/, int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ test_write(int, struct fb_fix_screeninfo *);
int main(int argc, char *argv[]) {
  int fb = open("/dev/fb", O_RDWR);
  if (fb == -1) {
    perror("open:\n");
    return fb;
  }
  struct fb_fix_screeninfo fixed;
  int rc = ioctl(fb, FBIOGET_FSCREENINFO, &fixed);
  if (rc == -1) {
    perror("ioctl: FBIOGET_FSCREENINFO\n");
    goto error;
  }
  dump_fixed_screen_info(&fixed);
  struct fb_cmap cmap;
  memset(&cmap, 0, sizeof(cmap));
  cmap.len = 64;
  cmap.red = (__u16 *)malloc(cmap.len * sizeof(__u16));
  cmap.green = (__u16 *)malloc(cmap.len * sizeof(__u16));
  cmap.blue = (__u16 *)malloc(cmap.len * sizeof(__u16));
  cmap.transp = (__u16 *)malloc(cmap.len * sizeof(__u16));
  rc = ioctl(fb, FBIOGETCMAP, &cmap);
  if (rc == -1) {
    perror("ioctl: FBIOGETCMAP\n");
    goto error;
  }
  dump_color_map(&cmap);
  printf("Test put pallette\n");
  rc = ioctl(fb, FBIOPUTCMAP, &cmap);
  if (rc == -1) {
    perror("ioctl: FBIOPUTCMAP\n");
    goto error;
  }
  free(cmap.red);
  free(cmap.blue);
  free(cmap.green);
  free(cmap.transp);
  printf("Test write to vid mem\n");
  test_write(fb, &fixed);
error:
  close(fb);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e1b(%rip),%rax
mov    (%rax),%esi
lea    0xe2a(%rip),%rdi
call   10b0 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11ff <main+0x4f>
lea    0xe19(%rip),%rdi
call   10c0 <perror@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1364 <main+0x1b4>
mov    -0x14(%rbp),%edi
mov    0x2dc7(%rip),%rax
mov    (%rax),%esi
lea    -0x40(%rbp),%rdx
call   1070 <ioctl@plt>
mov    %eax,-0x44(%rbp)
cmpl   $0xffffffff,-0x44(%rbp)
jne    1232 <main+0x82>
lea    0xde7(%rip),%rdi
call   10c0 <perror@plt>
jmp    1355 <main+0x1a5>
lea    -0x40(%rbp),%rdi
call   10a0 <dump_fixed_screen_info@plt>
lea    -0x70(%rbp),%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1060 <memset@plt>
movl   $0x40,-0x70(%rbp)
movslq -0x70(%rbp),%rax
shl    $0x2,%rax
mov    %eax,%edi
call   1090 <malloc@plt>
mov    %rax,-0x50(%rbp)
movslq -0x70(%rbp),%rax
shl    $0x2,%rax
mov    %eax,%edi
call   1090 <malloc@plt>
mov    %rax,-0x60(%rbp)
movslq -0x70(%rbp),%rax
shl    $0x2,%rax
mov    %eax,%edi
call   1090 <malloc@plt>
mov    %rax,-0x58(%rbp)
movslq -0x70(%rbp),%rax
shl    $0x2,%rax
mov    %eax,%edi
call   1090 <malloc@plt>
mov    %rax,-0x68(%rbp)
mov    -0x14(%rbp),%edi
mov    0x2d38(%rip),%rax
mov    (%rax),%esi
lea    -0x70(%rbp),%rdx
call   1070 <ioctl@plt>
mov    %eax,-0x44(%rbp)
cmpl   $0xffffffff,-0x44(%rbp)
jne    12d1 <main+0x121>
lea    0xd64(%rip),%rdi
call   10c0 <perror@plt>
jmp    1355 <main+0x1a5>
lea    -0x70(%rbp),%rdi
call   10d0 <dump_color_map@plt>
lea    0xd5e(%rip),%rdi
call   1050 <printf@plt>
mov    -0x14(%rbp),%edi
mov    0x2cd8(%rip),%rax
mov    (%rax),%esi
lea    -0x70(%rbp),%rdx
call   1070 <ioctl@plt>
mov    %eax,-0x44(%rbp)
cmpl   $0xffffffff,-0x44(%rbp)
jne    1319 <main+0x169>
lea    0xd43(%rip),%rdi
call   10c0 <perror@plt>
jmp    1355 <main+0x1a5>
mov    -0x50(%rbp),%rdi
call   1030 <free@plt>
mov    -0x58(%rbp),%rdi
call   1030 <free@plt>
mov    -0x60(%rbp),%rdi
call   1030 <free@plt>
mov    -0x68(%rbp),%rdi
call   1030 <free@plt>
lea    0xd22(%rip),%rdi
call   1050 <printf@plt>
mov    -0x14(%rbp),%edi
lea    -0x40(%rbp),%rsi
call   1040 <test_write@plt>
mov    -0x14(%rbp),%edi
call   1080 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x58,%rsp
mov    0x2e2a(%rip),%rax
mov    (%rax),%esi
lea    0xe39(%rip),%rdi
call   10b0 <open@plt>
cmp    $0xffffffff,%eax
je     12ff <main+0x14f>
mov    %eax,%ebx
mov    0x2df2(%rip),%rax
mov    (%rax),%esi
lea    0x30(%rsp),%rdx
mov    %ebx,%edi
call   1070 <ioctl@plt>
cmp    $0xffffffff,%eax
je     1312 <main+0x162>
lea    0x30(%rsp),%rdi
call   10a0 <dump_fixed_screen_info@plt>
lea    0x8(%rsp),%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1060 <memset@plt>
movl   $0x40,0x8(%rsp)
mov    $0x100,%edi
call   1090 <malloc@plt>
mov    %rax,0x28(%rsp)
mov    0x8(%rsp),%edi
shl    $0x2,%edi
call   1090 <malloc@plt>
mov    %rax,0x18(%rsp)
mov    0x8(%rsp),%edi
shl    $0x2,%edi
call   1090 <malloc@plt>
mov    %rax,0x20(%rsp)
mov    0x8(%rsp),%edi
shl    $0x2,%edi
call   1090 <malloc@plt>
mov    %rax,0x10(%rsp)
mov    0x2d7c(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
mov    %r14,%rdx
call   1070 <ioctl@plt>
cmp    $0xffffffff,%eax
je     1320 <main+0x170>
lea    0x8(%rsp),%r14
mov    %r14,%rdi
call   10d0 <dump_color_map@plt>
lea    0xdc6(%rip),%rdi
call   1050 <puts@plt>
mov    0x2d2f(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
mov    %r14,%rdx
call   1070 <ioctl@plt>
cmp    $0xffffffff,%eax
je     132e <main+0x17e>
mov    0x28(%rsp),%rdi
call   1030 <free@plt>
mov    0x20(%rsp),%rdi
call   1030 <free@plt>
mov    0x18(%rsp),%rdi
call   1030 <free@plt>
mov    0x10(%rsp),%rdi
call   1030 <free@plt>
lea    0xd88(%rip),%rdi
call   1050 <puts@plt>
lea    0x30(%rsp),%rsi
mov    %ebx,%edi
call   1040 <test_write@plt>
mov    %ebx,%edi
call   1080 <close@plt>
xor    %eax,%eax
add    $0x58,%rsp
pop    %rbx
pop    %r14
ret
lea    0xd02(%rip),%rdi
call   10c0 <perror@plt>
mov    $0xffffffff,%eax
jmp    12f7 <main+0x147>
lea    0xcf6(%rip),%rdi
call   10c0 <perror@plt>
jmp    12ee <main+0x13e>
lea    0xd04(%rip),%rdi
call   10c0 <perror@plt>
jmp    12ee <main+0x13e>
lea    0xd0a(%rip),%rdi
call   10c0 <perror@plt>
jmp    12ee <main+0x13e>

LLVM IR (-O0):
%struct.fb_fix_screeninfo = type { i32, i32*, i32*, i32*, i32* }
%struct.fb_cmap = type { i32, i32*, i32*, i32*, i32* }
@.str = [8 x i8] c"/dev/fb\00"
@O_RDWR = global i32 0
@.str.1 = [7 x i8] c"open:\0A\00"
@FBIOGET_FSCREENINFO = global i32 0
@.str.2 = [28 x i8] c"ioctl: FBIOGET_FSCREENINFO\0A\00"
@FBIOGETCMAP = global i32 0
@.str.3 = [20 x i8] c"ioctl: FBIOGETCMAP\0A\00"
@.str.4 = [19 x i8] c"Test put pallette\0A\00"
@FBIOPUTCMAP = global i32 0
@.str.5 = [20 x i8] c"ioctl: FBIOPUTCMAP\0A\00"
@.str.6 = [23 x i8] c"Test write to vid mem\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_fix_screeninfo
%8 = alloca i32
%9 = alloca %struct.fb_cmap
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* @O_RDWR
%11 = call i32 @open(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32 %10)
store i32 %11, i32* %6
%12 = load i32, i32* %6
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %17
14: ; preds = %2
%15 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%16 = load i32, i32* %6
store i32 %16, i32* %3
br label %101
17: ; preds = %2
%18 = load i32, i32* %6
%19 = load i32, i32* @FBIOGET_FSCREENINFO
%20 = call i32 @ioctl(i32 %18, i32 %19, %struct.fb_fix_screeninfo* %7)
store i32 %20, i32* %8
%21 = load i32, i32* %8
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %25
23: ; preds = %17
%24 = call i32 @perror(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0))
br label %98
25: ; preds = %17
%26 = call i32 @dump_fixed_screen_info(%struct.fb_fix_screeninfo* %7)
%27 = bitcast %struct.fb_cmap* %9 to %struct.fb_fix_screeninfo*
%28 = call i32 @memset(%struct.fb_fix_screeninfo* %27, i32 0, i32 40)
%29 = getelementptr inbounds %struct.fb_cmap, %struct.fb_cmap* %9, i32 0, i32 0
store i32 64, i32* %29
%30 = getelementptr inbounds %struct.fb_cmap, %struct.fb_cmap* %9, i32 0, i32 0
%31 = load i32, i32* %30
%32 = sext i32 %31 to i64
%33 = mul i64 %32, 4
%34 = trunc i64 %33 to i32
%35 = call i64 @malloc(i32 %34)
%36 = inttoptr i64 %35 to i32*
%37 = getelementptr inbounds %struct.fb_cmap, %struct.fb_cmap* %9, i32 0, i32 4
store i32* %36, i32** %37
%38 = getelementptr inbounds %struct.fb_cmap, %struct.fb_cmap* %9, i32 0, i32 0
%39 = load i32, i32* %38
%40 = sext i32 %39 to i64
%41 = mul i64 %40, 4
%42 = trunc i64 %41 to i32
%43 = call i64 @malloc(i32 %42)
%44 = inttoptr i64 %43 to i32*
%45 = getelementptr inbounds %struct.fb_cmap, %struct.fb_cmap* %9, i32 0, i32 2
store i32* %44, i32** %45
%46 = getelementptr inbounds %struct.fb_cmap, %struct.fb_cmap* %9, i32 0, i32 0
%47 = load i32, i32* %46
%48 = sext i32 %47 to i64
%49 = mul i64 %48, 4
%50 = trunc i64 %49 to i32
%51 = call i64 @malloc(i32 %50)
%52 = inttoptr i64 %51 to i32*
%53 = getelementptr inbounds %struct.fb_cmap, %struct.fb_cmap* %9, i32 0, i32 3
store i32* %52, i32** %53
%54 = getelementptr inbounds %struct.fb_cmap, %struct.fb_cmap* %9, i32 0, i32 0
%55 = load i32, i32* %54
%56 = sext i32 %55 to i64
%57 = mul i64 %56, 4
%58 = trunc i64 %57 to i32
%59 = call i64 @malloc(i32 %58)
%60 = inttoptr i64 %59 to i32*
%61 = getelementptr inbounds %struct.fb_cmap, %struct.fb_cmap* %9, i32 0, i32 1
store i32* %60, i32** %61
%62 = load i32, i32* %6
%63 = load i32, i32* @FBIOGETCMAP
%64 = bitcast %struct.fb_cmap* %9 to %struct.fb_fix_screeninfo*
%65 = call i32 @ioctl(i32 %62, i32 %63, %struct.fb_fix_screeninfo* %64)
store i32 %65, i32* %8
%66 = load i32, i32* %8
%67 = icmp eq i32 %66, -1
br i1 %67, label %68, label %70
68: ; preds = %25
%69 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0))
br label %98
70: ; preds = %25
%71 = bitcast %struct.fb_cmap* %9 to %struct.fb_fix_screeninfo*
%72 = call i32 @dump_color_map(%struct.fb_fix_screeninfo* %71)
%73 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0))
%74 = load i32, i32* %6
%75 = load i32, i32* @FBIOPUTCMAP
%76 = bitcast %struct.fb_cmap* %9 to %struct.fb_fix_screeninfo*
%77 = call i32 @ioctl(i32 %74, i32 %75, %struct.fb_fix_screeninfo* %76)
store i32 %77, i32* %8
%78 = load i32, i32* %8
%79 = icmp eq i32 %78, -1
br i1 %79, label %80, label %82
80: ; preds = %70
%81 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.5, i64 0, i64 0))
br label %98
82: ; preds = %70
%83 = getelementptr inbounds %struct.fb_cmap, %struct.fb_cmap* %9, i32 0, i32 4
%84 = load i32*, i32** %83
%85 = call i32 @free(i32* %84)
%86 = getelementptr inbounds %struct.fb_cmap, %struct.fb_cmap* %9, i32 0, i32 3
%87 = load i32*, i32** %86
%88 = call i32 @free(i32* %87)
%89 = getelementptr inbounds %struct.fb_cmap, %struct.fb_cmap* %9, i32 0, i32 2
%90 = load i32*, i32** %89
%91 = call i32 @free(i32* %90)
%92 = getelementptr inbounds %struct.fb_cmap, %struct.fb_cmap* %9, i32 0, i32 1
%93 = load i32*, i32** %92
%94 = call i32 @free(i32* %93)
%95 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.6, i64 0, i64 0))
%96 = load i32, i32* %6
%97 = call i32 @test_write(i32 %96, %struct.fb_fix_screeninfo* %7)
br label %98
98: ; preds = %82, %80, %68, %23
%99 = load i32, i32* %6
%100 = call i32 @close(i32 %99)
store i32 0, i32* %3
br label %101
101: ; preds = %98, %14
%102 = load i32, i32* %3
ret i32 %102
}

LLVM IR (-O2):
%struct.fb_fix_screeninfo = type { i32, i32*, i32*, i32*, i32* }
%struct.fb_cmap = type { i32, i32*, i32*, i32*, i32* }
@.str = [8 x i8] c"/dev/fb\00"
@O_RDWR = global i32 0
@.str.1 = [7 x i8] c"open:\0A\00"
@FBIOGET_FSCREENINFO = global i32 0
@.str.2 = [28 x i8] c"ioctl: FBIOGET_FSCREENINFO\0A\00"
@FBIOGETCMAP = global i32 0
@.str.3 = [20 x i8] c"ioctl: FBIOGETCMAP\0A\00"
@FBIOPUTCMAP = global i32 0
@.str.5 = [20 x i8] c"ioctl: FBIOPUTCMAP\0A\00"
@str = [18 x i8] c"Test put pallette\00"
@str.7 = [22 x i8] c"Test write to vid mem\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca %struct.fb_fix_screeninfo
%4 = alloca %struct.fb_cmap
%5 = load i32, i32* @O_RDWR
%6 = tail call i32 @open(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32 %5)
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %10
8: ; preds = %2
%9 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
br label %66
10: ; preds = %2
%11 = bitcast %struct.fb_fix_screeninfo* %3 to i8*
%12 = load i32, i32* @FBIOGET_FSCREENINFO
%13 = call i32 @ioctl(i32 %6, i32 %12, %struct.fb_fix_screeninfo* %3)
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %17
15: ; preds = %10
%16 = call i32 @perror(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0))
br label %64
17: ; preds = %10
%18 = call i32 @dump_fixed_screen_info(%struct.fb_fix_screeninfo* %3)
%19 = bitcast %struct.fb_cmap* %4 to %struct.fb_fix_screeninfo*
%20 = call i32 @memset(%struct.fb_fix_screeninfo* %19, i32 0, i32 40)
%21 = getelementptr inbounds %struct.fb_cmap, %struct.fb_cmap* %4, i64 0, i32 0
store i32 64, i32* %21
%22 = call i64 @malloc(i32 256)
%23 = inttoptr i64 %22 to i32*
%24 = getelementptr inbounds %struct.fb_cmap, %struct.fb_cmap* %4, i64 0, i32 4
store i32* %23, i32** %24
%25 = load i32, i32* %21
%26 = shl i32 %25, 2
%27 = call i64 @malloc(i32 %26)
%28 = inttoptr i64 %27 to i32*
%29 = getelementptr inbounds %struct.fb_cmap, %struct.fb_cmap* %4, i64 0, i32 2
store i32* %28, i32** %29
%30 = load i32, i32* %21
%31 = shl i32 %30, 2
%32 = call i64 @malloc(i32 %31)
%33 = inttoptr i64 %32 to i32*
%34 = getelementptr inbounds %struct.fb_cmap, %struct.fb_cmap* %4, i64 0, i32 3
store i32* %33, i32** %34
%35 = load i32, i32* %21
%36 = shl i32 %35, 2
%37 = call i64 @malloc(i32 %36)
%38 = inttoptr i64 %37 to i32*
%39 = getelementptr inbounds %struct.fb_cmap, %struct.fb_cmap* %4, i64 0, i32 1
store i32* %38, i32** %39
%40 = load i32, i32* @FBIOGETCMAP
%41 = call i32 @ioctl(i32 %6, i32 %40, %struct.fb_fix_screeninfo* %19)
%42 = icmp eq i32 %41, -1
br i1 %42, label %43, label %45
43: ; preds = %17
%44 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0))
br label %64
45: ; preds = %17
%46 = call i32 @dump_color_map(%struct.fb_fix_screeninfo* %19)
%47 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str, i64 0, i64 0))
%48 = load i32, i32* @FBIOPUTCMAP
%49 = call i32 @ioctl(i32 %6, i32 %48, %struct.fb_fix_screeninfo* %19)
%50 = icmp eq i32 %49, -1
br i1 %50, label %51, label %53
51: ; preds = %45
%52 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.5, i64 0, i64 0))
br label %64
53: ; preds = %45
%54 = load i32*, i32** %24
%55 = call i32 @free(i32* %54)
%56 = load i32*, i32** %34
%57 = call i32 @free(i32* %56)
%58 = load i32*, i32** %29
%59 = call i32 @free(i32* %58)
%60 = load i32*, i32** %39
%61 = call i32 @free(i32* %60)
%62 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @str.7, i64 0, i64 0))
%63 = call i32 @test_write(i32 %6, %struct.fb_fix_screeninfo* %3)
br label %64
64: ; preds = %53, %51, %43, %15
%65 = call i32 @close(i32 %6)
br label %66
66: ; preds = %64, %8
%67 = phi i32 [ -1, %8 ], [ 0, %64 ]
ret i32 %67
}