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

/* Variables and functions */
void *MAP_FAILED;
int /*<<< orphan*/ MAP_SHARED;
int /*<<< orphan*/ O_RDWR;
int PROT_READ;
int PROT_WRITE;
int S_IRUSR;
int S_IWUSR;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
void *mmap(int /*<<< orphan*/ *, int, int, int /*<<< orphan*/, int,
           int /*<<< orphan*/);
int /*<<< orphan*/ munmap(void *, int);
int shm_open(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int main(int argc, char **argv) {
  void *shm_buf;
  int shm_fd = shm_open("shared_fb", O_RDWR, S_IRUSR | S_IWUSR);
  if (shm_fd > 0) {
    shm_buf = mmap(NULL, 320 * 200 * 3, PROT_READ | PROT_WRITE, MAP_SHARED,
                   shm_fd, (off_t)0);
    if (shm_buf == MAP_FAILED) {
      fprintf(stderr, "mmap failed: %s\n", strerror(errno));
      shm_buf = NULL;
      return 1;
    }
  } else {
    fprintf(stderr, "shm_open failed: %s\n", strerror(errno));
    return 1;
  }
  unsigned char *fb = (unsigned char *)shm_buf;
  int color = 0;
  while (1) {
    color += 8;
    color = color % 255;
    sleep(1);
    unsigned char *data = fb;
    for (int y = 0; y < 200; y++) {
      for (int x = 0; x < 320; x++) {
        *data++ = color;
        *data++ = color;
        *data++ = color;
      }
    }
  }
  munmap(shm_buf, 320 * 200 * 3);
  close(shm_fd);
  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)
mov    0x2e6b(%rip),%rax
mov    (%rax),%esi
mov    0x2e32(%rip),%rax
mov    (%rax),%edx
mov    0x2e51(%rip),%rax
or     (%rax),%edx
lea    0xe78(%rip),%rdi
call   1050 <shm_open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jle    1227 <main+0xd7>
mov    0x2e57(%rip),%rax
mov    (%rax),%edx
mov    0x2e06(%rip),%rax
or     (%rax),%edx
mov    0x2e35(%rip),%rax
mov    (%rax),%ecx
mov    -0x1c(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
mov    $0x2ee00,%esi
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    0x2dc7(%rip),%rcx
cmp    (%rcx),%rax
jne    1222 <main+0xd2>
mov    0x2dd7(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x44(%rbp)
mov    0x2dc3(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
mov    -0x44(%rbp),%edi
mov    %rax,%rdx
lea    0xe01(%rip),%rsi
call   1040 <fprintf@plt>
movq   $0x0,-0x18(%rbp)
movl   $0x1,-0x4(%rbp)
jmp    1327 <main+0x1d7>
jmp    125f <main+0x10f>
mov    0x2d92(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x48(%rbp)
mov    0x2d7e(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
mov    -0x48(%rbp),%edi
mov    %rax,%rdx
lea    0xdcd(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1327 <main+0x1d7>
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
add    $0x8,%eax
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
mov    $0xff,%ecx
cltd
idiv   %ecx
mov    %edx,-0x2c(%rbp)
mov    $0x1,%edi
call   1070 <sleep@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x38(%rbp)
movl   $0x0,-0x3c(%rbp)
cmpl   $0xc8,-0x3c(%rbp)
jge    1322 <main+0x1d2>
movl   $0x0,-0x40(%rbp)
cmpl   $0x140,-0x40(%rbp)
jge    130f <main+0x1bf>
mov    -0x2c(%rbp),%eax
mov    %al,%cl
mov    -0x38(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x38(%rbp)
mov    %cl,(%rax)
mov    -0x2c(%rbp),%eax
mov    %al,%cl
mov    -0x38(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x38(%rbp)
mov    %cl,(%rax)
mov    -0x2c(%rbp),%eax
mov    %al,%cl
mov    -0x38(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x38(%rbp)
mov    %cl,(%rax)
mov    -0x40(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x40(%rbp)
jmp    12b2 <main+0x162>
jmp    1314 <main+0x1c4>
mov    -0x3c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x3c(%rbp)
jmp    129e <main+0x14e>
jmp    126e <main+0x11e>
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
mov    0x2e6d(%rip),%rax
mov    (%rax),%esi
mov    0x2e34(%rip),%rax
mov    0x2e55(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
lea    0xe7a(%rip),%rdi
call   1060 <shm_open@plt>
test   %eax,%eax
jle    1228 <main+0xc8>
mov    0x2e5e(%rip),%rcx
mov    0x2e0f(%rip),%rdx
mov    (%rdx),%edx
or     (%rcx),%edx
mov    0x2e3c(%rip),%rcx
mov    (%rcx),%ecx
xor    %ebx,%ebx
xor    %edi,%edi
mov    $0x2ee00,%esi
mov    %eax,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,%r14
mov    0x2dd4(%rip),%rax
cmp    (%rax),%r14
je     1231 <main+0xd1>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %ebx,%eax
add    $0x8,%eax
cltq
imul   $0xffffffff80808081,%rax,%rax
shr    $0x20,%rax
add    %ebx,%eax
add    $0x8,%eax
mov    %eax,%ecx
shr    $0x1f,%ecx
sar    $0x7,%eax
add    %ecx,%eax
mov    %eax,%ecx
shl    $0x8,%ecx
sub    %ecx,%eax
add    %eax,%ebx
add    $0x8,%ebx
mov    $0x1,%edi
call   1080 <sleep@plt>
mov    $0x2ee00,%edx
mov    %r14,%rdi
mov    %ebx,%esi
call   1040 <memset@plt>
jmp    11e0 <main+0x80>
lea    0xdec(%rip),%rbx
jmp    1238 <main+0xd8>
lea    0xdd2(%rip),%rbx
mov    0x2d81(%rip),%rax
mov    (%rax),%ebp
mov    0x2d70(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %rax,%rdx
call   1050 <fprintf@plt>
mov    $0x1,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [10 x i8] c"shared_fb\00"
@O_RDWR = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@stderr = global i32 0
@.str.1 = [17 x i8] c"mmap failed: %s\0A\00"
@errno = global i32 0
@.str.2 = [21 x i8] c"shm_open failed: %s\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i32
%12 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%13 = load i32, i32* @O_RDWR
%14 = load i32, i32* @S_IRUSR
%15 = load i32, i32* @S_IWUSR
%16 = or i32 %14, %15
%17 = call i32 @shm_open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %13, i32 %16)
store i32 %17, i32* %7
%18 = load i32, i32* %7
%19 = icmp sgt i32 %18, 0
br i1 %19, label %20, label %36
20: ; preds = %2
%21 = load i32, i32* @PROT_READ
%22 = load i32, i32* @PROT_WRITE
%23 = or i32 %21, %22
%24 = load i32, i32* @MAP_SHARED
%25 = load i32, i32* %7
%26 = call i8* @mmap(i32* null, i32 192000, i32 %23, i32 %24, i32 %25, i32 0)
store i8* %26, i8** %6
%27 = load i8*, i8** %6
%28 = load i8*, i8** @MAP_FAILED
%29 = icmp eq i8* %27, %28
br i1 %29, label %30, label %35
30: ; preds = %20
%31 = load i32, i32* @stderr
%32 = load i32, i32* @errno
%33 = call i8* @strerror(i32 %32)
%34 = call i32 @fprintf(i32 %31, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i8* %33)
store i8* null, i8** %6
store i32 1, i32* %3
br label %78
35: ; preds = %20
br label %41
36: ; preds = %2
%37 = load i32, i32* @stderr
%38 = load i32, i32* @errno
%39 = call i8* @strerror(i32 %38)
%40 = call i32 @fprintf(i32 %37, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i8* %39)
store i32 1, i32* %3
br label %78
41: ; preds = %35
%42 = load i8*, i8** %6
store i8* %42, i8** %8
store i32 0, i32* %9
br label %43
43: ; preds = %41, %77
%44 = load i32, i32* %9
%45 = add nsw i32 %44, 8
store i32 %45, i32* %9
%46 = load i32, i32* %9
%47 = srem i32 %46, 255
store i32 %47, i32* %9
%48 = call i32 @sleep(i32 1)
%49 = load i8*, i8** %8
store i8* %49, i8** %10
store i32 0, i32* %11
br label %50
50: ; preds = %74, %43
%51 = load i32, i32* %11
%52 = icmp slt i32 %51, 200
br i1 %52, label %53, label %77
53: ; preds = %50
store i32 0, i32* %12
br label %54
54: ; preds = %70, %53
%55 = load i32, i32* %12
%56 = icmp slt i32 %55, 320
br i1 %56, label %57, label %73
57: ; preds = %54
%58 = load i32, i32* %9
%59 = trunc i32 %58 to i8
%60 = load i8*, i8** %10
%61 = getelementptr inbounds i8, i8* %60, i32 1
store i8* %61, i8** %10
store i8 %59, i8* %60
%62 = load i32, i32* %9
%63 = trunc i32 %62 to i8
%64 = load i8*, i8** %10
%65 = getelementptr inbounds i8, i8* %64, i32 1
store i8* %65, i8** %10
store i8 %63, i8* %64
%66 = load i32, i32* %9
%67 = trunc i32 %66 to i8
%68 = load i8*, i8** %10
%69 = getelementptr inbounds i8, i8* %68, i32 1
store i8* %69, i8** %10
store i8 %67, i8* %68
br label %70
70: ; preds = %57
%71 = load i32, i32* %12
%72 = add nsw i32 %71, 1
store i32 %72, i32* %12
br label %54
73: ; preds = %54
br label %74
74: ; preds = %73
%75 = load i32, i32* %11
%76 = add nsw i32 %75, 1
store i32 %76, i32* %11
br label %50
77: ; preds = %50
br label %43
78: ; preds = %36, %30
%79 = load i32, i32* %3
ret i32 %79
}

LLVM IR (-O2):
@.str = [10 x i8] c"shared_fb\00"
@O_RDWR = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@stderr = global i32 0
@.str.1 = [17 x i8] c"mmap failed: %s\0A\00"
@errno = global i32 0
@.str.2 = [21 x i8] c"shm_open failed: %s\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = load i32, i32* @O_RDWR
%4 = load i32, i32* @S_IRUSR
%5 = load i32, i32* @S_IWUSR
%6 = or i32 %5, %4
%7 = tail call i32 @shm_open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %3, i32 %6)
%8 = icmp sgt i32 %7, 0
br i1 %8, label %9, label %23
9: ; preds = %2
%10 = load i32, i32* @PROT_READ
%11 = load i32, i32* @PROT_WRITE
%12 = or i32 %11, %10
%13 = load i32, i32* @MAP_SHARED
%14 = tail call i8* @mmap(i32* null, i32 192000, i32 %12, i32 %13, i32 %7, i32 0)
%15 = load i8*, i8** @MAP_FAILED
%16 = icmp eq i8* %14, %15
br i1 %16, label %23, label %17
17: ; preds = %9, %17
%18 = phi i32 [ %20, %17 ], [ 0, %9 ]
%19 = add nsw i32 %18, 8
%20 = srem i32 %19, 255
%21 = tail call i32 @sleep(i32 1)
%22 = trunc i32 %20 to i8
br label %17
23: ; preds = %2, %9
%24 = phi i8* [ getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), %9 ], [ getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), %2 ]
%25 = load i32, i32* @stderr
%26 = load i32, i32* @errno
%27 = tail call i8* @strerror(i32 %26)
%28 = tail call i32 @fprintf(i32 %25, i8* %24, i8* %27)
ret i32 1
}