#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 scalar_t__ pid_t;

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ EXIT_SUCCESS;
int WEXITSTATUS(int);
scalar_t__ WIFEXITED(int);
scalar_t__ WIFSIGNALED(int);
int WTERMSIG(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fork();
int /*<<< orphan*/ nxweb_create_pid_file(char const *, scalar_t__);
int /*<<< orphan*/ nxweb_log_error(char *, ...);
int /*<<< orphan*/ unlink(char const *);
int waitpid(scalar_t__, int *, int /*<<< orphan*/);
int nxweb_relauncher(void (*main_func)(), const char *pid_file) {
  pid_t pid = fork();
  if (pid < 0) {
    exit(EXIT_FAILURE);
  } else if (pid > 0) {
    int status;
    if (pid_file)
      nxweb_create_pid_file(pid_file, pid);
    if (waitpid(pid, &status, 0) == -1) {
      nxweb_log_error("waitpid failure");
      exit(EXIT_FAILURE);
    }
    if (pid_file)
      unlink(pid_file);
    if (WIFEXITED(status)) {
      nxweb_log_error("Server exited, status=%d", WEXITSTATUS(status));
      return WEXITSTATUS(status);
    } else if (WIFSIGNALED(status)) {
      nxweb_log_error("Server killed by signal %d", WTERMSIG(status));
      return 1;
    }
  } else {
    main_func();
    exit(EXIT_SUCCESS);
  }
  return 0;
}


x86 assembly (clang, -O0):
<nxweb_relauncher>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    $0x0,%al
call   10c0 <fork@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jge    11d4 <nxweb_relauncher+0x34>
mov    0x2e13(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
cmpq   $0x0,-0x20(%rbp)
jle    12b9 <nxweb_relauncher+0x119>
cmpq   $0x0,-0x18(%rbp)
je     11f7 <nxweb_relauncher+0x57>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1040 <nxweb_create_pid_file@plt>
mov    -0x20(%rbp),%rdi
lea    -0x24(%rbp),%rsi
xor    %edx,%edx
call   1080 <waitpid@plt>
cmp    $0xffffffff,%eax
jne    122b <nxweb_relauncher+0x8b>
lea    0xdea(%rip),%rdi
mov    $0x0,%al
call   10b0 <nxweb_log_error@plt>
mov    0x2dbc(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
cmpq   $0x0,-0x18(%rbp)
je     123f <nxweb_relauncher+0x9f>
mov    -0x18(%rbp),%rdi
call   1030 <unlink@plt>
mov    -0x24(%rbp),%edi
call   1090 <WIFEXITED@plt>
cmp    $0x0,%rax
je     1279 <nxweb_relauncher+0xd9>
mov    -0x24(%rbp),%edi
call   1070 <WEXITSTATUS@plt>
mov    %eax,%esi
lea    0xdae(%rip),%rdi
mov    $0x0,%al
call   10b0 <nxweb_log_error@plt>
mov    -0x24(%rbp),%edi
call   1070 <WEXITSTATUS@plt>
mov    %eax,-0x4(%rbp)
jmp    12d8 <nxweb_relauncher+0x138>
mov    -0x24(%rbp),%edi
call   1050 <WIFSIGNALED@plt>
cmp    $0x0,%rax
je     12af <nxweb_relauncher+0x10f>
mov    -0x24(%rbp),%edi
call   1060 <WTERMSIG@plt>
mov    %eax,%esi
lea    0xd8d(%rip),%rdi
mov    $0x0,%al
call   10b0 <nxweb_log_error@plt>
movl   $0x1,-0x4(%rbp)
jmp    12d8 <nxweb_relauncher+0x138>
jmp    12b4 <nxweb_relauncher+0x114>
jmp    12cc <nxweb_relauncher+0x12c>
mov    $0x0,%al
call   *-0x10(%rbp)
mov    0x2d13(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
jmp    12d1 <nxweb_relauncher+0x131>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<nxweb_relauncher>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r14
mov    %rdi,%r15
xor    %eax,%eax
call   10c0 <fork@plt>
test   %rax,%rax
js     1297 <nxweb_relauncher+0xf7>
je     1276 <nxweb_relauncher+0xd6>
mov    %rax,%rbx
test   %r14,%r14
je     11fa <nxweb_relauncher+0x5a>
mov    %r14,%rdi
mov    %rbx,%rsi
call   1040 <nxweb_create_pid_file@plt>
lea    0xc(%rsp),%rsi
mov    %rbx,%rdi
xor    %edx,%edx
call   1080 <waitpid@plt>
cmp    $0xffffffff,%eax
je     1289 <nxweb_relauncher+0xe9>
mov    %r14,%rdi
call   1030 <unlink@plt>
jmp    120e <nxweb_relauncher+0x6e>
lea    0xc(%rsp),%rsi
mov    %rbx,%rdi
xor    %edx,%edx
call   1080 <waitpid@plt>
cmp    $0xffffffff,%eax
je     1289 <nxweb_relauncher+0xe9>
mov    0xc(%rsp),%edi
call   1090 <WIFEXITED@plt>
mov    0xc(%rsp),%edi
test   %rax,%rax
je     1240 <nxweb_relauncher+0xa0>
call   1070 <WEXITSTATUS@plt>
lea    0xde4(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   10b0 <nxweb_log_error@plt>
mov    0xc(%rsp),%edi
call   1070 <WEXITSTATUS@plt>
jmp    126c <nxweb_relauncher+0xcc>
call   1050 <WIFSIGNALED@plt>
test   %rax,%rax
je     126a <nxweb_relauncher+0xca>
mov    0xc(%rsp),%edi
call   1060 <WTERMSIG@plt>
lea    0xdcf(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   10b0 <nxweb_log_error@plt>
mov    $0x1,%eax
jmp    126c <nxweb_relauncher+0xcc>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
xor    %eax,%eax
call   *%r15
mov    0x2d56(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
lea    0xd70(%rip),%rdi
xor    %eax,%eax
call   10b0 <nxweb_log_error@plt>
mov    0x2d42(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@EXIT_FAILURE = global i32 0
@.str = [16 x i8] c"waitpid failure\00"
@.str.1 = [25 x i8] c"Server exited, status=%d\00"
@.str.2 = [27 x i8] c"Server killed by signal %d\00"
@EXIT_SUCCESS = global i32 0
define i32 @nxweb_relauncher(void (...)* %0, i8* %1) {
%3 = alloca i32
%4 = alloca void (...)*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i32
store void (...)* %0, void (...)** %4
store i8* %1, i8** %5
%8 = call i64 (...) @fork()
store i64 %8, i64* %6
%9 = load i64, i64* %6
%10 = icmp slt i64 %9, 0
br i1 %10, label %11, label %14
11: ; preds = %2
%12 = load i32, i32* @EXIT_FAILURE
%13 = call i32 @exit(i32 %12)
unreachable
14: ; preds = %2
%15 = load i64, i64* %6
%16 = icmp sgt i64 %15, 0
br i1 %16, label %17, label %58
17: ; preds = %14
%18 = load i8*, i8** %5
%19 = icmp ne i8* %18, null
br i1 %19, label %20, label %24
20: ; preds = %17
%21 = load i8*, i8** %5
%22 = load i64, i64* %6
%23 = call i32 @nxweb_create_pid_file(i8* %21, i64 %22)
br label %24
24: ; preds = %20, %17
%25 = load i64, i64* %6
%26 = call i32 @waitpid(i64 %25, i32* %7, i32 0)
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %32
28: ; preds = %24
%29 = call i32 (i8*, ...) @nxweb_log_error(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%30 = load i32, i32* @EXIT_FAILURE
%31 = call i32 @exit(i32 %30)
unreachable
32: ; preds = %24
%33 = load i8*, i8** %5
%34 = icmp ne i8* %33, null
br i1 %34, label %35, label %38
35: ; preds = %32
%36 = load i8*, i8** %5
%37 = call i32 @unlink(i8* %36)
br label %38
38: ; preds = %35, %32
%39 = load i32, i32* %7
%40 = call i64 @WIFEXITED(i32 %39)
%41 = icmp ne i64 %40, 0
br i1 %41, label %42, label %48
42: ; preds = %38
%43 = load i32, i32* %7
%44 = call i32 @WEXITSTATUS(i32 %43)
%45 = call i32 (i8*, ...) @nxweb_log_error(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i32 %44)
%46 = load i32, i32* %7
%47 = call i32 @WEXITSTATUS(i32 %46)
store i32 %47, i32* %3
br label %64
48: ; preds = %38
%49 = load i32, i32* %7
%50 = call i64 @WIFSIGNALED(i32 %49)
%51 = icmp ne i64 %50, 0
br i1 %51, label %52, label %56
52: ; preds = %48
%53 = load i32, i32* %7
%54 = call i32 @WTERMSIG(i32 %53)
%55 = call i32 (i8*, ...) @nxweb_log_error(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i32 %54)
store i32 1, i32* %3
br label %64
56: ; preds = %48
br label %57
57: ; preds = %56
br label %62
58: ; preds = %14
%59 = load void (...)*, void (...)** %4
%60 = load i32, i32* @EXIT_SUCCESS
%61 = call i32 @exit(i32 %60)
unreachable
62: ; preds = %57
br label %63
63: ; preds = %62
store i32 0, i32* %3
br label %64
64: ; preds = %63, %52, %42
%65 = load i32, i32* %3
ret i32 %65
}

LLVM IR (-O2):
@EXIT_FAILURE = global i32 0
@.str = [16 x i8] c"waitpid failure\00"
@.str.1 = [25 x i8] c"Server exited, status=%d\00"
@.str.2 = [27 x i8] c"Server killed by signal %d\00"
@EXIT_SUCCESS = global i32 0
define i32 @nxweb_relauncher(void (...)* nocapture %0, i8* %1) {
%3 = alloca i32
%4 = tail call i64 (...) @fork()
%5 = icmp slt i64 %4, 0
br i1 %5, label %6, label %9
6: ; preds = %2
%7 = load i32, i32* @EXIT_FAILURE
%8 = tail call i32 @exit(i32 %7)
unreachable
9: ; preds = %2
%10 = icmp eq i64 %4, 0
br i1 %10, label %49, label %11
11: ; preds = %9
%12 = bitcast i32* %3 to i8*
%13 = icmp eq i8* %1, null
br i1 %13, label %14, label %17
14: ; preds = %11
%15 = call i32 @waitpid(i64 %4, i32* %3, i32 0)
%16 = icmp eq i32 %15, -1
br i1 %16, label %21, label %27
17: ; preds = %11
%18 = tail call i32 @nxweb_create_pid_file(i8* %1, i64 %4)
%19 = call i32 @waitpid(i64 %4, i32* %3, i32 0)
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %25
21: ; preds = %17, %14
%22 = call i32 (i8*, ...) @nxweb_log_error(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%23 = load i32, i32* @EXIT_FAILURE
%24 = call i32 @exit(i32 %23)
unreachable
25: ; preds = %17
%26 = call i32 @unlink(i8* %1)
br label %27
27: ; preds = %14, %25
%28 = load i32, i32* %3
%29 = call i64 @WIFEXITED(i32 %28)
%30 = icmp eq i64 %29, 0
%31 = load i32, i32* %3
br i1 %30, label %37, label %32
32: ; preds = %27
%33 = call i32 @WEXITSTATUS(i32 %31)
%34 = call i32 (i8*, ...) @nxweb_log_error(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i32 %33)
%35 = load i32, i32* %3
%36 = call i32 @WEXITSTATUS(i32 %35)
br label %44
37: ; preds = %27
%38 = call i64 @WIFSIGNALED(i32 %31)
%39 = icmp eq i64 %38, 0
br i1 %39, label %46, label %40
40: ; preds = %37
%41 = load i32, i32* %3
%42 = call i32 @WTERMSIG(i32 %41)
%43 = call i32 (i8*, ...) @nxweb_log_error(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i32 %42)
br label %44
44: ; preds = %32, %40
%45 = phi i32 [ 1, %40 ], [ %36, %32 ]
br label %47
46: ; preds = %37
br label %47
47: ; preds = %44, %46
%48 = phi i32 [ 0, %46 ], [ %45, %44 ]
ret i32 %48
49: ; preds = %9
tail call void (...) %0()
%50 = load i32, i32* @EXIT_SUCCESS
%51 = tail call i32 @exit(i32 %50)
unreachable
}