#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*/ nodename;

/* Variables and functions */
scalar_t__ gethostname(char *, int);
char *strchr(char *, char);
int /*<<< orphan*/ strncpy(char *, char *, size_t);
int getdomainname(char *domain, size_t size) {
  char nodename[256];
  char *dot;
  if (gethostname(nodename, sizeof(nodename)) < 0)
    return -1;
  nodename[sizeof(nodename) - 1] = 0;
  if ((dot = strchr(nodename, '.')) == 0)
    dot = ".";
  strncpy(domain, dot + 1, size);
  if (size > 0)
    domain[size - 1] = 0;
  return 0;
}


x86 assembly (clang, -O0):
<getdomainname>:
push   %rbp
mov    %rsp,%rbp
sub    $0x130,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
lea    -0x120(%rbp),%rdi
mov    $0x100,%esi
call   1050 <gethostname@plt>
cmp    $0x0,%rax
jge    116a <getdomainname+0x3a>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d8 <getdomainname+0xa8>
movb   $0x0,-0x21(%rbp)
lea    -0x120(%rbp),%rdi
mov    $0x2e,%esi
call   1040 <strchr@plt>
mov    %rax,-0x128(%rbp)
cmp    $0x0,%rax
jne    119e <getdomainname+0x6e>
lea    0xe69(%rip),%rax
mov    %rax,-0x128(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x128(%rbp),%rsi
add    $0x1,%rsi
mov    -0x18(%rbp),%rdx
call   1030 <strncpy@plt>
cmpq   $0x0,-0x18(%rbp)
jbe    11d1 <getdomainname+0xa1>
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%rcx
sub    $0x1,%rcx
movb   $0x0,(%rax,%rcx,1)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x130,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getdomainname>:
push   %r14
push   %rbx
sub    $0x108,%rsp
mov    %rsi,%rbx
mov    %rdi,%r14
mov    %rsp,%rdi
mov    $0x100,%esi
call   1050 <gethostname@plt>
test   %rax,%rax
js     1193 <getdomainname+0x63>
movb   $0x0,0xff(%rsp)
mov    %rsp,%rdi
mov    $0x2e,%esi
call   1040 <strchr@plt>
test   %rax,%rax
lea    0xe8f(%rip),%rsi
cmovne %rax,%rsi
add    $0x1,%rsi
mov    %r14,%rdi
mov    %rbx,%rdx
call   1030 <strncpy@plt>
xor    %eax,%eax
test   %rbx,%rbx
je     1198 <getdomainname+0x68>
movb   $0x0,-0x1(%rbx,%r14,1)
jmp    1198 <getdomainname+0x68>
mov    $0xffffffff,%eax
add    $0x108,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [2 x i8] c".\00"
define i32 @getdomainname(i8* %0, i64 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i64
%6 = alloca [256 x i8]
%7 = alloca i8*
store i8* %0, i8** %4
store i64 %1, i64* %5
%8 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%9 = call i64 @gethostname(i8* %8, i32 256)
%10 = icmp slt i64 %9, 0
br i1 %10, label %11, label %12
11: ; preds = %2
store i32 -1, i32* %3
br label %32
12: ; preds = %2
%13 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 255
store i8 0, i8* %13
%14 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%15 = call i8* @strchr(i8* %14, i8 signext 46)
store i8* %15, i8** %7
%16 = icmp eq i8* %15, null
br i1 %16, label %17, label %18
17: ; preds = %12
store i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i8** %7
br label %18
18: ; preds = %17, %12
%19 = load i8*, i8** %4
%20 = load i8*, i8** %7
%21 = getelementptr inbounds i8, i8* %20, i64 1
%22 = load i64, i64* %5
%23 = call i32 @strncpy(i8* %19, i8* %21, i64 %22)
%24 = load i64, i64* %5
%25 = icmp ugt i64 %24, 0
br i1 %25, label %26, label %31
26: ; preds = %18
%27 = load i8*, i8** %4
%28 = load i64, i64* %5
%29 = sub i64 %28, 1
%30 = getelementptr inbounds i8, i8* %27, i64 %29
store i8 0, i8* %30
br label %31
31: ; preds = %26, %18
store i32 0, i32* %3
br label %32
32: ; preds = %31, %11
%33 = load i32, i32* %3
ret i32 %33
}

LLVM IR (-O2):
@.str = [2 x i8] c".\00"
define i32 @getdomainname(i8* %0, i64 %1) {
%3 = alloca [256 x i8]
%4 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%5 = call i64 @gethostname(i8* %4, i32 256)
%6 = icmp slt i64 %5, 0
br i1 %6, label %18, label %7
7: ; preds = %2
%8 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 255
store i8 0, i8* %8
%9 = call i8* @strchr(i8* %4, i8 signext 46)
%10 = icmp eq i8* %9, null
%11 = select i1 %10, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i8* %9
%12 = getelementptr inbounds i8, i8* %11, i64 1
%13 = call i32 @strncpy(i8* %0, i8* %12, i64 %1)
%14 = icmp eq i64 %1, 0
br i1 %14, label %18, label %15
15: ; preds = %7
%16 = add i64 %1, -1
%17 = getelementptr inbounds i8, i8* %0, i64 %16
store i8 0, i8* %17
br label %18
18: ; preds = %7, %15, %2
%19 = phi i32 [ -1, %2 ], [ 0, %15 ], [ 0, %7 ]
ret i32 %19
}