#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 /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ bs_chip_final();
int /*<<< orphan*/ bs_chip_init();
int /*<<< orphan*/ bs_cmd_flag_hif_mode_cmd();
int /*<<< orphan*/ bs_cmd_ld_img_area_data(int, int /*<<< orphan*/, int, int,
                                           int, unsigned char *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int fread(unsigned char *, int, int, int /*<<< orphan*/ *);
char *getenv(char *);
int /*<<< orphan*/ sscanf(char *, char *, int *, int *, int *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ update_area(int, int /*<<< orphan*/, int, int, int);
int main(int argc, char *argv[]) {
  if (argc != 2) {
    fprintf(stderr, "Usage: %s <msg_file>\n", argv[0]);
    return -1;
  }
  int screen_width = 600, screen_height = 800, grayscale = 8;
  char *disp_info = getenv("video");
  if (disp_info) {
    sscanf(disp_info, "%d*%d:%d", &screen_width, &screen_height, &grayscale);
  }
  int wfm_mode = grayscale == 8 ? 3 : 2;
  FILE *fp = fopen(argv[1], "rb");
  if (fp == NULL) {
    fprintf(stderr, "Can't open %s for reading!\n", argv[1]);
    return -1;
  }
  unsigned char bmp[600 * 18];
  if (fread(bmp, 1, 600 * 18, fp) != 600 * 18) {
    fprintf(stderr, "Invalid message file!\n");
    fclose(fp);
    return -1;
  }
  fclose(fp);
  bs_chip_init();
  bs_cmd_flag_hif_mode_cmd();
  bs_cmd_ld_img_area_data(3, 0, screen_height - 2 - 18, 600, 18, bmp);
  update_area(wfm_mode, 0, screen_height - 2 - 18, 600, 18);
  bs_chip_final();
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x2a70,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     11fd <main+0x4d>
mov    0x2e06(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe16(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1368 <main+0x1b8>
movl   $0x258,-0x14(%rbp)
movl   $0x320,-0x18(%rbp)
movl   $0x8,-0x1c(%rbp)
lea    0xdfd(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     1249 <main+0x99>
mov    -0x28(%rbp),%rdi
lea    0xde4(%rip),%rsi
lea    -0x14(%rbp),%rdx
lea    -0x18(%rbp),%rcx
lea    -0x1c(%rbp),%r8
call   10b0 <sscanf@plt>
mov    -0x1c(%rbp),%edx
mov    $0x2,%eax
mov    $0x3,%ecx
cmp    $0x8,%edx
cmove  %ecx,%eax
mov    %eax,-0x2c(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xdb7(%rip),%rsi
call   10d0 <fopen@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    12ad <main+0xfd>
mov    0x2d57(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdx
lea    0xd8e(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1368 <main+0x1b8>
lea    -0x2a70(%rbp),%rdi
mov    -0x38(%rbp),%rcx
mov    $0x1,%esi
mov    $0x2a30,%edx
call   1060 <fread@plt>
cmp    $0x2a30,%eax
je     12fe <main+0x14e>
mov    0x2d07(%rip),%rax
mov    (%rax),%edi
lea    0xd62(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    -0x38(%rbp),%rdi
call   1070 <fclose@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1368 <main+0x1b8>
mov    -0x38(%rbp),%rdi
call   1070 <fclose@plt>
mov    $0x0,%al
call   1050 <bs_chip_init@plt>
mov    $0x0,%al
call   1040 <bs_cmd_flag_hif_mode_cmd@plt>
mov    -0x18(%rbp),%edx
sub    $0x2,%edx
sub    $0x12,%edx
lea    -0x2a70(%rbp),%r9
mov    $0x3,%edi
xor    %esi,%esi
mov    $0x258,%ecx
mov    $0x12,%r8d
call   1090 <bs_cmd_ld_img_area_data@plt>
mov    -0x2c(%rbp),%edi
mov    -0x18(%rbp),%edx
sub    $0x2,%edx
sub    $0x12,%edx
xor    %esi,%esi
mov    $0x258,%ecx
mov    $0x12,%r8d
call   10a0 <update_area@plt>
mov    $0x0,%al
call   10c0 <bs_chip_final@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x2a70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x2a40,%rsp
mov    %rsi,%rbx
cmp    $0x2,%edi
jne    121c <main+0x6c>
movl   $0x258,0xc(%rsp)
movl   $0x320,0x4(%rsp)
movl   $0x8,0x8(%rsp)
lea    0xe34(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     1234 <main+0x84>
lea    0xe29(%rip),%rsi
lea    0xc(%rsp),%rdx
lea    0x4(%rsp),%rcx
lea    0x8(%rsp),%r8
mov    %rax,%rdi
call   10b0 <sscanf@plt>
xor    %r14d,%r14d
cmpl   $0x8,0x8(%rsp)
sete   %r14b
or     $0x2,%r14d
jmp    123a <main+0x8a>
mov    0x2dbd(%rip),%rax
mov    (%rax),%edi
mov    (%rbx),%rdx
lea    0xdd1(%rip),%rsi
jmp    12ec <main+0x13c>
mov    $0x3,%r14d
mov    0x8(%rbx),%rdi
lea    0xde0(%rip),%rsi
call   10d0 <fopen@plt>
test   %rax,%rax
je     12d8 <main+0x128>
mov    %rax,%rbp
lea    0x10(%rsp),%rdi
mov    $0x1,%esi
mov    $0x2a30,%edx
mov    %rax,%rcx
call   1060 <fread@plt>
cmp    $0x2a30,%eax
jne    1306 <main+0x156>
mov    %rbp,%rdi
call   1070 <fclose@plt>
xor    %ebx,%ebx
xor    %eax,%eax
call   1050 <bs_chip_init@plt>
xor    %eax,%eax
call   1040 <bs_cmd_flag_hif_mode_cmd@plt>
mov    0x4(%rsp),%edx
add    $0xffffffec,%edx
lea    0x10(%rsp),%r9
mov    $0x3,%edi
xor    %esi,%esi
mov    $0x258,%ecx
mov    $0x12,%r8d
call   1090 <bs_cmd_ld_img_area_data@plt>
mov    0x4(%rsp),%edx
add    $0xffffffec,%edx
mov    %r14d,%edi
xor    %esi,%esi
mov    $0x258,%ecx
mov    $0x12,%r8d
call   10a0 <update_area@plt>
xor    %eax,%eax
call   10c0 <bs_chip_final@plt>
jmp    12f8 <main+0x148>
mov    0x2d01(%rip),%rax
mov    (%rax),%edi
mov    0x8(%rbx),%rdx
lea    0xd3c(%rip),%rsi
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x2a40,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    0x2cd3(%rip),%rax
mov    (%rax),%edi
lea    0xd2e(%rip),%rsi
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    %rbp,%rdi
call   1070 <fclose@plt>
jmp    12f3 <main+0x143>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [22 x i8] c"Usage: %s <msg_file>\0A\00"
@.str.1 = [6 x i8] c"video\00"
@.str.2 = [9 x i8] c"%d*%d:%d\00"
@.str.3 = [3 x i8] c"rb\00"
@.str.4 = [28 x i8] c"Can't open %s for reading!\0A\00"
@.str.5 = [23 x i8] c"Invalid message file!\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i32*
%12 = alloca [10800 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%13 = load i32, i32* %4
%14 = icmp ne i32 %13, 2
br i1 %14, label %15, label %21
15: ; preds = %2
%16 = load i32, i32* @stderr
%17 = load i8**, i8*** %5
%18 = getelementptr inbounds i8*, i8** %17, i64 0
%19 = load i8*, i8** %18
%20 = call i32 (i32, i8*, ...) @fprintf(i32 %16, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* %19)
store i32 -1, i32* %3
br label %71
21: ; preds = %2
store i32 600, i32* %6
store i32 800, i32* %7
store i32 8, i32* %8
%22 = call i8* @getenv(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
store i8* %22, i8** %9
%23 = load i8*, i8** %9
%24 = icmp ne i8* %23, null
br i1 %24, label %25, label %28
25: ; preds = %21
%26 = load i8*, i8** %9
%27 = call i32 @sscanf(i8* %26, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i32* %6, i32* %7, i32* %8)
br label %28
28: ; preds = %25, %21
%29 = load i32, i32* %8
%30 = icmp eq i32 %29, 8
%31 = zext i1 %30 to i64
%32 = select i1 %30, i32 3, i32 2
store i32 %32, i32* %10
%33 = load i8**, i8*** %5
%34 = getelementptr inbounds i8*, i8** %33, i64 1
%35 = load i8*, i8** %34
%36 = call i32* @fopen(i8* %35, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
store i32* %36, i32** %11
%37 = load i32*, i32** %11
%38 = icmp eq i32* %37, null
br i1 %38, label %39, label %45
39: ; preds = %28
%40 = load i32, i32* @stderr
%41 = load i8**, i8*** %5
%42 = getelementptr inbounds i8*, i8** %41, i64 1
%43 = load i8*, i8** %42
%44 = call i32 (i32, i8*, ...) @fprintf(i32 %40, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.4, i64 0, i64 0), i8* %43)
store i32 -1, i32* %3
br label %71
45: ; preds = %28
%46 = getelementptr inbounds [10800 x i8], [10800 x i8]* %12, i64 0, i64 0
%47 = load i32*, i32** %11
%48 = call i32 @fread(i8* %46, i32 1, i32 10800, i32* %47)
%49 = icmp ne i32 %48, 10800
br i1 %49, label %50, label %55
50: ; preds = %45
%51 = load i32, i32* @stderr
%52 = call i32 (i32, i8*, ...) @fprintf(i32 %51, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.5, i64 0, i64 0))
%53 = load i32*, i32** %11
%54 = call i32 @fclose(i32* %53)
store i32 -1, i32* %3
br label %71
55: ; preds = %45
%56 = load i32*, i32** %11
%57 = call i32 @fclose(i32* %56)
%58 = call i32 (...) @bs_chip_init()
%59 = call i32 (...) @bs_cmd_flag_hif_mode_cmd()
%60 = load i32, i32* %7
%61 = sub nsw i32 %60, 2
%62 = sub nsw i32 %61, 18
%63 = getelementptr inbounds [10800 x i8], [10800 x i8]* %12, i64 0, i64 0
%64 = call i32 @bs_cmd_ld_img_area_data(i32 3, i32 0, i32 %62, i32 600, i32 18, i8* %63)
%65 = load i32, i32* %10
%66 = load i32, i32* %7
%67 = sub nsw i32 %66, 2
%68 = sub nsw i32 %67, 18
%69 = call i32 @update_area(i32 %65, i32 0, i32 %68, i32 600, i32 18)
%70 = call i32 (...) @bs_chip_final()
store i32 0, i32* %3
br label %71
71: ; preds = %55, %50, %39, %15
%72 = load i32, i32* %3
ret i32 %72
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [22 x i8] c"Usage: %s <msg_file>\0A\00"
@.str.1 = [6 x i8] c"video\00"
@.str.2 = [9 x i8] c"%d*%d:%d\00"
@.str.3 = [3 x i8] c"rb\00"
@.str.4 = [28 x i8] c"Can't open %s for reading!\0A\00"
@.str.5 = [23 x i8] c"Invalid message file!\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca [10800 x i8]
%7 = icmp eq i32 %0, 2
br i1 %7, label %12, label %8
8: ; preds = %2
%9 = load i32, i32* @stderr
%10 = load i8*, i8** %1
%11 = tail call i32 (i32, i8*, ...) @fprintf(i32 %9, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* %10)
br label %56
12: ; preds = %2
%13 = bitcast i32* %3 to i8*
store i32 600, i32* %3
%14 = bitcast i32* %4 to i8*
store i32 800, i32* %4
%15 = bitcast i32* %5 to i8*
store i32 8, i32* %5
%16 = tail call i8* @getenv(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%17 = icmp eq i8* %16, null
br i1 %17, label %23, label %18
18: ; preds = %12
%19 = call i32 @sscanf(i8* %16, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i32* %3, i32* %4, i32* %5)
%20 = load i32, i32* %5
%21 = icmp eq i32 %20, 8
%22 = select i1 %21, i32 3, i32 2
br label %23
23: ; preds = %18, %12
%24 = phi i32 [ %22, %18 ], [ 3, %12 ]
%25 = getelementptr inbounds i8*, i8** %1, i64 1
%26 = load i8*, i8** %25
%27 = call i32* @fopen(i8* %26, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
%28 = icmp eq i32* %27, null
br i1 %28, label %29, label %33
29: ; preds = %23
%30 = load i32, i32* @stderr
%31 = load i8*, i8** %25
%32 = call i32 (i32, i8*, ...) @fprintf(i32 %30, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.4, i64 0, i64 0), i8* %31)
br label %54
33: ; preds = %23
%34 = getelementptr inbounds [10800 x i8], [10800 x i8]* %6, i64 0, i64 0
%35 = call i32 @fread(i8* %34, i32 1, i32 10800, i32* %27)
%36 = icmp eq i32 %35, 10800
br i1 %36, label %41, label %37
37: ; preds = %33
%38 = load i32, i32* @stderr
%39 = call i32 (i32, i8*, ...) @fprintf(i32 %38, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.5, i64 0, i64 0))
%40 = call i32 @fclose(i32* %27)
br label %52
41: ; preds = %33
%42 = call i32 @fclose(i32* %27)
%43 = call i32 (...) @bs_chip_init()
%44 = call i32 (...) @bs_cmd_flag_hif_mode_cmd()
%45 = load i32, i32* %4
%46 = add nsw i32 %45, -20
%47 = call i32 @bs_cmd_ld_img_area_data(i32 3, i32 0, i32 %46, i32 600, i32 18, i8* %34)
%48 = load i32, i32* %4
%49 = add nsw i32 %48, -20
%50 = call i32 @update_area(i32 %24, i32 0, i32 %49, i32 600, i32 18)
%51 = call i32 (...) @bs_chip_final()
br label %52
52: ; preds = %41, %37
%53 = phi i32 [ -1, %37 ], [ 0, %41 ]
br label %54
54: ; preds = %52, %29
%55 = phi i32 [ -1, %29 ], [ %53, %52 ]
br label %56
56: ; preds = %54, %8
%57 = phi i32 [ -1, %8 ], [ %55, %54 ]
ret i32 %57
}