#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*/ Display;

/* Variables and functions */
int /*<<< orphan*/ XCloseDisplay(int /*<<< orphan*/ *);
int /*<<< orphan*/ *XOpenDisplay(char *);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ sprintf(char *, char *, int);
int main(void) {
  int i;
  char name[10];
  Display *display;
  for (i = 0; i < 1000; i++) {
    sprintf(name, ":%d", i);
    if ((display = XOpenDisplay(name))) {
      XCloseDisplay(display);
      continue;
    }
    printf("%s\n", name);
    return 0;
  }
  return 1;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x8(%rbp)
cmpl   $0x3e8,-0x8(%rbp)
jge    11c5 <main+0x85>
lea    -0x12(%rbp),%rdi
mov    -0x8(%rbp),%edx
lea    0xe8f(%rip),%rsi
call   1050 <sprintf@plt>
lea    -0x12(%rbp),%rdi
call   1040 <XOpenDisplay@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     119b <main+0x5b>
mov    -0x20(%rbp),%rdi
call   1060 <XCloseDisplay@plt>
jmp    11b7 <main+0x77>
lea    -0x12(%rbp),%rsi
lea    0xe5e(%rip),%rdi
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    11cc <main+0x8c>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    1156 <main+0x16>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
xor    %ebx,%ebx
lea    0xeae(%rip),%r14
lea    0x6(%rsp),%r15
nopw   0x0(%rax,%rax,1)
mov    %r15,%rdi
mov    %r14,%rsi
mov    %ebx,%edx
call   1050 <sprintf@plt>
mov    %r15,%rdi
call   1040 <XOpenDisplay@plt>
test   %rax,%rax
je     1194 <main+0x54>
mov    %rax,%rdi
call   1060 <XCloseDisplay@plt>
add    $0x1,%ebx
cmp    $0x3e8,%ebx
jne    1160 <main+0x20>
mov    $0x1,%eax
jmp    11a7 <main+0x67>
lea    0xe69(%rip),%rdi
lea    0x6(%rsp),%rsi
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [4 x i8] c":%d\00"
@.str.1 = [4 x i8] c"%s\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca [10 x i8]
%4 = alloca i32*
store i32 0, i32* %1
store i32 0, i32* %2
br label %5
5: ; preds = %21, %0
%6 = load i32, i32* %2
%7 = icmp slt i32 %6, 1000
br i1 %7, label %8, label %24
8: ; preds = %5
%9 = getelementptr inbounds [10 x i8], [10 x i8]* %3, i64 0, i64 0
%10 = load i32, i32* %2
%11 = call i32 @sprintf(i8* %9, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %10)
%12 = getelementptr inbounds [10 x i8], [10 x i8]* %3, i64 0, i64 0
%13 = call i32* @XOpenDisplay(i8* %12)
store i32* %13, i32** %4
%14 = icmp ne i32* %13, null
br i1 %14, label %15, label %18
15: ; preds = %8
%16 = load i32*, i32** %4
%17 = call i32 @XCloseDisplay(i32* %16)
br label %21
18: ; preds = %8
%19 = getelementptr inbounds [10 x i8], [10 x i8]* %3, i64 0, i64 0
%20 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %19)
store i32 0, i32* %1
br label %25
21: ; preds = %15
%22 = load i32, i32* %2
%23 = add nsw i32 %22, 1
store i32 %23, i32* %2
br label %5
24: ; preds = %5
store i32 1, i32* %1
br label %25
25: ; preds = %24, %18
%26 = load i32, i32* %1
ret i32 %26
}

LLVM IR (-O2):
@.str = [4 x i8] c":%d\00"
@.str.1 = [4 x i8] c"%s\0A\00"
define i32 @main() {
%1 = alloca [10 x i8]
%2 = getelementptr inbounds [10 x i8], [10 x i8]* %1, i64 0, i64 0
br label %3
3: ; preds = %0, %8
%4 = phi i32 [ 0, %0 ], [ %10, %8 ]
%5 = call i32 @sprintf(i8* %2, i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %4)
%6 = call i32* @XOpenDisplay(i8* %2)
%7 = icmp eq i32* %6, null
br i1 %7, label %12, label %8
8: ; preds = %3
%9 = call i32 @XCloseDisplay(i32* %6)
%10 = add nuw nsw i32 %4, 1
%11 = icmp eq i32 %10, 1000
br i1 %11, label %14, label %3
12: ; preds = %3
%13 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %2)
br label %14
14: ; preds = %8, %12
%15 = phi i32 [ 0, %12 ], [ 1, %8 ]
ret i32 %15
}