#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*/ name;
typedef int /*<<< orphan*/ hosts;

/* Variables and functions */
int /*<<< orphan*/ AT_EACCESS;
int /*<<< orphan*/ F_OK;
int O_CREAT;
int O_WRONLY;
scalar_t__ close(int);
int /*<<< orphan*/ errno;
scalar_t__ faccessat(int, char *, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ get_machine_name(char *, int);
int openat(int, char *, int, int);
int snprintf(char *, int, char *, char *, char *, char *, char *);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int write(int, char *, int);
int ensure_etc_hosts_exists(const char *root, int rootfd) {
  char name[(sizeof("rkt-01234567-89ab-cdef-0123-456789abcdef") - 1) + 1];
  char hosts[128];
  int fd, len;
  if (faccessat(rootfd, "etc/hosts", F_OK, AT_EACCESS) == 0)
    return 1;
  if (!get_machine_name(name, sizeof(name))) {
    fprintf(stderr, "Error: "
                    "Failed to get machine name"
                    "\n");
    ;
    goto _fail;
  };
  if ((len = snprintf(hosts, sizeof(hosts), "%s\t%s\t%s\t%s\n", "127.0.0.1",
                      name, "localhost", "localhost.localdomain")) >=
      sizeof(hosts)) {
    fprintf(stderr,
            "Error: "
            "/etc/hosts line too long: \"%s\""
            "\n",
            hosts);
    ;
    goto _fail;
  };
  if ((fd = openat(rootfd, "etc/hosts", O_WRONLY | O_CREAT, 0644)) == -1) {
    fprintf(stderr,
            "Error: "
            "Failed to create \"%s/etc/hosts\""
            ": %s"
            "\n",
            root, strerror(errno));
    ;
    goto _fail;
  };
  ;
  if (write(fd, hosts, len) != len) {
    fprintf(stderr,
            "Error: "
            "Failed to write \"%s/etc/hosts\""
            ": %s"
            "\n",
            root, strerror(errno));
    ;
    goto _fail_fd;
  };
  ;
  if (close(fd) != 0) {
    fprintf(stderr,
            "Error: "
            "Failed to close \"%s/etc/hosts\""
            ": %s"
            "\n",
            root, strerror(errno));
    ;
    goto _fail;
  };
  ;
  return 1;
_fail_fd:
  close(fd);
_fail:
  return 0;
}


x86 assembly (clang, -O0):
<ensure_etc_hosts_exists>:
push   %rbp
mov    %rsp,%rbp
sub    $0x100,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x14(%rbp),%edi
mov    0x2e14(%rip),%rax
mov    (%rax),%edx
mov    0x2e23(%rip),%rax
mov    (%rax),%ecx
lea    0xe52(%rip),%rsi
call   1030 <faccessat@plt>
cmp    $0x0,%rax
jne    11c9 <ensure_etc_hosts_exists+0x49>
movl   $0x1,-0x4(%rbp)
jmp    13d5 <ensure_etc_hosts_exists+0x255>
lea    -0x40(%rbp),%rdi
mov    $0x29,%esi
call   1070 <get_machine_name@plt>
cmp    $0x0,%eax
jne    11fc <ensure_etc_hosts_exists+0x7c>
mov    0x2df1(%rip),%rax
mov    (%rax),%edi
lea    0xe1a(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
jmp    13ce <ensure_etc_hosts_exists+0x24e>
lea    -0xc0(%rbp),%rdi
lea    -0x40(%rbp),%r8
mov    $0x80,%esi
lea    0xe1a(%rip),%rdx
lea    0xe20(%rip),%rcx
lea    0xe23(%rip),%r9
lea    0xe26(%rip),%rax
mov    %rax,(%rsp)
call   1060 <snprintf@plt>
mov    %eax,-0xc8(%rbp)
cltq
cmp    $0x80,%rax
jb     1268 <ensure_etc_hosts_exists+0xe8>
mov    0x2d8c(%rip),%rax
mov    (%rax),%edi
lea    -0xc0(%rbp),%rdx
lea    0xe08(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
jmp    13ce <ensure_etc_hosts_exists+0x24e>
mov    -0x14(%rbp),%edi
mov    0x2d46(%rip),%rax
mov    (%rax),%edx
mov    0x2d7d(%rip),%rax
or     (%rax),%edx
lea    0xd7c(%rip),%rsi
mov    $0x1a4,%ecx
call   1050 <openat@plt>
mov    %eax,-0xc4(%rbp)
cmp    $0xffffffff,%eax
jne    12e8 <ensure_etc_hosts_exists+0x168>
mov    0x2d34(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xd4(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0xd0(%rbp)
mov    0x2d12(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0xd4(%rbp),%edi
mov    -0xd0(%rbp),%rdx
mov    %rax,%rcx
lea    0xdaf(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
jmp    13ce <ensure_etc_hosts_exists+0x24e>
mov    -0xc4(%rbp),%edi
lea    -0xc0(%rbp),%rsi
mov    -0xc8(%rbp),%edx
call   1040 <write@plt>
cmp    -0xc8(%rbp),%eax
je     1357 <ensure_etc_hosts_exists+0x1d7>
mov    0x2cc5(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xe4(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0xe0(%rbp)
mov    0x2ca3(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0xe4(%rbp),%edi
mov    -0xe0(%rbp),%rdx
mov    %rax,%rcx
lea    0xd6c(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
jmp    13c3 <ensure_etc_hosts_exists+0x243>
mov    -0xc4(%rbp),%edi
call   1080 <close@plt>
cmp    $0x0,%rax
je     13b7 <ensure_etc_hosts_exists+0x237>
mov    0x2c65(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xf4(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0xf0(%rbp)
mov    0x2c43(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0xf4(%rbp),%edi
mov    -0xf0(%rbp),%rdx
mov    %rax,%rcx
lea    0xd37(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
jmp    13ce <ensure_etc_hosts_exists+0x24e>
movl   $0x1,-0x4(%rbp)
jmp    13d5 <ensure_etc_hosts_exists+0x255>
mov    -0xc4(%rbp),%edi
call   1080 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x100,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ensure_etc_hosts_exists>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0xc8,%rsp
mov    %esi,%ebx
mov    %rdi,%r14
mov    0x2e17(%rip),%rax
mov    (%rax),%edx
mov    0x2e26(%rip),%rax
mov    (%rax),%ecx
lea    0xe55(%rip),%rsi
mov    %ebx,%edi
call   1030 <faccessat@plt>
mov    $0x1,%r15d
test   %rax,%rax
je     132b <ensure_etc_hosts_exists+0x1ab>
lea    0x10(%rsp),%rdi
mov    $0x29,%esi
call   1070 <get_machine_name@plt>
test   %eax,%eax
je     1233 <ensure_etc_hosts_exists+0xb3>
lea    0xe73(%rip),%rax
mov    %rax,(%rsp)
lea    0xe47(%rip),%rdx
lea    0xe4d(%rip),%rcx
lea    0xe50(%rip),%r9
lea    0x40(%rsp),%rdi
lea    0x10(%rsp),%r8
mov    $0x80,%esi
call   1060 <snprintf@plt>
cmp    $0x80,%eax
jb     1252 <ensure_etc_hosts_exists+0xd2>
mov    0x2dc2(%rip),%rax
mov    (%rax),%edi
lea    0xe45(%rip),%rsi
xor    %r15d,%r15d
lea    0x40(%rsp),%rdx
xor    %eax,%eax
call   1090 <fprintf@plt>
jmp    132b <ensure_etc_hosts_exists+0x1ab>
mov    0x2d9e(%rip),%rax
mov    (%rax),%edi
lea    0xdc7(%rip),%rsi
xor    %r15d,%r15d
xor    %eax,%eax
call   1090 <fprintf@plt>
jmp    132b <ensure_etc_hosts_exists+0x1ab>
mov    %eax,%ebp
mov    0x2d5d(%rip),%rax
mov    0x2d96(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
lea    0xd93(%rip),%rsi
mov    %ebx,%edi
mov    $0x1a4,%ecx
call   1050 <openat@plt>
cmp    $0xffffffff,%eax
je     12c2 <ensure_etc_hosts_exists+0x142>
mov    %eax,%ebx
lea    0x40(%rsp),%rsi
mov    %eax,%edi
mov    %ebp,%edx
call   1040 <write@plt>
cmp    %ebp,%eax
jne    12f4 <ensure_etc_hosts_exists+0x174>
mov    %ebx,%edi
call   1080 <close@plt>
test   %rax,%rax
je     132b <ensure_etc_hosts_exists+0x1ab>
mov    0x2d2f(%rip),%rax
mov    (%rax),%ebx
mov    0x2d1e(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xe22(%rip),%rsi
jmp    12e0 <ensure_etc_hosts_exists+0x160>
mov    0x2d0f(%rip),%rax
mov    (%rax),%ebx
mov    0x2cfe(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xdab(%rip),%rsi
xor    %r15d,%r15d
mov    %ebx,%edi
mov    %r14,%rdx
mov    %rax,%rcx
xor    %eax,%eax
call   1090 <fprintf@plt>
jmp    132b <ensure_etc_hosts_exists+0x1ab>
mov    0x2cdd(%rip),%rax
mov    (%rax),%ebp
mov    0x2ccc(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xda5(%rip),%rsi
xor    %r15d,%r15d
mov    %ebp,%edi
mov    %r14,%rdx
mov    %rax,%rcx
xor    %eax,%eax
call   1090 <fprintf@plt>
mov    %ebx,%edi
call   1080 <close@plt>
mov    %r15d,%eax
add    $0xc8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [10 x i8] c"etc/hosts\00"
@F_OK = global i32 0
@AT_EACCESS = global i32 0
@stderr = global i32 0
@.str.1 = [35 x i8] c"Error: Failed to get machine name\0A\00"
@.str.2 = [13 x i8] c"%s\09%s\09%s\09%s\0A\00"
@.str.3 = [10 x i8] c"127.0.0.1\00"
@.str.4 = [10 x i8] c"localhost\00"
@.str.5 = [22 x i8] c"localhost.localdomain\00"
@.str.6 = [39 x i8] c"Error: /etc/hosts line too long: \22%s\22\0A\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@.str.7 = [44 x i8] c"Error: Failed to create \22%s/etc/hosts\22: %s\0A\00"
@errno = global i32 0
@.str.8 = [43 x i8] c"Error: Failed to write \22%s/etc/hosts\22: %s\0A\00"
@.str.9 = [43 x i8] c"Error: Failed to close \22%s/etc/hosts\22: %s\0A\00"
define i32 @ensure_etc_hosts_exists(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca [41 x i8]
%7 = alloca [128 x i8]
%8 = alloca i32
%9 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
%10 = load i32, i32* %5
%11 = load i32, i32* @F_OK
%12 = load i32, i32* @AT_EACCESS
%13 = call i64 @faccessat(i32 %10, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %11, i32 %12)
%14 = icmp eq i64 %13, 0
br i1 %14, label %15, label %16
15: ; preds = %2
store i32 1, i32* %3
br label %74
16: ; preds = %2
%17 = getelementptr inbounds [41 x i8], [41 x i8]* %6, i64 0, i64 0
%18 = call i32 @get_machine_name(i8* %17, i32 41)
%19 = icmp ne i32 %18, 0
br i1 %19, label %23, label %20
20: ; preds = %16
%21 = load i32, i32* @stderr
%22 = call i32 (i32, i8*, ...) @fprintf(i32 %21, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0))
br label %73
23: ; preds = %16
%24 = getelementptr inbounds [128 x i8], [128 x i8]* %7, i64 0, i64 0
%25 = getelementptr inbounds [41 x i8], [41 x i8]* %6, i64 0, i64 0
%26 = call i32 @snprintf(i8* %24, i32 128, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i8* %25, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.5, i64 0, i64 0))
store i32 %26, i32* %9
%27 = sext i32 %26 to i64
%28 = icmp uge i64 %27, 128
br i1 %28, label %29, label %33
29: ; preds = %23
%30 = load i32, i32* @stderr
%31 = getelementptr inbounds [128 x i8], [128 x i8]* %7, i64 0, i64 0
%32 = call i32 (i32, i8*, ...) @fprintf(i32 %30, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.6, i64 0, i64 0), i8* %31)
br label %73
33: ; preds = %23
%34 = load i32, i32* %5
%35 = load i32, i32* @O_WRONLY
%36 = load i32, i32* @O_CREAT
%37 = or i32 %35, %36
%38 = call i32 @openat(i32 %34, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %37, i32 420)
store i32 %38, i32* %8
%39 = icmp eq i32 %38, -1
br i1 %39, label %40, label %46
40: ; preds = %33
%41 = load i32, i32* @stderr
%42 = load i8*, i8** %4
%43 = load i32, i32* @errno
%44 = call i8* @strerror(i32 %43)
%45 = call i32 (i32, i8*, ...) @fprintf(i32 %41, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.7, i64 0, i64 0), i8* %42, i8* %44)
br label %73
46: ; preds = %33
%47 = load i32, i32* %8
%48 = getelementptr inbounds [128 x i8], [128 x i8]* %7, i64 0, i64 0
%49 = load i32, i32* %9
%50 = call i32 @write(i32 %47, i8* %48, i32 %49)
%51 = load i32, i32* %9
%52 = icmp ne i32 %50, %51
br i1 %52, label %53, label %59
53: ; preds = %46
%54 = load i32, i32* @stderr
%55 = load i8*, i8** %4
%56 = load i32, i32* @errno
%57 = call i8* @strerror(i32 %56)
%58 = call i32 (i32, i8*, ...) @fprintf(i32 %54, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.8, i64 0, i64 0), i8* %55, i8* %57)
br label %70
59: ; preds = %46
%60 = load i32, i32* %8
%61 = call i64 @close(i32 %60)
%62 = icmp ne i64 %61, 0
br i1 %62, label %63, label %69
63: ; preds = %59
%64 = load i32, i32* @stderr
%65 = load i8*, i8** %4
%66 = load i32, i32* @errno
%67 = call i8* @strerror(i32 %66)
%68 = call i32 (i32, i8*, ...) @fprintf(i32 %64, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.9, i64 0, i64 0), i8* %65, i8* %67)
br label %73
69: ; preds = %59
store i32 1, i32* %3
br label %74
70: ; preds = %53
%71 = load i32, i32* %8
%72 = call i64 @close(i32 %71)
br label %73
73: ; preds = %70, %63, %40, %29, %20
store i32 0, i32* %3
br label %74
74: ; preds = %73, %69, %15
%75 = load i32, i32* %3
ret i32 %75
}

LLVM IR (-O2):
@.str = [10 x i8] c"etc/hosts\00"
@F_OK = global i32 0
@AT_EACCESS = global i32 0
@stderr = global i32 0
@.str.1 = [35 x i8] c"Error: Failed to get machine name\0A\00"
@.str.2 = [13 x i8] c"%s\09%s\09%s\09%s\0A\00"
@.str.3 = [10 x i8] c"127.0.0.1\00"
@.str.4 = [10 x i8] c"localhost\00"
@.str.5 = [22 x i8] c"localhost.localdomain\00"
@.str.6 = [39 x i8] c"Error: /etc/hosts line too long: \22%s\22\0A\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@.str.7 = [44 x i8] c"Error: Failed to create \22%s/etc/hosts\22: %s\0A\00"
@errno = global i32 0
@.str.8 = [43 x i8] c"Error: Failed to write \22%s/etc/hosts\22: %s\0A\00"
@.str.9 = [43 x i8] c"Error: Failed to close \22%s/etc/hosts\22: %s\0A\00"
define i32 @ensure_etc_hosts_exists(i8* %0, i32 %1) {
%3 = alloca [41 x i8]
%4 = alloca [128 x i8]
%5 = getelementptr inbounds [41 x i8], [41 x i8]* %3, i64 0, i64 0
%6 = getelementptr inbounds [128 x i8], [128 x i8]* %4, i64 0, i64 0
%7 = load i32, i32* @F_OK
%8 = load i32, i32* @AT_EACCESS
%9 = tail call i64 @faccessat(i32 %1, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %7, i32 %8)
%10 = icmp eq i64 %9, 0
br i1 %10, label %51, label %11
11: ; preds = %2
%12 = call i32 @get_machine_name(i8* %5, i32 41)
%13 = icmp eq i32 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %11
%15 = load i32, i32* @stderr
%16 = call i32 (i32, i8*, ...) @fprintf(i32 %15, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0))
br label %51
17: ; preds = %11
%18 = call i32 @snprintf(i8* %6, i32 128, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i8* %5, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.5, i64 0, i64 0))
%19 = icmp ugt i32 %18, 127
br i1 %19, label %20, label %23
20: ; preds = %17
%21 = load i32, i32* @stderr
%22 = call i32 (i32, i8*, ...) @fprintf(i32 %21, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.6, i64 0, i64 0), i8* %6)
br label %51
23: ; preds = %17
%24 = load i32, i32* @O_WRONLY
%25 = load i32, i32* @O_CREAT
%26 = or i32 %25, %24
%27 = call i32 @openat(i32 %1, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %26, i32 420)
%28 = icmp eq i32 %27, -1
br i1 %28, label %29, label %34
29: ; preds = %23
%30 = load i32, i32* @stderr
%31 = load i32, i32* @errno
%32 = call i8* @strerror(i32 %31)
%33 = call i32 (i32, i8*, ...) @fprintf(i32 %30, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.7, i64 0, i64 0), i8* %0, i8* %32)
br label %51
34: ; preds = %23
%35 = call i32 @write(i32 %27, i8* %6, i32 %18)
%36 = icmp eq i32 %35, %18
br i1 %36, label %43, label %37
37: ; preds = %34
%38 = load i32, i32* @stderr
%39 = load i32, i32* @errno
%40 = call i8* @strerror(i32 %39)
%41 = call i32 (i32, i8*, ...) @fprintf(i32 %38, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.8, i64 0, i64 0), i8* %0, i8* %40)
%42 = call i64 @close(i32 %27)
br label %51
43: ; preds = %34
%44 = call i64 @close(i32 %27)
%45 = icmp eq i64 %44, 0
br i1 %45, label %51, label %46
46: ; preds = %43
%47 = load i32, i32* @stderr
%48 = load i32, i32* @errno
%49 = call i8* @strerror(i32 %48)
%50 = call i32 (i32, i8*, ...) @fprintf(i32 %47, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.9, i64 0, i64 0), i8* %0, i8* %49)
br label %51
51: ; preds = %14, %20, %29, %46, %37, %43, %2
%52 = phi i32 [ 1, %2 ], [ 1, %43 ], [ 0, %37 ], [ 0, %46 ], [ 0, %29 ], [ 0, %20 ], [ 0, %14 ]
ret i32 %52
}