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

/* Variables and functions */
scalar_t__ XResourceManagerString(int /*<<< orphan*/ *);
int /*<<< orphan*/ *XrmGetFileDatabase(char *);
int /*<<< orphan*/ *XrmGetStringDatabase(scalar_t__);
int /*<<< orphan*/ XrmInitialize();
int /*<<< orphan*/ XrmMergeDatabases(int /*<<< orphan*/ *,
                                     int /*<<< orphan*/ **);
int /*<<< orphan*/ free(char *);
char *getenv(char *);
scalar_t__ malloc(int);
int /*<<< orphan*/ sprintf(char *, char *, char *, char *, char *);
int strlen(char *);
char *xw_home_dir();
XrmDatabase xw_get_xrdb(Display *display, XrmDatabase cmd_db) {
  XrmDatabase user_db = NULL;
  XrmDatabase env_db = NULL;
  XrmInitialize();
  if (XResourceManagerString(display)) {
    user_db = XrmGetStringDatabase(XResourceManagerString(display));
  } else {
    char *dir = xw_home_dir();
    char *sep = "/";
    char *file = ".Xdefaults";
    if (dir) {
      char *path = (char *)malloc(
          sizeof(char) * (strlen(dir) + strlen(sep) + strlen(file) + 1));
      if (path) {
        sprintf(path, "%s%s%s", dir, sep, file);
        user_db = XrmGetFileDatabase(path);
        free(path);
      };
    };
  };
  {
    char *env = getenv("XENVIRONMENT");
    if (env)
      env_db = XrmGetFileDatabase(env);
  };
  XrmMergeDatabases(env_db, &user_db);
  XrmMergeDatabases(cmd_db, &user_db);
  return user_db;
}


x86 assembly (clang, -O0):
<xw_get_xrdb>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    $0x0,%al
call   1070 <XrmInitialize@plt>
mov    -0x8(%rbp),%rdi
call   10b0 <XResourceManagerString@plt>
cmp    $0x0,%rax
je     1204 <xw_get_xrdb+0x54>
mov    -0x8(%rbp),%rdi
call   10b0 <XResourceManagerString@plt>
mov    %rax,%rdi
call   1090 <XrmGetStringDatabase@plt>
mov    %rax,-0x18(%rbp)
jmp    12ba <xw_get_xrdb+0x10a>
mov    $0x0,%al
call   10c0 <xw_home_dir@plt>
mov    %rax,-0x28(%rbp)
lea    0xdea(%rip),%rax
mov    %rax,-0x30(%rbp)
lea    0xde1(%rip),%rax
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     12b5 <xw_get_xrdb+0x105>
mov    -0x28(%rbp),%rdi
call   1080 <strlen@plt>
mov    %eax,-0x50(%rbp)
mov    -0x30(%rbp),%rdi
call   1080 <strlen@plt>
mov    %eax,%ecx
mov    -0x50(%rbp),%eax
add    %ecx,%eax
mov    %eax,-0x4c(%rbp)
mov    -0x38(%rbp),%rdi
call   1080 <strlen@plt>
mov    %eax,%ecx
mov    -0x4c(%rbp),%eax
add    %ecx,%eax
add    $0x1,%eax
cltq
shl    $0x0,%rax
mov    %eax,%edi
call   10a0 <malloc@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
je     12b0 <xw_get_xrdb+0x100>
mov    -0x40(%rbp),%rdi
mov    -0x28(%rbp),%rdx
mov    -0x30(%rbp),%rcx
mov    -0x38(%rbp),%r8
lea    0xd78(%rip),%rsi
call   10d0 <sprintf@plt>
mov    -0x40(%rbp),%rdi
call   1060 <XrmGetFileDatabase@plt>
mov    %rax,-0x18(%rbp)
mov    -0x40(%rbp),%rdi
call   1040 <free@plt>
jmp    12b5 <xw_get_xrdb+0x105>
jmp    12ba <xw_get_xrdb+0x10a>
lea    0xd53(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x48(%rbp)
je     12e2 <xw_get_xrdb+0x132>
mov    -0x48(%rbp),%rdi
call   1060 <XrmGetFileDatabase@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1050 <XrmMergeDatabases@plt>
mov    -0x10(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1050 <XrmMergeDatabases@plt>
mov    -0x18(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<xw_get_xrdb>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbx
movq   $0x0,(%rsp)
xor    %eax,%eax
call   1070 <XrmInitialize@plt>
mov    %rbx,%rdi
call   10b0 <XResourceManagerString@plt>
test   %rax,%rax
je     11ef <xw_get_xrdb+0x3f>
mov    %rbx,%rdi
call   10b0 <XResourceManagerString@plt>
mov    %rax,%rdi
call   1090 <XrmGetStringDatabase@plt>
mov    %rax,(%rsp)
jmp    126b <xw_get_xrdb+0xbb>
xor    %eax,%eax
call   10c0 <xw_home_dir@plt>
test   %rax,%rax
je     126b <xw_get_xrdb+0xbb>
mov    %rax,%r15
mov    %rax,%rdi
call   1080 <strlen@plt>
mov    %eax,%ebp
lea    0xdf1(%rip),%rdi
call   1080 <strlen@plt>
mov    %eax,%ebx
lea    0xde5(%rip),%rdi
call   1080 <strlen@plt>
add    %ebp,%ebx
lea    (%rax,%rbx,1),%edi
add    $0x1,%edi
call   10a0 <malloc@plt>
test   %rax,%rax
je     126b <xw_get_xrdb+0xbb>
mov    %rax,%rbx
lea    0xdcf(%rip),%rsi
lea    0xdbb(%rip),%rcx
lea    0xdb6(%rip),%r8
mov    %rax,%rdi
mov    %r15,%rdx
call   10d0 <sprintf@plt>
mov    %rbx,%rdi
call   1060 <XrmGetFileDatabase@plt>
mov    %rax,(%rsp)
mov    %rbx,%rdi
call   1040 <free@plt>
lea    0xda2(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     1289 <xw_get_xrdb+0xd9>
mov    %rax,%rdi
call   1060 <XrmGetFileDatabase@plt>
mov    %rax,%rdi
jmp    128b <xw_get_xrdb+0xdb>
xor    %edi,%edi
mov    %rsp,%rbx
mov    %rbx,%rsi
call   1050 <XrmMergeDatabases@plt>
mov    %r14,%rdi
mov    %rbx,%rsi
call   1050 <XrmMergeDatabases@plt>
mov    (%rsp),%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"/\00"
@.str.1 = [11 x i8] c".Xdefaults\00"
@.str.2 = [7 x i8] c"%s%s%s\00"
@.str.3 = [13 x i8] c"XENVIRONMENT\00"
define i32* @xw_get_xrdb(i32* %0, i32* %1) {
%3 = alloca i32*
%4 = alloca i32*
%5 = alloca i32*
%6 = alloca i32*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i8*
store i32* %0, i32** %3
store i32* %1, i32** %4
store i32* null, i32** %5
store i32* null, i32** %6
%12 = call i32 (...) @XrmInitialize()
%13 = load i32*, i32** %3
%14 = call i64 @XResourceManagerString(i32* %13)
%15 = icmp ne i64 %14, 0
br i1 %15, label %16, label %20
16: ; preds = %2
%17 = load i32*, i32** %3
%18 = call i64 @XResourceManagerString(i32* %17)
%19 = call i32* @XrmGetStringDatabase(i64 %18)
store i32* %19, i32** %5
br label %53
20: ; preds = %2
%21 = call i8* (...) @xw_home_dir()
store i8* %21, i8** %7
store i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i8** %8
store i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i8** %9
%22 = load i8*, i8** %7
%23 = icmp ne i8* %22, null
br i1 %23, label %24, label %52
24: ; preds = %20
%25 = load i8*, i8** %7
%26 = call i32 @strlen(i8* %25)
%27 = load i8*, i8** %8
%28 = call i32 @strlen(i8* %27)
%29 = add nsw i32 %26, %28
%30 = load i8*, i8** %9
%31 = call i32 @strlen(i8* %30)
%32 = add nsw i32 %29, %31
%33 = add nsw i32 %32, 1
%34 = sext i32 %33 to i64
%35 = mul i64 1, %34
%36 = trunc i64 %35 to i32
%37 = call i64 @malloc(i32 %36)
%38 = inttoptr i64 %37 to i8*
store i8* %38, i8** %10
%39 = load i8*, i8** %10
%40 = icmp ne i8* %39, null
br i1 %40, label %41, label %51
41: ; preds = %24
%42 = load i8*, i8** %10
%43 = load i8*, i8** %7
%44 = load i8*, i8** %8
%45 = load i8*, i8** %9
%46 = call i32 @sprintf(i8* %42, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i8* %43, i8* %44, i8* %45)
%47 = load i8*, i8** %10
%48 = call i32* @XrmGetFileDatabase(i8* %47)
store i32* %48, i32** %5
%49 = load i8*, i8** %10
%50 = call i32 @free(i8* %49)
br label %51
51: ; preds = %41, %24
br label %52
52: ; preds = %51, %20
br label %53
53: ; preds = %52, %16
%54 = call i8* @getenv(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
store i8* %54, i8** %11
%55 = load i8*, i8** %11
%56 = icmp ne i8* %55, null
br i1 %56, label %57, label %60
57: ; preds = %53
%58 = load i8*, i8** %11
%59 = call i32* @XrmGetFileDatabase(i8* %58)
store i32* %59, i32** %6
br label %60
60: ; preds = %57, %53
%61 = load i32*, i32** %6
%62 = call i32 @XrmMergeDatabases(i32* %61, i32** %5)
%63 = load i32*, i32** %4
%64 = call i32 @XrmMergeDatabases(i32* %63, i32** %5)
%65 = load i32*, i32** %5
ret i32* %65
}

LLVM IR (-O2):
@.str = [2 x i8] c"/\00"
@.str.1 = [11 x i8] c".Xdefaults\00"
@.str.2 = [7 x i8] c"%s%s%s\00"
@.str.3 = [13 x i8] c"XENVIRONMENT\00"
define i32* @xw_get_xrdb(i32* %0, i32* %1) {
%3 = alloca i32*
%4 = bitcast i32** %3 to i8*
store i32* null, i32** %3
%5 = tail call i32 (...) @XrmInitialize()
%6 = tail call i64 @XResourceManagerString(i32* %0)
%7 = icmp eq i64 %6, 0
br i1 %7, label %11, label %8
8: ; preds = %2
%9 = tail call i64 @XResourceManagerString(i32* %0)
%10 = tail call i32* @XrmGetStringDatabase(i64 %9)
store i32* %10, i32** %3
br label %28
11: ; preds = %2
%12 = tail call i8* (...) @xw_home_dir()
%13 = icmp eq i8* %12, null
br i1 %13, label %28, label %14
14: ; preds = %11
%15 = tail call i32 @strlen(i8* %12)
%16 = tail call i32 @strlen(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%17 = tail call i32 @strlen(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
%18 = add i32 %15, 1
%19 = add i32 %18, %16
%20 = add i32 %19, %17
%21 = tail call i64 @malloc(i32 %20)
%22 = inttoptr i64 %21 to i8*
%23 = icmp eq i64 %21, 0
br i1 %23, label %28, label %24
24: ; preds = %14
%25 = tail call i32 @sprintf(i8* %22, i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i8* %12, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
%26 = tail call i32* @XrmGetFileDatabase(i8* %22)
store i32* %26, i32** %3
%27 = tail call i32 @free(i8* %22)
br label %28
28: ; preds = %11, %24, %14, %8
%29 = tail call i8* @getenv(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
%30 = icmp eq i8* %29, null
br i1 %30, label %33, label %31
31: ; preds = %28
%32 = tail call i32* @XrmGetFileDatabase(i8* %29)
br label %33
33: ; preds = %31, %28
%34 = phi i32* [ %32, %31 ], [ null, %28 ]
%35 = call i32 @XrmMergeDatabases(i32* %34, i32** %3)
%36 = call i32 @XrmMergeDatabases(i32* %1, i32** %3)
%37 = load i32*, i32** %3
ret i32* %37
}