#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*/ szHostname;
struct in_addr {
  int dummy;
};
struct hostent {
  scalar_t__ *h_addr_list;
};

/* Variables and functions */
struct hostent *gethostbyname(char *);
scalar_t__ gethostname(char *, int);
char *inet_ntoa(struct in_addr);
int /*<<< orphan*/ qstrcpy(char *, size_t, char *);
bool qsysgetip(char *buf, size_t bufsize) {
  char szHostname[63 + 1];
  if (gethostname(szHostname, sizeof(szHostname)) != 0)
    return false;
  struct hostent *pHostEntry = gethostbyname(szHostname);
  if (pHostEntry == NULL)
    return false;
  char *pszLocalIp = inet_ntoa(*(struct in_addr *)*pHostEntry->h_addr_list);
  if (pszLocalIp == NULL)
    return false;
  qstrcpy(buf, bufsize, pszLocalIp);
  return true;
}


x86 assembly (clang, -O0):
<qsysgetip>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
lea    -0x60(%rbp),%rdi
mov    $0x40,%esi
call   1060 <gethostname@plt>
cmp    $0x0,%rax
je     1174 <qsysgetip+0x34>
movl   $0x0,-0x4(%rbp)
jmp    11dc <qsysgetip+0x9c>
lea    -0x60(%rbp),%rdi
call   1040 <gethostbyname@plt>
mov    %rax,-0x68(%rbp)
cmpq   $0x0,-0x68(%rbp)
jne    1198 <qsysgetip+0x58>
movl   $0x0,-0x4(%rbp)
jmp    11dc <qsysgetip+0x9c>
mov    -0x68(%rbp),%rax
mov    (%rax),%rax
mov    (%rax),%rax
mov    (%rax),%edi
call   1030 <inet_ntoa@plt>
mov    %rax,-0x70(%rbp)
cmpq   $0x0,-0x70(%rbp)
jne    11c4 <qsysgetip+0x84>
movl   $0x0,-0x4(%rbp)
jmp    11dc <qsysgetip+0x9c>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x70(%rbp),%rdx
call   1050 <qstrcpy@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<qsysgetip>:
push   %rbp
push   %r14
push   %rbx
sub    $0x40,%rsp
mov    %rsi,%r14
mov    %rdi,%rbx
mov    %rsp,%rdi
mov    $0x40,%esi
call   1060 <gethostname@plt>
xor    %ebp,%ebp
test   %rax,%rax
jne    1194 <qsysgetip+0x54>
mov    %rsp,%rdi
call   1040 <gethostbyname@plt>
test   %rax,%rax
je     1194 <qsysgetip+0x54>
mov    (%rax),%rax
mov    (%rax),%rax
mov    (%rax),%edi
call   1030 <inet_ntoa@plt>
test   %rax,%rax
je     1194 <qsysgetip+0x54>
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %rax,%rdx
call   1050 <qstrcpy@plt>
mov    $0x1,%ebp
mov    %ebp,%eax
add    $0x40,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.hostent = type { i64* }
%struct.in_addr = type { i32 }
define i32 @qsysgetip(i8* %0, i64 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i64
%6 = alloca [64 x i8]
%7 = alloca %struct.hostent*
%8 = alloca i8*
store i8* %0, i8** %4
store i64 %1, i64* %5
%9 = getelementptr inbounds [64 x i8], [64 x i8]* %6, i64 0, i64 0
%10 = call i64 @gethostname(i8* %9, i32 64)
%11 = icmp ne i64 %10, 0
br i1 %11, label %12, label %13
12: ; preds = %2
store i32 0, i32* %3
br label %36
13: ; preds = %2
%14 = getelementptr inbounds [64 x i8], [64 x i8]* %6, i64 0, i64 0
%15 = call %struct.hostent* @gethostbyname(i8* %14)
store %struct.hostent* %15, %struct.hostent** %7
%16 = load %struct.hostent*, %struct.hostent** %7
%17 = icmp eq %struct.hostent* %16, null
br i1 %17, label %18, label %19
18: ; preds = %13
store i32 0, i32* %3
br label %36
19: ; preds = %13
%20 = load %struct.hostent*, %struct.hostent** %7
%21 = getelementptr inbounds %struct.hostent, %struct.hostent* %20, i32 0, i32 0
%22 = load i64*, i64** %21
%23 = load i64, i64* %22
%24 = inttoptr i64 %23 to %struct.in_addr*
%25 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %24, i32 0, i32 0
%26 = load i32, i32* %25
%27 = call i8* @inet_ntoa(i32 %26)
store i8* %27, i8** %8
%28 = load i8*, i8** %8
%29 = icmp eq i8* %28, null
br i1 %29, label %30, label %31
30: ; preds = %19
store i32 0, i32* %3
br label %36
31: ; preds = %19
%32 = load i8*, i8** %4
%33 = load i64, i64* %5
%34 = load i8*, i8** %8
%35 = call i32 @qstrcpy(i8* %32, i64 %33, i8* %34)
store i32 1, i32* %3
br label %36
36: ; preds = %31, %30, %18, %12
%37 = load i32, i32* %3
ret i32 %37
}

LLVM IR (-O2):
%struct.hostent = type { i64* }
%struct.in_addr = type { i32 }
define i32 @qsysgetip(i8* %0, i64 %1) {
%3 = alloca [64 x i8]
%4 = getelementptr inbounds [64 x i8], [64 x i8]* %3, i64 0, i64 0
%5 = call i64 @gethostname(i8* %4, i32 64)
%6 = icmp eq i64 %5, 0
br i1 %6, label %7, label %21
7: ; preds = %2
%8 = call %struct.hostent* @gethostbyname(i8* %4)
%9 = icmp eq %struct.hostent* %8, null
br i1 %9, label %21, label %10
10: ; preds = %7
%11 = getelementptr inbounds %struct.hostent, %struct.hostent* %8, i64 0, i32 0
%12 = load i64*, i64** %11
%13 = load i64, i64* %12
%14 = inttoptr i64 %13 to %struct.in_addr*
%15 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %14, i64 0, i32 0
%16 = load i32, i32* %15
%17 = call i8* @inet_ntoa(i32 %16)
%18 = icmp eq i8* %17, null
br i1 %18, label %21, label %19
19: ; preds = %10
%20 = call i32 @qstrcpy(i8* %0, i64 %1, i8* %17)
br label %21
21: ; preds = %7, %10, %19, %2
%22 = phi i32 [ 0, %2 ], [ 0, %7 ], [ 1, %19 ], [ 0, %10 ]
ret i32 %22
}