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

/* Variables and functions */
int /*<<< orphan*/ bail(char *);
int /*<<< orphan*/ free(char *);
int gethostname(char *, int);
scalar_t__ malloc(int);
int /*<<< orphan*/ sprintf(char *, char *, char const *, char const *,
                           char const *, char *, char const *);
int system(char *);
int reconfigure_hosts(const char *path, const char *ip) {
  int retval = -1;
  char *hostname = (char *)malloc(sizeof(char) * 256);
  char *command = (char *)malloc(sizeof(char) * 1024);
  retval = gethostname(hostname, 256 - 1);
  if (!retval) {
    sprintf(command,
            "echo \"127.0.0.1\tlocalhost\" > %s;echo -n %s >> %s;echo \"\t%s\" "
            ">> %s ",
            path, ip, path, hostname, path);
    retval = system(command);
    if (retval != -1) {
      retval = 0;
    } else {
      bail("Error reconfiguring hosts file");
      retval = -1;
    }
  } else {
    retval = -1;
    bail("Error using gethostname(2).");
  }
  free(hostname);
  free(command);
  return retval;
}


x86 assembly (clang, -O0):
<reconfigure_hosts>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0xffffffff,-0x14(%rbp)
mov    $0x100,%edi
call   1050 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    $0x400,%edi
call   1050 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rdi
mov    $0xff,%esi
call   1070 <gethostname@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jne    1210 <reconfigure_hosts+0xb0>
mov    -0x28(%rbp),%rdi
mov    -0x8(%rbp),%rdx
mov    -0x10(%rbp),%rcx
mov    -0x8(%rbp),%r8
mov    -0x20(%rbp),%r9
mov    -0x8(%rbp),%rax
lea    0xe33(%rip),%rsi
mov    %rax,(%rsp)
call   1080 <sprintf@plt>
mov    -0x28(%rbp),%rdi
call   1040 <system@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
je     11f8 <reconfigure_hosts+0x98>
movl   $0x0,-0x14(%rbp)
jmp    120b <reconfigure_hosts+0xab>
lea    0xe44(%rip),%rdi
call   1060 <bail@plt>
movl   $0xffffffff,-0x14(%rbp)
jmp    1223 <reconfigure_hosts+0xc3>
movl   $0xffffffff,-0x14(%rbp)
lea    0xe44(%rip),%rdi
call   1060 <bail@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x14(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<reconfigure_hosts>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r12
mov    %rdi,%r15
mov    $0x100,%edi
call   1050 <malloc@plt>
mov    %rax,%rbx
mov    $0x400,%edi
call   1050 <malloc@plt>
mov    %rax,%r14
mov    %rbx,%rdi
mov    $0xff,%esi
call   1070 <gethostname@plt>
test   %eax,%eax
je     11b0 <reconfigure_hosts+0x50>
lea    0xebe(%rip),%rdi
call   1060 <bail@plt>
mov    $0xffffffff,%ebp
jmp    11de <reconfigure_hosts+0x7e>
mov    %r15,(%rsp)
lea    0xe45(%rip),%rsi
mov    %r14,%rdi
mov    %r15,%rdx
mov    %r12,%rcx
mov    %r15,%r8
mov    %rbx,%r9
call   1080 <sprintf@plt>
mov    %r14,%rdi
call   1040 <system@plt>
xor    %ebp,%ebp
cmp    $0xffffffff,%eax
je     11fd <reconfigure_hosts+0x9d>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %r14,%rdi
call   1030 <free@plt>
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe3f(%rip),%rdi
jmp    11a4 <reconfigure_hosts+0x44>

LLVM IR (-O0):
@.str = [67 x i8] c"echo \22127.0.0.1\09localhost\22 > %s;echo -n %s >> %s;echo \22\09%s\22 >> %s \00"
@.str.1 = [31 x i8] c"Error reconfiguring hosts file\00"
@.str.2 = [28 x i8] c"Error using gethostname(2).\00"
define i32 @reconfigure_hosts(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
store i8* %0, i8** %3
store i8* %1, i8** %4
store i32 -1, i32* %5
%8 = call i64 @malloc(i32 256)
%9 = inttoptr i64 %8 to i8*
store i8* %9, i8** %6
%10 = call i64 @malloc(i32 1024)
%11 = inttoptr i64 %10 to i8*
store i8* %11, i8** %7
%12 = load i8*, i8** %6
%13 = call i32 @gethostname(i8* %12, i32 255)
store i32 %13, i32* %5
%14 = load i32, i32* %5
%15 = icmp ne i32 %14, 0
br i1 %15, label %32, label %16
16: ; preds = %2
%17 = load i8*, i8** %7
%18 = load i8*, i8** %3
%19 = load i8*, i8** %4
%20 = load i8*, i8** %3
%21 = load i8*, i8** %6
%22 = load i8*, i8** %3
%23 = call i32 @sprintf(i8* %17, i8* getelementptr inbounds ([67 x i8], [67 x i8]* @.str, i64 0, i64 0), i8* %18, i8* %19, i8* %20, i8* %21, i8* %22)
%24 = load i8*, i8** %7
%25 = call i32 @system(i8* %24)
store i32 %25, i32* %5
%26 = load i32, i32* %5
%27 = icmp ne i32 %26, -1
br i1 %27, label %28, label %29
28: ; preds = %16
store i32 0, i32* %5
br label %31
29: ; preds = %16
%30 = call i32 @bail(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %5
br label %31
31: ; preds = %29, %28
br label %34
32: ; preds = %2
store i32 -1, i32* %5
%33 = call i32 @bail(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0))
br label %34
34: ; preds = %32, %31
%35 = load i8*, i8** %6
%36 = call i32 @free(i8* %35)
%37 = load i8*, i8** %7
%38 = call i32 @free(i8* %37)
%39 = load i32, i32* %5
ret i32 %39
}

LLVM IR (-O2):
@.str = [67 x i8] c"echo \22127.0.0.1\09localhost\22 > %s;echo -n %s >> %s;echo \22\09%s\22 >> %s \00"
@.str.1 = [31 x i8] c"Error reconfiguring hosts file\00"
@.str.2 = [28 x i8] c"Error using gethostname(2).\00"
define i32 @reconfigure_hosts(i8* %0, i8* %1) {
%3 = tail call i64 @malloc(i32 256)
%4 = inttoptr i64 %3 to i8*
%5 = tail call i64 @malloc(i32 1024)
%6 = inttoptr i64 %5 to i8*
%7 = tail call i32 @gethostname(i8* %4, i32 255)
%8 = icmp eq i32 %7, 0
br i1 %8, label %9, label %13
9: ; preds = %2
%10 = tail call i32 @sprintf(i8* dereferenceable(1) %6, i8* dereferenceable(1) getelementptr inbounds ([67 x i8], [67 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %1, i8* %0, i8* %4, i8* %0)
%11 = tail call i32 @system(i8* %6)
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %16
13: ; preds = %2, %9
%14 = phi i8* [ getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0), %9 ], [ getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0), %2 ]
%15 = tail call i32 @bail(i8* %14)
br label %16
16: ; preds = %13, %9
%17 = phi i32 [ 0, %9 ], [ -1, %13 ]
%18 = tail call i32 @free(i8* %4)
%19 = tail call i32 @free(i8* %6)
ret i32 %17
}