#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 /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dup2(int, int);
int /*<<< orphan*/ execv(char *, char **);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
char *getenv(char *);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int main(int argc, char *argv[]) {
  int tty = open("/dev/tty", O_RDWR);
  char *tmux = getenv("REAL_TMUX");
  int i;
  if (!tmux) {
    fprintf(
        stderr,
        "error: REAL_TMUX environment variable needs to point to real tmux\n");
    exit(1);
  }
  for (i = 0; i < 3; i++) {
    close(i);
    dup2(tty, i);
  }
  close(tty);
  argv[0] = tmux;
  execv(tmux, argv);
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e5b(%rip),%rax
mov    (%rax),%esi
lea    0xe6a(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
lea    0xe64(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11d8 <main+0x68>
mov    0x2e18(%rip),%rax
mov    (%rax),%edi
lea    0xe4a(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
movl   $0x0,-0x24(%rbp)
cmpl   $0x3,-0x24(%rbp)
jge    120a <main+0x9a>
mov    -0x24(%rbp),%edi
call   1050 <close@plt>
mov    -0x14(%rbp),%edi
mov    -0x24(%rbp),%esi
call   1040 <dup2@plt>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    11df <main+0x6f>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
mov    -0x20(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1090 <execv@plt>
xor    %edi,%edi
call   1080 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
mov    %rsi,%r14
mov    0x2e6a(%rip),%rax
mov    (%rax),%esi
lea    0xe79(%rip),%rdi
call   1070 <open@plt>
mov    %eax,%ebp
lea    0xe74(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
jne    11be <main+0x4e>
mov    0x2e32(%rip),%rax
mov    (%rax),%edi
lea    0xe64(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    %rax,%rbx
xor    %edi,%edi
call   1050 <close@plt>
mov    %ebp,%edi
xor    %esi,%esi
call   1040 <dup2@plt>
mov    $0x1,%edi
call   1050 <close@plt>
mov    %ebp,%edi
mov    $0x1,%esi
call   1040 <dup2@plt>
mov    $0x2,%edi
call   1050 <close@plt>
mov    %ebp,%edi
mov    $0x2,%esi
call   1040 <dup2@plt>
mov    %ebp,%edi
call   1050 <close@plt>
mov    %rbx,(%r14)
mov    %rbx,%rdi
mov    %r14,%rsi
call   1090 <execv@plt>
xor    %edi,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [9 x i8] c"/dev/tty\00"
@O_RDWR = global i32 0
@.str.1 = [10 x i8] c"REAL_TMUX\00"
@stderr = global i32 0
@.str.2 = [67 x i8] c"error: REAL_TMUX environment variable needs to point to real tmux\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = load i32, i32* @O_RDWR
%10 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %9)
store i32 %10, i32* %6
%11 = call i8* @getenv(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
store i8* %11, i8** %7
%12 = load i8*, i8** %7
%13 = icmp ne i8* %12, null
br i1 %13, label %18, label %14
14: ; preds = %2
%15 = load i32, i32* @stderr
%16 = call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([67 x i8], [67 x i8]* @.str.2, i64 0, i64 0))
%17 = call i32 @exit(i32 1)
unreachable
18: ; preds = %2
store i32 0, i32* %8
br label %19
19: ; preds = %28, %18
%20 = load i32, i32* %8
%21 = icmp slt i32 %20, 3
br i1 %21, label %22, label %31
22: ; preds = %19
%23 = load i32, i32* %8
%24 = call i32 @close(i32 %23)
%25 = load i32, i32* %6
%26 = load i32, i32* %8
%27 = call i32 @dup2(i32 %25, i32 %26)
br label %28
28: ; preds = %22
%29 = load i32, i32* %8
%30 = add nsw i32 %29, 1
store i32 %30, i32* %8
br label %19
31: ; preds = %19
%32 = load i32, i32* %6
%33 = call i32 @close(i32 %32)
%34 = load i8*, i8** %7
%35 = load i8**, i8*** %5
%36 = getelementptr inbounds i8*, i8** %35, i64 0
store i8* %34, i8** %36
%37 = load i8*, i8** %7
%38 = load i8**, i8*** %5
%39 = call i32 @execv(i8* %37, i8** %38)
%40 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
@.str = [9 x i8] c"/dev/tty\00"
@O_RDWR = global i32 0
@.str.1 = [10 x i8] c"REAL_TMUX\00"
@stderr = global i32 0
@.str.2 = [67 x i8] c"error: REAL_TMUX environment variable needs to point to real tmux\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = load i32, i32* @O_RDWR
%4 = tail call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %3)
%5 = tail call i8* @getenv(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
%6 = icmp eq i8* %5, null
br i1 %6, label %17, label %7
7: ; preds = %2
%8 = tail call i32 @close(i32 0)
%9 = tail call i32 @dup2(i32 %4, i32 0)
%10 = tail call i32 @close(i32 1)
%11 = tail call i32 @dup2(i32 %4, i32 1)
%12 = tail call i32 @close(i32 2)
%13 = tail call i32 @dup2(i32 %4, i32 2)
%14 = tail call i32 @close(i32 %4)
store i8* %5, i8** %1
%15 = tail call i32 @execv(i8* %5, i8** %1)
%16 = tail call i32 @exit(i32 0)
unreachable
17: ; preds = %2
%18 = load i32, i32* @stderr
%19 = tail call i32 @fprintf(i32 %18, i8* getelementptr inbounds ([67 x i8], [67 x i8]* @.str.2, i64 0, i64 0))
%20 = tail call i32 @exit(i32 1)
unreachable
}