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

/* Type definitions */
struct TYPE_3__ {
  int format;
  int /*<<< orphan*/ nitems;
  int /*<<< orphan*/ value;
  int /*<<< orphan*/ encoding;
};
typedef TYPE_1__ XTextProperty;
typedef int /*<<< orphan*/ FILE;
typedef int /*<<< orphan*/ Display;

/* Variables and functions */
int /*<<< orphan*/ XCompoundTextStyle;
char *XGetAtomName(int /*<<< orphan*/ *, int /*<<< orphan*/);
int /*<<< orphan*/ *XOpenDisplay(int /*<<< orphan*/ *);
int Xutf8TextListToTextProperty(int /*<<< orphan*/ *, char **, int,
                                int /*<<< orphan*/, TYPE_1__ *);
int /*<<< orphan*/ abort();
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
size_t fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fwrite(int /*<<< orphan*/, int /*<<< orphan*/, int,
                          int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, ...);
int main(void) {
  Display *display;
  display = XOpenDisplay(NULL);
  printf("display %p\n", display);
  {
    FILE *fp = fopen("encode-xlib.utf8", "r");
    if (!fp) {
      printf("cannot open\n");
      abort();
    }
    char buf[500000];
    size_t len = fread(buf, 1, 1000000, fp);
    fclose(fp);
    buf[len] = '\0';
    static char *ulist[2];
    static XTextProperty text_prop;
    int ret;
    ulist[0] = buf;
    ret = Xutf8TextListToTextProperty(display, ulist, 1, XCompoundTextStyle,
                                      &text_prop);
    printf("ret %d\n", ret);
    if (ret >= 0) {
      printf("text encoding %lu\n", text_prop.encoding);
      printf("text encoding %s\n", XGetAtomName(display, text_prop.encoding));
      printf("text format %d\n", text_prop.format);
      printf("text nitems %lu\n", text_prop.nitems);
      printf("text value: ");
      FILE *fp = fopen("encode-emacs23xc.ctext", "w");
      if (!fp) {
        printf("cannot open\n");
        abort();
      }
      fwrite(text_prop.value, text_prop.nitems, 1, fp);
      fclose(fp);
    }
    return 0;
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x7a160,%rsp
movl   $0x0,-0x4(%rbp)
xor    %eax,%eax
mov    %eax,%edi
call   1080 <XOpenDisplay@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rsi
lea    0xe46(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
lea    0xe44(%rip),%rdi
lea    0xe4e(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11f8 <main+0x68>
lea    0xe35(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0x0,%al
call   1030 <abort@plt>
lea    -0x7a140(%rbp),%rdi
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
mov    $0xf4240,%edx
call   1040 <fread@plt>
mov    %rax,-0x7a148(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x7a148(%rbp),%rax
movb   $0x0,-0x7a140(%rbp,%rax,1)
lea    -0x7a140(%rbp),%rax
mov    %rax,0x2e41(%rip)
mov    -0x10(%rbp),%rdi
mov    0x2d9e(%rip),%rax
mov    (%rax),%ecx
lea    0x2e2d(%rip),%rsi
mov    $0x1,%edx
lea    0x2e31(%rip),%r8
call   1070 <Xutf8TextListToTextProperty@plt>
mov    %eax,-0x7a14c(%rbp)
mov    -0x7a14c(%rbp),%esi
lea    0xdb5(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
cmpl   $0x0,-0x7a14c(%rbp)
jl     135b <main+0x1cb>
mov    0x2e0b(%rip),%esi
lea    0xd9c(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x10(%rbp),%rdi
mov    0x2df3(%rip),%esi
call   1090 <XGetAtomName@plt>
mov    %rax,%rsi
lea    0xd8f(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    0x2dcb(%rip),%esi
lea    0xd8d(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    0x2dbb(%rip),%esi
lea    0xd89(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
lea    0xd8c(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
lea    0xd8b(%rip),%rdi
lea    0xd9b(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x7a158(%rbp)
cmpq   $0x0,-0x7a158(%rbp)
jne    1332 <main+0x1a2>
lea    0xcfb(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0x0,%al
call   1030 <abort@plt>
mov    0x2d60(%rip),%edi
mov    0x2d56(%rip),%esi
mov    -0x7a158(%rbp),%rcx
mov    $0x1,%edx
call   10b0 <fwrite@plt>
mov    -0x7a158(%rbp),%rdi
call   1050 <fclose@plt>
xor    %eax,%eax
add    $0x7a160,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x7a128,%rsp
xor    %edi,%edi
call   1090 <XOpenDisplay@plt>
mov    %rax,%r14
lea    0xe41(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1070 <printf@plt>
lea    0xe3c(%rip),%rdi
lea    0xe46(%rip),%rsi
call   10b0 <fopen@plt>
test   %rax,%rax
je     130b <main+0x16b>
mov    %rax,%rbx
mov    %rsp,%r15
mov    %r15,%rdi
mov    $0x1,%esi
mov    $0xf4240,%edx
mov    %rax,%rcx
call   1050 <fread@plt>
mov    %rax,%r12
mov    %rbx,%rdi
call   1060 <fclose@plt>
movb   $0x0,(%rsp,%r12,1)
mov    %r15,0x2e69(%rip)
lea    0x2e62(%rip),%rsi
mov    0x2dc3(%rip),%rax
mov    (%rax),%ecx
lea    0x2e62(%rip),%r8
mov    %r14,%rdi
mov    $0x1,%edx
call   1080 <Xutf8TextListToTextProperty@plt>
mov    %eax,%ebx
lea    0xddb(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1070 <printf@plt>
test   %ebx,%ebx
js     12fa <main+0x15a>
mov    0x2e41(%rip),%esi
lea    0xdc5(%rip),%rdi
xor    %eax,%eax
call   1070 <printf@plt>
mov    0x2e2d(%rip),%esi
mov    %r14,%rdi
call   10a0 <XGetAtomName@plt>
lea    0xdbc(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1070 <printf@plt>
mov    0x2e02(%rip),%esi
lea    0xdb7(%rip),%rdi
xor    %eax,%eax
call   1070 <printf@plt>
mov    0x2df2(%rip),%esi
lea    0xdb3(%rip),%rdi
xor    %eax,%eax
call   1070 <printf@plt>
lea    0xdb6(%rip),%rdi
xor    %eax,%eax
call   1070 <printf@plt>
lea    0xdb5(%rip),%rdi
lea    0xdc5(%rip),%rsi
call   10b0 <fopen@plt>
test   %rax,%rax
je     130b <main+0x16b>
mov    %rax,%rbx
mov    0x2db9(%rip),%edi
mov    0x2daf(%rip),%esi
mov    $0x1,%edx
mov    %rax,%rcx
call   10c0 <fwrite@plt>
mov    %rbx,%rdi
call   1060 <fclose@plt>
xor    %eax,%eax
add    $0x7a128,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
lea    0xd81(%rip),%rdi
call   1040 <puts@plt>
xor    %eax,%eax
call   1030 <abort@plt>

LLVM IR (-O0):
%struct.TYPE_3__ = type { i32, i32, i32, i32 }
@.str = [12 x i8] c"display %p\0A\00"
@.str.1 = [17 x i8] c"encode-xlib.utf8\00"
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [13 x i8] c"cannot open\0A\00"
@main.ulist = internal global [2 x i8*] zeroinitializer
@main.text_prop = internal global %struct.TYPE_3__ zeroinitializer
@XCompoundTextStyle = global i32 0
@.str.4 = [8 x i8] c"ret %d\0A\00"
@.str.5 = [19 x i8] c"text encoding %lu\0A\00"
@.str.6 = [18 x i8] c"text encoding %s\0A\00"
@.str.7 = [16 x i8] c"text format %d\0A\00"
@.str.8 = [17 x i8] c"text nitems %lu\0A\00"
@.str.9 = [13 x i8] c"text value: \00"
@.str.10 = [23 x i8] c"encode-emacs23xc.ctext\00"
@.str.11 = [2 x i8] c"w\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca i32*
%4 = alloca [500000 x i8]
%5 = alloca i64
%6 = alloca i32
%7 = alloca i32*
store i32 0, i32* %1
%8 = call i32* @XOpenDisplay(i32* null)
store i32* %8, i32** %2
%9 = load i32*, i32** %2
%10 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32* %9)
%11 = call i32* @fopen(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %11, i32** %3
%12 = load i32*, i32** %3
%13 = icmp ne i32* %12, null
br i1 %13, label %17, label %14
14: ; preds = %0
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
%16 = call i32 (...) @abort()
unreachable
17: ; preds = %0
%18 = getelementptr inbounds [500000 x i8], [500000 x i8]* %4, i64 0, i64 0
%19 = load i32*, i32** %3
%20 = call i64 @fread(i8* %18, i32 1, i32 1000000, i32* %19)
store i64 %20, i64* %5
%21 = load i32*, i32** %3
%22 = call i32 @fclose(i32* %21)
%23 = load i64, i64* %5
%24 = getelementptr inbounds [500000 x i8], [500000 x i8]* %4, i64 0, i64 %23
store i8 0, i8* %24
%25 = getelementptr inbounds [500000 x i8], [500000 x i8]* %4, i64 0, i64 0
store i8* %25, i8** getelementptr inbounds ([2 x i8*], [2 x i8*]* @main.ulist, i64 0, i64 0)
%26 = load i32*, i32** %2
%27 = load i32, i32* @XCompoundTextStyle
%28 = call i32 @Xutf8TextListToTextProperty(i32* %26, i8** getelementptr inbounds ([2 x i8*], [2 x i8*]* @main.ulist, i64 0, i64 0), i32 1, i32 %27, %struct.TYPE_3__* @main.text_prop)
store i32 %28, i32* %6
%29 = load i32, i32* %6
%30 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0), i32 %29)
%31 = load i32, i32* %6
%32 = icmp sge i32 %31, 0
br i1 %32, label %33, label %58
33: ; preds = %17
%34 = load i32, i32* getelementptr inbounds (%struct.TYPE_3__, %struct.TYPE_3__* @main.text_prop, i32 0, i32 3)
%35 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.5, i64 0, i64 0), i32 %34)
%36 = load i32*, i32** %2
%37 = load i32, i32* getelementptr inbounds (%struct.TYPE_3__, %struct.TYPE_3__* @main.text_prop, i32 0, i32 3)
%38 = call i8* @XGetAtomName(i32* %36, i32 %37)
%39 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.6, i64 0, i64 0), i8* %38)
%40 = load i32, i32* getelementptr inbounds (%struct.TYPE_3__, %struct.TYPE_3__* @main.text_prop, i32 0, i32 0)
%41 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.7, i64 0, i64 0), i32 %40)
%42 = load i32, i32* getelementptr inbounds (%struct.TYPE_3__, %struct.TYPE_3__* @main.text_prop, i32 0, i32 1)
%43 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.8, i64 0, i64 0), i32 %42)
%44 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.9, i64 0, i64 0))
%45 = call i32* @fopen(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.10, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.11, i64 0, i64 0))
store i32* %45, i32** %7
%46 = load i32*, i32** %7
%47 = icmp ne i32* %46, null
br i1 %47, label %51, label %48
48: ; preds = %33
%49 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
%50 = call i32 (...) @abort()
unreachable
51: ; preds = %33
%52 = load i32, i32* getelementptr inbounds (%struct.TYPE_3__, %struct.TYPE_3__* @main.text_prop, i32 0, i32 2)
%53 = load i32, i32* getelementptr inbounds (%struct.TYPE_3__, %struct.TYPE_3__* @main.text_prop, i32 0, i32 1)
%54 = load i32*, i32** %7
%55 = call i32 @fwrite(i32 %52, i32 %53, i32 1, i32* %54)
%56 = load i32*, i32** %7
%57 = call i32 @fclose(i32* %56)
br label %58
58: ; preds = %51, %17
ret i32 0
}

LLVM IR (-O2):
%struct.TYPE_3__ = type { i32, i32, i32, i32 }
@.str = [12 x i8] c"display %p\0A\00"
@.str.1 = [17 x i8] c"encode-xlib.utf8\00"
@.str.2 = [2 x i8] c"r\00"
@main.ulist = internal global [2 x i8*] zeroinitializer
@main.text_prop = internal global %struct.TYPE_3__ zeroinitializer
@XCompoundTextStyle = global i32 0
@.str.4 = [8 x i8] c"ret %d\0A\00"
@.str.5 = [19 x i8] c"text encoding %lu\0A\00"
@.str.6 = [18 x i8] c"text encoding %s\0A\00"
@.str.7 = [16 x i8] c"text format %d\0A\00"
@.str.8 = [17 x i8] c"text nitems %lu\0A\00"
@.str.9 = [13 x i8] c"text value: \00"
@.str.10 = [23 x i8] c"encode-emacs23xc.ctext\00"
@.str.11 = [2 x i8] c"w\00"
@str.12 = [12 x i8] c"cannot open\00"
define i32 @main() {
%1 = alloca [500000 x i8]
%2 = tail call i32* @XOpenDisplay(i32* null)
%3 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32* %2)
%4 = tail call i32* @fopen(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%5 = icmp eq i32* %4, null
br i1 %5, label %6, label %9
6: ; preds = %0
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str.12, i64 0, i64 0))
%8 = tail call i32 (...) @abort()
unreachable
9: ; preds = %0
%10 = getelementptr inbounds [500000 x i8], [500000 x i8]* %1, i64 0, i64 0
%11 = call i64 @fread(i8* %10, i32 1, i32 1000000, i32* %4)
%12 = tail call i32 @fclose(i32* %4)
%13 = getelementptr inbounds [500000 x i8], [500000 x i8]* %1, i64 0, i64 %11
store i8 0, i8* %13
store i8* %10, i8** getelementptr inbounds ([2 x i8*], [2 x i8*]* @main.ulist, i64 0, i64 0)
%14 = load i32, i32* @XCompoundTextStyle
%15 = call i32 @Xutf8TextListToTextProperty(i32* %2, i8** getelementptr inbounds ([2 x i8*], [2 x i8*]* @main.ulist, i64 0, i64 0), i32 1, i32 %14, %struct.TYPE_3__* @main.text_prop)
%16 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0), i32 %15)
%17 = icmp sgt i32 %15, -1
br i1 %17, label %18, label %39
18: ; preds = %9
%19 = load i32, i32* getelementptr inbounds (%struct.TYPE_3__, %struct.TYPE_3__* @main.text_prop, i64 0, i32 3)
%20 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.5, i64 0, i64 0), i32 %19)
%21 = load i32, i32* getelementptr inbounds (%struct.TYPE_3__, %struct.TYPE_3__* @main.text_prop, i64 0, i32 3)
%22 = call i8* @XGetAtomName(i32* %2, i32 %21)
%23 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.6, i64 0, i64 0), i8* %22)
%24 = load i32, i32* getelementptr inbounds (%struct.TYPE_3__, %struct.TYPE_3__* @main.text_prop, i64 0, i32 0)
%25 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.7, i64 0, i64 0), i32 %24)
%26 = load i32, i32* getelementptr inbounds (%struct.TYPE_3__, %struct.TYPE_3__* @main.text_prop, i64 0, i32 1)
%27 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str.8, i64 0, i64 0), i32 %26)
%28 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.9, i64 0, i64 0))
%29 = call i32* @fopen(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.10, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.11, i64 0, i64 0))
%30 = icmp eq i32* %29, null
br i1 %30, label %31, label %34
31: ; preds = %18
%32 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str.12, i64 0, i64 0))
%33 = call i32 (...) @abort()
unreachable
34: ; preds = %18
%35 = load i32, i32* getelementptr inbounds (%struct.TYPE_3__, %struct.TYPE_3__* @main.text_prop, i64 0, i32 2)
%36 = load i32, i32* getelementptr inbounds (%struct.TYPE_3__, %struct.TYPE_3__* @main.text_prop, i64 0, i32 1)
%37 = call i32 @fwrite(i32 %35, i32 %36, i32 1, i32* %29)
%38 = call i32 @fclose(i32* %29)
br label %39
39: ; preds = %34, %9
ret i32 0
}