#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_RDONLY;
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ TIOCSWINSZ;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dup(int /*<<< orphan*/);
int /*<<< orphan*/ errno;
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*/ put_status(int /*<<< orphan*/ *, char *, ...);
int /*<<< orphan*/ setenv(char *, char *, int);
int /*<<< orphan*/ setsid();
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
int /*<<< orphan*/ unsetenv(char *);
int run_process(const char *path, const char **argv, int *fd_ptr, int *pid_ptr,
                int cols, int rows, int is_shell) {
  int pty_fd, pid, i, nb_fds;
  char tty_name[32];
  struct winsize ws;
  pty_fd = get_pty(tty_name, sizeof(tty_name));
  if (pty_fd < 0) {
    put_status(NULL, "run_process: cannot get tty: %s", strerror(errno));
    return -1;
  }
  fcntl(pty_fd, F_SETFL, O_NONBLOCK);
  ws.ws_col = cols;
  ws.ws_row = rows;
  ws.ws_xpixel = ws.ws_col;
  ws.ws_ypixel = ws.ws_row;
  ioctl(pty_fd, TIOCSWINSZ, &ws);
  pid = fork();
  if (pid < 0) {
    put_status(NULL, "run_process: cannot fork");
    return -1;
  }
  if (pid == 0) {
    nb_fds = getdtablesize();
    for (i = 0; i < nb_fds; i++)
      close(i);
    if (is_shell == 2) {
      open("/dev/null", O_RDONLY);
      open(tty_name, O_RDWR);
      dup(0);
    } else {
      open(tty_name, O_RDWR);
      dup(0);
      dup(0);
    }
    setsid();
    setenv("TERM", "xterm", 1);
    unsetenv("PAGER");
    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    $0x70,%rsp
mov    0x10(%rbp),%eax
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    %r8d,-0x2c(%rbp)
mov    %r9d,-0x30(%rbp)
lea    -0x60(%rbp),%rdi
mov    $0x20,%esi
call   1100 <get_pty@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
jge    125c <run_process+0x6c>
mov    0x2d83(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
mov    %eax,%edx
xor    %eax,%eax
mov    %eax,%edi
lea    0xdb7(%rip),%rsi
mov    $0x0,%al
call   1110 <put_status@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13ca <run_process+0x1da>
mov    -0x34(%rbp),%edi
mov    0x2d5a(%rip),%rax
mov    (%rax),%esi
mov    0x2d71(%rip),%rax
mov    (%rax),%edx
call   1030 <fcntl@plt>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x70(%rbp)
mov    -0x30(%rbp),%eax
mov    %eax,-0x6c(%rbp)
mov    -0x70(%rbp),%eax
mov    %eax,-0x68(%rbp)
mov    -0x6c(%rbp),%eax
mov    %eax,-0x64(%rbp)
mov    -0x34(%rbp),%edi
mov    0x2d38(%rip),%rax
mov    (%rax),%esi
lea    -0x70(%rbp),%rdx
call   1060 <ioctl@plt>
mov    $0x0,%al
call   10e0 <fork@plt>
mov    %eax,-0x38(%rbp)
cmpl   $0x0,-0x38(%rbp)
jge    12d5 <run_process+0xe5>
xor    %eax,%eax
mov    %eax,%edi
lea    0xd5e(%rip),%rsi
mov    $0x0,%al
call   1110 <put_status@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13ca <run_process+0x1da>
cmpl   $0x0,-0x38(%rbp)
jne    13b1 <run_process+0x1c1>
mov    $0x0,%al
call   10f0 <getdtablesize@plt>
mov    %eax,-0x40(%rbp)
movl   $0x0,-0x3c(%rbp)
mov    -0x3c(%rbp),%eax
cmp    -0x40(%rbp),%eax
jge    1312 <run_process+0x122>
mov    -0x3c(%rbp),%edi
call   1070 <close@plt>
mov    -0x3c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x3c(%rbp)
jmp    12f0 <run_process+0x100>
cmpl   $0x2,0x10(%rbp)
jne    134f <run_process+0x15f>
mov    0x2cb5(%rip),%rax
mov    (%rax),%esi
lea    0xd0d(%rip),%rdi
call   1090 <open@plt>
lea    -0x60(%rbp),%rdi
mov    0x2cac(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
xor    %edi,%edi
call   1050 <dup@plt>
jmp    136f <run_process+0x17f>
lea    -0x60(%rbp),%rdi
mov    0x2c8e(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
xor    %edi,%edi
call   1050 <dup@plt>
xor    %edi,%edi
call   1050 <dup@plt>
mov    $0x0,%al
call   1080 <setsid@plt>
lea    0xcc6(%rip),%rdi
lea    0xcc4(%rip),%rsi
mov    $0x1,%edx
call   1040 <setenv@plt>
lea    0xcb9(%rip),%rdi
call   10a0 <unsetenv@plt>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   10d0 <execv@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0x34(%rbp),%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x38(%rbp),%ecx
mov    -0x28(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<run_process>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x48,%rsp
mov    %r9d,%ebx
mov    %r8d,%r14d
mov    %rcx,%r12
mov    %rdx,%r13
mov    %rsi,0x18(%rsp)
mov    %rdi,%r15
lea    0x20(%rsp),%rdi
mov    $0x20,%esi
call   1100 <get_pty@plt>
test   %eax,%eax
js     1280 <run_process+0x90>
mov    %eax,%ebp
mov    0x2d92(%rip),%rax
mov    (%rax),%esi
mov    0x2da9(%rip),%rax
mov    (%rax),%edx
mov    %ebp,%edi
call   1030 <fcntl@plt>
mov    %r14d,0x8(%rsp)
mov    %ebx,0xc(%rsp)
mov    %r14d,0x10(%rsp)
mov    %ebx,0x14(%rsp)
mov    0x2d77(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
mov    %ebp,%edi
call   1060 <ioctl@plt>
xor    %eax,%eax
call   10e0 <fork@plt>
test   %eax,%eax
js     12a2 <run_process+0xb2>
je     12c6 <run_process+0xd6>
mov    %ebp,0x0(%r13)
mov    %eax,(%r12)
xor    %eax,%eax
jmp    12b7 <run_process+0xc7>
mov    0x2d31(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
lea    0xd6b(%rip),%rsi
xor    %edi,%edi
mov    %eax,%edx
xor    %eax,%eax
call   1110 <put_status@plt>
jmp    12b2 <run_process+0xc2>
lea    0xd77(%rip),%rsi
xor    %edi,%edi
xor    %eax,%eax
call   1110 <put_status@plt>
mov    $0xffffffff,%eax
add    $0x48,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x80(%rsp),%r14d
xor    %ebx,%ebx
xor    %eax,%eax
call   10f0 <getdtablesize@plt>
test   %eax,%eax
jle    12eb <run_process+0xfb>
mov    %eax,%ebp
mov    %ebx,%edi
call   1070 <close@plt>
add    $0x1,%ebx
cmp    %ebx,%ebp
jne    12dd <run_process+0xed>
cmp    $0x2,%r14d
jne    131b <run_process+0x12b>
mov    0x2ce0(%rip),%rax
mov    (%rax),%esi
lea    0xd38(%rip),%rdi
call   1090 <open@plt>
mov    0x2cdb(%rip),%rax
mov    (%rax),%esi
lea    0x20(%rsp),%rdi
call   1090 <open@plt>
jmp    1335 <run_process+0x145>
mov    0x2cc6(%rip),%rax
mov    (%rax),%esi
lea    0x20(%rsp),%rdi
call   1090 <open@plt>
xor    %edi,%edi
call   1050 <dup@plt>
xor    %edi,%edi
call   1050 <dup@plt>
xor    %eax,%eax
call   1080 <setsid@plt>
lea    0xcf9(%rip),%rdi
lea    0xcf7(%rip),%rsi
mov    $0x1,%edx
call   1040 <setenv@plt>
lea    0xcec(%rip),%rdi
call   10a0 <unsetenv@plt>
mov    %r15,%rdi
mov    0x18(%rsp),%rsi
call   10d0 <execv@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
%struct.winsize = type { i32, i32, i32, i32 }
@.str = [32 x i8] c"run_process: cannot get tty: %s\00"
@errno = global i32 0
@F_SETFL = global i32 0
@O_NONBLOCK = global i32 0
@TIOCSWINSZ = global i32 0
@.str.1 = [25 x i8] c"run_process: cannot fork\00"
@.str.2 = [10 x i8] c"/dev/null\00"
@O_RDONLY = global i32 0
@O_RDWR = global i32 0
@.str.3 = [5 x i8] c"TERM\00"
@.str.4 = [6 x i8] c"xterm\00"
@.str.5 = [6 x i8] c"PAGER\00"
define i32 @run_process(i8* %0, i8** %1, i32* %2, i32* %3, i32 %4, i32 %5, i32 %6) {
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i8**
%11 = alloca i32*
%12 = alloca i32*
%13 = alloca i32
%14 = alloca i32
%15 = alloca i32
%16 = alloca i32
%17 = alloca i32
%18 = alloca i32
%19 = alloca i32
%20 = alloca [32 x i8]
%21 = alloca %struct.winsize
store i8* %0, i8** %9
store i8** %1, i8*** %10
store i32* %2, i32** %11
store i32* %3, i32** %12
store i32 %4, i32* %13
store i32 %5, i32* %14
store i32 %6, i32* %15
%22 = getelementptr inbounds [32 x i8], [32 x i8]* %20, i64 0, i64 0
%23 = call i32 @get_pty(i8* %22, i32 32)
store i32 %23, i32* %16
%24 = load i32, i32* %16
%25 = icmp slt i32 %24, 0
br i1 %25, label %26, label %30
26: ; preds = %7
%27 = load i32, i32* @errno
%28 = call i32 @strerror(i32 %27)
%29 = call i32 (i32*, i8*, ...) @put_status(i32* null, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i32 %28)
store i32 -1, i32* %8
br label %97
30: ; preds = %7
%31 = load i32, i32* %16
%32 = load i32, i32* @F_SETFL
%33 = load i32, i32* @O_NONBLOCK
%34 = call i32 @fcntl(i32 %31, i32 %32, i32 %33)
%35 = load i32, i32* %13
%36 = getelementptr inbounds %struct.winsize, %struct.winsize* %21, i32 0, i32 0
store i32 %35, i32* %36
%37 = load i32, i32* %14
%38 = getelementptr inbounds %struct.winsize, %struct.winsize* %21, i32 0, i32 1
store i32 %37, i32* %38
%39 = getelementptr inbounds %struct.winsize, %struct.winsize* %21, i32 0, i32 0
%40 = load i32, i32* %39
%41 = getelementptr inbounds %struct.winsize, %struct.winsize* %21, i32 0, i32 2
store i32 %40, i32* %41
%42 = getelementptr inbounds %struct.winsize, %struct.winsize* %21, i32 0, i32 1
%43 = load i32, i32* %42
%44 = getelementptr inbounds %struct.winsize, %struct.winsize* %21, i32 0, i32 3
store i32 %43, i32* %44
%45 = load i32, i32* %16
%46 = load i32, i32* @TIOCSWINSZ
%47 = call i32 @ioctl(i32 %45, i32 %46, %struct.winsize* %21)
%48 = call i32 (...) @fork()
store i32 %48, i32* %17
%49 = load i32, i32* %17
%50 = icmp slt i32 %49, 0
br i1 %50, label %51, label %53
51: ; preds = %30
%52 = call i32 (i32*, i8*, ...) @put_status(i32* null, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %8
br label %97
53: ; preds = %30
%54 = load i32, i32* %17
%55 = icmp eq i32 %54, 0
br i1 %55, label %56, label %92
56: ; preds = %53
%57 = call i32 (...) @getdtablesize()
store i32 %57, i32* %19
store i32 0, i32* %18
br label %58
58: ; preds = %65, %56
%59 = load i32, i32* %18
%60 = load i32, i32* %19
%61 = icmp slt i32 %59, %60
br i1 %61, label %62, label %68
62: ; preds = %58
%63 = load i32, i32* %18
%64 = call i32 @close(i32 %63)
br label %65
65: ; preds = %62
%66 = load i32, i32* %18
%67 = add nsw i32 %66, 1
store i32 %67, i32* %18
br label %58
68: ; preds = %58
%69 = load i32, i32* %15
%70 = icmp eq i32 %69, 2
br i1 %70, label %71, label %78
71: ; preds = %68
%72 = load i32, i32* @O_RDONLY
%73 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i32 %72)
%74 = getelementptr inbounds [32 x i8], [32 x i8]* %20, i64 0, i64 0
%75 = load i32, i32* @O_RDWR
%76 = call i32 @open(i8* %74, i32 %75)
%77 = call i32 @dup(i32 0)
br label %84
78: ; preds = %68
%79 = getelementptr inbounds [32 x i8], [32 x i8]* %20, i64 0, i64 0
%80 = load i32, i32* @O_RDWR
%81 = call i32 @open(i8* %79, i32 %80)
%82 = call i32 @dup(i32 0)
%83 = call i32 @dup(i32 0)
br label %84
84: ; preds = %78, %71
%85 = call i32 (...) @setsid()
%86 = call i32 @setenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), i32 1)
%87 = call i32 @unsetenv(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0))
%88 = load i8*, i8** %9
%89 = load i8**, i8*** %10
%90 = call i32 @execv(i8* %88, i8** %89)
%91 = call i32 @exit(i32 1)
unreachable
92: ; preds = %53
%93 = load i32, i32* %16
%94 = load i32*, i32** %11
store i32 %93, i32* %94
%95 = load i32, i32* %17
%96 = load i32*, i32** %12
store i32 %95, i32* %96
store i32 0, i32* %8
br label %97
97: ; preds = %92, %51, %26
%98 = load i32, i32* %8
ret i32 %98
}

LLVM IR (-O2):
%struct.winsize = type { i32, i32, i32, i32 }
@.str = [32 x i8] c"run_process: cannot get tty: %s\00"
@errno = global i32 0
@F_SETFL = global i32 0
@O_NONBLOCK = global i32 0
@TIOCSWINSZ = global i32 0
@.str.1 = [25 x i8] c"run_process: cannot fork\00"
@.str.2 = [10 x i8] c"/dev/null\00"
@O_RDONLY = global i32 0
@O_RDWR = global i32 0
@.str.3 = [5 x i8] c"TERM\00"
@.str.4 = [6 x i8] c"xterm\00"
@.str.5 = [6 x i8] c"PAGER\00"
define i32 @run_process(i8* %0, i8** %1, i32* nocapture writeonly %2, i32* nocapture writeonly %3, i32 %4, i32 %5, i32 %6) {
%8 = alloca [32 x i8]
%9 = alloca %struct.winsize
%10 = getelementptr inbounds [32 x i8], [32 x i8]* %8, i64 0, i64 0
%11 = bitcast %struct.winsize* %9 to i8*
%12 = call i32 @get_pty(i8* %10, i32 32)
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %18
14: ; preds = %7
%15 = load i32, i32* @errno
%16 = call i32 @strerror(i32 %15)
%17 = call i32 (i32*, i8*, ...) @put_status(i32* null, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i32 %16)
br label %61
18: ; preds = %7
%19 = load i32, i32* @F_SETFL
%20 = load i32, i32* @O_NONBLOCK
%21 = call i32 @fcntl(i32 %12, i32 %19, i32 %20)
%22 = getelementptr inbounds %struct.winsize, %struct.winsize* %9, i64 0, i32 0
store i32 %4, i32* %22
%23 = getelementptr inbounds %struct.winsize, %struct.winsize* %9, i64 0, i32 1
store i32 %5, i32* %23
%24 = getelementptr inbounds %struct.winsize, %struct.winsize* %9, i64 0, i32 2
store i32 %4, i32* %24
%25 = getelementptr inbounds %struct.winsize, %struct.winsize* %9, i64 0, i32 3
store i32 %5, i32* %25
%26 = load i32, i32* @TIOCSWINSZ
%27 = call i32 @ioctl(i32 %12, i32 %26, %struct.winsize* %9)
%28 = call i32 (...) @fork()
%29 = icmp slt i32 %28, 0
br i1 %29, label %30, label %32
30: ; preds = %18
%31 = call i32 (i32*, i8*, ...) @put_status(i32* null, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
br label %61
32: ; preds = %18
%33 = icmp eq i32 %28, 0
br i1 %33, label %34, label %60
34: ; preds = %32
%35 = call i32 (...) @getdtablesize()
%36 = icmp sgt i32 %35, 0
br i1 %36, label %37, label %42
37: ; preds = %34, %37
%38 = phi i32 [ %40, %37 ], [ 0, %34 ]
%39 = call i32 @close(i32 %38)
%40 = add nuw nsw i32 %38, 1
%41 = icmp eq i32 %40, %35
br i1 %41, label %42, label %37
42: ; preds = %37, %34
%43 = icmp eq i32 %6, 2
br i1 %43, label %44, label %49
44: ; preds = %42
%45 = load i32, i32* @O_RDONLY
%46 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i32 %45)
%47 = load i32, i32* @O_RDWR
%48 = call i32 @open(i8* %10, i32 %47)
br label %53
49: ; preds = %42
%50 = load i32, i32* @O_RDWR
%51 = call i32 @open(i8* %10, i32 %50)
%52 = call i32 @dup(i32 0)
br label %53
53: ; preds = %49, %44
%54 = call i32 @dup(i32 0)
%55 = call i32 (...) @setsid()
%56 = call i32 @setenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), i32 1)
%57 = call i32 @unsetenv(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0))
%58 = call i32 @execv(i8* %0, i8** %1)
%59 = call i32 @exit(i32 1)
unreachable
60: ; preds = %32
store i32 %12, i32* %2
store i32 %28, i32* %3
br label %61
61: ; preds = %60, %30, %14
%62 = phi i32 [ -1, %14 ], [ -1, %30 ], [ 0, %60 ]
ret i32 %62
}