#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 */
struct dirent {
  char *d_name;
};
typedef int /*<<< orphan*/ Display;
typedef int /*<<< orphan*/ DIR;

/* Variables and functions */
int /*<<< orphan*/ XCloseDisplay(int /*<<< orphan*/ *);
int XDisplayHeight(int /*<<< orphan*/ *, int);
int XDisplayWidth(int /*<<< orphan*/ *, int);
int /*<<< orphan*/ *XOpenDisplay(char *);
int XScreenCount(int /*<<< orphan*/ *);
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ *opendir(char *);
int /*<<< orphan*/ printf(char *, char *, int, int, int);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ strcat(char *, char *);
int main(void) {
  DIR *d = opendir("/tmp/.X11-unix");
  if (d != NULL) {
    struct dirent *dr;
    while ((dr = readdir(d)) != NULL) {
      if (dr->d_name[0] != 'X')
        continue;
      char display_name[64] = ":";
      strcat(display_name, dr->d_name + 1);
      Display *disp = XOpenDisplay(display_name);
      if (disp != NULL) {
        int count = XScreenCount(disp);
        int i;
        for (i = 0; i < count; i++)
          printf("%s.%d %dx%d\n", display_name, i, XDisplayWidth(disp, i),
                 XDisplayHeight(disp, i));
        XCloseDisplay(disp);
      }
    }
    closedir(d);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe37(%rip),%rdi
call   1040 <opendir@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     12d7 <main+0x127>
jmp    11e2 <main+0x32>
mov    -0x10(%rbp),%rdi
call   10a0 <readdir@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
je     12ce <main+0x11e>
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
movsbl (%rax),%eax
cmp    $0x58,%eax
je     1211 <main+0x61>
jmp    11e2 <main+0x32>
lea    -0x60(%rbp),%rdi
xor    %esi,%esi
mov    $0x40,%edx
call   1070 <memset@plt>
movb   $0x3a,-0x60(%rbp)
lea    -0x60(%rbp),%rdi
mov    -0x18(%rbp),%rax
mov    (%rax),%rsi
add    $0x1,%rsi
call   10c0 <strcat@plt>
lea    -0x60(%rbp),%rdi
call   1090 <XOpenDisplay@plt>
mov    %rax,-0x68(%rbp)
cmpq   $0x0,-0x68(%rbp)
je     12c9 <main+0x119>
mov    -0x68(%rbp),%rdi
call   1030 <XScreenCount@plt>
mov    %eax,-0x6c(%rbp)
movl   $0x0,-0x70(%rbp)
mov    -0x70(%rbp),%eax
cmp    -0x6c(%rbp),%eax
jge    12c0 <main+0x110>
lea    -0x60(%rbp),%rax
mov    %rax,-0x80(%rbp)
mov    -0x70(%rbp),%eax
mov    %eax,-0x78(%rbp)
mov    -0x68(%rbp),%rdi
mov    -0x70(%rbp),%esi
call   1050 <XDisplayWidth@plt>
mov    %eax,-0x74(%rbp)
mov    -0x68(%rbp),%rdi
mov    -0x70(%rbp),%esi
call   10b0 <XDisplayHeight@plt>
mov    -0x80(%rbp),%rsi
mov    -0x78(%rbp),%edx
mov    -0x74(%rbp),%ecx
mov    %eax,%r8d
lea    0xd62(%rip),%rdi
call   1060 <printf@plt>
mov    -0x70(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x70(%rbp)
jmp    1264 <main+0xb4>
mov    -0x68(%rbp),%rdi
call   10d0 <XCloseDisplay@plt>
jmp    11e2 <main+0x32>
mov    -0x10(%rbp),%rdi
call   1080 <closedir@plt>
xor    %eax,%eax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x58,%rsp
lea    0xe4b(%rip),%rdi
call   1040 <opendir@plt>
test   %rax,%rax
je     12af <main+0x10f>
mov    %rax,%rbp
mov    %rax,%rdi
call   1090 <readdir@plt>
test   %rax,%rax
je     12a7 <main+0x107>
lea    0x10(%rsp),%r12
lea    0xe2c(%rip),%r15
mov    %rbp,0x8(%rsp)
jmp    120e <main+0x6e>
nopw   0x0(%rax,%rax,1)
mov    %rbx,%rdi
call   10c0 <XCloseDisplay@plt>
mov    0x8(%rsp),%rbp
mov    %rbp,%rdi
call   1090 <readdir@plt>
test   %rax,%rax
je     12a7 <main+0x107>
mov    (%rax),%rcx
cmpb   $0x58,(%rcx)
jne    11fd <main+0x5d>
xorps  %xmm0,%xmm0
movaps %xmm0,0x10(%rsp)
movaps %xmm0,0x40(%rsp)
movaps %xmm0,0x30(%rsp)
movaps %xmm0,0x20(%rsp)
movb   $0x3a,0x10(%rsp)
mov    (%rax),%rsi
add    $0x1,%rsi
mov    %r12,%rdi
call   10b0 <strcat@plt>
mov    %r12,%rdi
call   1080 <XOpenDisplay@plt>
test   %rax,%rax
je     11fd <main+0x5d>
mov    %rax,%rbx
mov    %rax,%rdi
call   1030 <XScreenCount@plt>
test   %eax,%eax
jle    11f0 <main+0x50>
mov    %eax,%r13d
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %rbx,%rdi
mov    %ebp,%esi
call   1050 <XDisplayWidth@plt>
mov    %eax,%r14d
mov    %rbx,%rdi
mov    %ebp,%esi
call   10a0 <XDisplayHeight@plt>
mov    %r15,%rdi
mov    %r12,%rsi
mov    %ebp,%edx
mov    %r14d,%ecx
mov    %eax,%r8d
call   1060 <printf@plt>
add    $0x1,%ebp
cmp    %ebp,%r13d
jne    1270 <main+0xd0>
jmp    11f0 <main+0x50>
mov    %rbp,%rdi
call   1070 <closedir@plt>
xor    %eax,%eax
add    $0x58,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.dirent = type { i8* }
@.str = [15 x i8] c"/tmp/.X11-unix\00"
@.str.1 = [13 x i8] c"%s.%d %dx%d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca %struct.dirent*
%4 = alloca [64 x i8]
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i32
store i32 0, i32* %1
%8 = call i32* @opendir(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
store i32* %8, i32** %2
%9 = load i32*, i32** %2
%10 = icmp ne i32* %9, null
br i1 %10, label %11, label %66
11: ; preds = %0
br label %12
12: ; preds = %62, %24, %11
%13 = load i32*, i32** %2
%14 = call %struct.dirent* @readdir(i32* %13)
store %struct.dirent* %14, %struct.dirent** %3
%15 = icmp ne %struct.dirent* %14, null
br i1 %15, label %16, label %63
16: ; preds = %12
%17 = load %struct.dirent*, %struct.dirent** %3
%18 = getelementptr inbounds %struct.dirent, %struct.dirent* %17, i32 0, i32 0
%19 = load i8*, i8** %18
%20 = getelementptr inbounds i8, i8* %19, i64 0
%21 = load i8, i8* %20
%22 = sext i8 %21 to i32
%23 = icmp ne i32 %22, 88
br i1 %23, label %24, label %25
24: ; preds = %16
br label %12
25: ; preds = %16
%26 = bitcast [64 x i8]* %4 to i8*
%27 = bitcast i8* %26 to [64 x i8]*
%28 = getelementptr inbounds [64 x i8], [64 x i8]* %27, i32 0, i32 0
store i8 58, i8* %28
%29 = getelementptr inbounds [64 x i8], [64 x i8]* %4, i64 0, i64 0
%30 = load %struct.dirent*, %struct.dirent** %3
%31 = getelementptr inbounds %struct.dirent, %struct.dirent* %30, i32 0, i32 0
%32 = load i8*, i8** %31
%33 = getelementptr inbounds i8, i8* %32, i64 1
%34 = call i32 @strcat(i8* %29, i8* %33)
%35 = getelementptr inbounds [64 x i8], [64 x i8]* %4, i64 0, i64 0
%36 = call i32* @XOpenDisplay(i8* %35)
store i32* %36, i32** %5
%37 = load i32*, i32** %5
%38 = icmp ne i32* %37, null
br i1 %38, label %39, label %62
39: ; preds = %25
%40 = load i32*, i32** %5
%41 = call i32 @XScreenCount(i32* %40)
store i32 %41, i32* %6
store i32 0, i32* %7
br label %42
42: ; preds = %56, %39
%43 = load i32, i32* %7
%44 = load i32, i32* %6
%45 = icmp slt i32 %43, %44
br i1 %45, label %46, label %59
46: ; preds = %42
%47 = getelementptr inbounds [64 x i8], [64 x i8]* %4, i64 0, i64 0
%48 = load i32, i32* %7
%49 = load i32*, i32** %5
%50 = load i32, i32* %7
%51 = call i32 @XDisplayWidth(i32* %49, i32 %50)
%52 = load i32*, i32** %5
%53 = load i32, i32* %7
%54 = call i32 @XDisplayHeight(i32* %52, i32 %53)
%55 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i8* %47, i32 %48, i32 %51, i32 %54)
br label %56
56: ; preds = %46
%57 = load i32, i32* %7
%58 = add nsw i32 %57, 1
store i32 %58, i32* %7
br label %42
59: ; preds = %42
%60 = load i32*, i32** %5
%61 = call i32 @XCloseDisplay(i32* %60)
br label %62
62: ; preds = %59, %25
br label %12
63: ; preds = %12
%64 = load i32*, i32** %2
%65 = call i32 @closedir(i32* %64)
br label %66
66: ; preds = %63, %0
ret i32 0
}

LLVM IR (-O2):
%struct.dirent = type { i8* }
@.str = [15 x i8] c"/tmp/.X11-unix\00"
@.str.1 = [13 x i8] c"%s.%d %dx%d\0A\00"
define i32 @main() {
%1 = alloca [64 x i8]
%2 = tail call i32* @opendir(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%3 = icmp eq i32* %2, null
br i1 %3, label %39, label %4
4: ; preds = %0
%5 = call %struct.dirent* @readdir(i32* %2)
%6 = icmp eq %struct.dirent* %5, null
br i1 %6, label %37, label %7
7: ; preds = %4
%8 = getelementptr inbounds [64 x i8], [64 x i8]* %1, i64 0, i64 0
br label %9
9: ; preds = %7, %34
%10 = phi %struct.dirent* [ %5, %7 ], [ %35, %34 ]
%11 = getelementptr inbounds %struct.dirent, %struct.dirent* %10, i64 0, i32 0
%12 = load i8*, i8** %11
%13 = load i8, i8* %12
%14 = icmp eq i8 %13, 88
br i1 %14, label %15, label %34
15: ; preds = %9
store i8 58, i8* %8
%16 = load i8*, i8** %11
%17 = getelementptr inbounds i8, i8* %16, i64 1
%18 = call i32 @strcat(i8* %8, i8* %17)
%19 = call i32* @XOpenDisplay(i8* %8)
%20 = icmp eq i32* %19, null
br i1 %20, label %33, label %21
21: ; preds = %15
%22 = call i32 @XScreenCount(i32* %19)
%23 = icmp sgt i32 %22, 0
br i1 %23, label %24, label %31
24: ; preds = %21, %24
%25 = phi i32 [ %29, %24 ], [ 0, %21 ]
%26 = call i32 @XDisplayWidth(i32* %19, i32 %25)
%27 = call i32 @XDisplayHeight(i32* %19, i32 %25)
%28 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i8* %8, i32 %25, i32 %26, i32 %27)
%29 = add nuw nsw i32 %25, 1
%30 = icmp eq i32 %29, %22
br i1 %30, label %31, label %24
31: ; preds = %24, %21
%32 = call i32 @XCloseDisplay(i32* %19)
br label %33
33: ; preds = %31, %15
br label %34
34: ; preds = %33, %9
%35 = call %struct.dirent* @readdir(i32* %2)
%36 = icmp eq %struct.dirent* %35, null
br i1 %36, label %37, label %9
37: ; preds = %34, %4
%38 = call i32 @closedir(i32* %2)
br label %39
39: ; preds = %37, %0
ret i32 0
}