const char *gai_strerror(int ecode) {
  static const char *eai_descr[] = {
      "no error",
      "address family for nodename not supported",
      "temporary failure in name resolution",
      "invalid value for ai_flags",
      "non-recoverable failure in name resolution",
      "ai_family not supported",
      "memory allocation failure",
      "no address associated with nodename",
      "nodename nor servname provided, or not known",
      "servname not supported for ai_socktype",
      "ai_socktype not supported",
      "system error returned in errno",
  };
  if (ecode < 0 || ecode > (int)(sizeof eai_descr / sizeof eai_descr[0]))
    return "unknown error";
  return eai_descr[ecode];
}


x86 assembly (clang, -O0):
<gai_strerror>:
push   %rbp
mov    %rsp,%rbp
mov    %edi,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jl     111b <gai_strerror+0x1b>
cmpl   $0xc,-0xc(%rbp)
jle    112b <gai_strerror+0x2b>
lea    0x105f(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    113e <gai_strerror+0x3e>
movslq -0xc(%rbp),%rcx
lea    0x2efa(%rip),%rax
mov    (%rax,%rcx,8),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
pop    %rbp
ret

x86 assembly (clang, -O2):
<gai_strerror>:
cmp    $0xc,%edi
jbe    110d <gai_strerror+0xd>
lea    0x10a5(%rip),%rax
ret
mov    %edi,%eax
lea    0xeea(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
ret

LLVM IR (-O0):
@gai_strerror.eai_descr = internal global [12 x i8*] [i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i32 0, i32 0), i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.1, i32 0, i32 0), i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.2, i32 0, i32 0), i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.3, i32 0, i32 0), i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.4, i32 0, i32 0), i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.5, i32 0, i32 0), i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.6, i32 0, i32 0), i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.7, i32 0, i32 0), i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.8, i32 0, i32 0), i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.9, i32 0, i32 0), i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.10, i32 0, i32 0), i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.11, i32 0, i32 0)]
@.str = [9 x i8] c"no error\00"
@.str.1 = [42 x i8] c"address family for nodename not supported\00"
@.str.2 = [37 x i8] c"temporary failure in name resolution\00"
@.str.3 = [27 x i8] c"invalid value for ai_flags\00"
@.str.4 = [43 x i8] c"non-recoverable failure in name resolution\00"
@.str.5 = [24 x i8] c"ai_family not supported\00"
@.str.6 = [26 x i8] c"memory allocation failure\00"
@.str.7 = [36 x i8] c"no address associated with nodename\00"
@.str.8 = [45 x i8] c"nodename nor servname provided, or not known\00"
@.str.9 = [39 x i8] c"servname not supported for ai_socktype\00"
@.str.10 = [26 x i8] c"ai_socktype not supported\00"
@.str.11 = [31 x i8] c"system error returned in errno\00"
@.str.12 = [14 x i8] c"unknown error\00"
define i8* @gai_strerror(i32 %0) {
%2 = alloca i8*
%3 = alloca i32
store i32 %0, i32* %3
%4 = load i32, i32* %3
%5 = icmp slt i32 %4, 0
br i1 %5, label %9, label %6
6: ; preds = %1
%7 = load i32, i32* %3
%8 = icmp sgt i32 %7, 12
br i1 %8, label %9, label %10
9: ; preds = %6, %1
store i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.12, i64 0, i64 0), i8** %2
br label %15
10: ; preds = %6
%11 = load i32, i32* %3
%12 = sext i32 %11 to i64
%13 = getelementptr inbounds [12 x i8*], [12 x i8*]* @gai_strerror.eai_descr, i64 0, i64 %12
%14 = load i8*, i8** %13
store i8* %14, i8** %2
br label %15
15: ; preds = %10, %9
%16 = load i8*, i8** %2
ret i8* %16
}

LLVM IR (-O2):
@reltable.gai_strerror = internal unnamed_addr constant [12 x i32] [i32 trunc (i64 sub (i64 ptrtoint ([9 x i8]* @.str to i64), i64 ptrtoint ([12 x i32]* @reltable.gai_strerror to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([42 x i8]* @.str.1 to i64), i64 ptrtoint ([12 x i32]* @reltable.gai_strerror to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([37 x i8]* @.str.2 to i64), i64 ptrtoint ([12 x i32]* @reltable.gai_strerror to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([27 x i8]* @.str.3 to i64), i64 ptrtoint ([12 x i32]* @reltable.gai_strerror to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([43 x i8]* @.str.4 to i64), i64 ptrtoint ([12 x i32]* @reltable.gai_strerror to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([24 x i8]* @.str.5 to i64), i64 ptrtoint ([12 x i32]* @reltable.gai_strerror to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([26 x i8]* @.str.6 to i64), i64 ptrtoint ([12 x i32]* @reltable.gai_strerror to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([36 x i8]* @.str.7 to i64), i64 ptrtoint ([12 x i32]* @reltable.gai_strerror to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([45 x i8]* @.str.8 to i64), i64 ptrtoint ([12 x i32]* @reltable.gai_strerror to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([39 x i8]* @.str.9 to i64), i64 ptrtoint ([12 x i32]* @reltable.gai_strerror to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([26 x i8]* @.str.10 to i64), i64 ptrtoint ([12 x i32]* @reltable.gai_strerror to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([31 x i8]* @.str.11 to i64), i64 ptrtoint ([12 x i32]* @reltable.gai_strerror to i64)) to i32)]
@.str = [9 x i8] c"no error\00"
@.str.1 = [42 x i8] c"address family for nodename not supported\00"
@.str.2 = [37 x i8] c"temporary failure in name resolution\00"
@.str.3 = [27 x i8] c"invalid value for ai_flags\00"
@.str.4 = [43 x i8] c"non-recoverable failure in name resolution\00"
@.str.5 = [24 x i8] c"ai_family not supported\00"
@.str.6 = [26 x i8] c"memory allocation failure\00"
@.str.7 = [36 x i8] c"no address associated with nodename\00"
@.str.8 = [45 x i8] c"nodename nor servname provided, or not known\00"
@.str.9 = [39 x i8] c"servname not supported for ai_socktype\00"
@.str.10 = [26 x i8] c"ai_socktype not supported\00"
@.str.11 = [31 x i8] c"system error returned in errno\00"
@.str.12 = [14 x i8] c"unknown error\00"
define i8* @gai_strerror(i32 %0) {
%2 = icmp ugt i32 %0, 12
br i1 %2, label %7, label %3
3: ; preds = %1
%4 = zext i32 %0 to i64
%5 = shl i64 %4, 2
%6 = call i8* @llvm.load.relative.i64(i8* bitcast ([12 x i32]* @reltable.gai_strerror to i8*), i64 %5)
br label %7
7: ; preds = %1, %3
%8 = phi i8* [ %6, %3 ], [ getelementptr inbounds ([14 x i8], [14 x i8]* @.str.12, i64 0, i64 0), %1 ]
ret i8* %8
}