#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*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ free(char *);
int parseTwoTokenLine(int /*<<< orphan*/ *, char *, char *);
int /*<<< orphan*/ strcmp(char *, char const *);
char *strdup(char const *);
char *resolveLocale(const char *locale) {
  FILE *f;
  char first[1024], second[1024];
  char *resolved = NULL;
  int rc;
  f = fopen("/usr/X11R6/lib/X11/locale/locale.alias", "r");
  if (f == NULL)
    goto bail;
  do {
    rc = parseTwoTokenLine(f, first, second);
    if (rc < -1)
      goto bail;
    if (!strcmp(first, locale)) {
      resolved = strdup(second);
      if (resolved == NULL)
        goto bail;
      break;
    }
  } while (rc >= 0);
  if (resolved == NULL) {
    resolved = strdup(locale);
    if (resolved == NULL)
      goto bail;
  }
  fclose(f);
  return resolved;
bail:
  if (f != NULL)
    fclose(f);
  if (resolved != NULL)
    free(resolved);
  return NULL;
}


x86 assembly (clang, -O0):
<resolveLocale>:
push   %rbp
mov    %rsp,%rbp
sub    $0x830,%rsp
mov    %rdi,-0x10(%rbp)
movq   $0x0,-0x828(%rbp)
lea    0xe7f(%rip),%rdi
lea    0xe9f(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11a1 <resolveLocale+0x41>
jmp    127a <resolveLocale+0x11a>
jmp    11a6 <resolveLocale+0x46>
mov    -0x18(%rbp),%rdi
lea    -0x420(%rbp),%rsi
lea    -0x820(%rbp),%rdx
call   1060 <parseTwoTokenLine@plt>
mov    %eax,-0x82c(%rbp)
cmpl   $0xffffffff,-0x82c(%rbp)
jge    11d5 <resolveLocale+0x75>
jmp    127a <resolveLocale+0x11a>
lea    -0x420(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1050 <strcmp@plt>
cmp    $0x0,%eax
jne    1219 <resolveLocale+0xb9>
lea    -0x820(%rbp),%rdi
call   1080 <strdup@plt>
mov    %rax,-0x828(%rbp)
cmpq   $0x0,-0x828(%rbp)
jne    1214 <resolveLocale+0xb4>
jmp    127a <resolveLocale+0x11a>
jmp    122b <resolveLocale+0xcb>
jmp    121e <resolveLocale+0xbe>
cmpl   $0x0,-0x82c(%rbp)
jge    11a6 <resolveLocale+0x46>
cmpq   $0x0,-0x828(%rbp)
jne    1261 <resolveLocale+0x101>
mov    -0x10(%rbp),%rdi
call   1080 <strdup@plt>
mov    %rax,-0x828(%rbp)
cmpq   $0x0,-0x828(%rbp)
jne    125c <resolveLocale+0xfc>
jmp    127a <resolveLocale+0x11a>
jmp    1261 <resolveLocale+0x101>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x828(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    12b0 <resolveLocale+0x150>
cmpq   $0x0,-0x18(%rbp)
je     128e <resolveLocale+0x12e>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
cmpq   $0x0,-0x828(%rbp)
je     12a8 <resolveLocale+0x148>
mov    -0x828(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x830,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<resolveLocale>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x800,%rsp
mov    %rdi,%r15
lea    0xe97(%rip),%rdi
lea    0xeb7(%rip),%rsi
call   1060 <fopen@plt>
test   %rax,%rax
je     11bd <resolveLocale+0x6d>
mov    %rax,%r14
lea    0x400(%rsp),%rbx
mov    %rsp,%r12
nopl   0x0(%rax,%rax,1)
mov    %r14,%rdi
mov    %rbx,%rsi
mov    %r12,%rdx
call   1050 <parseTwoTokenLine@plt>
cmp    $0xffffffff,%eax
jl     11d1 <resolveLocale+0x81>
mov    %eax,%ebp
mov    %rbx,%rdi
mov    %r15,%rsi
call   1040 <strcmp@plt>
test   %eax,%eax
je     11c1 <resolveLocale+0x71>
test   %ebp,%ebp
jns    1190 <resolveLocale+0x40>
mov    %r15,%rdi
jmp    11c4 <resolveLocale+0x74>
xor    %ebx,%ebx
jmp    11db <resolveLocale+0x8b>
mov    %rsp,%rdi
call   1070 <strdup@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    11d3 <resolveLocale+0x83>
xor    %ebx,%ebx
mov    %r14,%rdi
call   1030 <fclose@plt>
mov    %rbx,%rax
add    $0x800,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [39 x i8] c"/usr/X11R6/lib/X11/locale/locale.alias\00"
@.str.1 = [2 x i8] c"r\00"
define i8* @resolveLocale(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca [1024 x i8]
%6 = alloca [1024 x i8]
%7 = alloca i8*
%8 = alloca i32
store i8* %0, i8** %3
store i8* null, i8** %7
%9 = call i32* @fopen(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %9, i32** %4
%10 = load i32*, i32** %4
%11 = icmp eq i32* %10, null
br i1 %11, label %12, label %13
12: ; preds = %1
br label %52
13: ; preds = %1
br label %14
14: ; preds = %35, %13
%15 = load i32*, i32** %4
%16 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%17 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%18 = call i32 @parseTwoTokenLine(i32* %15, i8* %16, i8* %17)
store i32 %18, i32* %8
%19 = load i32, i32* %8
%20 = icmp slt i32 %19, -1
br i1 %20, label %21, label %22
21: ; preds = %14
br label %52
22: ; preds = %14
%23 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%24 = load i8*, i8** %3
%25 = call i32 @strcmp(i8* %23, i8* %24)
%26 = icmp ne i32 %25, 0
br i1 %26, label %34, label %27
27: ; preds = %22
%28 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%29 = call i8* @strdup(i8* %28)
store i8* %29, i8** %7
%30 = load i8*, i8** %7
%31 = icmp eq i8* %30, null
br i1 %31, label %32, label %33
32: ; preds = %27
br label %52
33: ; preds = %27
br label %38
34: ; preds = %22
br label %35
35: ; preds = %34
%36 = load i32, i32* %8
%37 = icmp sge i32 %36, 0
br i1 %37, label %14, label %38
38: ; preds = %35, %33
%39 = load i8*, i8** %7
%40 = icmp eq i8* %39, null
br i1 %40, label %41, label %48
41: ; preds = %38
%42 = load i8*, i8** %3
%43 = call i8* @strdup(i8* %42)
store i8* %43, i8** %7
%44 = load i8*, i8** %7
%45 = icmp eq i8* %44, null
br i1 %45, label %46, label %47
46: ; preds = %41
br label %52
47: ; preds = %41
br label %48
48: ; preds = %47, %38
%49 = load i32*, i32** %4
%50 = call i32 @fclose(i32* %49)
%51 = load i8*, i8** %7
store i8* %51, i8** %2
br label %65
52: ; preds = %46, %32, %21, %12
%53 = load i32*, i32** %4
%54 = icmp ne i32* %53, null
br i1 %54, label %55, label %58
55: ; preds = %52
%56 = load i32*, i32** %4
%57 = call i32 @fclose(i32* %56)
br label %58
58: ; preds = %55, %52
%59 = load i8*, i8** %7
%60 = icmp ne i8* %59, null
br i1 %60, label %61, label %64
61: ; preds = %58
%62 = load i8*, i8** %7
%63 = call i32 @free(i8* %62)
br label %64
64: ; preds = %61, %58
store i8* null, i8** %2
br label %65
65: ; preds = %64, %48
%66 = load i8*, i8** %2
ret i8* %66
}

LLVM IR (-O2):
@.str = [39 x i8] c"/usr/X11R6/lib/X11/locale/locale.alias\00"
@.str.1 = [2 x i8] c"r\00"
define i8* @resolveLocale(i8* nocapture %0) {
%2 = alloca [1024 x i8]
%3 = alloca [1024 x i8]
%4 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%5 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%6 = tail call i32* @fopen(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%7 = icmp eq i32* %6, null
br i1 %7, label %26, label %8
8: ; preds = %1, %17
%9 = call i32 @parseTwoTokenLine(i32* %6, i8* %4, i8* %5)
%10 = icmp slt i32 %9, -1
br i1 %10, label %22, label %11
11: ; preds = %8
%12 = call i32 @strcmp(i8* %4, i8* dereferenceable(1) %0)
%13 = icmp eq i32 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %11
%15 = call i8* @strdup(i8* %5)
%16 = icmp eq i8* %15, null
br i1 %16, label %22, label %23
17: ; preds = %11
%18 = icmp sgt i32 %9, -1
br i1 %18, label %8, label %19
19: ; preds = %17
%20 = call i8* @strdup(i8* %0)
%21 = icmp eq i8* %20, null
br i1 %21, label %22, label %23
22: ; preds = %8, %14, %19
br label %23
23: ; preds = %19, %14, %22
%24 = phi i8* [ null, %22 ], [ %20, %19 ], [ %15, %14 ]
%25 = call i32 @fclose(i32* %6)
br label %26
26: ; preds = %23, %1
%27 = phi i8* [ null, %1 ], [ %24, %23 ]
ret i8* %27
}