#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 in_addr {
  int /*<<< orphan*/ s_addr;
};
struct hostent {
  int /*<<< orphan*/ *h_addr_list;
};

/* Variables and functions */
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
struct hostent *gethostbyname(char *);
int gethostname(char *, int);
int /*<<< orphan*/ h_errno;
int /*<<< orphan*/ inet_ntoa(struct in_addr);
int /*<<< orphan*/ memcpy(int /*<<< orphan*/ *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
char *sock_herrstr(int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strncpy(char *, int /*<<< orphan*/, int);
int sock_getname(char *self, int length, int alias) {
  int ret;
  struct hostent *hp;
  struct in_addr in;
  ret = gethostname(self, length);
  if (ret < 0) {
    perror("sock_getname: gethostname");
    return -1;
  }
  if (!alias) {
    hp = gethostbyname(self);
    if (hp) {
      memcpy(&in.s_addr, *(hp->h_addr_list), sizeof(in.s_addr));
      strncpy(self, inet_ntoa(in), length);
    }
    if (!hp) {
      fprintf(stderr, "sock_getname: gethostbyname: %s\n",
              sock_herrstr(h_errno));
      return -1;
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<sock_getname>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   10a0 <gethostname@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    11c3 <sock_getname+0x43>
lea    0xe4e(%rip),%rdi
call   1090 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1269 <sock_getname+0xe9>
cmpl   $0x0,-0x18(%rbp)
jne    1262 <sock_getname+0xe2>
mov    -0x10(%rbp),%rdi
call   1050 <gethostbyname@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     121a <sock_getname+0x9a>
lea    -0x30(%rbp),%rdi
mov    -0x28(%rbp),%rax
mov    (%rax),%rax
mov    (%rax),%esi
mov    $0x4,%edx
call   1070 <memcpy@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x30(%rbp),%edi
call   1040 <inet_ntoa@plt>
mov    -0x38(%rbp),%rdi
mov    %eax,%esi
mov    -0x14(%rbp),%edx
call   1030 <strncpy@plt>
cmpq   $0x0,-0x28(%rbp)
jne    125d <sock_getname+0xdd>
mov    0x2dac(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
mov    0x2db0(%rip),%rax
mov    (%rax),%edi
call   1080 <sock_herrstr@plt>
mov    -0x3c(%rbp),%edi
mov    %rax,%rdx
lea    0xdce(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1269 <sock_getname+0xe9>
jmp    1262 <sock_getname+0xe2>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sock_getname>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%ebp
mov    %esi,%r15d
mov    %rdi,%rbx
call   10a0 <gethostname@plt>
test   %eax,%eax
js     11ff <sock_getname+0x7f>
xor    %r14d,%r14d
test   %ebp,%ebp
jne    1211 <sock_getname+0x91>
mov    %rbx,%rdi
call   1050 <gethostbyname@plt>
test   %rax,%rax
je     11d5 <sock_getname+0x55>
mov    (%rax),%rax
mov    (%rax),%esi
mov    %rsp,%rdi
mov    $0x4,%edx
call   1070 <memcpy@plt>
mov    (%rsp),%edi
call   1040 <inet_ntoa@plt>
mov    %rbx,%rdi
mov    %eax,%esi
mov    %r15d,%edx
call   1030 <strncpy@plt>
jmp    1211 <sock_getname+0x91>
mov    0x2dfc(%rip),%rax
mov    (%rax),%ebx
mov    0x2e03(%rip),%rax
mov    (%rax),%edi
call   1080 <sock_herrstr@plt>
lea    0xe27(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1060 <fprintf@plt>
jmp    120b <sock_getname+0x8b>
lea    0xdfa(%rip),%rdi
call   1090 <perror@plt>
mov    $0xffffffff,%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.hostent = type { i32* }
%struct.in_addr = type { i32 }
@.str = [26 x i8] c"sock_getname: gethostname\00"
@stderr = global i32 0
@.str.1 = [33 x i8] c"sock_getname: gethostbyname: %s\0A\00"
@h_errno = global i32 0
define i32 @sock_getname(i8* %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca %struct.hostent*
%10 = alloca %struct.in_addr
store i8* %0, i8** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
%11 = load i8*, i8** %5
%12 = load i32, i32* %6
%13 = call i32 @gethostname(i8* %11, i32 %12)
store i32 %13, i32* %8
%14 = load i32, i32* %8
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %3
%17 = call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %4
br label %49
18: ; preds = %3
%19 = load i32, i32* %7
%20 = icmp ne i32 %19, 0
br i1 %20, label %48, label %21
21: ; preds = %18
%22 = load i8*, i8** %5
%23 = call %struct.hostent* @gethostbyname(i8* %22)
store %struct.hostent* %23, %struct.hostent** %9
%24 = load %struct.hostent*, %struct.hostent** %9
%25 = icmp ne %struct.hostent* %24, null
br i1 %25, label %26, label %39
26: ; preds = %21
%27 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %10, i32 0, i32 0
%28 = load %struct.hostent*, %struct.hostent** %9
%29 = getelementptr inbounds %struct.hostent, %struct.hostent* %28, i32 0, i32 0
%30 = load i32*, i32** %29
%31 = load i32, i32* %30
%32 = call i32 @memcpy(i32* %27, i32 %31, i32 4)
%33 = load i8*, i8** %5
%34 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %10, i32 0, i32 0
%35 = load i32, i32* %34
%36 = call i32 @inet_ntoa(i32 %35)
%37 = load i32, i32* %6
%38 = call i32 @strncpy(i8* %33, i32 %36, i32 %37)
br label %39
39: ; preds = %26, %21
%40 = load %struct.hostent*, %struct.hostent** %9
%41 = icmp ne %struct.hostent* %40, null
br i1 %41, label %47, label %42
42: ; preds = %39
%43 = load i32, i32* @stderr
%44 = load i32, i32* @h_errno
%45 = call i8* @sock_herrstr(i32 %44)
%46 = call i32 @fprintf(i32 %43, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i8* %45)
store i32 -1, i32* %4
br label %49
47: ; preds = %39
br label %48
48: ; preds = %47, %18
store i32 0, i32* %4
br label %49
49: ; preds = %48, %42, %16
%50 = load i32, i32* %4
ret i32 %50
}

LLVM IR (-O2):
%struct.in_addr = type { i32 }
%struct.hostent = type { i32* }
@.str = [26 x i8] c"sock_getname: gethostname\00"
@stderr = global i32 0
@.str.1 = [33 x i8] c"sock_getname: gethostbyname: %s\0A\00"
@h_errno = global i32 0
define i32 @sock_getname(i8* %0, i32 %1, i32 %2) {
%4 = alloca %struct.in_addr
%5 = bitcast %struct.in_addr* %4 to i8*
%6 = tail call i32 @gethostname(i8* %0, i32 %1)
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %3
%9 = tail call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
br label %29
10: ; preds = %3
%11 = icmp eq i32 %2, 0
br i1 %11, label %12, label %29
12: ; preds = %10
%13 = tail call %struct.hostent* @gethostbyname(i8* %0)
%14 = icmp eq %struct.hostent* %13, null
br i1 %14, label %24, label %15
15: ; preds = %12
%16 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %4, i64 0, i32 0
%17 = getelementptr inbounds %struct.hostent, %struct.hostent* %13, i64 0, i32 0
%18 = load i32*, i32** %17
%19 = load i32, i32* %18
%20 = call i32 @memcpy(i32* %16, i32 %19, i32 4)
%21 = load i32, i32* %16
%22 = call i32 @inet_ntoa(i32 %21)
%23 = call i32 @strncpy(i8* %0, i32 %22, i32 %1)
br label %29
24: ; preds = %12
%25 = load i32, i32* @stderr
%26 = load i32, i32* @h_errno
%27 = tail call i8* @sock_herrstr(i32 %26)
%28 = tail call i32 @fprintf(i32 %25, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i8* %27)
br label %29
29: ; preds = %15, %10, %24, %8
%30 = phi i32 [ -1, %8 ], [ -1, %24 ], [ 0, %15 ], [ 0, %10 ]
ret i32 %30
}