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

/* Variables and functions */
int O_NOCTTY;
int O_RDWR;
int /*<<< orphan*/ STDERR_FILENO;
int /*<<< orphan*/ STDIN_FILENO;
int /*<<< orphan*/ STDOUT_FILENO;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ die(char *, char const *, ...);
int /*<<< orphan*/ dup2(int, int /*<<< orphan*/);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ execvp(char *, char **);
int open(char const *, int);
char *ptsname(int);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
void run_command(char *argv[], int master_in, int master_out) {
  const char *slavename;
  int slave;
  if (!(slavename = ptsname(master_in))) {
    die("Unable to determine name of slave pty: %s\n", strerror(errno));
  }
  if ((slave = open(slavename, O_RDWR)) == -1) {
    die("Unable to open slave pty \"%s\": %s\n", slavename, strerror(errno));
  }
  dup2(slave, STDIN_FILENO);
  close(slave);
  close(master_in);
  if (!(slavename = ptsname(master_out))) {
    die("Unable to determine name of slave pty: %s\n", strerror(errno));
  }
  if ((slave = open(slavename, O_RDWR | O_NOCTTY)) < 0) {
    die("Unable to open slave pty \"%s\": %s\n", slavename, strerror(errno));
  }
  dup2(slave, STDOUT_FILENO);
  dup2(slave, STDERR_FILENO);
  close(slave);
  close(master_out);
  execvp(*argv, argv);
  die("execvp(%s) failed: %s\n", *argv, strerror(errno));
}


x86 assembly (clang, -O0):
<run_command>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    -0xc(%rbp),%edi
call   1030 <ptsname@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    11b7 <run_command+0x47>
mov    0x2e29(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
movslq %eax,%rsi
lea    0xe50(%rip),%rdi
mov    $0x0,%al
call   1070 <die@plt>
mov    -0x18(%rbp),%rdi
mov    0x2e1e(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0xffffffff,%eax
jne    11ff <run_command+0x8f>
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    0x2de4(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x28(%rbp),%rsi
mov    %eax,%edx
lea    0xe33(%rip),%rdi
mov    $0x0,%al
call   1070 <die@plt>
mov    -0x1c(%rbp),%edi
mov    0x2daf(%rip),%rax
mov    (%rax),%esi
call   1040 <dup2@plt>
mov    -0x1c(%rbp),%edi
call   1050 <close@plt>
mov    -0xc(%rbp),%edi
call   1050 <close@plt>
mov    -0x10(%rbp),%edi
call   1030 <ptsname@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    1255 <run_command+0xe5>
mov    0x2d8b(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
movslq %eax,%rsi
lea    0xdb2(%rip),%rdi
mov    $0x0,%al
call   1070 <die@plt>
mov    -0x18(%rbp),%rdi
mov    0x2d80(%rip),%rax
mov    (%rax),%esi
mov    0x2d67(%rip),%rax
or     (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jge    12a6 <run_command+0x136>
mov    -0x18(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    0x2d3d(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x30(%rbp),%rsi
mov    %eax,%edx
lea    0xd8c(%rip),%rdi
mov    $0x0,%al
call   1070 <die@plt>
mov    -0x1c(%rbp),%edi
mov    0x2d40(%rip),%rax
mov    (%rax),%esi
call   1040 <dup2@plt>
mov    -0x1c(%rbp),%edi
mov    0x2cff(%rip),%rax
mov    (%rax),%esi
call   1040 <dup2@plt>
mov    -0x1c(%rbp),%edi
call   1050 <close@plt>
mov    -0x10(%rbp),%edi
call   1050 <close@plt>
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
mov    -0x8(%rbp),%rsi
call   1080 <execvp@plt>
mov    -0x8(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x38(%rbp)
mov    0x2cce(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x38(%rbp),%rsi
mov    %eax,%edx
lea    0xd40(%rip),%rdi
mov    $0x0,%al
call   1070 <die@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<run_command>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,%r14d
mov    %esi,%r12d
mov    %rdi,%r15
mov    %esi,%edi
call   1030 <ptsname@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    11b2 <run_command+0x42>
mov    0x2e2e(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
movslq %eax,%rsi
lea    0xe55(%rip),%rdi
xor    %eax,%eax
call   1070 <die@plt>
mov    0x2e27(%rip),%r13
mov    0x0(%r13),%esi
mov    %rbx,%rdi
call   1060 <open@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
jne    11ed <run_command+0x7d>
mov    0x2df5(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xe4a(%rip),%rdi
mov    %rbx,%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1070 <die@plt>
mov    0x2dc4(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
call   1040 <dup2@plt>
mov    %ebp,%edi
call   1050 <close@plt>
mov    %r12d,%edi
call   1050 <close@plt>
mov    %r14d,%edi
call   1030 <ptsname@plt>
mov    %rax,%rbp
test   %rax,%rax
jne    123b <run_command+0xcb>
mov    0x2da5(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
movslq %eax,%rsi
lea    0xdcc(%rip),%rdi
xor    %eax,%eax
call   1070 <die@plt>
mov    0x2d8e(%rip),%rax
mov    (%rax),%esi
or     0x0(%r13),%esi
mov    %rbp,%rdi
call   1060 <open@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    1277 <run_command+0x107>
mov    0x2d6b(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xdc0(%rip),%rdi
mov    %rbp,%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1070 <die@plt>
mov    0x2d72(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1040 <dup2@plt>
mov    0x2d32(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1040 <dup2@plt>
mov    %ebx,%edi
call   1050 <close@plt>
mov    %r14d,%edi
call   1050 <close@plt>
mov    (%r15),%rdi
mov    %r15,%rsi
call   1080 <execvp@plt>
mov    (%r15),%rbx
mov    0x2d0d(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xd85(%rip),%rdi
mov    %rbx,%rsi
mov    %eax,%edx
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1070 <die@plt>

LLVM IR (-O0):
@.str = [43 x i8] c"Unable to determine name of slave pty: %s\0A\00"
@errno = global i32 0
@O_RDWR = global i32 0
@.str.1 = [35 x i8] c"Unable to open slave pty \22%s\22: %s\0A\00"
@STDIN_FILENO = global i32 0
@O_NOCTTY = global i32 0
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
@.str.2 = [23 x i8] c"execvp(%s) failed: %s\0A\00"
define void @run_command(i8** %0, i32 %1, i32 %2) {
%4 = alloca i8**
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
store i8** %0, i8*** %4
store i32 %1, i32* %5
store i32 %2, i32* %6
%9 = load i32, i32* %5
%10 = call i8* @ptsname(i32 %9)
store i8* %10, i8** %7
%11 = icmp ne i8* %10, null
br i1 %11, label %18, label %12
12: ; preds = %3
%13 = load i32, i32* @errno
%14 = call i32 @strerror(i32 %13)
%15 = sext i32 %14 to i64
%16 = inttoptr i64 %15 to i8*
%17 = call i32 (i8*, i8*, ...) @die(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str, i64 0, i64 0), i8* %16)
br label %18
18: ; preds = %12, %3
%19 = load i8*, i8** %7
%20 = load i32, i32* @O_RDWR
%21 = call i32 @open(i8* %19, i32 %20)
store i32 %21, i32* %8
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %28
23: ; preds = %18
%24 = load i8*, i8** %7
%25 = load i32, i32* @errno
%26 = call i32 @strerror(i32 %25)
%27 = call i32 (i8*, i8*, ...) @die(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0), i8* %24, i32 %26)
br label %28
28: ; preds = %23, %18
%29 = load i32, i32* %8
%30 = load i32, i32* @STDIN_FILENO
%31 = call i32 @dup2(i32 %29, i32 %30)
%32 = load i32, i32* %8
%33 = call i32 @close(i32 %32)
%34 = load i32, i32* %5
%35 = call i32 @close(i32 %34)
%36 = load i32, i32* %6
%37 = call i8* @ptsname(i32 %36)
store i8* %37, i8** %7
%38 = icmp ne i8* %37, null
br i1 %38, label %45, label %39
39: ; preds = %28
%40 = load i32, i32* @errno
%41 = call i32 @strerror(i32 %40)
%42 = sext i32 %41 to i64
%43 = inttoptr i64 %42 to i8*
%44 = call i32 (i8*, i8*, ...) @die(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str, i64 0, i64 0), i8* %43)
br label %45
45: ; preds = %39, %28
%46 = load i8*, i8** %7
%47 = load i32, i32* @O_RDWR
%48 = load i32, i32* @O_NOCTTY
%49 = or i32 %47, %48
%50 = call i32 @open(i8* %46, i32 %49)
store i32 %50, i32* %8
%51 = icmp slt i32 %50, 0
br i1 %51, label %52, label %57
52: ; preds = %45
%53 = load i8*, i8** %7
%54 = load i32, i32* @errno
%55 = call i32 @strerror(i32 %54)
%56 = call i32 (i8*, i8*, ...) @die(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0), i8* %53, i32 %55)
br label %57
57: ; preds = %52, %45
%58 = load i32, i32* %8
%59 = load i32, i32* @STDOUT_FILENO
%60 = call i32 @dup2(i32 %58, i32 %59)
%61 = load i32, i32* %8
%62 = load i32, i32* @STDERR_FILENO
%63 = call i32 @dup2(i32 %61, i32 %62)
%64 = load i32, i32* %8
%65 = call i32 @close(i32 %64)
%66 = load i32, i32* %6
%67 = call i32 @close(i32 %66)
%68 = load i8**, i8*** %4
%69 = load i8*, i8** %68
%70 = load i8**, i8*** %4
%71 = call i32 @execvp(i8* %69, i8** %70)
%72 = load i8**, i8*** %4
%73 = load i8*, i8** %72
%74 = load i32, i32* @errno
%75 = call i32 @strerror(i32 %74)
%76 = call i32 (i8*, i8*, ...) @die(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i8* %73, i32 %75)
ret void
}

LLVM IR (-O2):
@.str = [43 x i8] c"Unable to determine name of slave pty: %s\0A\00"
@errno = global i32 0
@O_RDWR = global i32 0
@.str.1 = [35 x i8] c"Unable to open slave pty \22%s\22: %s\0A\00"
@STDIN_FILENO = global i32 0
@O_NOCTTY = global i32 0
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
@.str.2 = [23 x i8] c"execvp(%s) failed: %s\0A\00"
define void @run_command(i8** %0, i32 %1, i32 %2) {
%4 = tail call i8* @ptsname(i32 %1)
%5 = icmp eq i8* %4, null
br i1 %5, label %6, label %12
6: ; preds = %3
%7 = load i32, i32* @errno
%8 = tail call i32 @strerror(i32 %7)
%9 = sext i32 %8 to i64
%10 = inttoptr i64 %9 to i8*
%11 = tail call i32 (i8*, i8*, ...) @die(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str, i64 0, i64 0), i8* %10)
br label %12
12: ; preds = %6, %3
%13 = load i32, i32* @O_RDWR
%14 = tail call i32 @open(i8* %4, i32 %13)
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %20
16: ; preds = %12
%17 = load i32, i32* @errno
%18 = tail call i32 @strerror(i32 %17)
%19 = tail call i32 (i8*, i8*, ...) @die(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0), i8* %4, i32 %18)
br label %20
20: ; preds = %16, %12
%21 = load i32, i32* @STDIN_FILENO
%22 = tail call i32 @dup2(i32 %14, i32 %21)
%23 = tail call i32 @close(i32 %14)
%24 = tail call i32 @close(i32 %1)
%25 = tail call i8* @ptsname(i32 %2)
%26 = icmp eq i8* %25, null
br i1 %26, label %27, label %33
27: ; preds = %20
%28 = load i32, i32* @errno
%29 = tail call i32 @strerror(i32 %28)
%30 = sext i32 %29 to i64
%31 = inttoptr i64 %30 to i8*
%32 = tail call i32 (i8*, i8*, ...) @die(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str, i64 0, i64 0), i8* %31)
br label %33
33: ; preds = %27, %20
%34 = load i32, i32* @O_RDWR
%35 = load i32, i32* @O_NOCTTY
%36 = or i32 %35, %34
%37 = tail call i32 @open(i8* %25, i32 %36)
%38 = icmp slt i32 %37, 0
br i1 %38, label %39, label %43
39: ; preds = %33
%40 = load i32, i32* @errno
%41 = tail call i32 @strerror(i32 %40)
%42 = tail call i32 (i8*, i8*, ...) @die(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0), i8* %25, i32 %41)
br label %43
43: ; preds = %39, %33
%44 = load i32, i32* @STDOUT_FILENO
%45 = tail call i32 @dup2(i32 %37, i32 %44)
%46 = load i32, i32* @STDERR_FILENO
%47 = tail call i32 @dup2(i32 %37, i32 %46)
%48 = tail call i32 @close(i32 %37)
%49 = tail call i32 @close(i32 %2)
%50 = load i8*, i8** %0
%51 = tail call i32 @execvp(i8* %50, i8** %0)
%52 = load i8*, i8** %0
%53 = load i32, i32* @errno
%54 = tail call i32 @strerror(i32 %53)
%55 = tail call i32 (i8*, i8*, ...) @die(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i8* %52, i32 %54)
ret void
}