#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_4__ TYPE_1__;

/* Type definitions */
struct TYPE_4__ {
  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*/ *);
TYPE_1__ *XGetImage(int /*<<< orphan*/ *, int /*<<< orphan*/, int const,
                    int const, int const, int const, int /*<<< orphan*/,
                    int /*<<< orphan*/);
unsigned long XGetPixel(TYPE_1__ *, int, int);
int /*<<< orphan*/ *XOpenDisplay(int /*<<< orphan*/ *);
int /*<<< orphan*/ ZPixmap;
void getScreen(const int xx, const int yy, const int width, const int height,
               unsigned char *data) {
  Display *display = XOpenDisplay(NULL);
  Window root = DefaultRootWindow(display);
  XImage *image =
      XGetImage(display, root, xx, yy, width, height, 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 < width; x++) {
    for (y = 0; y < height; y++) {
      unsigned long pixel = XGetPixel(image, x, y);
      int ii = (x + width * 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;
    }
  }
  XCloseDisplay(display);
}


x86 assembly (clang, -O0):
<getScreen>:
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   1050 <XOpenDisplay@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
call   1040 <DefaultRootWindow@plt>
mov    %eax,-0x24(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x24(%rbp),%esi
mov    -0x4(%rbp),%edx
mov    -0x8(%rbp),%ecx
mov    -0xc(%rbp),%r8d
mov    -0x10(%rbp),%r9d
mov    0x2e3b(%rip),%rax
mov    (%rax),%r10d
mov    0x2e41(%rip),%rax
mov    (%rax),%eax
mov    %r10d,(%rsp)
mov    %eax,0x8(%rsp)
call   1070 <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    12ab <getScreen+0x15b>
movl   $0x0,-0x50(%rbp)
mov    -0x50(%rbp),%eax
cmp    -0x10(%rbp),%eax
jge    1298 <getScreen+0x148>
mov    -0x30(%rbp),%rdi
mov    -0x4c(%rbp),%esi
mov    -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    11f7 <getScreen+0xa7>
jmp    129d <getScreen+0x14d>
mov    -0x4c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x4c(%rbp)
jmp    11e4 <getScreen+0x94>
mov    -0x20(%rbp),%rdi
call   1060 <XCloseDisplay@plt>
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getScreen>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    %r8,0x8(%rsp)
mov    %ecx,%r12d
mov    %edx,%r15d
mov    %esi,%r13d
mov    %edi,%r14d
xor    %edi,%edi
call   1050 <XOpenDisplay@plt>
mov    %rax,%rbx
mov    %rax,%rdi
call   1040 <DefaultRootWindow@plt>
mov    0x2e50(%rip),%rcx
mov    (%rcx),%r10d
mov    0x2e56(%rip),%rcx
mov    (%rcx),%ebp
mov    %rbx,0x10(%rsp)
mov    %rbx,%rdi
mov    %eax,%esi
mov    %r14d,%edx
mov    %r13d,%ecx
mov    %r15d,%r8d
mov    %r12d,0x4(%rsp)
mov    %r12d,%r9d
push   %rbp
push   %r10
call   1070 <XGetImage@plt>
add    $0x10,%rsp
test   %r15d,%r15d
jle    1275 <getScreen+0x125>
mov    %rax,%r12
mov    (%rax),%r13
mov    0x8(%rax),%r14
mov    0x10(%rax),%r9
mov    %r15d,%eax
mov    0x4(%rsp),%ecx
addq   $0x2,0x8(%rsp)
mov    %rax,0x18(%rsp)
lea    (%rax,%rax,2),%rax
xor    %esi,%esi
mov    %rcx,0x28(%rsp)
mov    %rax,0x20(%rsp)
jmp    1211 <getScreen+0xc1>
cs nopw 0x0(%rax,%rax,1)
add    $0x1,%rsi
addq   $0x3,0x8(%rsp)
cmp    0x18(%rsp),%rsi
je     1275 <getScreen+0x125>
cmpl   $0x0,0x4(%rsp)
jle    1200 <getScreen+0xb0>
mov    0x8(%rsp),%rbx
xor    %ebp,%ebp
mov    %rsi,0x30(%rsp)
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %r12,%rdi
mov    %ebp,%edx
mov    %r9,%r15
call   1030 <XGetPixel@plt>
mov    %r15,%r9
mov    %eax,%edx
and    %r9d,%edx
mov    %eax,%ecx
and    %r14d,%ecx
and    %r13d,%eax
shr    $0x10,%eax
mov    %dl,(%rbx)
mov    0x30(%rsp),%rsi
mov    %ch,-0x1(%rbx)
mov    0x28(%rsp),%rcx
mov    %al,-0x2(%rbx)
mov    0x20(%rsp),%rax
add    $0x1,%rbp
add    %rax,%rbx
cmp    %rbp,%rcx
jne    1230 <getScreen+0xe0>
jmp    1200 <getScreen+0xb0>
mov    0x10(%rsp),%rdi
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1060 <XCloseDisplay@plt>

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

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