#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*/ wchar_t;
typedef int /*<<< orphan*/ state;
typedef int /*<<< orphan*/ mbstate_t;

/* Variables and functions */
scalar_t__ EILSEQ;
int /*<<< orphan*/ LC_ALL;
scalar_t__ errno;
int /*<<< orphan*/ memset(int /*<<< orphan*/ *, char, int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ putchar(char);
int /*<<< orphan*/ puts(char *);
char *setlocale(int /*<<< orphan*/, char const *);
int /*<<< orphan*/ strerror(scalar_t__);
size_t wcrtomb(char *, int /*<<< orphan*/, int /*<<< orphan*/ *);
int do_test(const char *loc) {
  char buf[100];
  size_t n;
  mbstate_t state;
  const char *nloc;
  int res;
  nloc = setlocale(LC_ALL, loc);
  if (nloc == NULL) {
    printf("could not set locale \"%s\"\n", loc);
    return 1;
  }
  printf("new locale: %s\n", nloc);
  memset(&state, '\0', sizeof(state));
  errno = 0;
  n = wcrtomb(buf, (wchar_t)-15l, &state);
  printf("n = %zd, errno = %d (%s)\n", n, errno, strerror(errno));
  res = n != (size_t)-1 || errno != EILSEQ;
  if (res)
    puts("*** FAIL");
  putchar('\n');
  return res;
}


x86 assembly (clang, -O0):
<do_test>:
push   %rbp
mov    %rsp,%rbp
sub    $0xb0,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e4a(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rsi
call   1080 <setlocale@plt>
mov    %rax,-0x98(%rbp)
cmpq   $0x0,-0x98(%rbp)
jne    11c4 <do_test+0x54>
mov    -0x10(%rbp),%rsi
lea    0xe4f(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12d2 <do_test+0x162>
mov    -0x98(%rbp),%rsi
lea    0xe49(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    -0x8c(%rbp),%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1060 <memset@plt>
mov    0x2de5(%rip),%rax
movq   $0x0,(%rax)
lea    -0x80(%rbp),%rdi
mov    $0xfffffff1,%esi
lea    -0x8c(%rbp),%rdx
call   1070 <wcrtomb@plt>
mov    %rax,-0x88(%rbp)
mov    -0x88(%rbp),%rax
mov    %rax,-0xb0(%rbp)
mov    0x2dad(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0xa8(%rbp)
mov    0x2d9c(%rip),%rax
mov    (%rax),%rdi
call   1090 <strerror@plt>
mov    -0xb0(%rbp),%rsi
mov    -0xa8(%rbp),%rdx
mov    %eax,%ecx
lea    0xdd0(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x1,%al
cmpq   $0xffffffffffffffff,-0x88(%rbp)
ff
mov    %al,-0x9d(%rbp)
jne    1295 <do_test+0x125>
mov    0x2d59(%rip),%rax
mov    (%rax),%rax
mov    0x2d67(%rip),%rcx
cmp    (%rcx),%rax
setne  %al
mov    %al,-0x9d(%rbp)
mov    -0x9d(%rbp),%al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x9c(%rbp)
cmpl   $0x0,-0x9c(%rbp)
je     12bf <do_test+0x14f>
lea    0xd8b(%rip),%rdi
call   1040 <puts@plt>
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    -0x9c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0xb0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_test>:
push   %rbp
push   %r14
push   %rbx
sub    $0x80,%rsp
mov    %rdi,%rbx
mov    0x2e4b(%rip),%rax
mov    (%rax),%edi
mov    %rbx,%rsi
call   1080 <setlocale@plt>
test   %rax,%rax
je     1236 <do_test+0xc6>
lea    0xe7c(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xc(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1060 <memset@plt>
mov    0x2e14(%rip),%r14
movq   $0x0,(%r14)
lea    0x10(%rsp),%rdi
mov    $0xfffffff1,%esi
mov    %rbx,%rdx
call   1070 <wcrtomb@plt>
mov    %rax,%rbx
mov    (%r14),%rbp
mov    %rbp,%rdi
call   1090 <strerror@plt>
lea    0xe39(%rip),%rdi
mov    %rbx,%rsi
mov    %rbp,%rdx
mov    %eax,%ecx
xor    %eax,%eax
call   1050 <printf@plt>
mov    (%r14),%rax
mov    0x2de5(%rip),%rcx
xor    (%rcx),%rax
not    %rbx
xor    %ebp,%ebp
or     %rbx,%rax
setne  %bl
je     1227 <do_test+0xb7>
lea    0xe23(%rip),%rdi
call   1040 <puts@plt>
mov    %bl,%bpl
mov    $0xa,%edi
call   1030 <putchar@plt>
jmp    124c <do_test+0xdc>
lea    0xdc3(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x1,%ebp
mov    %ebp,%eax
add    $0x80,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@LC_ALL = global i32 0
@.str = [27 x i8] c"could not set locale \22%s\22\0A\00"
@.str.1 = [16 x i8] c"new locale: %s\0A\00"
@errno = global i64 0
@.str.2 = [26 x i8] c"n = %zd, errno = %d (%s)\0A\00"
@EILSEQ = global i64 0
@.str.3 = [9 x i8] c"*** FAIL\00"
define i32 @do_test(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca [100 x i8]
%5 = alloca i64
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
store i8* %0, i8** %3
%9 = load i32, i32* @LC_ALL
%10 = load i8*, i8** %3
%11 = call i8* @setlocale(i32 %9, i8* %10)
store i8* %11, i8** %7
%12 = load i8*, i8** %7
%13 = icmp eq i8* %12, null
br i1 %13, label %14, label %17
14: ; preds = %1
%15 = load i8*, i8** %3
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i8* %15)
store i32 1, i32* %2
br label %44
17: ; preds = %1
%18 = load i8*, i8** %7
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8* %18)
%20 = call i32 @memset(i32* %6, i8 signext 0, i32 4)
store i64 0, i64* @errno
%21 = getelementptr inbounds [100 x i8], [100 x i8]* %4, i64 0, i64 0
%22 = call i64 @wcrtomb(i8* %21, i32 -15, i32* %6)
store i64 %22, i64* %5
%23 = load i64, i64* %5
%24 = load i64, i64* @errno
%25 = load i64, i64* @errno
%26 = call i32 @strerror(i64 %25)
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i64 %23, i64 %24, i32 %26)
%28 = load i64, i64* %5
%29 = icmp ne i64 %28, -1
br i1 %29, label %34, label %30
30: ; preds = %17
%31 = load i64, i64* @errno
%32 = load i64, i64* @EILSEQ
%33 = icmp ne i64 %31, %32
br label %34
34: ; preds = %30, %17
%35 = phi i1 [ true, %17 ], [ %33, %30 ]
%36 = zext i1 %35 to i32
store i32 %36, i32* %8
%37 = load i32, i32* %8
%38 = icmp ne i32 %37, 0
br i1 %38, label %39, label %41
39: ; preds = %34
%40 = call i32 @puts(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0))
br label %41
41: ; preds = %39, %34
%42 = call i32 @putchar(i8 signext 10)
%43 = load i32, i32* %8
store i32 %43, i32* %2
br label %44
44: ; preds = %41, %14
%45 = load i32, i32* %2
ret i32 %45
}

LLVM IR (-O2):
@LC_ALL = global i32 0
@.str = [27 x i8] c"could not set locale \22%s\22\0A\00"
@.str.1 = [16 x i8] c"new locale: %s\0A\00"
@errno = global i64 0
@.str.2 = [26 x i8] c"n = %zd, errno = %d (%s)\0A\00"
@EILSEQ = global i64 0
@.str.3 = [9 x i8] c"*** FAIL\00"
define i32 @do_test(i8* %0) {
%2 = alloca [100 x i8]
%3 = alloca i32
%4 = getelementptr inbounds [100 x i8], [100 x i8]* %2, i64 0, i64 0
%5 = bitcast i32* %3 to i8*
%6 = load i32, i32* @LC_ALL
%7 = tail call i8* @setlocale(i32 %6, i8* %0)
%8 = icmp eq i8* %7, null
br i1 %8, label %9, label %11
9: ; preds = %1
%10 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %28
11: ; preds = %1
%12 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8* %7)
%13 = call i32 @memset(i32* %3, i8 signext 0, i32 4)
store i64 0, i64* @errno
%14 = call i64 @wcrtomb(i8* %4, i32 -15, i32* %3)
%15 = load i64, i64* @errno
%16 = call i32 @strerror(i64 %15)
%17 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i64 %14, i64 %15, i32 %16)
%18 = icmp ne i64 %14, -1
%19 = load i64, i64* @errno
%20 = load i64, i64* @EILSEQ
%21 = icmp ne i64 %19, %20
%22 = select i1 %18, i1 true, i1 %21
%23 = zext i1 %22 to i32
br i1 %22, label %24, label %26
24: ; preds = %11
%25 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0))
br label %26
26: ; preds = %24, %11
%27 = call i32 @putchar(i8 signext 10)
br label %28
28: ; preds = %26, %9
%29 = phi i32 [ 1, %9 ], [ %23, %26 ]
ret i32 %29
}