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

/* Variables and functions */
int /*<<< orphan*/ LC_COLLATE;
char *malloc(int);
int /*<<< orphan*/ memset(char *, char, int);
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ puts(char *);
int /*<<< orphan*/ *setlocale(int /*<<< orphan*/, char *);
int strcoll(char *, char *);
int do_test(void) {
  if (setlocale(LC_COLLATE, "en_GB.UTF-8") == NULL) {
    puts("setlocale failed, cannot test for overflow");
    return 0;
  }
  char *p = malloc(0x40000000ul);
  if (p == NULL) {
    puts("could not allocate memory");
    return 1;
  }
  memset(p, 'x', 0x40000000ul - 1);
  p[0x40000000ul - 1] = 0;
  printf("%d\n", strcoll(p, p));
  return 0;
}


x86 assembly (clang, -O0):
<do_test>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    0x2e79(%rip),%rax
mov    (%rax),%edi
lea    0xe88(%rip),%rsi
call   1080 <setlocale@plt>
cmp    $0x0,%rax
jne    119f <do_test+0x3f>
lea    0xe7e(%rip),%rdi
call   1030 <puts@plt>
movl   $0x0,-0x4(%rbp)
jmp    1210 <do_test+0xb0>
mov    $0x40000000,%edi
call   1060 <malloc@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11d0 <do_test+0x70>
lea    0xe78(%rip),%rdi
call   1030 <puts@plt>
movl   $0x1,-0x4(%rbp)
jmp    1210 <do_test+0xb0>
mov    -0x10(%rbp),%rdi
mov    $0x78,%esi
mov    $0x3fffffff,%edx
call   1050 <memset@plt>
mov    -0x10(%rbp),%rax
movb   $0x0,0x3fffffff(%rax)
mov    -0x10(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1070 <strcoll@plt>
mov    %eax,%esi
lea    0xe4d(%rip),%rdi
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_test>:
push   %rbx
mov    0x2e80(%rip),%rax
mov    (%rax),%edi
lea    0xe8f(%rip),%rsi
call   1080 <setlocale@plt>
test   %rax,%rax
je     11c1 <do_test+0x61>
mov    $0x40000000,%edi
call   1060 <malloc@plt>
test   %rax,%rax
je     11d1 <do_test+0x71>
mov    %rax,%rbx
mov    %rax,%rdi
mov    $0x78,%esi
mov    $0x3fffffff,%edx
call   1050 <memset@plt>
movb   $0x0,0x3fffffff(%rbx)
mov    %rbx,%rdi
mov    %rbx,%rsi
call   1070 <strcoll@plt>
lea    0xe99(%rip),%rdi
mov    %eax,%esi
call   1040 <printf@plt>
jmp    11cd <do_test+0x6d>
lea    0xe44(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
pop    %rbx
ret
lea    0xe5f(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%eax
pop    %rbx
ret

LLVM IR (-O0):
@LC_COLLATE = global i32 0
@.str = [12 x i8] c"en_GB.UTF-8\00"
@.str.1 = [43 x i8] c"setlocale failed, cannot test for overflow\00"
@.str.2 = [26 x i8] c"could not allocate memory\00"
@.str.3 = [4 x i8] c"%d\0A\00"
define i32 @do_test() {
%1 = alloca i32
%2 = alloca i8*
%3 = load i32, i32* @LC_COLLATE
%4 = call i32* @setlocale(i32 %3, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq i32* %4, null
br i1 %5, label %6, label %8
6: ; preds = %0
%7 = call i32 @puts(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.1, i64 0, i64 0))
store i32 0, i32* %1
br label %23
8: ; preds = %0
%9 = call i8* @malloc(i32 1073741824)
store i8* %9, i8** %2
%10 = load i8*, i8** %2
%11 = icmp eq i8* %10, null
br i1 %11, label %12, label %14
12: ; preds = %8
%13 = call i32 @puts(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %1
br label %23
14: ; preds = %8
%15 = load i8*, i8** %2
%16 = call i32 @memset(i8* %15, i8 signext 120, i32 1073741823)
%17 = load i8*, i8** %2
%18 = getelementptr inbounds i8, i8* %17, i64 1073741823
store i8 0, i8* %18
%19 = load i8*, i8** %2
%20 = load i8*, i8** %2
%21 = call i32 @strcoll(i8* %19, i8* %20)
%22 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i32 %21)
store i32 0, i32* %1
br label %23
23: ; preds = %14, %12, %6
%24 = load i32, i32* %1
ret i32 %24
}

LLVM IR (-O2):
@LC_COLLATE = global i32 0
@.str = [12 x i8] c"en_GB.UTF-8\00"
@.str.1 = [43 x i8] c"setlocale failed, cannot test for overflow\00"
@.str.2 = [26 x i8] c"could not allocate memory\00"
@.str.3 = [4 x i8] c"%d\0A\00"
define i32 @do_test() {
%1 = load i32, i32* @LC_COLLATE
%2 = tail call i32* @setlocale(i32 %1, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%3 = icmp eq i32* %2, null
br i1 %3, label %4, label %6
4: ; preds = %0
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([43 x i8], [43 x i8]* @.str.1, i64 0, i64 0))
br label %16
6: ; preds = %0
%7 = tail call dereferenceable_or_null(1073741824) i8* @malloc(i32 1073741824)
%8 = icmp eq i8* %7, null
br i1 %8, label %9, label %11
9: ; preds = %6
%10 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0))
br label %16
11: ; preds = %6
%12 = tail call i32 @memset(i8* %7, i8 signext 120, i32 1073741823)
%13 = getelementptr inbounds i8, i8* %7, i64 1073741823
store i8 0, i8* %13
%14 = tail call i32 @strcoll(i8* %7, i8* %7)
%15 = tail call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i32 %14)
br label %16
16: ; preds = %9, %11, %4
%17 = phi i32 [ 0, %4 ], [ 1, %9 ], [ 0, %11 ]
ret i32 %17
}