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

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int EXIT_SUCCESS;
int /*<<< orphan*/ IPPROTO_TCP;
scalar_t__ STDERR_FILENO;
scalar_t__ STDIN_FILENO;
scalar_t__ STDOUT_FILENO;
int /*<<< orphan*/ TCP_NODELAY;
int WEXITSTATUS(int);
scalar_t__ WIFEXITED(int);
scalar_t__ WIFSIGNALED(int);
int WTERMSIG(int);
int /*<<< orphan*/ close(scalar_t__);
scalar_t__ dup(int);
int /*<<< orphan*/ execv(char *, char **);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
int fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ recv_comm_until(int, unsigned char **, char *);
int /*<<< orphan*/ send_comm(int, unsigned char *, int /*<<< orphan*/);
int /*<<< orphan*/ setenv(char *, char *, int);
int /*<<< orphan*/ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/,
                              char *, int);
int /*<<< orphan*/ snprintf(char *, int, char *, int, char *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdout;
int /*<<< orphan*/ strlen(char *);
char *strsignal(int);
int /*<<< orphan*/ waitpid(int, int *, int /*<<< orphan*/);
int host_exec(int socket, char *exec_name, char **exec_args) {
  int status;
  int ret;
  int flag;
  char restart_prompt[] = "[netdeploy] Restart process? (y/n): ";
  char exit_message[1024];
  char *answer_buf;
  int pid = fork();
  flag = 1;
  if (pid < 0) {
    fprintf(stderr, "Fork failed\n");
    return 0;
  } else if (pid > 0) {
    printf("Waiting for client %d to be finished\n", socket);
    waitpid(pid, &status, 0);
    if (WIFSIGNALED(status)) {
      ret = WTERMSIG(status);
      snprintf(exit_message, sizeof(exit_message),
               "\n[netdeploy] Hosted process was killed by signal: %d (%s)\n",
               ret, strsignal(ret));
      printf("%s", exit_message);
      send_comm(socket, (unsigned char *)exit_message, strlen(exit_message));
    }
    if (WIFEXITED(status)) {
      ret = WEXITSTATUS(status);
      snprintf(
          exit_message, sizeof(exit_message),
          "\n[netdeploy] Hosted process exited with status code: %d (%s)\n",
          ret, ret == EXIT_SUCCESS ? "EXIT_SUCCESS" : "EXIT_FAILURE");
      printf("%s", exit_message);
      send_comm(socket, (unsigned char *)exit_message, strlen(exit_message));
    }
    send_comm(socket, (unsigned char *)restart_prompt, strlen(restart_prompt));
    recv_comm_until(socket, (unsigned char **)&answer_buf, "\n");
    if (answer_buf[0] == 'y' || answer_buf[0] == 'Y') {
      free(answer_buf);
      printf("Client %d is restarting hosted process\n", socket);
      return 1;
    }
    free(answer_buf);
    printf("Client %d finished\n", socket);
    return 0;
  }
  close(STDIN_FILENO);
  close(STDOUT_FILENO);
  close(STDERR_FILENO);
  setsockopt(socket, IPPROTO_TCP, TCP_NODELAY, (char *)&flag, sizeof(int));
  if (dup(socket) != STDIN_FILENO || dup(socket) != STDOUT_FILENO ||
      dup(socket) != STDERR_FILENO) {
    perror("Error duplicating socket for stdin/out/err");
  }
  printf("[netdeploy] Connected to binary \"%s\"\n", exec_name);
  fflush(stdout);
  setenv("LD_PRELOAD", "./custom_links.so", 1);
  execv(exec_name, exec_args);
  perror("execv");
  exit(EXIT_FAILURE);
  return 0;
}


x86 assembly (clang, -O0):
<host_exec>:
push   %rbp
mov    %rsp,%rbp
sub    $0x4a0,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
lea    -0x50(%rbp),%rdi
lea    0xd6f(%rip),%rsi
mov    $0x25,%edx
call   10e0 <memcpy@plt>
mov    $0x0,%al
call   1190 <fork@plt>
mov    %eax,-0x45c(%rbp)
movl   $0x1,-0x24(%rbp)
cmpl   $0x0,-0x45c(%rbp)
jge    12dd <host_exec+0x6d>
mov    0x2d05(%rip),%rax
mov    (%rax),%edi
lea    0xd59(%rip),%rsi
call   10d0 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1621 <host_exec+0x3b1>
cmpl   $0x0,-0x45c(%rbp)
jle    1518 <host_exec+0x2a8>
mov    -0x8(%rbp),%esi
lea    0xd3e(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x45c(%rbp),%edi
lea    -0x1c(%rbp),%rsi
xor    %edx,%edx
call   1140 <waitpid@plt>
mov    -0x1c(%rbp),%edi
call   10c0 <WIFSIGNALED@plt>
cmp    $0x0,%rax
je     13b5 <host_exec+0x145>
mov    -0x1c(%rbp),%edi
call   1100 <WTERMSIG@plt>
mov    %eax,-0x20(%rbp)
lea    -0x450(%rbp),%rax
mov    %rax,-0x478(%rbp)
mov    -0x20(%rbp),%eax
mov    %eax,-0x470(%rbp)
mov    -0x20(%rbp),%edi
call   1120 <strsignal@plt>
mov    -0x478(%rbp),%rdi
mov    -0x470(%rbp),%ecx
mov    %rax,%r8
mov    $0x400,%esi
lea    0xcf4(%rip),%rdx
call   1080 <snprintf@plt>
lea    -0x450(%rbp),%rsi
lea    0xd1c(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x46c(%rbp)
lea    -0x450(%rbp),%rax
mov    %rax,-0x468(%rbp)
lea    -0x450(%rbp),%rdi
call   1060 <strlen@plt>
mov    -0x46c(%rbp),%edi
mov    -0x468(%rbp),%rsi
mov    %eax,%edx
call   10a0 <send_comm@plt>
mov    -0x1c(%rbp),%edi
call   1160 <WIFEXITED@plt>
cmp    $0x0,%rax
je     1459 <host_exec+0x1e9>
mov    -0x1c(%rbp),%edi
call   1130 <WEXITSTATUS@plt>
mov    %eax,-0x20(%rbp)
lea    -0x450(%rbp),%rdi
mov    -0x20(%rbp),%ecx
mov    -0x20(%rbp),%edx
mov    0x2bc2(%rip),%rax
mov    (%rax),%esi
lea    0xcf2(%rip),%r8
lea    0xcde(%rip),%rax
cmp    %esi,%edx
cmove  %rax,%r8
mov    $0x400,%esi
lea    0xc8e(%rip),%rdx
call   1080 <snprintf@plt>
lea    -0x450(%rbp),%rsi
lea    0xc78(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x484(%rbp)
lea    -0x450(%rbp),%rax
mov    %rax,-0x480(%rbp)
lea    -0x450(%rbp),%rdi
call   1060 <strlen@plt>
mov    -0x484(%rbp),%edi
mov    -0x480(%rbp),%rsi
mov    %eax,%edx
call   10a0 <send_comm@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x494(%rbp)
lea    -0x50(%rbp),%rax
mov    %rax,-0x490(%rbp)
lea    -0x50(%rbp),%rdi
call   1060 <strlen@plt>
mov    -0x494(%rbp),%edi
mov    -0x490(%rbp),%rsi
mov    %eax,%edx
call   10a0 <send_comm@plt>
mov    -0x8(%rbp),%edi
lea    -0x458(%rbp),%rsi
lea    0xcde(%rip),%rdx
call   1110 <recv_comm_until@plt>
mov    -0x458(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x79,%eax
je     14c6 <host_exec+0x256>
mov    -0x458(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x59,%eax
jne    14ef <host_exec+0x27f>
mov    -0x458(%rbp),%rdi
call   1030 <free@plt>
mov    -0x8(%rbp),%esi
lea    0xc12(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1621 <host_exec+0x3b1>
mov    -0x458(%rbp),%rdi
call   1030 <free@plt>
mov    -0x8(%rbp),%esi
lea    0xc11(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1621 <host_exec+0x3b1>
jmp    151d <host_exec+0x2ad>
mov    0x2a7c(%rip),%rax
mov    (%rax),%rdi
call   10b0 <close@plt>
mov    0x2abd(%rip),%rax
mov    (%rax),%rdi
call   10b0 <close@plt>
mov    0x2a7e(%rip),%rax
mov    (%rax),%rdi
call   10b0 <close@plt>
mov    -0x8(%rbp),%edi
mov    0x2a84(%rip),%rax
mov    (%rax),%esi
mov    0x2a83(%rip),%rax
mov    (%rax),%edx
lea    -0x24(%rbp),%rcx
mov    $0x4,%r8d
call   1040 <setsockopt@plt>
mov    -0x8(%rbp),%edi
call   1090 <dup@plt>
mov    0x2a23(%rip),%rcx
cmp    (%rcx),%rax
jne    15b6 <host_exec+0x346>
mov    -0x8(%rbp),%edi
call   1090 <dup@plt>
mov    0x2a5b(%rip),%rcx
cmp    (%rcx),%rax
jne    15b6 <host_exec+0x346>
mov    -0x8(%rbp),%edi
call   1090 <dup@plt>
mov    0x2a13(%rip),%rcx
cmp    (%rcx),%rax
je     15c2 <host_exec+0x352>
lea    0xb6d(%rip),%rdi
call   1150 <perror@plt>
mov    -0x10(%rbp),%rsi
lea    0xb88(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    0x29d5(%rip),%rax
mov    (%rax),%edi
call   10f0 <fflush@plt>
lea    0xb92(%rip),%rdi
lea    0xb96(%rip),%rsi
mov    $0x1,%edx
call   1050 <setenv@plt>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1180 <execv@plt>
lea    0xb8a(%rip),%rdi
call   1150 <perror@plt>
mov    0x299e(%rip),%rax
mov    (%rax),%edi
call   1170 <exit@plt>
mov    -0x4(%rbp),%eax
add    $0x4a0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<host_exec>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x448,%rsp
mov    %rdx,%r14
mov    %rsi,%r15
mov    %edi,%r12d
movaps 0xd8f(%rip),%xmm0
movaps %xmm0,0x20(%rsp)
movaps 0xd73(%rip),%xmm0
movaps %xmm0,0x10(%rsp)
movabs $0x203a296e2f7928,%rax
mov    %rax,0x2d(%rsp)
xor    %ebp,%ebp
xor    %eax,%eax
call   1180 <fork@plt>
movl   $0x1,0x4(%rsp)
test   %eax,%eax
js     140a <host_exec+0x1aa>
je     144d <host_exec+0x1ed>
mov    %eax,%ebx
lea    0xd69(%rip),%rdi
mov    %r12d,%esi
xor    %eax,%eax
call   1070 <printf@plt>
mov    %rsp,%rsi
mov    %ebx,%edi
xor    %edx,%edx
call   1130 <waitpid@plt>
mov    (%rsp),%edi
call   10c0 <WIFSIGNALED@plt>
test   %rax,%rax
je     1341 <host_exec+0xe1>
mov    (%rsp),%edi
call   10f0 <WTERMSIG@plt>
mov    %eax,%ebx
mov    %eax,%edi
call   1110 <strsignal@plt>
lea    0xd54(%rip),%rdx
lea    0x40(%rsp),%rbp
mov    %rbp,%rdi
mov    $0x400,%esi
mov    %ebx,%ecx
mov    %rax,%r8
call   1080 <snprintf@plt>
lea    0xd71(%rip),%rdi
mov    %rbp,%rsi
xor    %eax,%eax
call   1070 <printf@plt>
mov    %rbp,%rdi
call   1060 <strlen@plt>
mov    %r12d,%edi
mov    %rbp,%rsi
mov    %eax,%edx
call   10a0 <send_comm@plt>
mov    (%rsp),%edi
call   1150 <WIFEXITED@plt>
test   %rax,%rax
je     13b2 <host_exec+0x152>
mov    (%rsp),%edi
call   1120 <WEXITSTATUS@plt>
mov    0x2c4b(%rip),%rcx
cmp    (%rcx),%eax
lea    0xd6e(%rip),%rcx
lea    0xd74(%rip),%r8
cmove  %rcx,%r8
lea    0xd1e(%rip),%rdx
lea    0x40(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x400,%esi
mov    %eax,%ecx
call   1080 <snprintf@plt>
lea    0xd00(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1070 <printf@plt>
mov    %rbx,%rdi
call   1060 <strlen@plt>
mov    %r12d,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   10a0 <send_comm@plt>
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
call   1060 <strlen@plt>
mov    %r12d,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   10a0 <send_comm@plt>
lea    0xda6(%rip),%rdx
lea    0x8(%rsp),%rsi
mov    %r12d,%edi
call   1100 <recv_comm_until@plt>
mov    0x8(%rsp),%rdi
mov    (%rdi),%al
or     $0x20,%al
cmp    $0x79,%al
jne    1421 <host_exec+0x1c1>
call   1030 <free@plt>
lea    0xcf5(%rip),%rdi
mov    %r12d,%esi
xor    %eax,%eax
call   1070 <printf@plt>
mov    $0x1,%ebp
jmp    1439 <host_exec+0x1d9>
mov    0x2bb7(%rip),%rax
mov    (%rax),%edi
lea    0xc0b(%rip),%rsi
call   10d0 <fprintf@plt>
jmp    1439 <host_exec+0x1d9>
call   1030 <free@plt>
lea    0xce9(%rip),%rdi
xor    %ebp,%ebp
mov    %r12d,%esi
xor    %eax,%eax
call   1070 <printf@plt>
mov    %ebp,%eax
add    $0x448,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2b4c(%rip),%rbx
mov    (%rbx),%rdi
call   10b0 <close@plt>
mov    0x2b8d(%rip),%rbp
mov    0x0(%rbp),%rdi
call   10b0 <close@plt>
mov    0x2b4d(%rip),%r13
mov    0x0(%r13),%rdi
call   10b0 <close@plt>
mov    0x2b55(%rip),%rax
mov    (%rax),%esi
mov    0x2b54(%rip),%rax
mov    (%rax),%edx
lea    0x4(%rsp),%rcx
mov    %r12d,%edi
mov    $0x4,%r8d
call   1040 <setsockopt@plt>
mov    %r12d,%edi
call   1090 <dup@plt>
cmp    (%rbx),%rax
jne    14ca <host_exec+0x26a>
mov    %r12d,%edi
call   1090 <dup@plt>
cmp    0x0(%rbp),%rax
jne    14ca <host_exec+0x26a>
mov    %r12d,%edi
call   1090 <dup@plt>
cmp    0x0(%r13),%rax
je     14d6 <host_exec+0x276>
lea    0xc59(%rip),%rdi
call   1140 <perror@plt>
lea    0xc78(%rip),%rdi
mov    %r15,%rsi
xor    %eax,%eax
call   1070 <printf@plt>
mov    0x2ac2(%rip),%rax
mov    (%rax),%edi
call   10e0 <fflush@plt>
lea    0xc7f(%rip),%rdi
lea    0xc83(%rip),%rsi
mov    $0x1,%edx
call   1050 <setenv@plt>
mov    %r15,%rdi
mov    %r14,%rsi
call   1170 <execv@plt>
lea    0xc79(%rip),%rdi
call   1140 <perror@plt>
mov    0x2a8d(%rip),%rax
mov    (%rax),%edi
call   1160 <exit@plt>

LLVM IR (-O0):
@__const.host_exec.restart_prompt = [37 x i8] c"[netdeploy] Restart process? (y/n): \00"
@stderr = global i32 0
@.str = [13 x i8] c"Fork failed\0A\00"
@.str.1 = [38 x i8] c"Waiting for client %d to be finished\0A\00"
@.str.2 = [59 x i8] c"\0A[netdeploy] Hosted process was killed by signal: %d (%s)\0A\00"
@.str.3 = [3 x i8] c"%s\00"
@.str.4 = [62 x i8] c"\0A[netdeploy] Hosted process exited with status code: %d (%s)\0A\00"
@EXIT_SUCCESS = global i32 0
@.str.5 = [13 x i8] c"EXIT_SUCCESS\00"
@.str.6 = [13 x i8] c"EXIT_FAILURE\00"
@.str.7 = [2 x i8] c"\0A\00"
@.str.8 = [40 x i8] c"Client %d is restarting hosted process\0A\00"
@.str.9 = [20 x i8] c"Client %d finished\0A\00"
@STDIN_FILENO = global i64 0
@STDOUT_FILENO = global i64 0
@STDERR_FILENO = global i64 0
@IPPROTO_TCP = global i32 0
@TCP_NODELAY = global i32 0
@.str.10 = [43 x i8] c"Error duplicating socket for stdin/out/err\00"
@.str.11 = [38 x i8] c"[netdeploy] Connected to binary \22%s\22\0A\00"
@stdout = global i32 0
@.str.12 = [11 x i8] c"LD_PRELOAD\00"
@.str.13 = [18 x i8] c"./custom_links.so\00"
@.str.14 = [6 x i8] c"execv\00"
@EXIT_FAILURE = global i32 0
define i32 @host_exec(i32 %0, i8* %1, i8** %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8**
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca [37 x i8]
%12 = alloca [1024 x i8]
%13 = alloca i8*
%14 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i8** %2, i8*** %7
%15 = bitcast [37 x i8]* %11 to i8*
%16 = call i32 (...) @fork()
store i32 %16, i32* %14
store i32 1, i32* %10
%17 = load i32, i32* %14
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %3
%20 = load i32, i32* @stderr
%21 = call i32 @fprintf(i32 %20, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %4
br label %140
22: ; preds = %3
%23 = load i32, i32* %14
%24 = icmp sgt i32 %23, 0
br i1 %24, label %25, label %99
25: ; preds = %22
%26 = load i32, i32* %5
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.1, i64 0, i64 0), i32 %26)
%28 = load i32, i32* %14
%29 = call i32 @waitpid(i32 %28, i32* %8, i32 0)
%30 = load i32, i32* %8
%31 = call i64 @WIFSIGNALED(i32 %30)
%32 = icmp ne i64 %31, 0
br i1 %32, label %33, label %48
33: ; preds = %25
%34 = load i32, i32* %8
%35 = call i32 @WTERMSIG(i32 %34)
store i32 %35, i32* %9
%36 = getelementptr inbounds [1024 x i8], [1024 x i8]* %12, i64 0, i64 0
%37 = load i32, i32* %9
%38 = load i32, i32* %9
%39 = call i8* @strsignal(i32 %38)
%40 = call i32 @snprintf(i8* %36, i32 1024, i8* getelementptr inbounds ([59 x i8], [59 x i8]* @.str.2, i64 0, i64 0), i32 %37, i8* %39)
%41 = getelementptr inbounds [1024 x i8], [1024 x i8]* %12, i64 0, i64 0
%42 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* %41)
%43 = load i32, i32* %5
%44 = getelementptr inbounds [1024 x i8], [1024 x i8]* %12, i64 0, i64 0
%45 = getelementptr inbounds [1024 x i8], [1024 x i8]* %12, i64 0, i64 0
%46 = call i32 @strlen(i8* %45)
%47 = call i32 @send_comm(i32 %43, i8* %44, i32 %46)
br label %48
48: ; preds = %33, %25
%49 = load i32, i32* %8
%50 = call i64 @WIFEXITED(i32 %49)
%51 = icmp ne i64 %50, 0
br i1 %51, label %52, label %70
52: ; preds = %48
%53 = load i32, i32* %8
%54 = call i32 @WEXITSTATUS(i32 %53)
store i32 %54, i32* %9
%55 = getelementptr inbounds [1024 x i8], [1024 x i8]* %12, i64 0, i64 0
%56 = load i32, i32* %9
%57 = load i32, i32* %9
%58 = load i32, i32* @EXIT_SUCCESS
%59 = icmp eq i32 %57, %58
%60 = zext i1 %59 to i64
%61 = select i1 %59, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.6, i64 0, i64 0)
%62 = call i32 @snprintf(i8* %55, i32 1024, i8* getelementptr inbounds ([62 x i8], [62 x i8]* @.str.4, i64 0, i64 0), i32 %56, i8* %61)
%63 = getelementptr inbounds [1024 x i8], [1024 x i8]* %12, i64 0, i64 0
%64 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* %63)
%65 = load i32, i32* %5
%66 = getelementptr inbounds [1024 x i8], [1024 x i8]* %12, i64 0, i64 0
%67 = getelementptr inbounds [1024 x i8], [1024 x i8]* %12, i64 0, i64 0
%68 = call i32 @strlen(i8* %67)
%69 = call i32 @send_comm(i32 %65, i8* %66, i32 %68)
br label %70
70: ; preds = %52, %48
%71 = load i32, i32* %5
%72 = getelementptr inbounds [37 x i8], [37 x i8]* %11, i64 0, i64 0
%73 = getelementptr inbounds [37 x i8], [37 x i8]* %11, i64 0, i64 0
%74 = call i32 @strlen(i8* %73)
%75 = call i32 @send_comm(i32 %71, i8* %72, i32 %74)
%76 = load i32, i32* %5
%77 = call i32 @recv_comm_until(i32 %76, i8** %13, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.7, i64 0, i64 0))
%78 = load i8*, i8** %13
%79 = getelementptr inbounds i8, i8* %78, i64 0
%80 = load i8, i8* %79
%81 = sext i8 %80 to i32
%82 = icmp eq i32 %81, 121
br i1 %82, label %89, label %83
83: ; preds = %70
%84 = load i8*, i8** %13
%85 = getelementptr inbounds i8, i8* %84, i64 0
%86 = load i8, i8* %85
%87 = sext i8 %86 to i32
%88 = icmp eq i32 %87, 89
br i1 %88, label %89, label %94
89: ; preds = %83, %70
%90 = load i8*, i8** %13
%91 = call i32 @free(i8* %90)
%92 = load i32, i32* %5
%93 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.8, i64 0, i64 0), i32 %92)
store i32 1, i32* %4
br label %140
94: ; preds = %83
%95 = load i8*, i8** %13
%96 = call i32 @free(i8* %95)
%97 = load i32, i32* %5
%98 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.9, i64 0, i64 0), i32 %97)
store i32 0, i32* %4
br label %140
99: ; preds = %22
br label %100
100: ; preds = %99
%101 = load i64, i64* @STDIN_FILENO
%102 = call i32 @close(i64 %101)
%103 = load i64, i64* @STDOUT_FILENO
%104 = call i32 @close(i64 %103)
%105 = load i64, i64* @STDERR_FILENO
%106 = call i32 @close(i64 %105)
%107 = load i32, i32* %5
%108 = load i32, i32* @IPPROTO_TCP
%109 = load i32, i32* @TCP_NODELAY
%110 = bitcast i32* %10 to i8*
%111 = call i32 @setsockopt(i32 %107, i32 %108, i32 %109, i8* %110, i32 4)
%112 = load i32, i32* %5
%113 = call i64 @dup(i32 %112)
%114 = load i64, i64* @STDIN_FILENO
%115 = icmp ne i64 %113, %114
br i1 %115, label %126, label %116
116: ; preds = %100
%117 = load i32, i32* %5
%118 = call i64 @dup(i32 %117)
%119 = load i64, i64* @STDOUT_FILENO
%120 = icmp ne i64 %118, %119
br i1 %120, label %126, label %121
121: ; preds = %116
%122 = load i32, i32* %5
%123 = call i64 @dup(i32 %122)
%124 = load i64, i64* @STDERR_FILENO
%125 = icmp ne i64 %123, %124
br i1 %125, label %126, label %128
126: ; preds = %121, %116, %100
%127 = call i32 @perror(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.10, i64 0, i64 0))
br label %128
128: ; preds = %126, %121
%129 = load i8*, i8** %6
%130 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.11, i64 0, i64 0), i8* %129)
%131 = load i32, i32* @stdout
%132 = call i32 @fflush(i32 %131)
%133 = call i32 @setenv(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.12, i64 0, i64 0), i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.13, i64 0, i64 0), i32 1)
%134 = load i8*, i8** %6
%135 = load i8**, i8*** %7
%136 = call i32 @execv(i8* %134, i8** %135)
%137 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.14, i64 0, i64 0))
%138 = load i32, i32* @EXIT_FAILURE
%139 = call i32 @exit(i32 %138)
unreachable
140: ; preds = %94, %89, %19
%141 = load i32, i32* %4
ret i32 %141
}

LLVM IR (-O2):
@__const.host_exec.restart_prompt = [37 x i8] c"[netdeploy] Restart process? (y/n): \00"
@stderr = global i32 0
@.str = [13 x i8] c"Fork failed\0A\00"
@.str.1 = [38 x i8] c"Waiting for client %d to be finished\0A\00"
@.str.2 = [59 x i8] c"\0A[netdeploy] Hosted process was killed by signal: %d (%s)\0A\00"
@.str.3 = [3 x i8] c"%s\00"
@.str.4 = [62 x i8] c"\0A[netdeploy] Hosted process exited with status code: %d (%s)\0A\00"
@EXIT_SUCCESS = global i32 0
@.str.5 = [13 x i8] c"EXIT_SUCCESS\00"
@.str.6 = [13 x i8] c"EXIT_FAILURE\00"
@.str.7 = [2 x i8] c"\0A\00"
@.str.8 = [40 x i8] c"Client %d is restarting hosted process\0A\00"
@.str.9 = [20 x i8] c"Client %d finished\0A\00"
@STDIN_FILENO = global i64 0
@STDOUT_FILENO = global i64 0
@STDERR_FILENO = global i64 0
@IPPROTO_TCP = global i32 0
@TCP_NODELAY = global i32 0
@.str.10 = [43 x i8] c"Error duplicating socket for stdin/out/err\00"
@.str.11 = [38 x i8] c"[netdeploy] Connected to binary \22%s\22\0A\00"
@stdout = global i32 0
@.str.12 = [11 x i8] c"LD_PRELOAD\00"
@.str.13 = [18 x i8] c"./custom_links.so\00"
@.str.14 = [6 x i8] c"execv\00"
@EXIT_FAILURE = global i32 0
define i32 @host_exec(i32 %0, i8* %1, i8** %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca [37 x i8]
%7 = alloca [1024 x i8]
%8 = alloca i8*
%9 = bitcast i32* %4 to i8*
%10 = bitcast i32* %5 to i8*
%11 = getelementptr inbounds [37 x i8], [37 x i8]* %6, i64 0, i64 0
%12 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%13 = bitcast i8** %8 to i8*
%14 = tail call i32 (...) @fork()
store i32 1, i32* %5
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %19
16: ; preds = %3
%17 = load i32, i32* @stderr
%18 = tail call i32 @fprintf(i32 %17, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
br label %93
19: ; preds = %3
%20 = icmp eq i32 %14, 0
br i1 %20, label %61, label %21
21: ; preds = %19
%22 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([38 x i8], [38 x i8]* @.str.1, i64 0, i64 0), i32 %0)
%23 = call i32 @waitpid(i32 %14, i32* %4, i32 0)
%24 = load i32, i32* %4
%25 = call i64 @WIFSIGNALED(i32 %24)
%26 = icmp eq i64 %25, 0
br i1 %26, label %35, label %27
27: ; preds = %21
%28 = load i32, i32* %4
%29 = call i32 @WTERMSIG(i32 %28)
%30 = call i8* @strsignal(i32 %29)
%31 = call i32 @snprintf(i8* %12, i32 1024, i8* getelementptr inbounds ([59 x i8], [59 x i8]* @.str.2, i64 0, i64 0), i32 %29, i8* %30)
%32 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* %12)
%33 = call i32 @strlen(i8* %12)
%34 = call i32 @send_comm(i32 %0, i8* %12, i32 %33)
br label %35
35: ; preds = %27, %21
%36 = load i32, i32* %4
%37 = call i64 @WIFEXITED(i32 %36)
%38 = icmp eq i64 %37, 0
br i1 %38, label %49, label %39
39: ; preds = %35
%40 = load i32, i32* %4
%41 = call i32 @WEXITSTATUS(i32 %40)
%42 = load i32, i32* @EXIT_SUCCESS
%43 = icmp eq i32 %41, %42
%44 = select i1 %43, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.6, i64 0, i64 0)
%45 = call i32 @snprintf(i8* %12, i32 1024, i8* getelementptr inbounds ([62 x i8], [62 x i8]* @.str.4, i64 0, i64 0), i32 %41, i8* %44)
%46 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* %12)
%47 = call i32 @strlen(i8* %12)
%48 = call i32 @send_comm(i32 %0, i8* %12, i32 %47)
br label %49
49: ; preds = %39, %35
%50 = call i32 @strlen(i8* %11)
%51 = call i32 @send_comm(i32 %0, i8* %11, i32 %50)
%52 = call i32 @recv_comm_until(i32 %0, i8** %8, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.7, i64 0, i64 0))
%53 = load i8*, i8** %8
%54 = load i8, i8* %53
switch i8 %54, label %58 [
i8 121, label %55
i8 89, label %55
]
55: ; preds = %49, %49
%56 = call i32 @free(i8* %53)
%57 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([40 x i8], [40 x i8]* @.str.8, i64 0, i64 0), i32 %0)
br label %93
58: ; preds = %49
%59 = call i32 @free(i8* %53)
%60 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.9, i64 0, i64 0), i32 %0)
br label %93
61: ; preds = %19
%62 = load i64, i64* @STDIN_FILENO
%63 = tail call i32 @close(i64 %62)
%64 = load i64, i64* @STDOUT_FILENO
%65 = tail call i32 @close(i64 %64)
%66 = load i64, i64* @STDERR_FILENO
%67 = tail call i32 @close(i64 %66)
%68 = load i32, i32* @IPPROTO_TCP
%69 = load i32, i32* @TCP_NODELAY
%70 = call i32 @setsockopt(i32 %0, i32 %68, i32 %69, i8* %10, i32 4)
%71 = call i64 @dup(i32 %0)
%72 = load i64, i64* @STDIN_FILENO
%73 = icmp eq i64 %71, %72
br i1 %73, label %74, label %82
74: ; preds = %61
%75 = call i64 @dup(i32 %0)
%76 = load i64, i64* @STDOUT_FILENO
%77 = icmp eq i64 %75, %76
br i1 %77, label %78, label %82
78: ; preds = %74
%79 = call i64 @dup(i32 %0)
%80 = load i64, i64* @STDERR_FILENO
%81 = icmp eq i64 %79, %80
br i1 %81, label %84, label %82
82: ; preds = %78, %74, %61
%83 = call i32 @perror(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.10, i64 0, i64 0))
br label %84
84: ; preds = %82, %78
%85 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([38 x i8], [38 x i8]* @.str.11, i64 0, i64 0), i8* %1)
%86 = load i32, i32* @stdout
%87 = call i32 @fflush(i32 %86)
%88 = call i32 @setenv(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.12, i64 0, i64 0), i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.13, i64 0, i64 0), i32 1)
%89 = call i32 @execv(i8* %1, i8** %2)
%90 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.14, i64 0, i64 0))
%91 = load i32, i32* @EXIT_FAILURE
%92 = call i32 @exit(i32 %91)
unreachable
93: ; preds = %58, %55, %16
%94 = phi i32 [ 0, %16 ], [ 1, %55 ], [ 0, %58 ]
ret i32 %94
}