#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 addrinfo {
  char const *ai_canonname;
  int /*<<< orphan*/ ai_family;
  int /*<<< orphan*/ ai_flags;
};

/* Variables and functions */
int /*<<< orphan*/ AF_UNSPEC;
int /*<<< orphan*/ AI_CANONNAME;
int /*<<< orphan*/ _POSIX_HOST_NAME_MAX;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
scalar_t__ getaddrinfo(char const *, int /*<<< orphan*/ *, struct addrinfo *,
                       struct addrinfo **);
scalar_t__ gethostname(char *, int);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
int /*<<< orphan*/ puts(char const *);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  char buf[_POSIX_HOST_NAME_MAX + 1];
  const char *hostname;
  struct addrinfo hints, *res;
  if (argc > 2) {
    fprintf(stderr, "usage: %s [hostname]\n", argv[0]);
    return 1;
  }
  if (argc == 2)
    hostname = argv[1];
  else {
    if (gethostname(buf, sizeof buf)) {
      fprintf(stderr, "gethostname() failed: %s\n", strerror(errno));
      return 1;
    }
    hostname = buf;
  }
  memset(&hints, 0, sizeof hints);
  hints.ai_flags = AI_CANONNAME;
  hints.ai_family = AF_UNSPEC;
  if (getaddrinfo(hostname, NULL, &hints, &res)) {
    puts(hostname);
    return 1;
  }
  puts(res->ai_canonname);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e3b(%rip),%rax
mov    (%rax),%eax
add    $0x1,%eax
mov    %rax,-0x58(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x18(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x50(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x20(%rbp)
cmpl   $0x2,-0x8(%rbp)
jle    11e2 <main+0x82>
mov    0x2e16(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe36(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
movl   $0x1,-0x44(%rbp)
jmp    12da <main+0x17a>
cmpl   $0x2,-0x8(%rbp)
jne    11fd <main+0x9d>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x28(%rbp)
jmp    125d <main+0xfd>
mov    -0x50(%rbp),%rdi
mov    -0x58(%rbp),%rax
mov    %eax,%esi
call   1060 <gethostname@plt>
cmp    $0x0,%rax
je     1255 <main+0xf5>
mov    0x2db3(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x5c(%rbp)
mov    0x2d9f(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x5c(%rbp),%edi
mov    %rax,%rdx
lea    0xdd9(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
movl   $0x1,-0x44(%rbp)
jmp    12da <main+0x17a>
mov    -0x50(%rbp),%rax
mov    %rax,-0x28(%rbp)
lea    -0x38(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1040 <memset@plt>
mov    0x2d6c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    0x2d68(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    -0x28(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
lea    -0x38(%rbp),%rdx
lea    -0x40(%rbp),%rcx
call   1070 <getaddrinfo@plt>
cmp    $0x0,%rax
je     12c0 <main+0x160>
mov    -0x28(%rbp),%rdi
call   1030 <puts@plt>
movl   $0x1,-0x4(%rbp)
movl   $0x1,-0x44(%rbp)
jmp    12da <main+0x17a>
mov    -0x40(%rbp),%rax
mov    (%rax),%rdi
call   1030 <puts@plt>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x44(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    0x2e44(%rip),%rax
mov    (%rax),%eax
add    $0x1,%eax
mov    %rsp,%rbx
lea    0xf(%rax),%rcx
and    $0xfffffffffffffff0,%rcx
sub    %rcx,%rbx
mov    %rbx,%rsp
cmp    $0x3,%edi
jl     11a4 <main+0x44>
mov    0x2e3a(%rip),%rax
mov    (%rax),%edi
mov    (%rsi),%rdx
lea    0xe5e(%rip),%rsi
jmp    11e1 <main+0x81>
cmp    $0x2,%edi
jne    11af <main+0x4f>
mov    0x8(%rsi),%rbx
jmp    11ee <main+0x8e>
mov    %rbx,%rdi
mov    %eax,%esi
call   1060 <gethostname@plt>
test   %rax,%rax
je     11ee <main+0x8e>
mov    0x2e0b(%rip),%rax
mov    (%rax),%ebx
mov    0x2dfa(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xe3a(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1050 <fprintf@plt>
mov    $0x1,%r14d
jmp    124e <main+0xee>
lea    -0x30(%rbp),%r15
xor    %r14d,%r14d
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1040 <memset@plt>
mov    0x2dd5(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    0x2dd1(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
lea    -0x20(%rbp),%rcx
mov    %rbx,%rdi
xor    %esi,%esi
mov    %r15,%rdx
call   1070 <getaddrinfo@plt>
test   %rax,%rax
je     1242 <main+0xe2>
mov    %rbx,%rdi
call   1030 <puts@plt>
mov    $0x1,%r14d
jmp    124e <main+0xee>
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
call   1030 <puts@plt>
mov    %r14d,%eax
lea    -0x18(%rbp),%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.addrinfo = type { i8*, i32, i32 }
@_POSIX_HOST_NAME_MAX = global i32 0
@stderr = global i32 0
@.str = [22 x i8] c"usage: %s [hostname]\0A\00"
@.str.1 = [26 x i8] c"gethostname() failed: %s\0A\00"
@errno = global i32 0
@AI_CANONNAME = global i32 0
@AF_UNSPEC = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i8*
%9 = alloca %struct.addrinfo
%10 = alloca %struct.addrinfo*
%11 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = load i32, i32* @_POSIX_HOST_NAME_MAX
%13 = add nsw i32 %12, 1
%14 = zext i32 %13 to i64
%15 = call i8* @llvm.stacksave()
store i8* %15, i8** %6
%16 = alloca i8, i64 %14
store i64 %14, i64* %7
%17 = load i32, i32* %4
%18 = icmp sgt i32 %17, 2
br i1 %18, label %19, label %25
19: ; preds = %2
%20 = load i32, i32* @stderr
%21 = load i8**, i8*** %5
%22 = getelementptr inbounds i8*, i8** %21, i64 0
%23 = load i8*, i8** %22
%24 = call i32 @fprintf(i32 %20, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* %23)
store i32 1, i32* %3
store i32 1, i32* %11
br label %59
25: ; preds = %2
%26 = load i32, i32* %4
%27 = icmp eq i32 %26, 2
br i1 %27, label %28, label %32
28: ; preds = %25
%29 = load i8**, i8*** %5
%30 = getelementptr inbounds i8*, i8** %29, i64 1
%31 = load i8*, i8** %30
store i8* %31, i8** %8
br label %42
32: ; preds = %25
%33 = trunc i64 %14 to i32
%34 = call i64 @gethostname(i8* %16, i32 %33)
%35 = icmp ne i64 %34, 0
br i1 %35, label %36, label %41
36: ; preds = %32
%37 = load i32, i32* @stderr
%38 = load i32, i32* @errno
%39 = call i8* @strerror(i32 %38)
%40 = call i32 @fprintf(i32 %37, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i8* %39)
store i32 1, i32* %3
store i32 1, i32* %11
br label %59
41: ; preds = %32
store i8* %16, i8** %8
br label %42
42: ; preds = %41, %28
%43 = call i32 @memset(%struct.addrinfo* %9, i32 0, i32 16)
%44 = load i32, i32* @AI_CANONNAME
%45 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %9, i32 0, i32 2
store i32 %44, i32* %45
%46 = load i32, i32* @AF_UNSPEC
%47 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %9, i32 0, i32 1
store i32 %46, i32* %47
%48 = load i8*, i8** %8
%49 = call i64 @getaddrinfo(i8* %48, i32* null, %struct.addrinfo* %9, %struct.addrinfo** %10)
%50 = icmp ne i64 %49, 0
br i1 %50, label %51, label %54
51: ; preds = %42
%52 = load i8*, i8** %8
%53 = call i32 @puts(i8* %52)
store i32 1, i32* %3
store i32 1, i32* %11
br label %59
54: ; preds = %42
%55 = load %struct.addrinfo*, %struct.addrinfo** %10
%56 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %55, i32 0, i32 0
%57 = load i8*, i8** %56
%58 = call i32 @puts(i8* %57)
store i32 0, i32* %3
store i32 1, i32* %11
br label %59
59: ; preds = %54, %51, %36, %19
%60 = load i8*, i8** %6
%61 = load i32, i32* %3
ret i32 %61
}

LLVM IR (-O2):
%struct.addrinfo = type { i8*, i32, i32 }
@_POSIX_HOST_NAME_MAX = global i32 0
@stderr = global i32 0
@.str = [22 x i8] c"usage: %s [hostname]\0A\00"
@.str.1 = [26 x i8] c"gethostname() failed: %s\0A\00"
@errno = global i32 0
@AI_CANONNAME = global i32 0
@AF_UNSPEC = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.addrinfo
%4 = alloca %struct.addrinfo*
%5 = load i32, i32* @_POSIX_HOST_NAME_MAX
%6 = add nsw i32 %5, 1
%7 = zext i32 %6 to i64
%8 = alloca i8, i64 %7
%9 = bitcast %struct.addrinfo* %3 to i8*
%10 = bitcast %struct.addrinfo** %4 to i8*
%11 = icmp sgt i32 %0, 2
br i1 %11, label %12, label %16
12: ; preds = %2
%13 = load i32, i32* @stderr
%14 = load i8*, i8** %1
%15 = tail call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* %14)
br label %45
16: ; preds = %2
%17 = icmp eq i32 %0, 2
br i1 %17, label %18, label %21
18: ; preds = %16
%19 = getelementptr inbounds i8*, i8** %1, i64 1
%20 = load i8*, i8** %19
br label %29
21: ; preds = %16
%22 = call i64 @gethostname(i8* %8, i32 %6)
%23 = icmp eq i64 %22, 0
br i1 %23, label %29, label %24
24: ; preds = %21
%25 = load i32, i32* @stderr
%26 = load i32, i32* @errno
%27 = call i8* @strerror(i32 %26)
%28 = call i32 @fprintf(i32 %25, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i8* %27)
br label %45
29: ; preds = %21, %18
%30 = phi i8* [ %20, %18 ], [ %8, %21 ]
%31 = call i32 @memset(%struct.addrinfo* %3, i32 0, i32 16)
%32 = load i32, i32* @AI_CANONNAME
%33 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 2
store i32 %32, i32* %33
%34 = load i32, i32* @AF_UNSPEC
%35 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 1
store i32 %34, i32* %35
%36 = call i64 @getaddrinfo(i8* %30, i32* null, %struct.addrinfo* %3, %struct.addrinfo** %4)
%37 = icmp eq i64 %36, 0
br i1 %37, label %40, label %38
38: ; preds = %29
%39 = call i32 @puts(i8* dereferenceable(1) %30)
br label %45
40: ; preds = %29
%41 = load %struct.addrinfo*, %struct.addrinfo** %4
%42 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %41, i64 0, i32 0
%43 = load i8*, i8** %42
%44 = call i32 @puts(i8* dereferenceable(1) %43)
br label %45
45: ; preds = %40, %38, %24, %12
%46 = phi i32 [ 1, %12 ], [ 1, %38 ], [ 0, %40 ], [ 1, %24 ]
ret i32 %46
}