#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*/);
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 W, const int H,
               unsigned char *data) {
  Display *display = XOpenDisplay(NULL);
  Window root = DefaultRootWindow(display);
  XImage *image = XGetImage(display, root, xx, yy, W, H, 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;
  int ii = 0;
  for (y = 0; y < H; y++) {
    for (x = 0; x < W; x++) {
      unsigned long pixel = XGetPixel(image, x, y);
      unsigned char blue = (pixel & blue_mask);
      unsigned char green = (pixel & green_mask) >> 8;
      unsigned char red = (pixel & red_mask) >> 16;
      data[ii + 2] = red;
      data[ii + 1] = green;
      data[ii + 0] = blue;
      ii += 3;
    }
  }
  XDestroyImage(image);
  XDestroyWindow(display, root);
  XCloseDisplay(display);
}


x86 assembly (clang, -O0):
<getScreen>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%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
mov    -0x24(%rbp),%esi
mov    -0x4(%rbp),%edx
mov    -0x8(%rbp),%ecx
mov    -0xc(%rbp),%r8d
mov    -0x10(%rbp),%r9d
mov    0x2e18(%rip),%rax
mov    (%rax),%r10d
mov    0x2e1e(%rip),%rax
mov    (%rax),%eax
mov    %r10d,(%rsp)
mov    %eax,0x8(%rsp)
call   1090 <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,-0x54(%rbp)
movl   $0x0,-0x50(%rbp)
mov    -0x50(%rbp),%eax
cmp    -0x10(%rbp),%eax
jge    12cc <getScreen+0x15c>
movl   $0x0,-0x4c(%rbp)
mov    -0x4c(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    12b9 <getScreen+0x149>
mov    -0x30(%rbp),%rdi
mov    -0x4c(%rbp),%esi
mov    -0x50(%rbp),%edx
call   1030 <XGetPixel@plt>
mov    %rax,-0x60(%rbp)
mov    -0x60(%rbp),%rax
and    -0x48(%rbp),%rax
mov    %al,-0x61(%rbp)
mov    -0x60(%rbp),%rax
and    -0x40(%rbp),%rax
shr    $0x8,%rax
mov    %al,-0x62(%rbp)
mov    -0x60(%rbp),%rax
and    -0x38(%rbp),%rax
shr    $0x10,%rax
mov    %al,-0x63(%rbp)
mov    -0x63(%rbp),%dl
mov    -0x18(%rbp),%rax
mov    -0x54(%rbp),%ecx
add    $0x2,%ecx
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x62(%rbp),%dl
mov    -0x18(%rbp),%rax
mov    -0x54(%rbp),%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x61(%rbp),%dl
mov    -0x18(%rbp),%rax
mov    -0x54(%rbp),%ecx
add    $0x0,%ecx
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x54(%rbp),%eax
add    $0x3,%eax
mov    %eax,-0x54(%rbp)
mov    -0x4c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x4c(%rbp)
jmp    1221 <getScreen+0xb1>
jmp    12be <getScreen+0x14e>
mov    -0x50(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x50(%rbp)
jmp    120e <getScreen+0x9e>
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   1080 <XCloseDisplay@plt>
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getScreen>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %r8,0x10(%rsp)
mov    %ecx,%ebp
mov    %edx,%r15d
mov    %esi,%r14d
mov    %edi,%r13d
xor    %edi,%edi
call   1070 <XOpenDisplay@plt>
mov    %rax,%rbx
mov    %rax,%rdi
call   1040 <DefaultRootWindow@plt>
mov    %eax,%esi
mov    0x2e2f(%rip),%rax
mov    (%rax),%r10d
mov    0x2e35(%rip),%rcx
mov    (%rcx),%eax
mov    %rbx,0x20(%rsp)
mov    %rbx,%rdi
mov    %esi,(%rsp)
mov    %r13d,%edx
mov    %r14d,%ecx
mov    %r15d,%r8d
mov    %ebp,%r9d
push   %rax
push   %r10
call   1090 <XGetImage@plt>
xor    %ecx,%ecx
add    $0x10,%rsp
mov    %rax,%rbx
mov    %ebp,0x4(%rsp)
test   %ebp,%ebp
jle    1282 <getScreen+0x112>
mov    (%rbx),%rsi
mov    0x8(%rbx),%r13
mov    0x10(%rbx),%r12
addq   $0x2,0x10(%rsp)
xor    %eax,%eax
mov    %r15d,0x8(%rsp)
jmp    1219 <getScreen+0xa9>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
add    $0x1,%ecx
cmp    0x4(%rsp),%ecx
je     1282 <getScreen+0x112>
test   %r15d,%r15d
jle    1210 <getScreen+0xa0>
movslq %eax,%rbp
add    0x10(%rsp),%rbp
xor    %r14d,%r14d
mov    %rcx,0x18(%rsp)
xchg   %ax,%ax
mov    %eax,0xc(%rsp)
mov    %rbx,%rdi
mov    %rsi,%r15
mov    %r14d,%esi
mov    0x18(%rsp),%rdx
call   1030 <XGetPixel@plt>
mov    %r15,%rsi
mov    %eax,%edx
and    %r12d,%edx
mov    %eax,%ecx
and    %r13d,%ecx
and    %esi,%eax
shr    $0x10,%eax
mov    %al,0x0(%rbp)
mov    0xc(%rsp),%eax
mov    %ch,-0x1(%rbp)
mov    %dl,-0x2(%rbp)
mov    0x18(%rsp),%rcx
mov    0x8(%rsp),%r15d
add    $0x1,%r14d
add    $0x3,%rbp
add    $0x3,%eax
cmp    %r14d,%r15d
jne    1230 <getScreen+0xc0>
jmp    1210 <getScreen+0xa0>
mov    %rbx,%rdi
call   1050 <XDestroyImage@plt>
mov    0x20(%rsp),%rbx
mov    %rbx,%rdi
mov    (%rsp),%esi
call   1060 <XDestroyWindow@plt>
mov    %rbx,%rdi
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1080 <XCloseDisplay@plt>

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

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