#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_4__ TYPE_1__;

/* Type definitions */
struct TYPE_4__ {
  int visualid;
  int /*<<< orphan*/ *visual;
  int /*<<< orphan*/ screen;
};
typedef TYPE_1__ XVisualInfo;
typedef int /*<<< orphan*/ Visual;
typedef int /*<<< orphan*/ Screen;
typedef int /*<<< orphan*/ Display;

/* Variables and functions */
int /*<<< orphan*/ *DisplayOfScreen(int /*<<< orphan*/ *);
int VisualIDMask;
int VisualScreenMask;
int /*<<< orphan*/ XFree(char *);
TYPE_1__ *XGetVisualInfo(int /*<<< orphan*/ *, int, TYPE_1__ *, int *);
int /*<<< orphan*/ screen_number(int /*<<< orphan*/ *);
Visual *id_to_visual(Screen *screen, int id) {
  Display *dpy = DisplayOfScreen(screen);
  XVisualInfo vi_in, *vi_out;
  int out_count;
  vi_in.screen = screen_number(screen);
  vi_in.visualid = id;
  vi_out = XGetVisualInfo(dpy, (VisualScreenMask | VisualIDMask), &vi_in,
                          &out_count);
  if (vi_out) {
    Visual *v = vi_out[0].visual;
    XFree((char *)vi_out);
    return v;
  }
  return 0;
}


x86 assembly (clang, -O0):
<id_to_visual>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <DisplayOfScreen@plt>
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <screen_number@plt>
mov    %eax,-0x28(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    -0x20(%rbp),%rdi
mov    0x2e77(%rip),%rax
mov    (%rax),%esi
mov    0x2e5e(%rip),%rax
or     (%rax),%esi
lea    -0x38(%rbp),%rdx
lea    -0x44(%rbp),%rcx
call   1040 <XGetVisualInfo@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
je     11c2 <id_to_visual+0x82>
mov    -0x40(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x50(%rbp)
mov    -0x40(%rbp),%rdi
call   1030 <XFree@plt>
mov    -0x50(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11ca <id_to_visual+0x8a>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<id_to_visual>:
push   %rbp
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    %esi,%ebp
mov    %rdi,%rbx
call   1050 <DisplayOfScreen@plt>
mov    %rax,%r14
mov    %rbx,%rdi
call   1060 <screen_number@plt>
mov    %eax,0x18(%rsp)
mov    %ebp,0x8(%rsp)
mov    0x2e84(%rip),%rax
mov    0x2e6d(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0x8(%rsp),%rdx
lea    0x4(%rsp),%rcx
mov    %r14,%rdi
call   1040 <XGetVisualInfo@plt>
test   %rax,%rax
je     119c <id_to_visual+0x5c>
mov    0x8(%rax),%rbx
mov    %rax,%rdi
call   1030 <XFree@plt>
jmp    119e <id_to_visual+0x5e>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.TYPE_4__ = type { i32, i32*, i32 }
@VisualScreenMask = global i32 0
@VisualIDMask = global i32 0
define i32* @id_to_visual(i32* %0, i32 %1) {
%3 = alloca i32*
%4 = alloca i32*
%5 = alloca i32
%6 = alloca i32*
%7 = alloca %struct.TYPE_4__
%8 = alloca %struct.TYPE_4__*
%9 = alloca i32
%10 = alloca i32*
store i32* %0, i32** %4
store i32 %1, i32* %5
%11 = load i32*, i32** %4
%12 = call i32* @DisplayOfScreen(i32* %11)
store i32* %12, i32** %6
%13 = load i32*, i32** %4
%14 = call i32 @screen_number(i32* %13)
%15 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %7, i32 0, i32 2
store i32 %14, i32* %15
%16 = load i32, i32* %5
%17 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %7, i32 0, i32 0
store i32 %16, i32* %17
%18 = load i32*, i32** %6
%19 = load i32, i32* @VisualScreenMask
%20 = load i32, i32* @VisualIDMask
%21 = or i32 %19, %20
%22 = call %struct.TYPE_4__* @XGetVisualInfo(i32* %18, i32 %21, %struct.TYPE_4__* %7, i32* %9)
store %struct.TYPE_4__* %22, %struct.TYPE_4__** %8
%23 = load %struct.TYPE_4__*, %struct.TYPE_4__** %8
%24 = icmp ne %struct.TYPE_4__* %23, null
br i1 %24, label %25, label %34
25: ; preds = %2
%26 = load %struct.TYPE_4__*, %struct.TYPE_4__** %8
%27 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %26, i64 0
%28 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %27, i32 0, i32 1
%29 = load i32*, i32** %28
store i32* %29, i32** %10
%30 = load %struct.TYPE_4__*, %struct.TYPE_4__** %8
%31 = bitcast %struct.TYPE_4__* %30 to i8*
%32 = call i32 @XFree(i8* %31)
%33 = load i32*, i32** %10
store i32* %33, i32** %3
br label %35
34: ; preds = %2
store i32* null, i32** %3
br label %35
35: ; preds = %34, %25
%36 = load i32*, i32** %3
ret i32* %36
}

LLVM IR (-O2):
%struct.TYPE_4__ = type { i32, i32*, i32 }
@VisualScreenMask = global i32 0
@VisualIDMask = global i32 0
define i32* @id_to_visual(i32* %0, i32 %1) {
%3 = alloca %struct.TYPE_4__
%4 = alloca i32
%5 = tail call i32* @DisplayOfScreen(i32* %0)
%6 = bitcast %struct.TYPE_4__* %3 to i8*
%7 = bitcast i32* %4 to i8*
%8 = tail call i32 @screen_number(i32* %0)
%9 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %3, i64 0, i32 2
store i32 %8, i32* %9
%10 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %3, i64 0, i32 0
store i32 %1, i32* %10
%11 = load i32, i32* @VisualScreenMask
%12 = load i32, i32* @VisualIDMask
%13 = or i32 %12, %11
%14 = call %struct.TYPE_4__* @XGetVisualInfo(i32* %5, i32 %13, %struct.TYPE_4__* %3, i32* %4)
%15 = icmp eq %struct.TYPE_4__* %14, null
br i1 %15, label %21, label %16
16: ; preds = %2
%17 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %14, i64 0, i32 1
%18 = load i32*, i32** %17
%19 = bitcast %struct.TYPE_4__* %14 to i8*
%20 = call i32 @XFree(i8* %19)
br label %21
21: ; preds = %2, %16
%22 = phi i32* [ %18, %16 ], [ null, %2 ]
ret i32* %22
}