#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__ {
  int /*<<< orphan*/ *visual;
  int /*<<< orphan*/ depth;
  int /*<<< orphan*/ class;
  int /*<<< orphan*/ screen;
};
typedef TYPE_1__ XVisualInfo;
typedef int /*<<< orphan*/ Visual;
typedef int /*<<< orphan*/ Screen;
typedef int /*<<< orphan*/ Display;

/* Variables and functions */
int /*<<< orphan*/ *DefaultVisualOfScreen(int /*<<< orphan*/ *);
int /*<<< orphan*/ *DisplayOfScreen(int /*<<< orphan*/ *);
int VisualClassMask;
int VisualDepthMask;
int VisualScreenMask;
int /*<<< orphan*/ XFree(TYPE_1__ *);
TYPE_1__ *XGetVisualInfo(int /*<<< orphan*/ *, int, TYPE_1__ *, int *);
int /*<<< orphan*/ screen_number(int /*<<< orphan*/ *);
int /*<<< orphan*/ visual_class(int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int /*<<< orphan*/ visual_depth(int /*<<< orphan*/ *, int /*<<< orphan*/ *);
Visual *find_similar_visual(Screen *screen, Visual *old_visual) {
  Display *dpy = DisplayOfScreen(screen);
  XVisualInfo vi_in, *vi_out;
  Visual *result = 0;
  int out_count;
  vi_in.screen = screen_number(screen);
  vi_in.class = visual_class(screen, old_visual);
  vi_in.depth = visual_depth(screen, old_visual);
  vi_out = XGetVisualInfo(
      dpy, (VisualScreenMask | VisualClassMask | VisualDepthMask), &vi_in,
      &out_count);
  if (vi_out && out_count > 0)
    result = vi_out[0].visual;
  if (vi_out)
    XFree(vi_out);
  vi_out = 0;
  if (!result) {
    vi_out = XGetVisualInfo(dpy, (VisualScreenMask | VisualClassMask), &vi_in,
                            &out_count);
    if (vi_out && out_count > 0)
      result = vi_out[0].visual;
    if (vi_out)
      XFree(vi_out);
    vi_out = 0;
  }
  if (!result)
    result = DefaultVisualOfScreen(screen);
  return result;
}


x86 assembly (clang, -O0):
<find_similar_visual>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
call   1060 <DisplayOfScreen@plt>
mov    %rax,-0x18(%rbp)
movq   $0x0,-0x40(%rbp)
mov    -0x8(%rbp),%rdi
call   1090 <screen_number@plt>
mov    %eax,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1030 <visual_class@plt>
mov    %eax,-0x24(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1070 <visual_depth@plt>
mov    %eax,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
mov    0x2e24(%rip),%rax
mov    (%rax),%esi
mov    0x2dfb(%rip),%rax
or     (%rax),%esi
mov    0x2dfa(%rip),%rax
or     (%rax),%esi
lea    -0x30(%rbp),%rdx
lea    -0x44(%rbp),%rcx
call   1050 <XGetVisualInfo@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
je     1211 <find_similar_visual+0xa1>
cmpl   $0x0,-0x44(%rbp)
jle    1211 <find_similar_visual+0xa1>
mov    -0x38(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x38(%rbp)
je     1225 <find_similar_visual+0xb5>
mov    -0x38(%rbp),%rdi
call   1040 <XFree@plt>
movq   $0x0,-0x38(%rbp)
cmpq   $0x0,-0x40(%rbp)
jne    129b <find_similar_visual+0x12b>
mov    -0x18(%rbp),%rdi
mov    0x2dad(%rip),%rax
mov    (%rax),%esi
mov    0x2d84(%rip),%rax
or     (%rax),%esi
lea    -0x30(%rbp),%rdx
lea    -0x44(%rbp),%rcx
call   1050 <XGetVisualInfo@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
je     127f <find_similar_visual+0x10f>
cmpl   $0x0,-0x44(%rbp)
jle    127f <find_similar_visual+0x10f>
mov    -0x38(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x38(%rbp)
je     1293 <find_similar_visual+0x123>
mov    -0x38(%rbp),%rdi
call   1040 <XFree@plt>
movq   $0x0,-0x38(%rbp)
cmpq   $0x0,-0x40(%rbp)
jne    12b3 <find_similar_visual+0x143>
mov    -0x8(%rbp),%rdi
call   1080 <DefaultVisualOfScreen@plt>
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<find_similar_visual>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x20,%rsp
mov    %rsi,%rbx
mov    %rdi,%r15
call   1060 <DisplayOfScreen@plt>
mov    %rax,%r14
mov    %r15,%rdi
call   1090 <screen_number@plt>
mov    %eax,0x18(%rsp)
mov    %r15,%rdi
mov    %rbx,%rsi
call   1030 <visual_class@plt>
mov    %eax,0x14(%rsp)
mov    %r15,%rdi
mov    %rbx,%rsi
call   1070 <visual_depth@plt>
mov    0x2e38(%rip),%r12
mov    0x2e11(%rip),%r13
mov    0x0(%r13),%esi
or     (%r12),%esi
mov    %eax,0x10(%rsp)
mov    0x2e06(%rip),%rax
or     (%rax),%esi
lea    0x8(%rsp),%rdx
lea    0x4(%rsp),%rcx
mov    %r14,%rdi
call   1050 <XGetVisualInfo@plt>
xor    %ebx,%ebx
test   %rax,%rax
je     11f7 <find_similar_visual+0x87>
cmpl   $0x0,0x4(%rsp)
jle    11f7 <find_similar_visual+0x87>
mov    (%rax),%rbx
test   %rax,%rax
je     1204 <find_similar_visual+0x94>
mov    %rax,%rdi
call   1040 <XFree@plt>
test   %rbx,%rbx
jne    1251 <find_similar_visual+0xe1>
mov    0x0(%r13),%esi
or     (%r12),%esi
lea    0x8(%rsp),%rdx
lea    0x4(%rsp),%rcx
mov    %r14,%rdi
call   1050 <XGetVisualInfo@plt>
xor    %ebx,%ebx
test   %rax,%rax
je     1234 <find_similar_visual+0xc4>
cmpl   $0x0,0x4(%rsp)
jle    1234 <find_similar_visual+0xc4>
mov    (%rax),%rbx
test   %rax,%rax
je     1241 <find_similar_visual+0xd1>
mov    %rax,%rdi
call   1040 <XFree@plt>
test   %rbx,%rbx
jne    1251 <find_similar_visual+0xe1>
mov    %r15,%rdi
call   1080 <DefaultVisualOfScreen@plt>
mov    %rax,%rbx
mov    %rbx,%rax
add    $0x20,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.TYPE_5__ = type { i32*, i32, i32, i32 }
@VisualScreenMask = global i32 0
@VisualClassMask = global i32 0
@VisualDepthMask = global i32 0
define i32* @find_similar_visual(i32* %0, i32* %1) {
%3 = alloca i32*
%4 = alloca i32*
%5 = alloca i32*
%6 = alloca %struct.TYPE_5__
%7 = alloca %struct.TYPE_5__*
%8 = alloca i32*
%9 = alloca i32
store i32* %0, i32** %3
store i32* %1, i32** %4
%10 = load i32*, i32** %3
%11 = call i32* @DisplayOfScreen(i32* %10)
store i32* %11, i32** %5
store i32* null, i32** %8
%12 = load i32*, i32** %3
%13 = call i32 @screen_number(i32* %12)
%14 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %6, i32 0, i32 3
store i32 %13, i32* %14
%15 = load i32*, i32** %3
%16 = load i32*, i32** %4
%17 = call i32 @visual_class(i32* %15, i32* %16)
%18 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %6, i32 0, i32 2
store i32 %17, i32* %18
%19 = load i32*, i32** %3
%20 = load i32*, i32** %4
%21 = call i32 @visual_depth(i32* %19, i32* %20)
%22 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %6, i32 0, i32 1
store i32 %21, i32* %22
%23 = load i32*, i32** %5
%24 = load i32, i32* @VisualScreenMask
%25 = load i32, i32* @VisualClassMask
%26 = or i32 %24, %25
%27 = load i32, i32* @VisualDepthMask
%28 = or i32 %26, %27
%29 = call %struct.TYPE_5__* @XGetVisualInfo(i32* %23, i32 %28, %struct.TYPE_5__* %6, i32* %9)
store %struct.TYPE_5__* %29, %struct.TYPE_5__** %7
%30 = load %struct.TYPE_5__*, %struct.TYPE_5__** %7
%31 = icmp ne %struct.TYPE_5__* %30, null
br i1 %31, label %32, label %40
32: ; preds = %2
%33 = load i32, i32* %9
%34 = icmp sgt i32 %33, 0
br i1 %34, label %35, label %40
35: ; preds = %32
%36 = load %struct.TYPE_5__*, %struct.TYPE_5__** %7
%37 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %36, i64 0
%38 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %37, i32 0, i32 0
%39 = load i32*, i32** %38
store i32* %39, i32** %8
br label %40
40: ; preds = %35, %32, %2
%41 = load %struct.TYPE_5__*, %struct.TYPE_5__** %7
%42 = icmp ne %struct.TYPE_5__* %41, null
br i1 %42, label %43, label %46
43: ; preds = %40
%44 = load %struct.TYPE_5__*, %struct.TYPE_5__** %7
%45 = call i32 @XFree(%struct.TYPE_5__* %44)
br label %46
46: ; preds = %43, %40
store %struct.TYPE_5__* null, %struct.TYPE_5__** %7
%47 = load i32*, i32** %8
%48 = icmp ne i32* %47, null
br i1 %48, label %72, label %49
49: ; preds = %46
%50 = load i32*, i32** %5
%51 = load i32, i32* @VisualScreenMask
%52 = load i32, i32* @VisualClassMask
%53 = or i32 %51, %52
%54 = call %struct.TYPE_5__* @XGetVisualInfo(i32* %50, i32 %53, %struct.TYPE_5__* %6, i32* %9)
store %struct.TYPE_5__* %54, %struct.TYPE_5__** %7
%55 = load %struct.TYPE_5__*, %struct.TYPE_5__** %7
%56 = icmp ne %struct.TYPE_5__* %55, null
br i1 %56, label %57, label %65
57: ; preds = %49
%58 = load i32, i32* %9
%59 = icmp sgt i32 %58, 0
br i1 %59, label %60, label %65
60: ; preds = %57
%61 = load %struct.TYPE_5__*, %struct.TYPE_5__** %7
%62 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %61, i64 0
%63 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %62, i32 0, i32 0
%64 = load i32*, i32** %63
store i32* %64, i32** %8
br label %65
65: ; preds = %60, %57, %49
%66 = load %struct.TYPE_5__*, %struct.TYPE_5__** %7
%67 = icmp ne %struct.TYPE_5__* %66, null
br i1 %67, label %68, label %71
68: ; preds = %65
%69 = load %struct.TYPE_5__*, %struct.TYPE_5__** %7
%70 = call i32 @XFree(%struct.TYPE_5__* %69)
br label %71
71: ; preds = %68, %65
store %struct.TYPE_5__* null, %struct.TYPE_5__** %7
br label %72
72: ; preds = %71, %46
%73 = load i32*, i32** %8
%74 = icmp ne i32* %73, null
br i1 %74, label %78, label %75
75: ; preds = %72
%76 = load i32*, i32** %3
%77 = call i32* @DefaultVisualOfScreen(i32* %76)
store i32* %77, i32** %8
br label %78
78: ; preds = %75, %72
%79 = load i32*, i32** %8
ret i32* %79
}

LLVM IR (-O2):
%struct.TYPE_5__ = type { i32*, i32, i32, i32 }
@VisualScreenMask = global i32 0
@VisualClassMask = global i32 0
@VisualDepthMask = global i32 0
define i32* @find_similar_visual(i32* %0, i32* %1) {
%3 = alloca %struct.TYPE_5__
%4 = alloca i32
%5 = tail call i32* @DisplayOfScreen(i32* %0)
%6 = bitcast %struct.TYPE_5__* %3 to i8*
%7 = bitcast i32* %4 to i8*
%8 = tail call i32 @screen_number(i32* %0)
%9 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %3, i64 0, i32 3
store i32 %8, i32* %9
%10 = tail call i32 @visual_class(i32* %0, i32* %1)
%11 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %3, i64 0, i32 2
store i32 %10, i32* %11
%12 = tail call i32 @visual_depth(i32* %0, i32* %1)
%13 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %3, i64 0, i32 1
store i32 %12, i32* %13
%14 = load i32, i32* @VisualScreenMask
%15 = load i32, i32* @VisualClassMask
%16 = or i32 %15, %14
%17 = load i32, i32* @VisualDepthMask
%18 = or i32 %16, %17
%19 = call %struct.TYPE_5__* @XGetVisualInfo(i32* %5, i32 %18, %struct.TYPE_5__* %3, i32* %4)
%20 = icmp ne %struct.TYPE_5__* %19, null
%21 = load i32, i32* %4
%22 = icmp sgt i32 %21, 0
%23 = select i1 %20, i1 %22, i1 false
br i1 %23, label %24, label %27
24: ; preds = %2
%25 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %19, i64 0, i32 0
%26 = load i32*, i32** %25
br label %27
27: ; preds = %24, %2
%28 = phi i32* [ %26, %24 ], [ null, %2 ]
br i1 %20, label %29, label %31
29: ; preds = %27
%30 = call i32 @XFree(%struct.TYPE_5__* %19)
br label %31
31: ; preds = %29, %27
%32 = icmp eq i32* %28, null
br i1 %32, label %33, label %53
33: ; preds = %31
%34 = load i32, i32* @VisualScreenMask
%35 = load i32, i32* @VisualClassMask
%36 = or i32 %35, %34
%37 = call %struct.TYPE_5__* @XGetVisualInfo(i32* %5, i32 %36, %struct.TYPE_5__* %3, i32* %4)
%38 = icmp ne %struct.TYPE_5__* %37, null
%39 = load i32, i32* %4
%40 = icmp sgt i32 %39, 0
%41 = select i1 %38, i1 %40, i1 false
br i1 %41, label %42, label %45
42: ; preds = %33
%43 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %37, i64 0, i32 0
%44 = load i32*, i32** %43
br label %45
45: ; preds = %42, %33
%46 = phi i32* [ %44, %42 ], [ null, %33 ]
br i1 %38, label %47, label %49
47: ; preds = %45
%48 = call i32 @XFree(%struct.TYPE_5__* %37)
br label %49
49: ; preds = %45, %47
%50 = icmp eq i32* %46, null
br i1 %50, label %51, label %53
51: ; preds = %49
%52 = call i32* @DefaultVisualOfScreen(i32* %0)
br label %53
53: ; preds = %31, %51, %49
%54 = phi i32* [ %46, %49 ], [ %52, %51 ], [ %28, %31 ]
ret i32* %54
}