#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 */
typedef struct TYPE_5__ TYPE_1__;

/* Type definitions */
struct TYPE_5__ {
  unsigned long red_mask;
  unsigned long green_mask;
  unsigned long blue_mask;
};
typedef TYPE_1__ XImage;
typedef int /*<<< orphan*/ Window;
typedef int /*<<< orphan*/ Display;

/* Variables and functions */
int /*<<< orphan*/ AllPlanes;
int /*<<< orphan*/ DefaultRootWindow(int /*<<< orphan*/ *);
int /*<<< orphan*/ XCloseDisplay(int /*<<< orphan*/ *);
int /*<<< orphan*/ XDestroyImage(TYPE_1__ *);
int /*<<< orphan*/ XDestroyWindow(int /*<<< orphan*/ *, int /*<<< orphan*/);
int /*<<< orphan*/ XFlush(int /*<<< orphan*/ *);
TYPE_1__ *XGetImage(int /*<<< orphan*/ *, int /*<<< orphan*/,
                    int /*<<< orphan*/, int /*<<< orphan*/, int, int,
                    int /*<<< orphan*/, int /*<<< orphan*/);
unsigned long XGetPixel(TYPE_1__ *, int const, int const);
int /*<<< orphan*/ *XOpenDisplay(int /*<<< orphan*/ *);
int /*<<< orphan*/ ZPixmap;
void get_screen(const int xx, const int yy, const int W, const int H,
                unsigned char *data) {
  Display *display = XOpenDisplay(NULL);
  Window root = DefaultRootWindow(display);
  XFlush(display);
  XImage *image =
      XGetImage(display, root, 0, 0, 1920, 1080, AllPlanes, ZPixmap);
  unsigned long red_mask = image->red_mask;
  unsigned long green_mask = image->green_mask;
  unsigned long blue_mask = image->blue_mask;
  int x, y;
  for (x = 0; x < W; x++) {
    for (y = 0; y < H; y++) {
      unsigned long pixel = XGetPixel(image, xx + x, yy + y);
      int ii = (x + W * y) * 3;
      unsigned char blue = (pixel & blue_mask);
      unsigned char green = (pixel & green_mask) >> 8;
      unsigned char red = (pixel & red_mask) >> 16;
      data[ii + 2] = blue;
      data[ii + 1] = green;
      data[ii + 0] = red;
    }
  }
  XDestroyImage(image);
  XDestroyWindow(display, root);
  XCloseDisplay(display);
}


x86 assembly (clang, -O0):
<get_screen>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %edx,-0xc(%rbp)
mov    %ecx,-0x10(%rbp)
mov    %r8,-0x18(%rbp)
xor    %eax,%eax
mov    %eax,%edi
call   1070 <XOpenDisplay@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
call   1040 <DefaultRootWindow@plt>
mov    %eax,-0x24(%rbp)
mov    -0x20(%rbp),%rdi
call   1080 <XFlush@plt>
mov    -0x20(%rbp),%rdi
mov    -0x24(%rbp),%esi
mov    0x2e10(%rip),%rax
mov    (%rax),%r10d
mov    0x2e16(%rip),%rax
mov    (%rax),%eax
xor    %ecx,%ecx
mov    $0x780,%r8d
mov    $0x438,%r9d
mov    %ecx,%edx
mov    %r10d,(%rsp)
mov    %eax,0x8(%rsp)
call   10a0 <XGetImage@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x38(%rbp)
mov    -0x30(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x40(%rbp)
mov    -0x30(%rbp),%rax
mov    0x10(%rax),%rax
mov    %rax,-0x48(%rbp)
movl   $0x0,-0x4c(%rbp)
mov    -0x4c(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    12ec <get_screen+0x16c>
movl   $0x0,-0x50(%rbp)
mov    -0x50(%rbp),%eax
cmp    -0x10(%rbp),%eax
jge    12d9 <get_screen+0x159>
mov    -0x30(%rbp),%rdi
mov    -0x4(%rbp),%esi
add    -0x4c(%rbp),%esi
mov    -0x8(%rbp),%edx
add    -0x50(%rbp),%edx
call   1030 <XGetPixel@plt>
mov    %rax,-0x58(%rbp)
mov    -0x4c(%rbp),%eax
mov    -0xc(%rbp),%ecx
imul   -0x50(%rbp),%ecx
add    %ecx,%eax
imul   $0x3,%eax,%eax
mov    %eax,-0x5c(%rbp)
mov    -0x58(%rbp),%rax
and    -0x48(%rbp),%rax
mov    %al,-0x5d(%rbp)
mov    -0x58(%rbp),%rax
and    -0x40(%rbp),%rax
shr    $0x8,%rax
mov    %al,-0x5e(%rbp)
mov    -0x58(%rbp),%rax
and    -0x38(%rbp),%rax
shr    $0x10,%rax
mov    %al,-0x5f(%rbp)
mov    -0x5d(%rbp),%dl
mov    -0x18(%rbp),%rax
mov    -0x5c(%rbp),%ecx
add    $0x2,%ecx
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x5e(%rbp),%dl
mov    -0x18(%rbp),%rax
mov    -0x5c(%rbp),%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x5f(%rbp),%dl
mov    -0x18(%rbp),%rax
mov    -0x5c(%rbp),%ecx
add    $0x0,%ecx
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x50(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x50(%rbp)
jmp    1232 <get_screen+0xb2>
jmp    12de <get_screen+0x15e>
mov    -0x4c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x4c(%rbp)
jmp    121f <get_screen+0x9f>
mov    -0x30(%rbp),%rdi
call   1050 <XDestroyImage@plt>
mov    -0x20(%rbp),%rdi
mov    -0x24(%rbp),%esi
call   1060 <XDestroyWindow@plt>
mov    -0x20(%rbp),%rdi
call   1090 <XCloseDisplay@plt>
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_screen>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x58,%rsp
mov    %r8,0x8(%rsp)
mov    %ecx,0x4(%rsp)
mov    %edx,%r14d
mov    %esi,0x20(%rsp)
mov    %rdi,0x38(%rsp)
xor    %edi,%edi
call   1070 <XOpenDisplay@plt>
mov    %rax,%rbx
mov    %rax,%rdi
call   1040 <DefaultRootWindow@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1080 <XFlush@plt>
mov    0x2e12(%rip),%rax
mov    (%rax),%r10d
mov    0x2e18(%rip),%rcx
mov    (%rcx),%eax
mov    %rbx,0x28(%rsp)
mov    %rbx,%rdi
mov    %ebp,0x1c(%rsp)
mov    %ebp,%esi
xor    %edx,%edx
xor    %ecx,%ecx
mov    $0x780,%r8d
mov    $0x438,%r9d
push   %rax
push   %r10
call   10a0 <XGetImage@plt>
add    $0x10,%rsp
mov    %rax,%rbx
test   %r14d,%r14d
jle    12e9 <get_screen+0x169>
mov    (%rbx),%rsi
mov    0x8(%rbx),%r8
mov    0x10(%rbx),%r13
mov    %r14d,%eax
mov    0x4(%rsp),%ecx
mov    %rcx,0x30(%rsp)
addq   $0x2,0x8(%rsp)
mov    %rax,0x40(%rsp)
lea    (%rax,%rax,2),%rcx
xor    %eax,%eax
mov    %rax,0x10(%rsp)
mov    %rsi,0x50(%rsp)
mov    %rcx,0x48(%rsp)
jmp    126e <get_screen+0xee>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    0x10(%rsp),%rdx
add    $0x1,%rdx
addq   $0x3,0x8(%rsp)
mov    %rdx,%rax
mov    %rdx,0x10(%rsp)
cmp    0x40(%rsp),%rdx
je     12e9 <get_screen+0x169>
cmpl   $0x0,0x4(%rsp)
jle    1250 <get_screen+0xd0>
mov    0x38(%rsp),%rax
mov    0x10(%rsp),%rdx
add    %edx,%eax
mov    %eax,0x24(%rsp)
mov    0x30(%rsp),%r12
mov    0x8(%rsp),%rbp
mov    0x20(%rsp),%r15d
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %rbx,%rdi
mov    0x24(%rsp),%esi
mov    %r15d,%edx
mov    %r8,%r14
call   1030 <XGetPixel@plt>
mov    %r14,%r8
mov    0x50(%rsp),%rsi
mov    %eax,%edx
and    %r13d,%edx
mov    %eax,%ecx
and    %r8d,%ecx
and    %esi,%eax
shr    $0x10,%eax
mov    %dl,0x0(%rbp)
mov    %ch,-0x1(%rbp)
mov    0x48(%rsp),%rcx
mov    %al,-0x2(%rbp)
add    $0x1,%r15d
add    %rcx,%rbp
add    $0xffffffffffffffff,%r12
jne    12a0 <get_screen+0x120>
jmp    1250 <get_screen+0xd0>
mov    %rbx,%rdi
call   1050 <XDestroyImage@plt>
mov    0x28(%rsp),%rbx
mov    %rbx,%rdi
mov    0x1c(%rsp),%esi
call   1060 <XDestroyWindow@plt>
mov    %rbx,%rdi
add    $0x58,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1090 <XCloseDisplay@plt>

LLVM IR (-O0):
%struct.TYPE_5__ = type { i64, i64, i64 }
@AllPlanes = global i32 0
@ZPixmap = global i32 0
define void @get_screen(i32 %0, i32 %1, i32 %2, i32 %3, i8* %4) {
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i32*
%12 = alloca i32
%13 = alloca %struct.TYPE_5__*
%14 = alloca i64
%15 = alloca i64
%16 = alloca i64
%17 = alloca i32
%18 = alloca i32
%19 = alloca i64
%20 = alloca i32
%21 = alloca i8
%22 = alloca i8
%23 = alloca i8
store i32 %0, i32* %6
store i32 %1, i32* %7
store i32 %2, i32* %8
store i32 %3, i32* %9
store i8* %4, i8** %10
%24 = call i32* @XOpenDisplay(i32* null)
store i32* %24, i32** %11
%25 = load i32*, i32** %11
%26 = call i32 @DefaultRootWindow(i32* %25)
store i32 %26, i32* %12
%27 = load i32*, i32** %11
%28 = call i32 @XFlush(i32* %27)
%29 = load i32*, i32** %11
%30 = load i32, i32* %12
%31 = load i32, i32* @AllPlanes
%32 = load i32, i32* @ZPixmap
%33 = call %struct.TYPE_5__* @XGetImage(i32* %29, i32 %30, i32 0, i32 0, i32 1920, i32 1080, i32 %31, i32 %32)
store %struct.TYPE_5__* %33, %struct.TYPE_5__** %13
%34 = load %struct.TYPE_5__*, %struct.TYPE_5__** %13
%35 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %34, i32 0, i32 0
%36 = load i64, i64* %35
store i64 %36, i64* %14
%37 = load %struct.TYPE_5__*, %struct.TYPE_5__** %13
%38 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %37, i32 0, i32 1
%39 = load i64, i64* %38
store i64 %39, i64* %15
%40 = load %struct.TYPE_5__*, %struct.TYPE_5__** %13
%41 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %40, i32 0, i32 2
%42 = load i64, i64* %41
store i64 %42, i64* %16
store i32 0, i32* %17
br label %43
43: ; preds = %103, %5
%44 = load i32, i32* %17
%45 = load i32, i32* %8
%46 = icmp slt i32 %44, %45
br i1 %46, label %47, label %106
47: ; preds = %43
store i32 0, i32* %18
br label %48
48: ; preds = %99, %47
%49 = load i32, i32* %18
%50 = load i32, i32* %9
%51 = icmp slt i32 %49, %50
br i1 %51, label %52, label %102
52: ; preds = %48
%53 = load %struct.TYPE_5__*, %struct.TYPE_5__** %13
%54 = load i32, i32* %6
%55 = load i32, i32* %17
%56 = add nsw i32 %54, %55
%57 = load i32, i32* %7
%58 = load i32, i32* %18
%59 = add nsw i32 %57, %58
%60 = call i64 @XGetPixel(%struct.TYPE_5__* %53, i32 %56, i32 %59)
store i64 %60, i64* %19
%61 = load i32, i32* %17
%62 = load i32, i32* %8
%63 = load i32, i32* %18
%64 = mul nsw i32 %62, %63
%65 = add nsw i32 %61, %64
%66 = mul nsw i32 %65, 3
store i32 %66, i32* %20
%67 = load i64, i64* %19
%68 = load i64, i64* %16
%69 = and i64 %67, %68
%70 = trunc i64 %69 to i8
store i8 %70, i8* %21
%71 = load i64, i64* %19
%72 = load i64, i64* %15
%73 = and i64 %71, %72
%74 = lshr i64 %73, 8
%75 = trunc i64 %74 to i8
store i8 %75, i8* %22
%76 = load i64, i64* %19
%77 = load i64, i64* %14
%78 = and i64 %76, %77
%79 = lshr i64 %78, 16
%80 = trunc i64 %79 to i8
store i8 %80, i8* %23
%81 = load i8, i8* %21
%82 = load i8*, i8** %10
%83 = load i32, i32* %20
%84 = add nsw i32 %83, 2
%85 = sext i32 %84 to i64
%86 = getelementptr inbounds i8, i8* %82, i64 %85
store i8 %81, i8* %86
%87 = load i8, i8* %22
%88 = load i8*, i8** %10
%89 = load i32, i32* %20
%90 = add nsw i32 %89, 1
%91 = sext i32 %90 to i64
%92 = getelementptr inbounds i8, i8* %88, i64 %91
store i8 %87, i8* %92
%93 = load i8, i8* %23
%94 = load i8*, i8** %10
%95 = load i32, i32* %20
%96 = add nsw i32 %95, 0
%97 = sext i32 %96 to i64
%98 = getelementptr inbounds i8, i8* %94, i64 %97
store i8 %93, i8* %98
br label %99
99: ; preds = %52
%100 = load i32, i32* %18
%101 = add nsw i32 %100, 1
store i32 %101, i32* %18
br label %48
102: ; preds = %48
br label %103
103: ; preds = %102
%104 = load i32, i32* %17
%105 = add nsw i32 %104, 1
store i32 %105, i32* %17
br label %43
106: ; preds = %43
%107 = load %struct.TYPE_5__*, %struct.TYPE_5__** %13
%108 = call i32 @XDestroyImage(%struct.TYPE_5__* %107)
%109 = load i32*, i32** %11
%110 = load i32, i32* %12
%111 = call i32 @XDestroyWindow(i32* %109, i32 %110)
%112 = load i32*, i32** %11
%113 = call i32 @XCloseDisplay(i32* %112)
ret void
}

LLVM IR (-O2):
%struct.TYPE_5__ = type { i64, i64, i64 }
@AllPlanes = global i32 0
@ZPixmap = global i32 0
define void @get_screen(i32 %0, i32 %1, i32 %2, i32 %3, i8* nocapture writeonly %4) {
%6 = tail call i32* @XOpenDisplay(i32* null)
%7 = tail call i32 @DefaultRootWindow(i32* %6)
%8 = tail call i32 @XFlush(i32* %6)
%9 = load i32, i32* @AllPlanes
%10 = load i32, i32* @ZPixmap
%11 = tail call %struct.TYPE_5__* @XGetImage(i32* %6, i32 %7, i32 0, i32 0, i32 1920, i32 1080, i32 %9, i32 %10)
%12 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %11, i64 0, i32 0
%13 = load i64, i64* %12
%14 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %11, i64 0, i32 1
%15 = load i64, i64* %14
%16 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %11, i64 0, i32 2
%17 = load i64, i64* %16
%18 = icmp sgt i32 %2, 0
br i1 %18, label %19, label %55
19: ; preds = %5
%20 = icmp sgt i32 %3, 0
%21 = zext i32 %2 to i64
%22 = zext i32 %2 to i64
%23 = zext i32 %3 to i64
br label %24
24: ; preds = %19, %52
%25 = phi i64 [ 0, %19 ], [ %53, %52 ]
br i1 %20, label %26, label %52
26: ; preds = %24
%27 = trunc i64 %25 to i32
%28 = add i32 %27, %0
br label %29
29: ; preds = %26, %29
%30 = phi i64 [ 0, %26 ], [ %50, %29 ]
%31 = trunc i64 %30 to i32
%32 = add i32 %31, %1
%33 = tail call i64 @XGetPixel(%struct.TYPE_5__* %11, i32 %28, i32 %32)
%34 = mul nsw i64 %30, %21
%35 = add nuw nsw i64 %34, %25
%36 = mul nsw i64 %35, 3
%37 = and i64 %33, %17
%38 = trunc i64 %37 to i8
%39 = and i64 %33, %15
%40 = lshr i64 %39, 8
%41 = trunc i64 %40 to i8
%42 = and i64 %33, %13
%43 = lshr i64 %42, 16
%44 = trunc i64 %43 to i8
%45 = add nuw nsw i64 %36, 2
%46 = getelementptr inbounds i8, i8* %4, i64 %45
store i8 %38, i8* %46
%47 = add nuw nsw i64 %36, 1
%48 = getelementptr inbounds i8, i8* %4, i64 %47
store i8 %41, i8* %48
%49 = getelementptr inbounds i8, i8* %4, i64 %36
store i8 %44, i8* %49
%50 = add nuw nsw i64 %30, 1
%51 = icmp eq i64 %50, %23
br i1 %51, label %52, label %29
52: ; preds = %29, %24
%53 = add nuw nsw i64 %25, 1
%54 = icmp eq i64 %53, %22
br i1 %54, label %55, label %24
55: ; preds = %52, %5
%56 = tail call i32 @XDestroyImage(%struct.TYPE_5__* %11)
%57 = tail call i32 @XDestroyWindow(i32* %6, i32 %7)
%58 = tail call i32 @XCloseDisplay(i32* %6)
ret void
}