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

/* Variables and functions */
int MAXPATHLEN;
int O_APPEND;
int O_CREAT;
int O_RDWR;
int /*<<< orphan*/ STDOUT_FILENO;
int S_IRUSR;
int S_IWUSR;
int dup2(int, int /*<<< orphan*/);
int /*<<< orphan*/ err(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ errx(int, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *);
char *getenv(char *);
int open(char const *, int, int);
scalar_t__ snprintf(char *, int, char *, char const *, char *);
int /*<<< orphan*/ stderr;
void dofile(void) {
  int fd;
  char path[MAXPATHLEN];
  const char *p;
  p = "nohup.out";
  fd = open(p, O_RDWR | O_CREAT | O_APPEND, S_IRUSR | S_IWUSR);
  if (fd != -1)
    goto dupit;
  if ((p = getenv("HOME")) != NULL && *p != '\0' &&
      (size_t)snprintf(path, sizeof(path), "%s/%s", p, "nohup.out") <
          sizeof(path)) {
    fd = open(p = path, O_RDWR | O_CREAT | O_APPEND, S_IRUSR | S_IWUSR);
    if (fd != -1)
      goto dupit;
  }
  errx(127, "can't open a nohup.out file");
dupit:
  if (dup2(fd, STDOUT_FILENO) == -1)
    err(127, NULL);
  (void)fprintf(stderr, "appending output to %s\n", p);
}


x86 assembly (clang, -O0):
<dofile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    0x2e51(%rip),%rax
mov    (%rax),%eax
mov    %rax,-0x30(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x10(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x28(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x18(%rbp)
lea    0xe51(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    0x2e1a(%rip),%rax
mov    (%rax),%esi
mov    0x2e31(%rip),%rax
or     (%rax),%esi
mov    0x2dd0(%rip),%rax
or     (%rax),%esi
mov    0x2dd7(%rip),%rax
mov    (%rax),%edx
mov    0x2de6(%rip),%rax
or     (%rax),%edx
call   1090 <open@plt>
mov    %eax,-0x4(%rbp)
cmpl   $0xffffffff,-0x4(%rbp)
je     11fb <dofile+0x8b>
jmp    12b5 <dofile+0x145>
lea    0xe08(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     12a4 <dofile+0x134>
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     12a4 <dofile+0x134>
mov    -0x30(%rbp),%rax
mov    -0x28(%rbp),%rdi
mov    %eax,%esi
mov    -0x20(%rbp),%rcx
lea    0xdd5(%rip),%rdx
lea    0xdbf(%rip),%r8
call   1060 <snprintf@plt>
mov    -0x30(%rbp),%rcx
cmp    %rcx,%rax
jae    12a4 <dofile+0x134>
mov    -0x28(%rbp),%rdi
mov    %rdi,-0x20(%rbp)
mov    0x2d76(%rip),%rax
mov    (%rax),%esi
mov    0x2d8d(%rip),%rax
or     (%rax),%esi
mov    0x2d2c(%rip),%rax
or     (%rax),%esi
mov    0x2d33(%rip),%rax
mov    (%rax),%edx
mov    0x2d42(%rip),%rax
or     (%rax),%edx
call   1090 <open@plt>
mov    %eax,-0x4(%rbp)
cmpl   $0xffffffff,-0x4(%rbp)
je     129f <dofile+0x12f>
jmp    12b5 <dofile+0x145>
jmp    12a4 <dofile+0x134>
mov    $0x7f,%edi
lea    0xd65(%rip),%rsi
call   1040 <errx@plt>
mov    -0x4(%rbp),%edi
mov    0x2d29(%rip),%rax
mov    (%rax),%esi
call   1050 <dup2@plt>
cmp    $0xffffffff,%eax
jne    12dd <dofile+0x16d>
mov    $0x7f,%edi
xor    %eax,%eax
mov    %eax,%esi
call   1080 <err@plt>
mov    0x2cd4(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%rdx
lea    0xd40(%rip),%rsi
call   1070 <fprintf@plt>
mov    -0x10(%rbp),%rax
mov    %rax,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dofile>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    0x2e4b(%rip),%rax
mov    (%rax),%r15d
mov    %rsp,%rbx
lea    0xf(%r15),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
mov    0x2e38(%rip),%r13
mov    0x2e51(%rip),%rax
mov    (%rax),%esi
or     0x0(%r13),%esi
mov    0x2dec(%rip),%rax
or     (%rax),%esi
mov    0x2df3(%rip),%rax
mov    0x2e04(%rip),%r12
mov    (%r12),%edx
or     (%rax),%edx
lea    0xe2f(%rip),%r14
mov    %r14,%rdi
call   1090 <open@plt>
cmp    $0xffffffff,%eax
jne    126a <dofile+0xfa>
lea    0xe21(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     1251 <dofile+0xe1>
mov    %rax,%r14
cmpb   $0x0,(%rax)
je     1254 <dofile+0xe4>
lea    0xe0d(%rip),%rdx
lea    0xdf7(%rip),%r8
mov    %rbx,%rdi
mov    %r15d,%esi
mov    %r14,%rcx
call   1060 <snprintf@plt>
cmp    %r15,%rax
mov    0x2dd7(%rip),%rax
mov    0x2d78(%rip),%rcx
mov    0x2d81(%rip),%rdi
jae    1254 <dofile+0xe4>
mov    (%rax),%esi
or     0x0(%r13),%esi
or     (%rcx),%esi
mov    (%r12),%edx
or     (%rdi),%edx
mov    %rbx,%rdi
call   1090 <open@plt>
mov    %rbx,%r14
cmp    $0xffffffff,%eax
jne    126a <dofile+0xfa>
jmp    1254 <dofile+0xe4>
xor    %r14d,%r14d
lea    0xdba(%rip),%rsi
mov    $0x7f,%edi
call   1040 <errx@plt>
mov    $0xffffffff,%eax
mov    0x2d77(%rip),%rcx
mov    (%rcx),%esi
mov    %eax,%edi
call   1050 <dup2@plt>
cmp    $0xffffffff,%eax
jne    128b <dofile+0x11b>
mov    $0x7f,%edi
xor    %esi,%esi
call   1080 <err@plt>
mov    0x2d26(%rip),%rax
mov    (%rax),%edi
lea    0xd96(%rip),%rsi
mov    %r14,%rdx
call   1070 <fprintf@plt>
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@MAXPATHLEN = global i32 0
@.str = [10 x i8] c"nohup.out\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@O_APPEND = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@.str.1 = [5 x i8] c"HOME\00"
@.str.2 = [6 x i8] c"%s/%s\00"
@.str.3 = [28 x i8] c"can't open a nohup.out file\00"
@STDOUT_FILENO = global i32 0
@stderr = global i32 0
@.str.4 = [24 x i8] c"appending output to %s\0A\00"
define void @dofile() {
%1 = alloca i32
%2 = alloca i8*
%3 = alloca i64
%4 = alloca i8*
%5 = load i32, i32* @MAXPATHLEN
%6 = zext i32 %5 to i64
%7 = call i8* @llvm.stacksave()
store i8* %7, i8** %2
%8 = alloca i8, i64 %6
store i64 %6, i64* %3
store i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8** %4
%9 = load i8*, i8** %4
%10 = load i32, i32* @O_RDWR
%11 = load i32, i32* @O_CREAT
%12 = or i32 %10, %11
%13 = load i32, i32* @O_APPEND
%14 = or i32 %12, %13
%15 = load i32, i32* @S_IRUSR
%16 = load i32, i32* @S_IWUSR
%17 = or i32 %15, %16
%18 = call i32 @open(i8* %9, i32 %14, i32 %17)
store i32 %18, i32* %1
%19 = load i32, i32* %1
%20 = icmp ne i32 %19, -1
br i1 %20, label %21, label %22
21: ; preds = %0
br label %51
22: ; preds = %0
%23 = call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
store i8* %23, i8** %4
%24 = icmp ne i8* %23, null
br i1 %24, label %25, label %49
25: ; preds = %22
%26 = load i8*, i8** %4
%27 = load i8, i8* %26
%28 = sext i8 %27 to i32
%29 = icmp ne i32 %28, 0
br i1 %29, label %30, label %49
30: ; preds = %25
%31 = trunc i64 %6 to i32
%32 = load i8*, i8** %4
%33 = call i64 @snprintf(i8* %8, i32 %31, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* %32, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%34 = icmp ult i64 %33, %6
br i1 %34, label %35, label %49
35: ; preds = %30
store i8* %8, i8** %4
%36 = load i32, i32* @O_RDWR
%37 = load i32, i32* @O_CREAT
%38 = or i32 %36, %37
%39 = load i32, i32* @O_APPEND
%40 = or i32 %38, %39
%41 = load i32, i32* @S_IRUSR
%42 = load i32, i32* @S_IWUSR
%43 = or i32 %41, %42
%44 = call i32 @open(i8* %8, i32 %40, i32 %43)
store i32 %44, i32* %1
%45 = load i32, i32* %1
%46 = icmp ne i32 %45, -1
br i1 %46, label %47, label %48
47: ; preds = %35
br label %51
48: ; preds = %35
br label %49
49: ; preds = %48, %30, %25, %22
%50 = call i32 @errx(i32 127, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0))
br label %51
51: ; preds = %49, %47, %21
%52 = load i32, i32* %1
%53 = load i32, i32* @STDOUT_FILENO
%54 = call i32 @dup2(i32 %52, i32 %53)
%55 = icmp eq i32 %54, -1
br i1 %55, label %56, label %58
56: ; preds = %51
%57 = call i32 @err(i32 127, i32* null)
br label %58
58: ; preds = %56, %51
%59 = load i32, i32* @stderr
%60 = load i8*, i8** %4
%61 = call i32 @fprintf(i32 %59, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.4, i64 0, i64 0), i8* %60)
%62 = load i8*, i8** %2
ret void
}

LLVM IR (-O2):
@MAXPATHLEN = global i32 0
@.str = [10 x i8] c"nohup.out\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@O_APPEND = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@.str.1 = [5 x i8] c"HOME\00"
@.str.2 = [6 x i8] c"%s/%s\00"
@.str.3 = [28 x i8] c"can't open a nohup.out file\00"
@STDOUT_FILENO = global i32 0
@stderr = global i32 0
@.str.4 = [24 x i8] c"appending output to %s\0A\00"
define void @dofile() {
%1 = load i32, i32* @MAXPATHLEN
%2 = zext i32 %1 to i64
%3 = alloca i8, i64 %2
%4 = load i32, i32* @O_RDWR
%5 = load i32, i32* @O_CREAT
%6 = or i32 %5, %4
%7 = load i32, i32* @O_APPEND
%8 = or i32 %6, %7
%9 = load i32, i32* @S_IRUSR
%10 = load i32, i32* @S_IWUSR
%11 = or i32 %10, %9
%12 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %8, i32 %11)
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %37
14: ; preds = %0
%15 = tail call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%16 = icmp eq i8* %15, null
br i1 %16, label %34, label %17
17: ; preds = %14
%18 = load i8, i8* %15
%19 = icmp eq i8 %18, 0
br i1 %19, label %34, label %20
20: ; preds = %17
%21 = call i64 @snprintf(i8* %3, i32 %1, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* %15, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%22 = icmp ult i64 %21, %2
br i1 %22, label %23, label %34
23: ; preds = %20
%24 = load i32, i32* @O_RDWR
%25 = load i32, i32* @O_CREAT
%26 = or i32 %25, %24
%27 = load i32, i32* @O_APPEND
%28 = or i32 %26, %27
%29 = load i32, i32* @S_IRUSR
%30 = load i32, i32* @S_IWUSR
%31 = or i32 %30, %29
%32 = call i32 @open(i8* %3, i32 %28, i32 %31)
%33 = icmp eq i32 %32, -1
br i1 %33, label %34, label %37
34: ; preds = %23, %20, %17, %14
%35 = phi i8* [ %3, %23 ], [ %15, %20 ], [ %15, %17 ], [ null, %14 ]
%36 = call i32 @errx(i32 127, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0))
br label %37
37: ; preds = %23, %0, %34
%38 = phi i32 [ %12, %0 ], [ %32, %23 ], [ -1, %34 ]
%39 = phi i8* [ getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), %0 ], [ %3, %23 ], [ %35, %34 ]
%40 = load i32, i32* @STDOUT_FILENO
%41 = call i32 @dup2(i32 %38, i32 %40)
%42 = icmp eq i32 %41, -1
br i1 %42, label %43, label %45
43: ; preds = %37
%44 = call i32 @err(i32 127, i32* null)
br label %45
45: ; preds = %43, %37
%46 = load i32, i32* @stderr
%47 = call i32 @fprintf(i32 %46, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.4, i64 0, i64 0), i8* %39)
ret void
}