#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*/ FILE;
typedef int /*<<< orphan*/ Display;

/* Variables and functions */
int /*<<< orphan*/ SEEK_END;
int /*<<< orphan*/ XCloseDisplay(int /*<<< orphan*/ *);
int /*<<< orphan*/ *XOpenDisplay(char *);
int /*<<< orphan*/ *freopen(char *, char *, int /*<<< orphan*/);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, long, int /*<<< orphan*/);
int ftell(int /*<<< orphan*/ *);
int getpid();
int /*<<< orphan*/ sprintf(char *, char *, ...);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ unlink(char *);
int GetDisplay(void) {
  Display *dpy;
  char displayname[80] = "";
  char data[20] = "/tmp/disp";
  FILE *fd;
  int size;
  int i;
  sprintf(data, "%s-%d", data, getpid());
  for (i = 0; i <= 10; i++) {
    fd = freopen(data, "w", stderr);
    sprintf(displayname, ":%d.0", i);
    dpy = XOpenDisplay(displayname);
    fseek(fd, 0L, SEEK_END);
    size = ftell(fd) - 1;
    if (!dpy) {
      if (size <= 0) {
        unlink(data);
        return (i);
      }
    } else {
      XCloseDisplay(dpy);
      continue;
    }
  }
  unlink(data);
  return -1;
}


x86 assembly (clang, -O0):
<GetDisplay>:
push   %rbp
mov    %rsp,%rbp
sub    $0xa0,%rsp
lea    -0x60(%rbp),%rdi
xor    %esi,%esi
mov    $0x50,%edx
call   1050 <memset@plt>
mov    0xe4e(%rip),%rax
mov    %rax,-0x80(%rbp)
mov    0xe4b(%rip),%rax
mov    %rax,-0x78(%rbp)
mov    0xe49(%rip),%eax
mov    %eax,-0x70(%rbp)
lea    -0x80(%rbp),%rax
mov    %rax,-0xa0(%rbp)
lea    -0x80(%rbp),%rax
mov    %rax,-0x98(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    -0xa0(%rbp),%rdi
mov    -0x98(%rbp),%rdx
mov    %eax,%ecx
lea    0xe16(%rip),%rsi
mov    $0x0,%al
call   10a0 <sprintf@plt>
movl   $0x0,-0x90(%rbp)
cmpl   $0xa,-0x90(%rbp)
jg     12ea <GetDisplay+0x15a>
lea    -0x80(%rbp),%rdi
mov    0x2db1(%rip),%rax
mov    (%rax),%edx
lea    0xdea(%rip),%rsi
call   1060 <freopen@plt>
mov    %rax,-0x88(%rbp)
lea    -0x60(%rbp),%rdi
mov    -0x90(%rbp),%edx
lea    0xdcf(%rip),%rsi
mov    $0x0,%al
call   10a0 <sprintf@plt>
lea    -0x60(%rbp),%rdi
call   1070 <XOpenDisplay@plt>
mov    %rax,-0x10(%rbp)
mov    -0x88(%rbp),%rdi
mov    0x2d79(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
call   1090 <fseek@plt>
mov    -0x88(%rbp),%rdi
call   1080 <ftell@plt>
sub    $0x1,%eax
mov    %eax,-0x8c(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    12c3 <GetDisplay+0x133>
cmpl   $0x0,-0x8c(%rbp)
jg     12be <GetDisplay+0x12e>
lea    -0x80(%rbp),%rdi
call   1030 <unlink@plt>
mov    -0x90(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12fa <GetDisplay+0x16a>
jmp    12d1 <GetDisplay+0x141>
mov    -0x10(%rbp),%rdi
call   10b0 <XCloseDisplay@plt>
jmp    12d6 <GetDisplay+0x146>
jmp    12d6 <GetDisplay+0x146>
mov    -0x90(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x90(%rbp)
jmp    120f <GetDisplay+0x7f>
lea    -0x80(%rbp),%rdi
call   1030 <unlink@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0xa0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<GetDisplay>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x78,%rsp
xorps  %xmm0,%xmm0
movaps %xmm0,0x60(%rsp)
movaps %xmm0,0x50(%rsp)
movaps %xmm0,0x40(%rsp)
movaps %xmm0,0x30(%rsp)
movaps %xmm0,0x20(%rsp)
movaps 0xe4f(%rip),%xmm0
movaps %xmm0,(%rsp)
movl   $0x0,0x10(%rsp)
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe49(%rip),%rsi
mov    %rsp,%rdi
mov    %rdi,%rdx
mov    %eax,%ecx
xor    %eax,%eax
call   1090 <sprintf@plt>
lea    0xe3b(%rip),%r12
lea    0x20(%rsp),%r13
mov    0x2dfb(%rip),%r15
xor    %ebp,%ebp
jmp    1210 <GetDisplay+0x90>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
call   10a0 <XCloseDisplay@plt>
add    $0x1,%ebp
cmp    $0xb,%ebp
je     1266 <GetDisplay+0xe6>
mov    0x2dc1(%rip),%rax
mov    (%rax),%edx
mov    %rsp,%rdi
lea    0xdf7(%rip),%rsi
call   1050 <freopen@plt>
mov    %rax,%r14
mov    %r13,%rdi
mov    %r12,%rsi
mov    %ebp,%edx
xor    %eax,%eax
call   1090 <sprintf@plt>
mov    %r13,%rdi
call   1060 <XOpenDisplay@plt>
mov    %rax,%rbx
mov    (%r15),%edx
mov    %r14,%rdi
xor    %esi,%esi
call   1080 <fseek@plt>
mov    %r14,%rdi
call   1070 <ftell@plt>
test   %rbx,%rbx
jne    1200 <GetDisplay+0x80>
cmp    $0x2,%eax
jge    1208 <GetDisplay+0x88>
jmp    126b <GetDisplay+0xeb>
mov    $0xffffffff,%ebp
mov    %rsp,%rdi
call   1030 <unlink@plt>
mov    %ebp,%eax
add    $0x78,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@__const.GetDisplay.data = [20 x i8] c"/tmp/disp\00\00\00\00\00\00\00\00\00\00\00"
@.str = [6 x i8] c"%s-%d\00"
@.str.1 = [2 x i8] c"w\00"
@stderr = global i32 0
@.str.2 = [6 x i8] c":%d.0\00"
@SEEK_END = global i32 0
define i32 @GetDisplay() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca [80 x i8]
%4 = alloca [20 x i8]
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i32
%8 = bitcast [80 x i8]* %3 to i8*
%9 = bitcast [20 x i8]* %4 to i8*
%10 = getelementptr inbounds [20 x i8], [20 x i8]* %4, i64 0, i64 0
%11 = getelementptr inbounds [20 x i8], [20 x i8]* %4, i64 0, i64 0
%12 = call i32 (...) @getpid()
%13 = call i32 (i8*, i8*, ...) @sprintf(i8* %10, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %11, i32 %12)
store i32 0, i32* %7
br label %14
14: ; preds = %46, %0
%15 = load i32, i32* %7
%16 = icmp sle i32 %15, 10
br i1 %16, label %17, label %49
17: ; preds = %14
%18 = getelementptr inbounds [20 x i8], [20 x i8]* %4, i64 0, i64 0
%19 = load i32, i32* @stderr
%20 = call i32* @freopen(i8* %18, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32 %19)
store i32* %20, i32** %5
%21 = getelementptr inbounds [80 x i8], [80 x i8]* %3, i64 0, i64 0
%22 = load i32, i32* %7
%23 = call i32 (i8*, i8*, ...) @sprintf(i8* %21, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i32 %22)
%24 = getelementptr inbounds [80 x i8], [80 x i8]* %3, i64 0, i64 0
%25 = call i32* @XOpenDisplay(i8* %24)
store i32* %25, i32** %2
%26 = load i32*, i32** %5
%27 = load i32, i32* @SEEK_END
%28 = call i32 @fseek(i32* %26, i64 0, i32 %27)
%29 = load i32*, i32** %5
%30 = call i32 @ftell(i32* %29)
%31 = sub nsw i32 %30, 1
store i32 %31, i32* %6
%32 = load i32*, i32** %2
%33 = icmp ne i32* %32, null
br i1 %33, label %42, label %34
34: ; preds = %17
%35 = load i32, i32* %6
%36 = icmp sle i32 %35, 0
br i1 %36, label %37, label %41
37: ; preds = %34
%38 = getelementptr inbounds [20 x i8], [20 x i8]* %4, i64 0, i64 0
%39 = call i32 @unlink(i8* %38)
%40 = load i32, i32* %7
store i32 %40, i32* %1
br label %52
41: ; preds = %34
br label %45
42: ; preds = %17
%43 = load i32*, i32** %2
%44 = call i32 @XCloseDisplay(i32* %43)
br label %46
45: ; preds = %41
br label %46
46: ; preds = %45, %42
%47 = load i32, i32* %7
%48 = add nsw i32 %47, 1
store i32 %48, i32* %7
br label %14
49: ; preds = %14
%50 = getelementptr inbounds [20 x i8], [20 x i8]* %4, i64 0, i64 0
%51 = call i32 @unlink(i8* %50)
store i32 -1, i32* %1
br label %52
52: ; preds = %49, %37
%53 = load i32, i32* %1
ret i32 %53
}

LLVM IR (-O2):
@__const.GetDisplay.data = [20 x i8] c"/tmp/disp\00\00\00\00\00\00\00\00\00\00\00"
@.str = [6 x i8] c"%s-%d\00"
@.str.1 = [2 x i8] c"w\00"
@stderr = global i32 0
@.str.2 = [6 x i8] c":%d.0\00"
@SEEK_END = global i32 0
define i32 @GetDisplay() {
%1 = alloca [80 x i8]
%2 = alloca [20 x i8]
%3 = getelementptr inbounds [80 x i8], [80 x i8]* %1, i64 0, i64 0
%4 = getelementptr inbounds [20 x i8], [20 x i8]* %2, i64 0, i64 0
%5 = tail call i32 (...) @getpid()
%6 = call i32 (i8*, i8*, ...) @sprintf(i8* %4, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %4, i32 %5)
br label %7
7: ; preds = %0, %21
%8 = phi i32 [ 0, %0 ], [ %22, %21 ]
%9 = load i32, i32* @stderr
%10 = call i32* @freopen(i8* %4, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32 %9)
%11 = call i32 (i8*, i8*, ...) @sprintf(i8* %3, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i32 %8)
%12 = call i32* @XOpenDisplay(i8* %3)
%13 = load i32, i32* @SEEK_END
%14 = call i32 @fseek(i32* %10, i64 0, i32 %13)
%15 = call i32 @ftell(i32* %10)
%16 = icmp eq i32* %12, null
br i1 %16, label %17, label %19
17: ; preds = %7
%18 = icmp slt i32 %15, 2
br i1 %18, label %24, label %21
19: ; preds = %7
%20 = call i32 @XCloseDisplay(i32* %12)
br label %21
21: ; preds = %17, %19
%22 = add nuw nsw i32 %8, 1
%23 = icmp eq i32 %22, 11
br i1 %23, label %24, label %7
24: ; preds = %21, %17
%25 = phi i32 [ %8, %17 ], [ -1, %21 ]
%26 = call i32 @unlink(i8* %4)
ret i32 %25
}