#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*/ errno;
int /*<<< orphan*/ sprintf(char *, char *, int);
char *strerror(int /*<<< orphan*/);
int /*<<< orphan*/ strncpy(char *, char *, int);
void tosError_getText(int errCode, int errCodeDetail, char *msg, int msgSize) {
  char err[40];
  switch (errCode) {
  case 6000:
    strncpy(msg, "Win32 API error", msgSize - 1);
    msg[msgSize] = 0;
    break;
  case 6001:
    sprintf(err, "WinSocket error %d", errCodeDetail);
    strncpy(msg, err, msgSize - 1);
    msg[msgSize] = 0;
    break;
  default:
    strncpy(msg, strerror(errno), msgSize - 1);
    msg[msgSize] = 0;
  }
}


x86 assembly (clang, -O0):
<tosError_getText>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %rdx,-0x10(%rbp)
mov    %ecx,-0x14(%rbp)
mov    -0x4(%rbp),%eax
mov    %eax,-0x44(%rbp)
sub    $0x1770,%eax
je     116e <tosError_getText+0x3e>
jmp    115b <tosError_getText+0x2b>
mov    -0x44(%rbp),%eax
sub    $0x1771,%eax
je     1195 <tosError_getText+0x65>
jmp    11cc <tosError_getText+0x9c>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%edx
sub    $0x1,%edx
lea    0xe81(%rip),%rsi
call   1030 <strncpy@plt>
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    1200 <tosError_getText+0xd0>
lea    -0x40(%rbp),%rdi
mov    -0x8(%rbp),%edx
lea    0xe6d(%rip),%rsi
call   1040 <sprintf@plt>
mov    -0x10(%rbp),%rdi
lea    -0x40(%rbp),%rsi
mov    -0x14(%rbp),%edx
sub    $0x1,%edx
call   1030 <strncpy@plt>
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    1200 <tosError_getText+0xd0>
mov    -0x10(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    0x2e05(%rip),%rax
mov    (%rax),%edi
call   1050 <strerror@plt>
mov    -0x50(%rbp),%rdi
mov    %rax,%rsi
mov    -0x14(%rbp),%edx
sub    $0x1,%edx
call   1030 <strncpy@plt>
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tosError_getText>:
push   %r15
push   %r14
push   %rbx
sub    $0x30,%rsp
mov    %ecx,%r14d
mov    %rdx,%rbx
cmp    $0x1771,%edi
je     115f <tosError_getText+0x2f>
cmp    $0x1770,%edi
jne    117f <tosError_getText+0x4f>
lea    -0x1(%r14),%edx
lea    0xea6(%rip),%rsi
mov    %rbx,%rdi
jmp    1197 <tosError_getText+0x67>
mov    %esi,%edx
lea    0xea8(%rip),%rsi
mov    %rsp,%r15
mov    %r15,%rdi
call   1040 <sprintf@plt>
lea    -0x1(%r14),%edx
mov    %rbx,%rdi
mov    %r15,%rsi
jmp    1197 <tosError_getText+0x67>
mov    0x2e5a(%rip),%rax
mov    (%rax),%edi
call   1050 <strerror@plt>
lea    -0x1(%r14),%edx
mov    %rbx,%rdi
mov    %rax,%rsi
call   1030 <strncpy@plt>
movslq %r14d,%rax
movb   $0x0,(%rbx,%rax,1)
add    $0x30,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [16 x i8] c"Win32 API error\00"
@.str.1 = [19 x i8] c"WinSocket error %d\00"
@errno = global i32 0
define void @tosError_getText(i32 %0, i32 %1, i8* %2, i32 %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca [40 x i8]
store i32 %0, i32* %5
store i32 %1, i32* %6
store i8* %2, i8** %7
store i32 %3, i32* %8
%10 = load i32, i32* %5
switch i32 %10, label %33 [
i32 6000, label %11
i32 6001, label %20
]
11: ; preds = %4
%12 = load i8*, i8** %7
%13 = load i32, i32* %8
%14 = sub nsw i32 %13, 1
%15 = call i32 @strncpy(i8* %12, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %14)
%16 = load i8*, i8** %7
%17 = load i32, i32* %8
%18 = sext i32 %17 to i64
%19 = getelementptr inbounds i8, i8* %16, i64 %18
store i8 0, i8* %19
br label %44
20: ; preds = %4
%21 = getelementptr inbounds [40 x i8], [40 x i8]* %9, i64 0, i64 0
%22 = load i32, i32* %6
%23 = call i32 @sprintf(i8* %21, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i32 %22)
%24 = load i8*, i8** %7
%25 = getelementptr inbounds [40 x i8], [40 x i8]* %9, i64 0, i64 0
%26 = load i32, i32* %8
%27 = sub nsw i32 %26, 1
%28 = call i32 @strncpy(i8* %24, i8* %25, i32 %27)
%29 = load i8*, i8** %7
%30 = load i32, i32* %8
%31 = sext i32 %30 to i64
%32 = getelementptr inbounds i8, i8* %29, i64 %31
store i8 0, i8* %32
br label %44
33: ; preds = %4
%34 = load i8*, i8** %7
%35 = load i32, i32* @errno
%36 = call i8* @strerror(i32 %35)
%37 = load i32, i32* %8
%38 = sub nsw i32 %37, 1
%39 = call i32 @strncpy(i8* %34, i8* %36, i32 %38)
%40 = load i8*, i8** %7
%41 = load i32, i32* %8
%42 = sext i32 %41 to i64
%43 = getelementptr inbounds i8, i8* %40, i64 %42
store i8 0, i8* %43
br label %44
44: ; preds = %33, %20, %11
ret void
}

LLVM IR (-O2):
@.str = [16 x i8] c"Win32 API error\00"
@.str.1 = [19 x i8] c"WinSocket error %d\00"
@errno = global i32 0
define void @tosError_getText(i32 %0, i32 %1, i8* %2, i32 %3) {
%5 = alloca [40 x i8]
%6 = getelementptr inbounds [40 x i8], [40 x i8]* %5, i64 0, i64 0
switch i32 %0, label %14 [
i32 6000, label %7
i32 6001, label %10
]
7: ; preds = %4
%8 = add nsw i32 %3, -1
%9 = tail call i32 @strncpy(i8* %2, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %8)
br label %19
10: ; preds = %4
%11 = call i32 @sprintf(i8* %6, i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i32 %1)
%12 = add nsw i32 %3, -1
%13 = call i32 @strncpy(i8* %2, i8* %6, i32 %12)
br label %19
14: ; preds = %4
%15 = load i32, i32* @errno
%16 = tail call i8* @strerror(i32 %15)
%17 = add nsw i32 %3, -1
%18 = tail call i32 @strncpy(i8* %2, i8* %16, i32 %17)
br label %19
19: ; preds = %14, %10, %7
%20 = sext i32 %3 to i64
%21 = getelementptr inbounds i8, i8* %2, i64 %20
store i8 0, i8* %21
ret void
}