#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 scalar_t__ nl_catd;

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
scalar_t__ catopen(char *, int);
char *getenv(char *);
scalar_t__ index(char *, char);
scalar_t__ my_catopenpath(char *, char *);
scalar_t__ open(char *, int /*<<< orphan*/);
nl_catd my_catopen(char *name, int oflag) {
  nl_catd fd;
  fd = catopen(name, oflag);
  if (fd == (nl_catd)-1 && oflag) {
    oflag = 0;
    fd = catopen(name, oflag);
  }
  if (fd == (nl_catd)-1) {
    char *nlspath;
    if (oflag)
      return fd;
    if (index(name, '/')) {
      return open(name, O_RDONLY);
    }
    nlspath = getenv("NLSPATH");
    if (nlspath)
      fd = my_catopenpath(name, nlspath);
    if (fd == (nl_catd)-1)
      fd = my_catopenpath(name, "/usr/lib/locale/%N/%L");
  }
  return fd;
}


x86 assembly (clang, -O0):
<my_catopen>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1060 <catopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0xffffffffffffffff,-0x20(%rbp)
jne    119b <my_catopen+0x4b>
cmpl   $0x0,-0x14(%rbp)
je     119b <my_catopen+0x4b>
movl   $0x0,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1060 <catopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0xffffffffffffffff,-0x20(%rbp)
jne    1240 <my_catopen+0xf0>
cmpl   $0x0,-0x14(%rbp)
je     11bd <my_catopen+0x6d>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1248 <my_catopen+0xf8>
mov    -0x10(%rbp),%rdi
mov    $0x2f,%esi
call   1050 <index@plt>
cmp    $0x0,%rax
je     11f0 <my_catopen+0xa0>
mov    -0x10(%rbp),%rdi
mov    0x2e08(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %rax,-0x8(%rbp)
jmp    1248 <my_catopen+0xf8>
lea    0xe09(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     121c <my_catopen+0xcc>
mov    -0x10(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1040 <my_catopenpath@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0xffffffffffffffff,-0x20(%rbp)
jne    123b <my_catopen+0xeb>
mov    -0x10(%rbp),%rdi
lea    0xdd6(%rip),%rsi
call   1040 <my_catopenpath@plt>
mov    %rax,-0x20(%rbp)
jmp    1240 <my_catopen+0xf0>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<my_catopen>:
push   %rbp
push   %rbx
push   %rax
mov    %esi,%ebp
mov    %rdi,%rbx
call   1060 <catopen@plt>
test   %ebp,%ebp
je     1179 <my_catopen+0x29>
cmp    $0xffffffffffffffff,%rax
jne    1179 <my_catopen+0x29>
mov    %rbx,%rdi
xor    %esi,%esi
call   1060 <catopen@plt>
cmp    $0xffffffffffffffff,%rax
jne    11d5 <my_catopen+0x85>
jmp    118a <my_catopen+0x3a>
cmp    $0xffffffffffffffff,%rax
jne    11d5 <my_catopen+0x85>
mov    $0xffffffffffffffff,%rax
test   %ebp,%ebp
jne    11d5 <my_catopen+0x85>
mov    %rbx,%rdi
mov    $0x2f,%esi
call   1050 <index@plt>
test   %rax,%rax
je     11b3 <my_catopen+0x63>
mov    0x2e45(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %rbp
jmp    1070 <open@plt>
lea    0xe46(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     11dc <my_catopen+0x8c>
mov    %rbx,%rdi
mov    %rax,%rsi
call   1040 <my_catopenpath@plt>
cmp    $0xffffffffffffffff,%rax
je     11dc <my_catopen+0x8c>
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xe25(%rip),%rsi
mov    %rbx,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %rbp
jmp    1040 <my_catopenpath@plt>

LLVM IR (-O0):
@O_RDONLY = global i32 0
@.str = [8 x i8] c"NLSPATH\00"
@.str.1 = [22 x i8] c"/usr/lib/locale/%N/%L\00"
define i64 @my_catopen(i8* %0, i32 %1) {
%3 = alloca i64
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i64
%7 = alloca i8*
store i8* %0, i8** %4
store i32 %1, i32* %5
%8 = load i8*, i8** %4
%9 = load i32, i32* %5
%10 = call i64 @catopen(i8* %8, i32 %9)
store i64 %10, i64* %6
%11 = load i64, i64* %6
%12 = icmp eq i64 %11, -1
br i1 %12, label %13, label %20
13: ; preds = %2
%14 = load i32, i32* %5
%15 = icmp ne i32 %14, 0
br i1 %15, label %16, label %20
16: ; preds = %13
store i32 0, i32* %5
%17 = load i8*, i8** %4
%18 = load i32, i32* %5
%19 = call i64 @catopen(i8* %17, i32 %18)
store i64 %19, i64* %6
br label %20
20: ; preds = %16, %13, %2
%21 = load i64, i64* %6
%22 = icmp eq i64 %21, -1
br i1 %22, label %23, label %51
23: ; preds = %20
%24 = load i32, i32* %5
%25 = icmp ne i32 %24, 0
br i1 %25, label %26, label %28
26: ; preds = %23
%27 = load i64, i64* %6
store i64 %27, i64* %3
br label %53
28: ; preds = %23
%29 = load i8*, i8** %4
%30 = call i64 @index(i8* %29, i8 signext 47)
%31 = icmp ne i64 %30, 0
br i1 %31, label %32, label %36
32: ; preds = %28
%33 = load i8*, i8** %4
%34 = load i32, i32* @O_RDONLY
%35 = call i64 @open(i8* %33, i32 %34)
store i64 %35, i64* %3
br label %53
36: ; preds = %28
%37 = call i8* @getenv(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
store i8* %37, i8** %7
%38 = load i8*, i8** %7
%39 = icmp ne i8* %38, null
br i1 %39, label %40, label %44
40: ; preds = %36
%41 = load i8*, i8** %4
%42 = load i8*, i8** %7
%43 = call i64 @my_catopenpath(i8* %41, i8* %42)
store i64 %43, i64* %6
br label %44
44: ; preds = %40, %36
%45 = load i64, i64* %6
%46 = icmp eq i64 %45, -1
br i1 %46, label %47, label %50
47: ; preds = %44
%48 = load i8*, i8** %4
%49 = call i64 @my_catopenpath(i8* %48, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
store i64 %49, i64* %6
br label %50
50: ; preds = %47, %44
br label %51
51: ; preds = %50, %20
%52 = load i64, i64* %6
store i64 %52, i64* %3
br label %53
53: ; preds = %51, %32, %26
%54 = load i64, i64* %3
ret i64 %54
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@.str = [8 x i8] c"NLSPATH\00"
@.str.1 = [22 x i8] c"/usr/lib/locale/%N/%L\00"
define i64 @my_catopen(i8* %0, i32 %1) {
%3 = tail call i64 @catopen(i8* %0, i32 %1)
%4 = icmp eq i64 %3, -1
%5 = icmp ne i32 %1, 0
%6 = and i1 %5, %4
br i1 %6, label %8, label %7
7: ; preds = %2
br i1 %4, label %11, label %27
8: ; preds = %2
%9 = tail call i64 @catopen(i8* %0, i32 0)
%10 = icmp eq i64 %9, -1
br i1 %10, label %13, label %27
11: ; preds = %7
%12 = icmp eq i32 %1, 0
br i1 %12, label %13, label %27
13: ; preds = %8, %11
%14 = tail call i64 @index(i8* %0, i8 signext 47)
%15 = icmp eq i64 %14, 0
br i1 %15, label %19, label %16
16: ; preds = %13
%17 = load i32, i32* @O_RDONLY
%18 = tail call i64 @open(i8* %0, i32 %17)
br label %27
19: ; preds = %13
%20 = tail call i8* @getenv(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%21 = icmp eq i8* %20, null
br i1 %21, label %25, label %22
22: ; preds = %19
%23 = tail call i64 @my_catopenpath(i8* %0, i8* %20)
%24 = icmp eq i64 %23, -1
br i1 %24, label %25, label %27
25: ; preds = %19, %22
%26 = tail call i64 @my_catopenpath(i8* %0, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
br label %27
27: ; preds = %22, %25, %7, %8, %16, %11
%28 = phi i64 [ %18, %16 ], [ -1, %11 ], [ %3, %7 ], [ %9, %8 ], [ %23, %22 ], [ %26, %25 ]
ret i64 %28
}