#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*/ tty_name;
struct winsize {
  int ws_col;
  int ws_row;
  int ws_xpixel;
  int ws_ypixel;
};

/* Variables and functions */
int /*<<< orphan*/ F_SETFL;
int /*<<< orphan*/ O_NONBLOCK;
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ TIOCSWINSZ;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dup(int /*<<< orphan*/);
int /*<<< orphan*/ execv(char const *, char *const *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fcntl(int, int /*<<< orphan*/, int /*<<< orphan*/);
int fork();
int get_pty(char *, int);
int getdtablesize();
int /*<<< orphan*/ ioctl(int, int /*<<< orphan*/, struct winsize *);
int /*<<< orphan*/ open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ setsid();
int run_process(const char *path, char **argv, int *fd_ptr, int *pid_ptr) {
  int pty_fd, pid, i, nb_fds;
  char tty_name[1024];
  struct winsize ws;
  pty_fd = get_pty(tty_name, sizeof(tty_name));
  fcntl(pty_fd, F_SETFL, O_NONBLOCK);
  if (pty_fd < 0)
    return -1;
  ws.ws_col = 80;
  ws.ws_row = 25;
  ws.ws_xpixel = ws.ws_col;
  ws.ws_ypixel = ws.ws_row;
  ioctl(pty_fd, TIOCSWINSZ, &ws);
  pid = fork();
  if (pid < 0)
    return -1;
  if (pid == 0) {
    nb_fds = getdtablesize();
    for (i = 0; i < nb_fds; i++)
      close(i);
    open(tty_name, O_RDWR);
    dup(0);
    dup(0);
    setsid();
    execv(path, (char *const *)argv);
    exit(1);
  }
  *fd_ptr = pty_fd;
  *pid_ptr = pid;
  return 0;
}


x86 assembly (clang, -O0):
<run_process>:
push   %rbp
mov    %rsp,%rbp
sub    $0x450,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
lea    -0x440(%rbp),%rdi
mov    $0x400,%esi
call   10d0 <get_pty@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%edi
mov    0x2ddf(%rip),%rax
mov    (%rax),%esi
mov    0x2dee(%rip),%rax
mov    (%rax),%edx
call   1030 <fcntl@plt>
cmpl   $0x0,-0x2c(%rbp)
jge    120f <run_process+0x5f>
movl   $0xffffffff,-0x4(%rbp)
jmp    130a <run_process+0x15a>
movl   $0x50,-0x450(%rbp)
movl   $0x19,-0x44c(%rbp)
mov    -0x450(%rbp),%eax
mov    %eax,-0x448(%rbp)
mov    -0x44c(%rbp),%eax
mov    %eax,-0x444(%rbp)
mov    -0x2c(%rbp),%edi
mov    0x2d93(%rip),%rax
mov    (%rax),%esi
lea    -0x450(%rbp),%rdx
call   1050 <ioctl@plt>
mov    $0x0,%al
call   10b0 <fork@plt>
mov    %eax,-0x30(%rbp)
cmpl   $0x0,-0x30(%rbp)
jge    1273 <run_process+0xc3>
movl   $0xffffffff,-0x4(%rbp)
jmp    130a <run_process+0x15a>
cmpl   $0x0,-0x30(%rbp)
jne    12f1 <run_process+0x141>
mov    $0x0,%al
call   10c0 <getdtablesize@plt>
mov    %eax,-0x38(%rbp)
movl   $0x0,-0x34(%rbp)
mov    -0x34(%rbp),%eax
cmp    -0x38(%rbp),%eax
jge    12b0 <run_process+0x100>
mov    -0x34(%rbp),%edi
call   1060 <close@plt>
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    128e <run_process+0xde>
lea    -0x440(%rbp),%rdi
mov    0x2d2a(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
xor    %edi,%edi
call   1040 <dup@plt>
xor    %edi,%edi
call   1040 <dup@plt>
mov    $0x0,%al
call   1070 <setsid@plt>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   10a0 <execv@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x2c(%rbp),%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x30(%rbp),%ecx
mov    -0x28(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x450,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<run_process>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x418,%rsp
mov    %rcx,%r12
mov    %rdx,%r13
mov    %rsi,%r14
mov    %rdi,%r15
lea    0x10(%rsp),%rdi
mov    $0x400,%esi
call   10d0 <get_pty@plt>
mov    %eax,%ebp
mov    0x2de3(%rip),%rax
mov    (%rax),%esi
mov    0x2df2(%rip),%rax
mov    (%rax),%edx
mov    %ebp,%edi
call   1030 <fcntl@plt>
mov    $0xffffffff,%ebx
test   %ebp,%ebp
js     1235 <run_process+0x85>
movaps 0xdf9(%rip),%xmm0
movaps %xmm0,(%rsp)
mov    0x2dc6(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
mov    %ebp,%edi
call   1050 <ioctl@plt>
xor    %eax,%eax
call   10b0 <fork@plt>
test   %eax,%eax
js     1235 <run_process+0x85>
je     1249 <run_process+0x99>
mov    %ebp,0x0(%r13)
mov    %eax,(%r12)
xor    %ebx,%ebx
mov    %ebx,%eax
add    $0x418,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
xor    %ebx,%ebx
xor    %eax,%eax
call   10c0 <getdtablesize@plt>
test   %eax,%eax
jle    1266 <run_process+0xb6>
mov    %eax,%ebp
mov    %ebx,%edi
call   1060 <close@plt>
add    $0x1,%ebx
cmp    %ebx,%ebp
jne    1258 <run_process+0xa8>
mov    0x2d7b(%rip),%rax
mov    (%rax),%esi
lea    0x10(%rsp),%rdi
call   1080 <open@plt>
xor    %edi,%edi
call   1040 <dup@plt>
xor    %edi,%edi
call   1040 <dup@plt>
xor    %eax,%eax
call   1070 <setsid@plt>
mov    %r15,%rdi
mov    %r14,%rsi
call   10a0 <execv@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
%struct.winsize = type { i32, i32, i32, i32 }
@F_SETFL = global i32 0
@O_NONBLOCK = global i32 0
@TIOCSWINSZ = global i32 0
@O_RDWR = global i32 0
define i32 @run_process(i8* %0, i8** %1, i32* %2, i32* %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8**
%8 = alloca i32*
%9 = alloca i32*
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca [1024 x i8]
%15 = alloca %struct.winsize
store i8* %0, i8** %6
store i8** %1, i8*** %7
store i32* %2, i32** %8
store i32* %3, i32** %9
%16 = getelementptr inbounds [1024 x i8], [1024 x i8]* %14, i64 0, i64 0
%17 = call i32 @get_pty(i8* %16, i32 1024)
store i32 %17, i32* %10
%18 = load i32, i32* %10
%19 = load i32, i32* @F_SETFL
%20 = load i32, i32* @O_NONBLOCK
%21 = call i32 @fcntl(i32 %18, i32 %19, i32 %20)
%22 = load i32, i32* %10
%23 = icmp slt i32 %22, 0
br i1 %23, label %24, label %25
24: ; preds = %4
store i32 -1, i32* %5
br label %72
25: ; preds = %4
%26 = getelementptr inbounds %struct.winsize, %struct.winsize* %15, i32 0, i32 0
store i32 80, i32* %26
%27 = getelementptr inbounds %struct.winsize, %struct.winsize* %15, i32 0, i32 1
store i32 25, i32* %27
%28 = getelementptr inbounds %struct.winsize, %struct.winsize* %15, i32 0, i32 0
%29 = load i32, i32* %28
%30 = getelementptr inbounds %struct.winsize, %struct.winsize* %15, i32 0, i32 2
store i32 %29, i32* %30
%31 = getelementptr inbounds %struct.winsize, %struct.winsize* %15, i32 0, i32 1
%32 = load i32, i32* %31
%33 = getelementptr inbounds %struct.winsize, %struct.winsize* %15, i32 0, i32 3
store i32 %32, i32* %33
%34 = load i32, i32* %10
%35 = load i32, i32* @TIOCSWINSZ
%36 = call i32 @ioctl(i32 %34, i32 %35, %struct.winsize* %15)
%37 = call i32 (...) @fork()
store i32 %37, i32* %11
%38 = load i32, i32* %11
%39 = icmp slt i32 %38, 0
br i1 %39, label %40, label %41
40: ; preds = %25
store i32 -1, i32* %5
br label %72
41: ; preds = %25
%42 = load i32, i32* %11
%43 = icmp eq i32 %42, 0
br i1 %43, label %44, label %67
44: ; preds = %41
%45 = call i32 (...) @getdtablesize()
store i32 %45, i32* %13
store i32 0, i32* %12
br label %46
46: ; preds = %53, %44
%47 = load i32, i32* %12
%48 = load i32, i32* %13
%49 = icmp slt i32 %47, %48
br i1 %49, label %50, label %56
50: ; preds = %46
%51 = load i32, i32* %12
%52 = call i32 @close(i32 %51)
br label %53
53: ; preds = %50
%54 = load i32, i32* %12
%55 = add nsw i32 %54, 1
store i32 %55, i32* %12
br label %46
56: ; preds = %46
%57 = getelementptr inbounds [1024 x i8], [1024 x i8]* %14, i64 0, i64 0
%58 = load i32, i32* @O_RDWR
%59 = call i32 @open(i8* %57, i32 %58)
%60 = call i32 @dup(i32 0)
%61 = call i32 @dup(i32 0)
%62 = call i32 (...) @setsid()
%63 = load i8*, i8** %6
%64 = load i8**, i8*** %7
%65 = call i32 @execv(i8* %63, i8** %64)
%66 = call i32 @exit(i32 1)
unreachable
67: ; preds = %41
%68 = load i32, i32* %10
%69 = load i32*, i32** %8
store i32 %68, i32* %69
%70 = load i32, i32* %11
%71 = load i32*, i32** %9
store i32 %70, i32* %71
store i32 0, i32* %5
br label %72
72: ; preds = %67, %40, %24
%73 = load i32, i32* %5
ret i32 %73
}

LLVM IR (-O2):
%struct.winsize = type { i32, i32, i32, i32 }
@F_SETFL = global i32 0
@O_NONBLOCK = global i32 0
@TIOCSWINSZ = global i32 0
@O_RDWR = global i32 0
define i32 @run_process(i8* %0, i8** %1, i32* nocapture writeonly %2, i32* nocapture writeonly %3) {
%5 = alloca [1024 x i8]
%6 = alloca <4 x i32>
%7 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%8 = bitcast <4 x i32>* %6 to i8*
%9 = call i32 @get_pty(i8* %7, i32 1024)
%10 = load i32, i32* @F_SETFL
%11 = load i32, i32* @O_NONBLOCK
%12 = call i32 @fcntl(i32 %9, i32 %10, i32 %11)
%13 = icmp slt i32 %9, 0
br i1 %13, label %39, label %14
14: ; preds = %4
%15 = bitcast <4 x i32>* %6 to %struct.winsize*
store <4 x i32> <i32 80, i32 25, i32 80, i32 25>, <4 x i32>* %6
%16 = load i32, i32* @TIOCSWINSZ
%17 = call i32 @ioctl(i32 %9, i32 %16, %struct.winsize* %15)
%18 = call i32 (...) @fork()
%19 = icmp slt i32 %18, 0
br i1 %19, label %39, label %20
20: ; preds = %14
%21 = icmp eq i32 %18, 0
br i1 %21, label %22, label %38
22: ; preds = %20
%23 = call i32 (...) @getdtablesize()
%24 = icmp sgt i32 %23, 0
br i1 %24, label %25, label %30
25: ; preds = %22, %25
%26 = phi i32 [ %28, %25 ], [ 0, %22 ]
%27 = call i32 @close(i32 %26)
%28 = add nuw nsw i32 %26, 1
%29 = icmp eq i32 %28, %23
br i1 %29, label %30, label %25
30: ; preds = %25, %22
%31 = load i32, i32* @O_RDWR
%32 = call i32 @open(i8* %7, i32 %31)
%33 = call i32 @dup(i32 0)
%34 = call i32 @dup(i32 0)
%35 = call i32 (...) @setsid()
%36 = call i32 @execv(i8* %0, i8** %1)
%37 = call i32 @exit(i32 1)
unreachable
38: ; preds = %20
store i32 %9, i32* %2
store i32 %18, i32* %3
br label %39
39: ; preds = %14, %4, %38
%40 = phi i32 [ 0, %38 ], [ -1, %4 ], [ -1, %14 ]
ret i32 %40
}