#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 */
struct termios {
  int /*<<< orphan*/ c_oflag;
};

/* Variables and functions */
int /*<<< orphan*/ LOG_DEBUG;
int /*<<< orphan*/ LOG_MAKEPRI(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ LOG_USER;
int /*<<< orphan*/ ONLCR;
int /*<<< orphan*/ OPOST;
int /*<<< orphan*/ STDOUT_FILENO;
int /*<<< orphan*/ TCSANOW;
int /*<<< orphan*/ _exit(int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ execvp(char *, char **);
int /*<<< orphan*/ exit_error(char *);
int forkpty(int *, int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ tcgetattr(int /*<<< orphan*/, struct termios *);
int /*<<< orphan*/ tcsetattr(int /*<<< orphan*/, int /*<<< orphan*/,
                             struct termios *);
int handle_terminal(int *terminal, char **argv) {
  int pid = forkpty(terminal, 0, 0, 0);
  if (-1 == pid)
    exit_error("could not fork or more pseudo terminals available");
  if (0 != pid)
    return pid;
  struct termios settings;
  tcgetattr(STDOUT_FILENO, &settings);
  settings.c_oflag &= ~OPOST;
  settings.c_oflag &= ~ONLCR;
  tcsetattr(STDOUT_FILENO, TCSANOW, &settings);
  execvp(argv[0], &argv[0]);
  (void)(LOG_MAKEPRI(LOG_USER, LOG_DEBUG), "Could not execute\n");
  close(*terminal);
  _exit(1);
}


x86 assembly (clang, -O0):
<handle_terminal>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
xor    %ecx,%ecx
mov    %ecx,%esi
mov    %ecx,%edx
call   1080 <forkpty@plt>
mov    %eax,-0x14(%rbp)
mov    $0xffffffff,%eax
cmp    -0x14(%rbp),%eax
jne    11bc <handle_terminal+0x3c>
lea    0xe49(%rip),%rdi
call   1050 <exit_error@plt>
xor    %eax,%eax
cmp    -0x14(%rbp),%eax
je     11d0 <handle_terminal+0x50>
mov    -0x14(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret
mov    0x2e19(%rip),%rax
mov    (%rax),%edi
lea    -0x18(%rbp),%rsi
call   1070 <tcgetattr@plt>
mov    0x2def(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    0x2dc5(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    0x2de3(%rip),%rax
mov    (%rax),%edi
mov    0x2dba(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rdx
call   1090 <tcsetattr@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x10(%rbp),%rsi
call   10a0 <execvp@plt>
mov    0x2da8(%rip),%rax
mov    (%rax),%edi
mov    0x2d77(%rip),%rax
mov    (%rax),%esi
call   1060 <LOG_MAKEPRI@plt>
mov    -0x8(%rbp),%rax
mov    (%rax),%edi
call   1040 <close@plt>
mov    $0x1,%edi
call   1030 <_exit@plt>

x86 assembly (clang, -O2):
<handle_terminal>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbx
xor    %esi,%esi
xor    %edx,%edx
xor    %ecx,%ecx
call   1080 <forkpty@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
je     1216 <handle_terminal+0x96>
test   %ebp,%ebp
jne    1222 <handle_terminal+0xa2>
mov    0x2e46(%rip),%rbp
mov    0x0(%rbp),%edi
mov    %rsp,%r15
mov    %r15,%rsi
call   1070 <tcgetattr@plt>
mov    0x2e19(%rip),%rax
mov    (%rax),%eax
mov    0x2df8(%rip),%rcx
or     (%rcx),%eax
not    %eax
and    %eax,(%rsp)
mov    0x0(%rbp),%edi
mov    0x2df7(%rip),%rax
mov    (%rax),%esi
mov    %r15,%rdx
call   1090 <tcsetattr@plt>
mov    (%r14),%rdi
mov    %r14,%rsi
call   10a0 <execvp@plt>
mov    0x2deb(%rip),%rax
mov    (%rax),%edi
mov    0x2dba(%rip),%rax
mov    (%rax),%esi
call   1060 <LOG_MAKEPRI@plt>
mov    (%rbx),%edi
call   1040 <close@plt>
mov    $0x1,%edi
call   1030 <_exit@plt>
lea    0xde3(%rip),%rdi
call   1050 <exit_error@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.termios = type { i32 }
@.str = [50 x i8] c"could not fork or more pseudo terminals available\00"
@STDOUT_FILENO = global i32 0
@OPOST = global i32 0
@ONLCR = global i32 0
@TCSANOW = global i32 0
@LOG_USER = global i32 0
@LOG_DEBUG = global i32 0
@.str.1 = [19 x i8] c"Could not execute\0A\00"
define i32 @handle_terminal(i32* %0, i8** %1) {
%3 = alloca i32*
%4 = alloca i8**
%5 = alloca i32
%6 = alloca %struct.termios
store i32* %0, i32** %3
store i8** %1, i8*** %4
%7 = load i32*, i32** %3
%8 = call i32 @forkpty(i32* %7, i32 0, i32 0, i32 0)
store i32 %8, i32* %5
%9 = load i32, i32* %5
%10 = icmp eq i32 -1, %9
br i1 %10, label %11, label %13
11: ; preds = %2
%12 = call i32 @exit_error(i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str, i64 0, i64 0))
br label %13
13: ; preds = %11, %2
%14 = load i32, i32* %5
%15 = icmp ne i32 0, %14
br i1 %15, label %16, label %18
16: ; preds = %13
%17 = load i32, i32* %5
ret i32 %17
18: ; preds = %13
%19 = load i32, i32* @STDOUT_FILENO
%20 = call i32 @tcgetattr(i32 %19, %struct.termios* %6)
%21 = load i32, i32* @OPOST
%22 = xor i32 %21, -1
%23 = getelementptr inbounds %struct.termios, %struct.termios* %6, i32 0, i32 0
%24 = load i32, i32* %23
%25 = and i32 %24, %22
store i32 %25, i32* %23
%26 = load i32, i32* @ONLCR
%27 = xor i32 %26, -1
%28 = getelementptr inbounds %struct.termios, %struct.termios* %6, i32 0, i32 0
%29 = load i32, i32* %28
%30 = and i32 %29, %27
store i32 %30, i32* %28
%31 = load i32, i32* @STDOUT_FILENO
%32 = load i32, i32* @TCSANOW
%33 = call i32 @tcsetattr(i32 %31, i32 %32, %struct.termios* %6)
%34 = load i8**, i8*** %4
%35 = getelementptr inbounds i8*, i8** %34, i64 0
%36 = load i8*, i8** %35
%37 = load i8**, i8*** %4
%38 = getelementptr inbounds i8*, i8** %37, i64 0
%39 = call i32 @execvp(i8* %36, i8** %38)
%40 = load i32, i32* @LOG_USER
%41 = load i32, i32* @LOG_DEBUG
%42 = call i32 @LOG_MAKEPRI(i32 %40, i32 %41)
%43 = load i32*, i32** %3
%44 = load i32, i32* %43
%45 = call i32 @close(i32 %44)
%46 = call i32 @_exit(i32 1)
unreachable
}

LLVM IR (-O2):
%struct.termios = type { i32 }
@.str = [50 x i8] c"could not fork or more pseudo terminals available\00"
@STDOUT_FILENO = global i32 0
@OPOST = global i32 0
@ONLCR = global i32 0
@TCSANOW = global i32 0
@LOG_USER = global i32 0
@LOG_DEBUG = global i32 0
define i32 @handle_terminal(i32* %0, i8** %1) {
%3 = alloca %struct.termios
%4 = tail call i32 @forkpty(i32* %0, i32 0, i32 0, i32 0)
switch i32 %4, label %7 [
i32 -1, label %5
i32 0, label %8
]
5: ; preds = %2
%6 = tail call i32 @exit_error(i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str, i64 0, i64 0))
br label %7
7: ; preds = %5, %2
ret i32 %4
8: ; preds = %2
%9 = bitcast %struct.termios* %3 to i8*
%10 = load i32, i32* @STDOUT_FILENO
%11 = call i32 @tcgetattr(i32 %10, %struct.termios* %3)
%12 = load i32, i32* @OPOST
%13 = getelementptr inbounds %struct.termios, %struct.termios* %3, i64 0, i32 0
%14 = load i32, i32* %13
%15 = load i32, i32* @ONLCR
%16 = or i32 %12, %15
%17 = xor i32 %16, -1
%18 = and i32 %14, %17
store i32 %18, i32* %13
%19 = load i32, i32* @STDOUT_FILENO
%20 = load i32, i32* @TCSANOW
%21 = call i32 @tcsetattr(i32 %19, i32 %20, %struct.termios* %3)
%22 = load i8*, i8** %1
%23 = call i32 @execvp(i8* %22, i8** %1)
%24 = load i32, i32* @LOG_USER
%25 = load i32, i32* @LOG_DEBUG
%26 = call i32 @LOG_MAKEPRI(i32 %24, i32 %25)
%27 = load i32, i32* %0
%28 = call i32 @close(i32 %27)
%29 = call i32 @_exit(i32 1)
unreachable
}