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

/* Variables and functions */
int /*<<< orphan*/ exit(int);
int gethostname(char *, int);
int /*<<< orphan*/ perror(char *);
int sethostname(char *, int /*<<< orphan*/);
int /*<<< orphan*/ strlen(char *);
int main(int argc, char **argv) {
  int rc;
  char buf[255];
  rc = gethostname(buf, sizeof(buf));
  if (rc != 0) {
    perror("test_sethostname:gethostname");
    exit(2);
  }
  rc = sethostname(buf, strlen(buf));
  if (rc != 0) {
    perror("test_sethostname:sethostname");
    exit(1);
  }
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x130,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x120(%rbp),%rdi
mov    $0xff,%esi
call   1060 <gethostname@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     119d <main+0x4d>
lea    0xe72(%rip),%rdi
call   1050 <perror@plt>
mov    $0x2,%edi
call   1070 <exit@plt>
lea    -0x120(%rbp),%rax
mov    %rax,-0x128(%rbp)
lea    -0x120(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x128(%rbp),%rdi
mov    %eax,%esi
call   1040 <sethostname@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     11e8 <main+0x98>
lea    0xe44(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
xor    %edi,%edi
call   1070 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x100,%rsp
mov    %rsp,%rdi
mov    $0xff,%esi
call   1060 <gethostname@plt>
test   %eax,%eax
jne    1189 <main+0x39>
mov    %rsp,%rbx
mov    %rbx,%rdi
call   1030 <strlen@plt>
mov    %rbx,%rdi
mov    %eax,%esi
call   1040 <sethostname@plt>
test   %eax,%eax
jne    119f <main+0x4f>
xor    %edi,%edi
call   1070 <exit@plt>
lea    0xe70(%rip),%rdi
call   1050 <perror@plt>
mov    $0x2,%edi
call   1070 <exit@plt>
lea    0xe77(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [29 x i8] c"test_sethostname:gethostname\00"
@.str.1 = [29 x i8] c"test_sethostname:sethostname\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca [255 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = getelementptr inbounds [255 x i8], [255 x i8]* %7, i64 0, i64 0
%9 = call i32 @gethostname(i8* %8, i32 255)
store i32 %9, i32* %6
%10 = load i32, i32* %6
%11 = icmp ne i32 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %2
%13 = call i32 @perror(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0))
%14 = call i32 @exit(i32 2)
unreachable
15: ; preds = %2
%16 = getelementptr inbounds [255 x i8], [255 x i8]* %7, i64 0, i64 0
%17 = getelementptr inbounds [255 x i8], [255 x i8]* %7, i64 0, i64 0
%18 = call i32 @strlen(i8* %17)
%19 = call i32 @sethostname(i8* %16, i32 %18)
store i32 %19, i32* %6
%20 = load i32, i32* %6
%21 = icmp ne i32 %20, 0
br i1 %21, label %22, label %25
22: ; preds = %15
%23 = call i32 @perror(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0))
%24 = call i32 @exit(i32 1)
unreachable
25: ; preds = %15
%26 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
@.str = [29 x i8] c"test_sethostname:gethostname\00"
@.str.1 = [29 x i8] c"test_sethostname:sethostname\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [255 x i8]
%4 = getelementptr inbounds [255 x i8], [255 x i8]* %3, i64 0, i64 0
%5 = call i32 @gethostname(i8* %4, i32 255)
%6 = icmp eq i32 %5, 0
br i1 %6, label %10, label %7
7: ; preds = %2
%8 = call i32 @perror(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0))
%9 = call i32 @exit(i32 2)
unreachable
10: ; preds = %2
%11 = call i32 @strlen(i8* %4)
%12 = call i32 @sethostname(i8* %4, i32 %11)
%13 = icmp eq i32 %12, 0
br i1 %13, label %17, label %14
14: ; preds = %10
%15 = call i32 @perror(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0))
%16 = call i32 @exit(i32 1)
unreachable
17: ; preds = %10
%18 = call i32 @exit(i32 0)
unreachable
}