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

/* Variables and functions */
int /*<<< orphan*/ *DisplayOfScreen(int /*<<< orphan*/ *);
int ScreenCount(int /*<<< orphan*/ *);
int /*<<< orphan*/ *ScreenOfDisplay(int /*<<< orphan*/ *, int);
int /*<<< orphan*/ abort();
int screen_number(Screen *screen) {
  Display *dpy = DisplayOfScreen(screen);
  int i;
  for (i = 0; i < ScreenCount(dpy); i++)
    if (ScreenOfDisplay(dpy, i) == screen)
      return i;
  abort();
  return 0;
}


x86 assembly (clang, -O0):
<screen_number>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1050 <DisplayOfScreen@plt>
mov    %rax,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <ScreenCount@plt>
mov    %eax,%ecx
mov    -0x18(%rbp),%eax
cmp    %ecx,%eax
jge    11ae <screen_number+0x6e>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1060 <ScreenOfDisplay@plt>
cmp    -0x8(%rbp),%rax
jne    119b <screen_number+0x5b>
mov    -0x14(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret
jmp    11a0 <screen_number+0x60>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1160 <screen_number+0x20>
mov    $0x0,%al
call   1030 <abort@plt>

x86 assembly (clang, -O2):
<screen_number>:
push   %rbp
push   %r14
push   %rbx
mov    %rdi,%r14
call   1050 <DisplayOfScreen@plt>
mov    %rax,%rbx
mov    %rax,%rdi
call   1040 <ScreenCount@plt>
test   %eax,%eax
jle    117e <screen_number+0x3e>
xor    %ebp,%ebp
nopl   (%rax)
mov    %rbx,%rdi
mov    %ebp,%esi
call   1060 <ScreenOfDisplay@plt>
cmp    %r14,%rax
je     1185 <screen_number+0x45>
add    $0x1,%ebp
mov    %rbx,%rdi
call   1040 <ScreenCount@plt>
cmp    %eax,%ebp
jl     1160 <screen_number+0x20>
xor    %eax,%eax
call   1030 <abort@plt>
mov    %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
define i32 @screen_number(i32* %0) {
%2 = alloca i32*
%3 = alloca i32*
%4 = alloca i32
store i32* %0, i32** %2
%5 = load i32*, i32** %2
%6 = call i32* @DisplayOfScreen(i32* %5)
store i32* %6, i32** %3
store i32 0, i32* %4
br label %7
7: ; preds = %21, %1
%8 = load i32, i32* %4
%9 = load i32*, i32** %3
%10 = call i32 @ScreenCount(i32* %9)
%11 = icmp slt i32 %8, %10
br i1 %11, label %12, label %24
12: ; preds = %7
%13 = load i32*, i32** %3
%14 = load i32, i32* %4
%15 = call i32* @ScreenOfDisplay(i32* %13, i32 %14)
%16 = load i32*, i32** %2
%17 = icmp eq i32* %15, %16
br i1 %17, label %18, label %20
18: ; preds = %12
%19 = load i32, i32* %4
ret i32 %19
20: ; preds = %12
br label %21
21: ; preds = %20
%22 = load i32, i32* %4
%23 = add nsw i32 %22, 1
store i32 %23, i32* %4
br label %7
24: ; preds = %7
%25 = call i32 (...) @abort()
unreachable
}

LLVM IR (-O2):
define i32 @screen_number(i32* %0) {
%2 = tail call i32* @DisplayOfScreen(i32* %0)
%3 = tail call i32 @ScreenCount(i32* %2)
%4 = icmp sgt i32 %3, 0
br i1 %4, label %5, label %14
5: ; preds = %1, %10
%6 = phi i32 [ %11, %10 ], [ 0, %1 ]
%7 = tail call i32* @ScreenOfDisplay(i32* %2, i32 %6)
%8 = icmp eq i32* %7, %0
br i1 %8, label %9, label %10
9: ; preds = %5
ret i32 %6
10: ; preds = %5
%11 = add nuw nsw i32 %6, 1
%12 = tail call i32 @ScreenCount(i32* %2)
%13 = icmp slt i32 %11, %12
br i1 %13, label %5, label %14
14: ; preds = %10, %1
%15 = tail call i32 (...) @abort()
unreachable
}