#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 netent {
  scalar_t__ n_name;
};
struct in_addr {
  int /*<<< orphan*/ s_addr;
};

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
struct netent *getnetbyaddr(long, int /*<<< orphan*/);
scalar_t__ ntohl(int /*<<< orphan*/);
char *addr_to_network(struct in_addr *addr) {
  struct netent *net;
  if ((net = getnetbyaddr((long)ntohl(addr->s_addr), AF_INET)) != NULL)
    return (char *)net->n_name;
  else
    return (char *)NULL;
}


x86 assembly (clang, -O0):
<addr_to_network>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
call   1040 <ntohl@plt>
mov    %rax,%rdi
mov    0x2ea7(%rip),%rax
mov    (%rax),%esi
call   1030 <getnetbyaddr@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
je     1166 <addr_to_network+0x46>
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x8(%rbp)
jmp    116e <addr_to_network+0x4e>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<addr_to_network>:
push   %rax
mov    (%rdi),%edi
call   1040 <ntohl@plt>
mov    0x2eb9(%rip),%rcx
mov    (%rcx),%esi
mov    %rax,%rdi
call   1030 <getnetbyaddr@plt>
test   %rax,%rax
je     1143 <addr_to_network+0x23>
mov    (%rax),%rax
pop    %rcx
ret
xor    %eax,%eax
pop    %rcx
ret

LLVM IR (-O0):
%struct.in_addr = type { i32 }
%struct.netent = type { i64 }
@AF_INET = global i32 0
define i8* @addr_to_network(%struct.in_addr* %0) {
%2 = alloca i8*
%3 = alloca %struct.in_addr*
%4 = alloca %struct.netent*
store %struct.in_addr* %0, %struct.in_addr** %3
%5 = load %struct.in_addr*, %struct.in_addr** %3
%6 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %5, i32 0, i32 0
%7 = load i32, i32* %6
%8 = call i64 @ntohl(i32 %7)
%9 = load i32, i32* @AF_INET
%10 = call %struct.netent* @getnetbyaddr(i64 %8, i32 %9)
store %struct.netent* %10, %struct.netent** %4
%11 = icmp ne %struct.netent* %10, null
br i1 %11, label %12, label %17
12: ; preds = %1
%13 = load %struct.netent*, %struct.netent** %4
%14 = getelementptr inbounds %struct.netent, %struct.netent* %13, i32 0, i32 0
%15 = load i64, i64* %14
%16 = inttoptr i64 %15 to i8*
store i8* %16, i8** %2
br label %18
17: ; preds = %1
store i8* null, i8** %2
br label %18
18: ; preds = %17, %12
%19 = load i8*, i8** %2
ret i8* %19
}

LLVM IR (-O2):
%struct.in_addr = type { i32 }
%struct.netent = type { i64 }
@AF_INET = global i32 0
define i8* @addr_to_network(%struct.in_addr* nocapture %0) {
%2 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %0, i64 0, i32 0
%3 = load i32, i32* %2
%4 = tail call i64 @ntohl(i32 %3)
%5 = load i32, i32* @AF_INET
%6 = tail call %struct.netent* @getnetbyaddr(i64 %4, i32 %5)
%7 = icmp eq %struct.netent* %6, null
br i1 %7, label %12, label %8
8: ; preds = %1
%9 = getelementptr inbounds %struct.netent, %struct.netent* %6, i64 0, i32 0
%10 = load i64, i64* %9
%11 = inttoptr i64 %10 to i8*
br label %12
12: ; preds = %1, %8
%13 = phi i8* [ %11, %8 ], [ null, %1 ]
ret i8* %13
}