#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 */
struct sockaddr_in {
  int dummy;
};
struct sockaddr {
  int dummy;
};
typedef int socklen_t;
typedef int /*<<< orphan*/ ntop;

/* Variables and functions */
int NI_MAXHOST;
int /*<<< orphan*/ NI_NAMEREQD;
int /*<<< orphan*/ NI_NUMERICHOST;
char *eaistr(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
scalar_t__ getnameinfo(struct sockaddr *, int, char *, size_t,
                       int /*<<< orphan*/ *, int /*<<< orphan*/,
                       int /*<<< orphan*/);
scalar_t__ isupper(char);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strncpy(char *, char *, size_t);
char tolower(char);
const char *sockaddr2hostname(struct sockaddr_in *from, char *name,
                              size_t namelen) {
  int i, result;
  socklen_t fromlen;
  char ntop[NI_MAXHOST];

  fromlen = sizeof(struct sockaddr_in);
  if (result = getnameinfo((struct sockaddr *)from, fromlen, ntop, sizeof(ntop),
                           NULL, 0, NI_NUMERICHOST) != 0) {
    fprintf(stderr,
            "get_remote_hostname: getnameinfo NI_NUMERICHOST failed (%s)\n",
            eaistr(result));

    return NULL;
  }
  if (getnameinfo((struct sockaddr *)from, fromlen, name, namelen, NULL, 0,
                  NI_NAMEREQD) == 0) {
    name[namelen - 1] = '\0';
    for (i = 0; name[i]; i++)
      if (isupper(name[i]))
        name[i] = tolower(name[i]);
  } else {
    strncpy(name, ntop, namelen);
  }
  return name;
}


x86 assembly (clang, -O0):
<sockaddr2hostname>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    0x2e4d(%rip),%rax
mov    (%rax),%eax
mov    %eax,%ecx
mov    %rsp,%rax
mov    %rax,-0x38(%rbp)
mov    %rcx,%rax
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
mov    %rsp,%rdx
sub    %rax,%rdx
mov    %rdx,-0x50(%rbp)
mov    %rdx,%rsp
mov    %rcx,-0x40(%rbp)
movl   $0x4,-0x2c(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x2c(%rbp),%esi
mov    0x2e19(%rip),%rax
mov    (%rax),%eax
xor    %r8d,%r8d
xor    %r9d,%r9d
sub    $0x10,%rsp
mov    %eax,(%rsp)
call   1040 <getnameinfo@plt>
add    $0x10,%rsp
cmp    $0x0,%rax
setne  %al
mov    %al,%cl
and    $0x1,%cl
movzbl %cl,%ecx
mov    %ecx,-0x28(%rbp)
test   $0x1,%al
jne    11ee <sockaddr2hostname+0x8e>
jmp    1228 <sockaddr2hostname+0xc8>
mov    0x2de3(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x54(%rbp)
mov    -0x28(%rbp),%edi
call   1080 <eaistr@plt>
mov    -0x54(%rbp),%edi
mov    %rax,%rdx
lea    0xdf1(%rip),%rsi
call   1050 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
movl   $0x1,-0x44(%rbp)
jmp    12fa <sockaddr2hostname+0x19a>
mov    -0x10(%rbp),%rdi
mov    -0x2c(%rbp),%esi
mov    -0x18(%rbp),%rdx
mov    -0x20(%rbp),%rcx
mov    0x2db2(%rip),%rax
mov    (%rax),%eax
xor    %r8d,%r8d
xor    %r9d,%r9d
sub    $0x10,%rsp
mov    %eax,(%rsp)
call   1040 <getnameinfo@plt>
add    $0x10,%rsp
cmp    $0x0,%rax
jne    12da <sockaddr2hostname+0x17a>
mov    -0x18(%rbp),%rax
mov    -0x20(%rbp),%rcx
sub    $0x1,%rcx
movb   $0x0,(%rax,%rcx,1)
movl   $0x0,-0x24(%rbp)
mov    -0x18(%rbp),%rax
movslq -0x24(%rbp),%rcx
cmpb   $0x0,(%rax,%rcx,1)
je     12d5 <sockaddr2hostname+0x175>
mov    -0x18(%rbp),%rax
movslq -0x24(%rbp),%rcx
movsbl (%rax,%rcx,1),%edi
call   1070 <isupper@plt>
cmp    $0x0,%rax
je     12c2 <sockaddr2hostname+0x162>
mov    -0x18(%rbp),%rax
movslq -0x24(%rbp),%rcx
movsbl (%rax,%rcx,1),%edi
call   1060 <tolower@plt>
mov    %al,%dl
mov    -0x18(%rbp),%rax
movslq -0x24(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
jmp    12c7 <sockaddr2hostname+0x167>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    1277 <sockaddr2hostname+0x117>
jmp    12eb <sockaddr2hostname+0x18b>
mov    -0x50(%rbp),%rsi
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rdx
call   1030 <strncpy@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
movl   $0x1,-0x44(%rbp)
mov    -0x38(%rbp),%rax
mov    %rax,%rsp
mov    -0x8(%rbp),%rax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sockaddr2hostname>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%r12
mov    0x2e4d(%rip),%rax
mov    (%rax),%ecx
mov    %rsp,%rbx
lea    0xf(%rcx),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
mov    0x2e3b(%rip),%rax
mov    (%rax),%eax
sub    $0x8,%rsp
mov    $0x4,%esi
mov    %rbx,%rdx
xor    %r8d,%r8d
xor    %r9d,%r9d
push   %rax
call   1040 <getnameinfo@plt>
add    $0x10,%rsp
test   %rax,%rax
je     11e1 <sockaddr2hostname+0x81>
mov    0x2e19(%rip),%rax
mov    (%rax),%ebx
mov    $0x1,%edi
call   1080 <eaistr@plt>
lea    0xe2e(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1050 <fprintf@plt>
xor    %r15d,%r15d
jmp    121f <sockaddr2hostname+0xbf>
mov    0x2e08(%rip),%rax
mov    (%rax),%eax
sub    $0x8,%rsp
mov    %r12,%rdi
mov    $0x4,%esi
mov    %r15,%rdx
mov    %r14,%rcx
xor    %r8d,%r8d
xor    %r9d,%r9d
push   %rax
call   1040 <getnameinfo@plt>
add    $0x10,%rsp
test   %rax,%rax
je     122f <sockaddr2hostname+0xcf>
mov    %r15,%rdi
mov    %rbx,%rsi
mov    %r14,%rdx
call   1030 <strncpy@plt>
mov    %r15,%rax
lea    -0x20(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
movb   $0x0,-0x1(%r14,%r15,1)
mov    (%r15),%al
test   %al,%al
je     121f <sockaddr2hostname+0xbf>
mov    %r15,%rbx
jmp    125c <sockaddr2hostname+0xfc>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
movzbl 0x1(%rbx),%eax
add    $0x1,%rbx
test   %al,%al
je     121f <sockaddr2hostname+0xbf>
movsbl %al,%edi
call   1070 <isupper@plt>
test   %rax,%rax
je     1250 <sockaddr2hostname+0xf0>
movsbl (%rbx),%edi
call   1060 <tolower@plt>
mov    %al,(%rbx)
jmp    1250 <sockaddr2hostname+0xf0>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32 }
%struct.sockaddr = type { i32 }
@NI_MAXHOST = global i32 0
@NI_NUMERICHOST = global i32 0
@stderr = global i32 0
@.str = [61 x i8] c"get_remote_hostname: getnameinfo NI_NUMERICHOST failed (%s)\0A\00"
@NI_NAMEREQD = global i32 0
define i8* @sockaddr2hostname(%struct.sockaddr_in* %0, i8* %1, i64 %2) {
%4 = alloca i8*
%5 = alloca %struct.sockaddr_in*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i8*
%12 = alloca i64
%13 = alloca i32
store %struct.sockaddr_in* %0, %struct.sockaddr_in** %5
store i8* %1, i8** %6
store i64 %2, i64* %7
%14 = load i32, i32* @NI_MAXHOST
%15 = zext i32 %14 to i64
%16 = call i8* @llvm.stacksave()
store i8* %16, i8** %11
%17 = alloca i8, i64 %15
store i64 %15, i64* %12
store i32 4, i32* %10
%18 = load %struct.sockaddr_in*, %struct.sockaddr_in** %5
%19 = bitcast %struct.sockaddr_in* %18 to %struct.sockaddr*
%20 = load i32, i32* %10
%21 = load i32, i32* @NI_NUMERICHOST
%22 = call i64 @getnameinfo(%struct.sockaddr* %19, i32 %20, i8* %17, i64 %15, i32* null, i32 0, i32 %21)
%23 = icmp ne i64 %22, 0
%24 = zext i1 %23 to i32
store i32 %24, i32* %9
br i1 %23, label %25, label %30
25: ; preds = %3
%26 = load i32, i32* @stderr
%27 = load i32, i32* %9
%28 = call i8* @eaistr(i32 %27)
%29 = call i32 @fprintf(i32 %26, i8* getelementptr inbounds ([61 x i8], [61 x i8]* @.str, i64 0, i64 0), i8* %28)
store i8* null, i8** %4
store i32 1, i32* %13
br label %81
30: ; preds = %3
%31 = load %struct.sockaddr_in*, %struct.sockaddr_in** %5
%32 = bitcast %struct.sockaddr_in* %31 to %struct.sockaddr*
%33 = load i32, i32* %10
%34 = load i8*, i8** %6
%35 = load i64, i64* %7
%36 = load i32, i32* @NI_NAMEREQD
%37 = call i64 @getnameinfo(%struct.sockaddr* %32, i32 %33, i8* %34, i64 %35, i32* null, i32 0, i32 %36)
%38 = icmp eq i64 %37, 0
br i1 %38, label %39, label %75
39: ; preds = %30
%40 = load i8*, i8** %6
%41 = load i64, i64* %7
%42 = sub i64 %41, 1
%43 = getelementptr inbounds i8, i8* %40, i64 %42
store i8 0, i8* %43
store i32 0, i32* %8
br label %44
44: ; preds = %71, %39
%45 = load i8*, i8** %6
%46 = load i32, i32* %8
%47 = sext i32 %46 to i64
%48 = getelementptr inbounds i8, i8* %45, i64 %47
%49 = load i8, i8* %48
%50 = icmp ne i8 %49, 0
br i1 %50, label %51, label %74
51: ; preds = %44
%52 = load i8*, i8** %6
%53 = load i32, i32* %8
%54 = sext i32 %53 to i64
%55 = getelementptr inbounds i8, i8* %52, i64 %54
%56 = load i8, i8* %55
%57 = call i64 @isupper(i8 signext %56)
%58 = icmp ne i64 %57, 0
br i1 %58, label %59, label %70
59: ; preds = %51
%60 = load i8*, i8** %6
%61 = load i32, i32* %8
%62 = sext i32 %61 to i64
%63 = getelementptr inbounds i8, i8* %60, i64 %62
%64 = load i8, i8* %63
%65 = call signext i8 @tolower(i8 signext %64)
%66 = load i8*, i8** %6
%67 = load i32, i32* %8
%68 = sext i32 %67 to i64
%69 = getelementptr inbounds i8, i8* %66, i64 %68
store i8 %65, i8* %69
br label %70
70: ; preds = %59, %51
br label %71
71: ; preds = %70
%72 = load i32, i32* %8
%73 = add nsw i32 %72, 1
store i32 %73, i32* %8
br label %44
74: ; preds = %44
br label %79
75: ; preds = %30
%76 = load i8*, i8** %6
%77 = load i64, i64* %7
%78 = call i32 @strncpy(i8* %76, i8* %17, i64 %77)
br label %79
79: ; preds = %75, %74
%80 = load i8*, i8** %6
store i8* %80, i8** %4
store i32 1, i32* %13
br label %81
81: ; preds = %79, %25
%82 = load i8*, i8** %11
%83 = load i8*, i8** %4
ret i8* %83
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32 }
%struct.sockaddr = type { i32 }
@NI_MAXHOST = global i32 0
@NI_NUMERICHOST = global i32 0
@stderr = global i32 0
@.str = [61 x i8] c"get_remote_hostname: getnameinfo NI_NUMERICHOST failed (%s)\0A\00"
@NI_NAMEREQD = global i32 0
define i8* @sockaddr2hostname(%struct.sockaddr_in* %0, i8* %1, i64 %2) {
%4 = load i32, i32* @NI_MAXHOST
%5 = zext i32 %4 to i64
%6 = alloca i8, i64 %5
%7 = bitcast %struct.sockaddr_in* %0 to %struct.sockaddr*
%8 = load i32, i32* @NI_NUMERICHOST
%9 = call i64 @getnameinfo(%struct.sockaddr* %7, i32 4, i8* %6, i64 %5, i32* null, i32 0, i32 %8)
%10 = icmp eq i64 %9, 0
br i1 %10, label %15, label %11
11: ; preds = %3
%12 = load i32, i32* @stderr
%13 = call i8* @eaistr(i32 1)
%14 = call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([61 x i8], [61 x i8]* @.str, i64 0, i64 0), i8* %13)
br label %40
15: ; preds = %3
%16 = load i32, i32* @NI_NAMEREQD
%17 = call i64 @getnameinfo(%struct.sockaddr* %7, i32 4, i8* %1, i64 %2, i32* null, i32 0, i32 %16)
%18 = icmp eq i64 %17, 0
br i1 %18, label %19, label %38
19: ; preds = %15
%20 = add i64 %2, -1
%21 = getelementptr inbounds i8, i8* %1, i64 %20
store i8 0, i8* %21
%22 = load i8, i8* %1
%23 = icmp eq i8 %22, 0
br i1 %23, label %40, label %24
24: ; preds = %19, %33
%25 = phi i64 [ %34, %33 ], [ 0, %19 ]
%26 = phi i8 [ %36, %33 ], [ %22, %19 ]
%27 = phi i8* [ %35, %33 ], [ %1, %19 ]
%28 = call i64 @isupper(i8 signext %26)
%29 = icmp eq i64 %28, 0
br i1 %29, label %33, label %30
30: ; preds = %24
%31 = load i8, i8* %27
%32 = call signext i8 @tolower(i8 signext %31)
store i8 %32, i8* %27
br label %33
33: ; preds = %24, %30
%34 = add nuw i64 %25, 1
%35 = getelementptr inbounds i8, i8* %1, i64 %34
%36 = load i8, i8* %35
%37 = icmp eq i8 %36, 0
br i1 %37, label %40, label %24
38: ; preds = %15
%39 = call i32 @strncpy(i8* %1, i8* %6, i64 %2)
br label %40
40: ; preds = %33, %19, %38, %11
%41 = phi i8* [ null, %11 ], [ %1, %38 ], [ %1, %19 ], [ %1, %33 ]
ret i8* %41
}