#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 dummy;
};
struct sockaddr_in {
  struct in_addr sin_addr;
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
struct hostent {
  scalar_t__ h_addr;
};
typedef int socklen_t;
typedef int /*<<< orphan*/ saddr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SHUT_RDWR;
int /*<<< orphan*/ SOCK_STREAM;
scalar_t__ connect(int, struct sockaddr *, int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
struct hostent *gethostbyname(char *);
int /*<<< orphan*/ getsockname(int, struct sockaddr *, int *);
int /*<<< orphan*/ htons(int);
char *inet_ntoa(struct in_addr);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ shutdown(int, int /*<<< orphan*/);
int /*<<< orphan*/ snprintf(char *, int, char *, char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int get_local_addr(char *hostname, char *addr, int addrlen) {
  int sock;
  socklen_t n;
  struct sockaddr_in saddr;
  struct hostent *hostinfo;
  sock = socket(PF_INET, SOCK_STREAM, 0);
  saddr.sin_family = AF_INET;
  saddr.sin_port = htons(22);
  hostinfo = gethostbyname(hostname);
  if (hostinfo == NULL) {
    fprintf(stderr, "Unknown host %s.\n", hostname);
    return -1;
  }
  saddr.sin_addr = *(struct in_addr *)hostinfo->h_addr;
  if (0 > connect(sock, (struct sockaddr *)&saddr, sizeof(saddr))) {
    perror("connect");
    return -1;
  }
  n = sizeof(saddr);
  getsockname(sock, (struct sockaddr *)&saddr, &n);
  snprintf(addr, addrlen, "%s", inet_ntoa(saddr.sin_addr));
  shutdown(sock, SHUT_RDWR);
  return 0;
}


x86 assembly (clang, -O0):
<get_local_addr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    0x2e06(%rip),%rax
mov    (%rax),%edi
mov    0x2e05(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
mov    %eax,-0x20(%rbp)
mov    0x2e0a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    $0x16,%edi
call   1050 <htons@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x10(%rbp),%rdi
call   1070 <gethostbyname@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    1225 <get_local_addr+0x85>
mov    0x2dc9(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xdec(%rip),%rsi
call   1080 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12c3 <get_local_addr+0x123>
mov    -0x38(%rbp),%rax
mov    (%rax),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    -0x20(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0xc,%edx
call   10b0 <connect@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
jle    1268 <get_local_addr+0xc8>
lea    0xdbb(%rip),%rdi
call   1090 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12c3 <get_local_addr+0x123>
movl   $0xc,-0x24(%rbp)
mov    -0x20(%rbp),%edi
lea    -0x30(%rbp),%rsi
lea    -0x24(%rbp),%rdx
call   10a0 <getsockname@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x30(%rbp),%edi
call   1030 <inet_ntoa@plt>
mov    -0x48(%rbp),%rdi
mov    -0x3c(%rbp),%esi
mov    %rax,%rcx
lea    0xd74(%rip),%rdx
call   1060 <snprintf@plt>
mov    -0x20(%rbp),%edi
mov    0x2d3b(%rip),%rax
mov    (%rax),%esi
call   1040 <shutdown@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_local_addr>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %edx,%r14d
mov    %rsi,%r15
mov    %rdi,%rbx
mov    0x2e06(%rip),%rax
mov    (%rax),%edi
mov    0x2e05(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
mov    %eax,%ebp
mov    0x2e0b(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
mov    $0x16,%edi
call   1050 <htons@plt>
mov    %eax,0xc(%rsp)
mov    %rbx,%rdi
call   1070 <gethostbyname@plt>
test   %rax,%rax
je     1260 <get_local_addr+0xc0>
mov    (%rax),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
lea    0x8(%rsp),%rsi
mov    %ebp,%edi
mov    $0xc,%edx
call   10b0 <connect@plt>
test   %rax,%rax
js     1288 <get_local_addr+0xe8>
movl   $0xc,0x14(%rsp)
lea    0x8(%rsp),%rsi
lea    0x14(%rsp),%rdx
mov    %ebp,%edi
call   10a0 <getsockname@plt>
mov    0x8(%rsp),%edi
call   1030 <inet_ntoa@plt>
lea    0xddc(%rip),%rdx
mov    %r15,%rdi
mov    %r14d,%esi
mov    %rax,%rcx
call   1060 <snprintf@plt>
mov    0x2d9d(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
call   1040 <shutdown@plt>
xor    %eax,%eax
jmp    127d <get_local_addr+0xdd>
mov    0x2d69(%rip),%rax
mov    (%rax),%edi
lea    0xd90(%rip),%rsi
mov    %rbx,%rdx
call   1080 <fprintf@plt>
mov    $0xffffffff,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd83(%rip),%rdi
call   1090 <perror@plt>
jmp    1278 <get_local_addr+0xd8>

LLVM IR (-O0):
%struct.sockaddr_in = type { %struct.in_addr, i32, i32 }
%struct.in_addr = type { i32 }
%struct.hostent = type { i64 }
%struct.sockaddr = type { i32 }
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@AF_INET = global i32 0
@stderr = global i32 0
@.str = [18 x i8] c"Unknown host %s.\0A\00"
@.str.1 = [8 x i8] c"connect\00"
@.str.2 = [3 x i8] c"%s\00"
@SHUT_RDWR = global i32 0
define i32 @get_local_addr(i8* %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca %struct.sockaddr_in
%11 = alloca %struct.hostent*
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%12 = load i32, i32* @PF_INET
%13 = load i32, i32* @SOCK_STREAM
%14 = call i32 @socket(i32 %12, i32 %13, i32 0)
store i32 %14, i32* %8
%15 = load i32, i32* @AF_INET
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 2
store i32 %15, i32* %16
%17 = call i32 @htons(i32 22)
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 1
store i32 %17, i32* %18
%19 = load i8*, i8** %5
%20 = call %struct.hostent* @gethostbyname(i8* %19)
store %struct.hostent* %20, %struct.hostent** %11
%21 = load %struct.hostent*, %struct.hostent** %11
%22 = icmp eq %struct.hostent* %21, null
br i1 %22, label %23, label %27
23: ; preds = %3
%24 = load i32, i32* @stderr
%25 = load i8*, i8** %5
%26 = call i32 @fprintf(i32 %24, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %25)
store i32 -1, i32* %4
br label %55
27: ; preds = %3
%28 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 0
%29 = load %struct.hostent*, %struct.hostent** %11
%30 = getelementptr inbounds %struct.hostent, %struct.hostent* %29, i32 0, i32 0
%31 = load i64, i64* %30
%32 = inttoptr i64 %31 to %struct.in_addr*
%33 = bitcast %struct.in_addr* %28 to i8*
%34 = bitcast %struct.in_addr* %32 to i8*
%35 = load i32, i32* %8
%36 = bitcast %struct.sockaddr_in* %10 to %struct.sockaddr*
%37 = call i64 @connect(i32 %35, %struct.sockaddr* %36, i32 12)
%38 = icmp sgt i64 0, %37
br i1 %38, label %39, label %41
39: ; preds = %27
%40 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %4
br label %55
41: ; preds = %27
store i32 12, i32* %9
%42 = load i32, i32* %8
%43 = bitcast %struct.sockaddr_in* %10 to %struct.sockaddr*
%44 = call i32 @getsockname(i32 %42, %struct.sockaddr* %43, i32* %9)
%45 = load i8*, i8** %6
%46 = load i32, i32* %7
%47 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 0
%48 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %47, i32 0, i32 0
%49 = load i32, i32* %48
%50 = call i8* @inet_ntoa(i32 %49)
%51 = call i32 @snprintf(i8* %45, i32 %46, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* %50)
%52 = load i32, i32* %8
%53 = load i32, i32* @SHUT_RDWR
%54 = call i32 @shutdown(i32 %52, i32 %53)
store i32 0, i32* %4
br label %55
55: ; preds = %41, %39, %23
%56 = load i32, i32* %4
ret i32 %56
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.in_addr, i32, i32 }
%struct.in_addr = type { i32 }
%struct.hostent = type { i64 }
%struct.sockaddr = type { i32 }
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@AF_INET = global i32 0
@stderr = global i32 0
@.str = [18 x i8] c"Unknown host %s.\0A\00"
@.str.1 = [8 x i8] c"connect\00"
@.str.2 = [3 x i8] c"%s\00"
@SHUT_RDWR = global i32 0
define i32 @get_local_addr(i8* %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca %struct.sockaddr_in
%6 = bitcast i32* %4 to i8*
%7 = bitcast %struct.sockaddr_in* %5 to i8*
%8 = load i32, i32* @PF_INET
%9 = load i32, i32* @SOCK_STREAM
%10 = tail call i32 @socket(i32 %8, i32 %9, i32 0)
%11 = load i32, i32* @AF_INET
%12 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 2
store i32 %11, i32* %12
%13 = tail call i32 @htons(i32 22)
%14 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 1
store i32 %13, i32* %14
%15 = tail call %struct.hostent* @gethostbyname(i8* %0)
%16 = icmp eq %struct.hostent* %15, null
br i1 %16, label %17, label %20
17: ; preds = %3
%18 = load i32, i32* @stderr
%19 = tail call i32 @fprintf(i32 %18, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %38
20: ; preds = %3
%21 = getelementptr inbounds %struct.hostent, %struct.hostent* %15, i64 0, i32 0
%22 = load i64, i64* %21
%23 = inttoptr i64 %22 to i32*
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 0, i32 0
%25 = load i32, i32* %23
store i32 %25, i32* %24
%26 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%27 = call i64 @connect(i32 %10, %struct.sockaddr* %26, i32 12)
%28 = icmp slt i64 %27, 0
br i1 %28, label %29, label %31
29: ; preds = %20
%30 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
br label %38
31: ; preds = %20
store i32 12, i32* %4
%32 = call i32 @getsockname(i32 %10, %struct.sockaddr* %26, i32* %4)
%33 = load i32, i32* %24
%34 = call i8* @inet_ntoa(i32 %33)
%35 = call i32 @snprintf(i8* %1, i32 %2, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* %34)
%36 = load i32, i32* @SHUT_RDWR
%37 = call i32 @shutdown(i32 %10, i32 %36)
br label %38
38: ; preds = %31, %29, %17
%39 = phi i32 [ -1, %17 ], [ -1, %29 ], [ 0, %31 ]
ret i32 %39
}