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

/* Variables and functions */
int /*<<< orphan*/ ENOMEM;
int /*<<< orphan*/ *calloc(unsigned int, int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *, ...);
int /*<<< orphan*/ free(int /*<<< orphan*/ *);
size_t mbstowcs(int /*<<< orphan*/ *, char const *, unsigned int);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
unsigned int strlen(char const *);
int wchar_string_to_cp850_string(char *, int /*<<< orphan*/ *, unsigned int);
int local_string_to_cp850_string(char *out, const char *in,
                                 unsigned int out_size) {
  int ret;
  wchar_t *wcs;
  if (strlen(in) >= out_size) {
    fprintf(stderr,
            "Cannot convert input string '%s' to 'CP850': String is too long\n",
            in);
    return 0;
  }
  wcs = calloc(out_size, sizeof(wchar_t));
  if (!wcs) {
    fprintf(stderr, "Cannot convert input string '%s' to 'CP850': %s\n", in,
            strerror(ENOMEM));
    return 0;
  }
  if (mbstowcs(wcs, in, out_size) == (size_t)-1) {
    fprintf(stderr, "Cannot convert input string '%s' to 'CP850': %s\n", in,
            strerror(errno));
    free(wcs);
    return 0;
  }
  ret = wchar_string_to_cp850_string(out, wcs, out_size);
  free(wcs);
  return ret;
}


x86 assembly (clang, -O0):
<local_string_to_cp850_string>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <strlen@plt>
cmp    -0x1c(%rbp),%eax
jb     11bc <local_string_to_cp850_string+0x4c>
mov    0x2e3c(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rdx
lea    0xe57(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    12a9 <local_string_to_cp850_string+0x139>
mov    -0x1c(%rbp),%edi
mov    $0x4,%esi
call   1060 <calloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    121e <local_string_to_cp850_string+0xae>
mov    0x2df9(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    0x2ded(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x34(%rbp),%edi
mov    -0x30(%rbp),%rdx
mov    %rax,%rcx
lea    0xe36(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    12a9 <local_string_to_cp850_string+0x139>
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1040 <mbstowcs@plt>
cmp    $0xffffffffffffffff,%rax
jne    1287 <local_string_to_cp850_string+0x117>
mov    0x2d99(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x44(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    0x2d7d(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x44(%rbp),%edi
mov    -0x40(%rbp),%rdx
mov    %rax,%rcx
lea    0xdd6(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
movl   $0x0,-0x4(%rbp)
jmp    12a9 <local_string_to_cp850_string+0x139>
mov    -0x10(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1090 <wchar_string_to_cp850_string@plt>
mov    %eax,-0x20(%rbp)
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<local_string_to_cp850_string>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%ebp
mov    %rsi,%rbx
mov    %rdi,%r15
mov    %rsi,%rdi
call   1050 <strlen@plt>
cmp    %ebp,%eax
jae    11d0 <local_string_to_cp850_string+0x60>
mov    %ebp,%edi
mov    $0x4,%esi
call   1060 <calloc@plt>
test   %rax,%rax
je     11ec <local_string_to_cp850_string+0x7c>
mov    %rax,%r14
mov    %rax,%rdi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1040 <mbstowcs@plt>
cmp    $0xffffffffffffffff,%rax
je     121b <local_string_to_cp850_string+0xab>
mov    %r15,%rdi
mov    %r14,%rsi
mov    %ebp,%edx
call   1090 <wchar_string_to_cp850_string@plt>
mov    %eax,%ebx
mov    %r14,%rdi
call   1030 <free@plt>
mov    %ebx,%eax
jmp    1252 <local_string_to_cp850_string+0xe2>
mov    0x2e01(%rip),%rax
mov    (%rax),%edi
lea    0xe20(%rip),%rsi
mov    %rbx,%rdx
xor    %eax,%eax
call   1070 <fprintf@plt>
jmp    1250 <local_string_to_cp850_string+0xe0>
mov    0x2de5(%rip),%rax
mov    (%rax),%ebp
mov    0x2de4(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xe37(%rip),%rsi
mov    %ebp,%edi
mov    %rbx,%rdx
mov    %rax,%rcx
xor    %eax,%eax
call   1070 <fprintf@plt>
jmp    1250 <local_string_to_cp850_string+0xe0>
mov    0x2db6(%rip),%rax
mov    (%rax),%ebp
mov    0x2da5(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xe08(%rip),%rsi
mov    %ebp,%edi
mov    %rbx,%rdx
mov    %rax,%rcx
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    %r14,%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [65 x i8] c"Cannot convert input string '%s' to 'CP850': String is too long\0A\00"
@.str.1 = [49 x i8] c"Cannot convert input string '%s' to 'CP850': %s\0A\00"
@ENOMEM = global i32 0
@errno = global i32 0
define i32 @local_string_to_cp850_string(i8* %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32*
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%10 = load i8*, i8** %6
%11 = call i32 @strlen(i8* %10)
%12 = load i32, i32* %7
%13 = icmp uge i32 %11, %12
br i1 %13, label %14, label %18
14: ; preds = %3
%15 = load i32, i32* @stderr
%16 = load i8*, i8** %6
%17 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %15, i8* getelementptr inbounds ([65 x i8], [65 x i8]* @.str, i64 0, i64 0), i8* %16)
store i32 0, i32* %4
br label %51
18: ; preds = %3
%19 = load i32, i32* %7
%20 = call i32* @calloc(i32 %19, i32 4)
store i32* %20, i32** %9
%21 = load i32*, i32** %9
%22 = icmp ne i32* %21, null
br i1 %22, label %29, label %23
23: ; preds = %18
%24 = load i32, i32* @stderr
%25 = load i8*, i8** %6
%26 = load i32, i32* @ENOMEM
%27 = call i8* @strerror(i32 %26)
%28 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %24, i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.1, i64 0, i64 0), i8* %25, i8* %27)
store i32 0, i32* %4
br label %51
29: ; preds = %18
%30 = load i32*, i32** %9
%31 = load i8*, i8** %6
%32 = load i32, i32* %7
%33 = call i64 @mbstowcs(i32* %30, i8* %31, i32 %32)
%34 = icmp eq i64 %33, -1
br i1 %34, label %35, label %43
35: ; preds = %29
%36 = load i32, i32* @stderr
%37 = load i8*, i8** %6
%38 = load i32, i32* @errno
%39 = call i8* @strerror(i32 %38)
%40 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %36, i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.1, i64 0, i64 0), i8* %37, i8* %39)
%41 = load i32*, i32** %9
%42 = call i32 @free(i32* %41)
store i32 0, i32* %4
br label %51
43: ; preds = %29
%44 = load i8*, i8** %5
%45 = load i32*, i32** %9
%46 = load i32, i32* %7
%47 = call i32 @wchar_string_to_cp850_string(i8* %44, i32* %45, i32 %46)
store i32 %47, i32* %8
%48 = load i32*, i32** %9
%49 = call i32 @free(i32* %48)
%50 = load i32, i32* %8
store i32 %50, i32* %4
br label %51
51: ; preds = %43, %35, %23, %14
%52 = load i32, i32* %4
ret i32 %52
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [65 x i8] c"Cannot convert input string '%s' to 'CP850': String is too long\0A\00"
@.str.1 = [49 x i8] c"Cannot convert input string '%s' to 'CP850': %s\0A\00"
@ENOMEM = global i32 0
@errno = global i32 0
define i32 @local_string_to_cp850_string(i8* %0, i8* %1, i32 %2) {
%4 = tail call i32 @strlen(i8* %1)
%5 = icmp ult i32 %4, %2
br i1 %5, label %9, label %6
6: ; preds = %3
%7 = load i32, i32* @stderr
%8 = tail call i32 (i32, i8*, i8*, ...) @fprintf(i32 %7, i8* getelementptr inbounds ([65 x i8], [65 x i8]* @.str, i64 0, i64 0), i8* %1)
br label %29
9: ; preds = %3
%10 = tail call i32* @calloc(i32 %2, i32 4)
%11 = icmp eq i32* %10, null
br i1 %11, label %12, label %17
12: ; preds = %9
%13 = load i32, i32* @stderr
%14 = load i32, i32* @ENOMEM
%15 = tail call i8* @strerror(i32 %14)
%16 = tail call i32 (i32, i8*, i8*, ...) @fprintf(i32 %13, i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.1, i64 0, i64 0), i8* %1, i8* %15)
br label %29
17: ; preds = %9
%18 = tail call i64 @mbstowcs(i32* %10, i8* %1, i32 %2)
%19 = icmp eq i64 %18, -1
br i1 %19, label %20, label %26
20: ; preds = %17
%21 = load i32, i32* @stderr
%22 = load i32, i32* @errno
%23 = tail call i8* @strerror(i32 %22)
%24 = tail call i32 (i32, i8*, i8*, ...) @fprintf(i32 %21, i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.1, i64 0, i64 0), i8* %1, i8* %23)
%25 = tail call i32 @free(i32* %10)
br label %29
26: ; preds = %17
%27 = tail call i32 @wchar_string_to_cp850_string(i8* %0, i32* %10, i32 %2)
%28 = tail call i32 @free(i32* %10)
br label %29
29: ; preds = %26, %20, %12, %6
%30 = phi i32 [ 0, %6 ], [ 0, %20 ], [ %27, %26 ], [ 0, %12 ]
ret i32 %30
}