#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*/ O_RDWR;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dup(int /*<<< orphan*/);
scalar_t__ exec(char *, char **);
int /*<<< orphan*/ exit(int);
scalar_t__ fork();
scalar_t__ open(char *, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, char *, ...);
int /*<<< orphan*/ setsid();
pid_t run_on_tty(char *prog, char **argv, char *tty_name) {
  int i;
  pid_t pid;
  pid = fork();
  if (pid == 0) {
    for (i = 0; i < 3; i++)
      close(i);
    if (open(tty_name, O_RDWR, 0) < 0) {
      printf("Cannot open %s\n", tty_name);
      exit(1);
    }
    for (i = 0; i < 2; i++)
      dup(0);
    setsid();
    if (exec(prog, argv) < 0) {
      printf("Cannot exec %s on %s\n", prog, tty_name);
      exit(1);
    }
  }
  return pid;
}


x86 assembly (clang, -O0):
<run_on_tty>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1274 <run_on_tty+0xf4>
movl   $0x0,-0x1c(%rbp)
cmpl   $0x3,-0x1c(%rbp)
jge    11d1 <run_on_tty+0x51>
mov    -0x1c(%rbp),%edi
call   1050 <close@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    11b1 <run_on_tty+0x31>
mov    -0x18(%rbp),%rdi
mov    0x2e0c(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <open@plt>
cmp    $0x0,%rax
jge    120b <run_on_tty+0x8b>
mov    -0x18(%rbp),%rsi
lea    0xe06(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
movl   $0x0,-0x1c(%rbp)
cmpl   $0x2,-0x1c(%rbp)
jge    1231 <run_on_tty+0xb1>
xor    %edi,%edi
call   1040 <dup@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1212 <run_on_tty+0x92>
mov    $0x0,%al
call   1060 <setsid@plt>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1090 <exec@plt>
cmp    $0x0,%rax
jge    126f <run_on_tty+0xef>
mov    -0x8(%rbp),%rsi
mov    -0x18(%rbp),%rdx
lea    0xdb2(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
jmp    1274 <run_on_tty+0xf4>
mov    -0x28(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<run_on_tty>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%r12
mov    %rdi,%r15
xor    %eax,%eax
call   10a0 <fork@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    11f8 <run_on_tty+0x78>
xor    %edi,%edi
call   1050 <close@plt>
mov    $0x1,%edi
call   1050 <close@plt>
mov    $0x2,%edi
call   1050 <close@plt>
mov    0x2e26(%rip),%rax
mov    (%rax),%esi
mov    %r14,%rdi
xor    %edx,%edx
call   1070 <open@plt>
test   %rax,%rax
js     1207 <run_on_tty+0x87>
xor    %edi,%edi
call   1040 <dup@plt>
xor    %edi,%edi
call   1040 <dup@plt>
xor    %eax,%eax
call   1060 <setsid@plt>
mov    %r15,%rdi
mov    %r12,%rsi
call   1090 <exec@plt>
test   %rax,%rax
js     1222 <run_on_tty+0xa2>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
lea    0xdf2(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1030 <printf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
lea    0xde7(%rip),%rdi
mov    %r15,%rsi
mov    %r14,%rdx
xor    %eax,%eax
call   1030 <printf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@O_RDWR = global i32 0
@.str = [16 x i8] c"Cannot open %s\0A\00"
@.str.1 = [22 x i8] c"Cannot exec %s on %s\0A\00"
define i64 @run_on_tty(i8* %0, i8** %1, i8* %2) {
%4 = alloca i8*
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i64
store i8* %0, i8** %4
store i8** %1, i8*** %5
store i8* %2, i8** %6
%9 = call i64 (...) @fork()
store i64 %9, i64* %8
%10 = load i64, i64* %8
%11 = icmp eq i64 %10, 0
br i1 %11, label %12, label %52
12: ; preds = %3
store i32 0, i32* %7
br label %13
13: ; preds = %19, %12
%14 = load i32, i32* %7
%15 = icmp slt i32 %14, 3
br i1 %15, label %16, label %22
16: ; preds = %13
%17 = load i32, i32* %7
%18 = call i32 @close(i32 %17)
br label %19
19: ; preds = %16
%20 = load i32, i32* %7
%21 = add nsw i32 %20, 1
store i32 %21, i32* %7
br label %13
22: ; preds = %13
%23 = load i8*, i8** %6
%24 = load i32, i32* @O_RDWR
%25 = call i64 @open(i8* %23, i32 %24, i32 0)
%26 = icmp slt i64 %25, 0
br i1 %26, label %27, label %31
27: ; preds = %22
%28 = load i8*, i8** %6
%29 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8* %28)
%30 = call i32 @exit(i32 1)
unreachable
31: ; preds = %22
store i32 0, i32* %7
br label %32
32: ; preds = %37, %31
%33 = load i32, i32* %7
%34 = icmp slt i32 %33, 2
br i1 %34, label %35, label %40
35: ; preds = %32
%36 = call i32 @dup(i32 0)
br label %37
37: ; preds = %35
%38 = load i32, i32* %7
%39 = add nsw i32 %38, 1
store i32 %39, i32* %7
br label %32
40: ; preds = %32
%41 = call i32 (...) @setsid()
%42 = load i8*, i8** %4
%43 = load i8**, i8*** %5
%44 = call i64 @exec(i8* %42, i8** %43)
%45 = icmp slt i64 %44, 0
br i1 %45, label %46, label %51
46: ; preds = %40
%47 = load i8*, i8** %4
%48 = load i8*, i8** %6
%49 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i8* %47, i8* %48)
%50 = call i32 @exit(i32 1)
unreachable
51: ; preds = %40
br label %52
52: ; preds = %51, %3
%53 = load i64, i64* %8
ret i64 %53
}

LLVM IR (-O2):
@O_RDWR = global i32 0
@.str = [16 x i8] c"Cannot open %s\0A\00"
@.str.1 = [22 x i8] c"Cannot exec %s on %s\0A\00"
define i64 @run_on_tty(i8* %0, i8** %1, i8* %2) {
%4 = tail call i64 (...) @fork()
%5 = icmp eq i64 %4, 0
br i1 %5, label %6, label %25
6: ; preds = %3
%7 = tail call i32 @close(i32 0)
%8 = tail call i32 @close(i32 1)
%9 = tail call i32 @close(i32 2)
%10 = load i32, i32* @O_RDWR
%11 = tail call i64 @open(i8* %2, i32 %10, i32 0)
%12 = icmp slt i64 %11, 0
br i1 %12, label %19, label %13
13: ; preds = %6
%14 = tail call i32 @dup(i32 0)
%15 = tail call i32 @dup(i32 0)
%16 = tail call i32 (...) @setsid()
%17 = tail call i64 @exec(i8* %0, i8** %1)
%18 = icmp slt i64 %17, 0
br i1 %18, label %22, label %25
19: ; preds = %6
%20 = tail call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8* %2)
%21 = tail call i32 @exit(i32 1)
unreachable
22: ; preds = %13
%23 = tail call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i8* %0, i8* %2)
%24 = tail call i32 @exit(i32 1)
unreachable
25: ; preds = %13, %3
ret i64 %4
}