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

/* Variables and functions */
int /*<<< orphan*/ EINVAL;
int /*<<< orphan*/ *LC_GLOBAL_LOCALE;
int /*<<< orphan*/ *calloc(int, int);
int /*<<< orphan*/ errno;
locale_t duplocale(locale_t loc) {
  if (loc == LC_GLOBAL_LOCALE)
    return loc;
  if (loc == ((locale_t)0)) {
    errno = EINVAL;
    return ((locale_t)0);
  }
  locale_t copy = calloc(1, sizeof(*loc));
  copy[0] = loc[0];
  return copy;
}


x86 assembly (clang, -O0):
<duplocale>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    0x2ec1(%rip),%rcx
cmp    (%rcx),%rax
jne    113d <duplocale+0x2d>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    118e <duplocale+0x7e>
cmpq   $0x0,-0x10(%rbp)
jne    1167 <duplocale+0x57>
mov    0x2e81(%rip),%rax
mov    (%rax),%ecx
mov    0x2e80(%rip),%rax
mov    %ecx,(%rax)
movq   $0x0,-0x8(%rbp)
jmp    118e <duplocale+0x7e>
mov    $0x1,%edi
mov    $0x4,%esi
call   1030 <calloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<duplocale>:
push   %rbx
mov    %rdi,%rbx
mov    0x2ecd(%rip),%rax
cmp    %rdi,(%rax)
je     1150 <duplocale+0x40>
test   %rbx,%rbx
je     1139 <duplocale+0x29>
mov    $0x1,%edi
mov    $0x4,%esi
call   1030 <calloc@plt>
mov    %rax,%rcx
jmp    1149 <duplocale+0x39>
mov    0x2e98(%rip),%rax
mov    0x2e89(%rip),%rbx
xor    %ecx,%ecx
mov    (%rbx),%edx
mov    %edx,(%rax)
mov    %rcx,%rbx
mov    %rbx,%rax
pop    %rbx
ret

LLVM IR (-O0):
@LC_GLOBAL_LOCALE = global i32* null
@EINVAL = global i32 0
@errno = global i32 0
define i32* @duplocale(i32* %0) {
%2 = alloca i32*
%3 = alloca i32*
%4 = alloca i32*
store i32* %0, i32** %3
%5 = load i32*, i32** %3
%6 = load i32*, i32** @LC_GLOBAL_LOCALE
%7 = icmp eq i32* %5, %6
br i1 %7, label %8, label %10
8: ; preds = %1
%9 = load i32*, i32** %3
store i32* %9, i32** %2
br label %23
10: ; preds = %1
%11 = load i32*, i32** %3
%12 = icmp eq i32* %11, null
br i1 %12, label %13, label %15
13: ; preds = %10
%14 = load i32, i32* @EINVAL
store i32 %14, i32* @errno
store i32* null, i32** %2
br label %23
15: ; preds = %10
%16 = call i32* @calloc(i32 1, i32 4)
store i32* %16, i32** %4
%17 = load i32*, i32** %3
%18 = getelementptr inbounds i32, i32* %17, i64 0
%19 = load i32, i32* %18
%20 = load i32*, i32** %4
%21 = getelementptr inbounds i32, i32* %20, i64 0
store i32 %19, i32* %21
%22 = load i32*, i32** %4
store i32* %22, i32** %2
br label %23
23: ; preds = %15, %13, %8
%24 = load i32*, i32** %2
ret i32* %24
}

LLVM IR (-O2):
@LC_GLOBAL_LOCALE = global i32* null
@EINVAL = global i32 0
@errno = global i32 0
define i32* @duplocale(i32* %0) {
%2 = load i32*, i32** @LC_GLOBAL_LOCALE
%3 = icmp eq i32* %2, %0
br i1 %3, label %13, label %4
4: ; preds = %1
%5 = icmp eq i32* %0, null
br i1 %5, label %8, label %6
6: ; preds = %4
%7 = tail call i32* @calloc(i32 1, i32 4)
br label %8
8: ; preds = %4, %6
%9 = phi i32* [ %0, %6 ], [ @EINVAL, %4 ]
%10 = phi i32* [ %7, %6 ], [ @errno, %4 ]
%11 = phi i32* [ %7, %6 ], [ null, %4 ]
%12 = load i32, i32* %9
store i32 %12, i32* %10
br label %13
13: ; preds = %8, %1
%14 = phi i32* [ %0, %1 ], [ %11, %8 ]
ret i32* %14
}