#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 *);
char *malloc(scalar_t__);
int parseTwoTokenLine(int /*<<< orphan*/ *, char *, char *);
int /*<<< orphan*/ strcmp(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
scalar_t__ strlen(char *);
char *resolveLocale(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 = malloc(strlen(second) + 1);
      if (resolved == NULL)
        goto bail;
      strcpy(resolved, second);
      break;
    }
  } while (rc >= 0);
  if (resolved == NULL) {
    resolved = malloc(strlen(locale) + 1);
    if (resolved == NULL)
      goto bail;
    strcpy(resolved, locale);
  }
  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    0xe5f(%rip),%rdi
lea    0xe7f(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11c1 <resolveLocale+0x41>
jmp    12d0 <resolveLocale+0x150>
jmp    11c6 <resolveLocale+0x46>
mov    -0x18(%rbp),%rdi
lea    -0x420(%rbp),%rsi
lea    -0x820(%rbp),%rdx
call   1090 <parseTwoTokenLine@plt>
mov    %eax,-0x82c(%rbp)
cmpl   $0xffffffff,-0x82c(%rbp)
jge    11f5 <resolveLocale+0x75>
jmp    12d0 <resolveLocale+0x150>
lea    -0x420(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%eax
jne    1258 <resolveLocale+0xd8>
lea    -0x820(%rbp),%rdi
call   1060 <strlen@plt>
mov    %rax,%rdi
add    $0x1,%rdi
call   1080 <malloc@plt>
mov    %rax,-0x828(%rbp)
cmpq   $0x0,-0x828(%rbp)
jne    1240 <resolveLocale+0xc0>
jmp    12d0 <resolveLocale+0x150>
mov    -0x828(%rbp),%rdi
lea    -0x820(%rbp),%rsi
call   1040 <strcpy@plt>
jmp    126a <resolveLocale+0xea>
jmp    125d <resolveLocale+0xdd>
cmpl   $0x0,-0x82c(%rbp)
jge    11c6 <resolveLocale+0x46>
cmpq   $0x0,-0x828(%rbp)
jne    12b7 <resolveLocale+0x137>
mov    -0x10(%rbp),%rdi
call   1060 <strlen@plt>
mov    %rax,%rdi
add    $0x1,%rdi
call   1080 <malloc@plt>
mov    %rax,-0x828(%rbp)
cmpq   $0x0,-0x828(%rbp)
jne    12a7 <resolveLocale+0x127>
jmp    12d0 <resolveLocale+0x150>
mov    -0x828(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1040 <strcpy@plt>
mov    -0x18(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x828(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1306 <resolveLocale+0x186>
cmpq   $0x0,-0x18(%rbp)
je     12e4 <resolveLocale+0x164>
mov    -0x18(%rbp),%rdi
call   1050 <fclose@plt>
cmpq   $0x0,-0x828(%rbp)
je     12fe <resolveLocale+0x17e>
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   %r13
push   %r12
push   %rbx
sub    $0x808,%rsp
mov    %rdi,%r12
lea    0xe75(%rip),%rdi
lea    0xe95(%rip),%rsi
call   1090 <fopen@plt>
test   %rax,%rax
je     11f9 <resolveLocale+0x89>
mov    %rax,%r15
lea    0x400(%rsp),%rbx
mov    %rsp,%r13
xor    %r14d,%r14d
nopl   (%rax)
mov    %r15,%rdi
mov    %rbx,%rsi
mov    %r13,%rdx
call   1080 <parseTwoTokenLine@plt>
cmp    $0xffffffff,%eax
jl     1227 <resolveLocale+0xb7>
mov    %eax,%ebp
mov    %rbx,%rdi
mov    %r12,%rsi
call   1060 <strcmp@plt>
test   %eax,%eax
je     11fe <resolveLocale+0x8e>
test   %ebp,%ebp
jns    11b0 <resolveLocale+0x40>
mov    %r12,%rdi
call   1050 <strlen@plt>
lea    0x1(%rax),%rdi
call   1070 <malloc@plt>
test   %rax,%rax
je     1224 <resolveLocale+0xb4>
mov    %rax,%r14
mov    %rax,%rdi
mov    %r12,%rsi
jmp    121d <resolveLocale+0xad>
xor    %r14d,%r14d
jmp    122f <resolveLocale+0xbf>
mov    %rsp,%rdi
call   1050 <strlen@plt>
lea    0x1(%rax),%rdi
call   1070 <malloc@plt>
test   %rax,%rax
je     1224 <resolveLocale+0xb4>
mov    %rax,%r14
mov    %rsp,%rsi
mov    %rax,%rdi
call   1030 <strcpy@plt>
jmp    1227 <resolveLocale+0xb7>
xor    %r14d,%r14d
mov    %r15,%rdi
call   1040 <fclose@plt>
mov    %r14,%rax
add    $0x808,%rsp
pop    %rbx
pop    %r12
pop    %r13
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 %62
13: ; preds = %1
br label %14
14: ; preds = %40, %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 %62
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 %39, label %27
27: ; preds = %22
%28 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%29 = call i64 @strlen(i8* %28)
%30 = add nsw i64 %29, 1
%31 = call i8* @malloc(i64 %30)
store i8* %31, i8** %7
%32 = load i8*, i8** %7
%33 = icmp eq i8* %32, null
br i1 %33, label %34, label %35
34: ; preds = %27
br label %62
35: ; preds = %27
%36 = load i8*, i8** %7
%37 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%38 = call i32 @strcpy(i8* %36, i8* %37)
br label %43
39: ; preds = %22
br label %40
40: ; preds = %39
%41 = load i32, i32* %8
%42 = icmp sge i32 %41, 0
br i1 %42, label %14, label %43
43: ; preds = %40, %35
%44 = load i8*, i8** %7
%45 = icmp eq i8* %44, null
br i1 %45, label %46, label %58
46: ; preds = %43
%47 = load i8*, i8** %3
%48 = call i64 @strlen(i8* %47)
%49 = add nsw i64 %48, 1
%50 = call i8* @malloc(i64 %49)
store i8* %50, i8** %7
%51 = load i8*, i8** %7
%52 = icmp eq i8* %51, null
br i1 %52, label %53, label %54
53: ; preds = %46
br label %62
54: ; preds = %46
%55 = load i8*, i8** %7
%56 = load i8*, i8** %3
%57 = call i32 @strcpy(i8* %55, i8* %56)
br label %58
58: ; preds = %54, %43
%59 = load i32*, i32** %4
%60 = call i32 @fclose(i32* %59)
%61 = load i8*, i8** %7
store i8* %61, i8** %2
br label %75
62: ; preds = %53, %34, %21, %12
%63 = load i32*, i32** %4
%64 = icmp ne i32* %63, null
br i1 %64, label %65, label %68
65: ; preds = %62
%66 = load i32*, i32** %4
%67 = call i32 @fclose(i32* %66)
br label %68
68: ; preds = %65, %62
%69 = load i8*, i8** %7
%70 = icmp ne i8* %69, null
br i1 %70, label %71, label %74
71: ; preds = %68
%72 = load i8*, i8** %7
%73 = call i32 @free(i8* %72)
br label %74
74: ; preds = %71, %68
store i8* null, i8** %2
br label %75
75: ; preds = %74, %58
%76 = load i8*, i8** %2
ret i8* %76
}

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* %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 %33, label %8
8: ; preds = %1, %19
%9 = call i32 @parseTwoTokenLine(i32* %6, i8* %4, i8* %5)
%10 = icmp slt i32 %9, -1
br i1 %10, label %30, 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 %19
14: ; preds = %11
%15 = call i64 @strlen(i8* %5)
%16 = add nsw i64 %15, 1
%17 = call i8* @malloc(i64 %16)
%18 = icmp eq i8* %17, null
br i1 %18, label %30, label %21
19: ; preds = %11
%20 = icmp sgt i32 %9, -1
br i1 %20, label %8, label %23
21: ; preds = %14
%22 = call i32 @strcpy(i8* %17, i8* %5)
br label %30
23: ; preds = %19
%24 = call i64 @strlen(i8* dereferenceable(1) %0)
%25 = add nsw i64 %24, 1
%26 = call i8* @malloc(i64 %25)
%27 = icmp eq i8* %26, null
br i1 %27, label %30, label %28
28: ; preds = %23
%29 = call i32 @strcpy(i8* %26, i8* %0)
br label %30
30: ; preds = %8, %23, %14, %28, %21
%31 = phi i8* [ %26, %28 ], [ %17, %21 ], [ null, %14 ], [ null, %23 ], [ null, %8 ]
%32 = call i32 @fclose(i32* %6)
br label %33
33: ; preds = %30, %1
%34 = phi i8* [ null, %1 ], [ %31, %30 ]
ret i8* %34
}