#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*/ ws;
struct winsize {
  unsigned short ws_col;
  unsigned short ws_row;
};
struct termios {
  int *c_cc;
  int /*<<< orphan*/ c_iflag;
};

/* Variables and functions */
int /*<<< orphan*/ IUTF8;
scalar_t__ STDERR_FILENO;
scalar_t__ STDIN_FILENO;
scalar_t__ STDOUT_FILENO;
int /*<<< orphan*/ TCSANOW;
int /*<<< orphan*/ TIOCSWINSZ;
size_t VERASE;
scalar_t__ dup2(int, scalar_t__);
int errno;
scalar_t__ ioctl(int, int /*<<< orphan*/, struct winsize *);
int /*<<< orphan*/ memset(struct winsize *, int /*<<< orphan*/, int);
scalar_t__ tcgetattr(int, struct termios *);
scalar_t__ tcsetattr(int, int /*<<< orphan*/, struct termios *);
int pty_setup_child(int slave, unsigned short term_width,
                    unsigned short term_height) {
  struct termios attr;
  struct winsize ws;
  if (tcgetattr(slave, &attr) < 0)
    return -errno;
  attr.c_cc[VERASE] = 010;
  attr.c_iflag |= IUTF8;
  if (tcsetattr(slave, TCSANOW, &attr) < 0)
    return -errno;
  memset(&ws, 0, sizeof(ws));
  ws.ws_col = term_width;
  ws.ws_row = term_height;
  if (ioctl(slave, TIOCSWINSZ, &ws) < 0)
    return -errno;
  if (dup2(slave, STDIN_FILENO) != STDIN_FILENO ||
      dup2(slave, STDOUT_FILENO) != STDOUT_FILENO ||
      dup2(slave, STDERR_FILENO) != STDERR_FILENO)
    return -errno;
  return 0;
}


x86 assembly (clang, -O0):
<pty_setup_child>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %dx,%ax
mov    %si,%cx
mov    %edi,-0x8(%rbp)
mov    %cx,-0xa(%rbp)
mov    %ax,-0xc(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x20(%rbp),%rsi
call   1060 <tcgetattr@plt>
cmp    $0x0,%rax
jge    1192 <pty_setup_child+0x42>
xor    %eax,%eax
mov    0x2e30(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    12ba <pty_setup_child+0x16a>
mov    -0x20(%rbp),%rax
mov    0x2e43(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x8,(%rax,%rcx,4)
mov    0x2e12(%rip),%rax
mov    (%rax),%eax
or     -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e18(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1070 <tcsetattr@plt>
cmp    $0x0,%rax
jge    11e8 <pty_setup_child+0x98>
xor    %eax,%eax
mov    0x2dda(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    12ba <pty_setup_child+0x16a>
lea    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1040 <memset@plt>
mov    -0xa(%rbp),%ax
mov    %ax,-0x28(%rbp)
mov    -0xc(%rbp),%ax
mov    %ax,-0x26(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2dbe(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdx
call   1050 <ioctl@plt>
cmp    $0x0,%rax
jge    123a <pty_setup_child+0xea>
xor    %eax,%eax
mov    0x2d88(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    12ba <pty_setup_child+0x16a>
mov    -0x8(%rbp),%edi
mov    0x2d64(%rip),%rax
mov    (%rax),%rsi
call   1030 <dup2@plt>
mov    0x2d55(%rip),%rcx
cmp    (%rcx),%rax
jne    12a0 <pty_setup_child+0x150>
mov    -0x8(%rbp),%edi
mov    0x2d8a(%rip),%rax
mov    (%rax),%rsi
call   1030 <dup2@plt>
mov    0x2d7b(%rip),%rcx
cmp    (%rcx),%rax
jne    12a0 <pty_setup_child+0x150>
mov    -0x8(%rbp),%edi
mov    0x2d28(%rip),%rax
mov    (%rax),%rsi
call   1030 <dup2@plt>
mov    0x2d19(%rip),%rcx
cmp    (%rcx),%rax
je     12b3 <pty_setup_child+0x163>
xor    %eax,%eax
mov    0x2d0f(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    12ba <pty_setup_child+0x16a>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pty_setup_child>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x20,%rsp
mov    %edx,%r14d
mov    %esi,%r15d
mov    %edi,%ebx
lea    0x10(%rsp),%rsi
call   1060 <tcgetattr@plt>
test   %rax,%rax
js     123a <pty_setup_child+0xea>
mov    0x10(%rsp),%rax
mov    0x2e5d(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x8,(%rax,%rcx,4)
mov    0x2e2c(%rip),%rax
mov    (%rax),%eax
or     %eax,0x18(%rsp)
mov    0x2e37(%rip),%rax
mov    (%rax),%esi
lea    0x10(%rsp),%rdx
mov    %ebx,%edi
call   1070 <tcsetattr@plt>
test   %rax,%rax
js     123a <pty_setup_child+0xea>
lea    0x8(%rsp),%r12
xor    %ebp,%ebp
mov    %r12,%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1040 <memset@plt>
mov    %r15w,0x8(%rsp)
mov    %r14w,0xa(%rsp)
mov    0x2def(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
mov    %r12,%rdx
call   1050 <ioctl@plt>
test   %rax,%rax
js     1254 <pty_setup_child+0x104>
mov    0x2daf(%rip),%rbp
mov    0x0(%rbp),%rsi
mov    %ebx,%edi
call   1030 <dup2@plt>
cmp    0x0(%rbp),%rax
jne    123a <pty_setup_child+0xea>
mov    0x2ddf(%rip),%rbp
mov    0x0(%rbp),%rsi
mov    %ebx,%edi
call   1030 <dup2@plt>
cmp    0x0(%rbp),%rax
jne    123a <pty_setup_child+0xea>
mov    0x2d87(%rip),%r14
mov    (%r14),%rsi
mov    %ebx,%edi
call   1030 <dup2@plt>
xor    %ebp,%ebp
cmp    (%r14),%rax
je     1245 <pty_setup_child+0xf5>
mov    0x2d77(%rip),%rax
xor    %ebp,%ebp
sub    (%rax),%ebp
mov    %ebp,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2d5d(%rip),%rax
jmp    1243 <pty_setup_child+0xf3>

LLVM IR (-O0):
%struct.termios = type { i32*, i32 }
%struct.winsize = type { i16, i16 }
@errno = global i32 0
@VERASE = global i64 0
@IUTF8 = global i32 0
@TCSANOW = global i32 0
@TIOCSWINSZ = global i32 0
@STDIN_FILENO = global i64 0
@STDOUT_FILENO = global i64 0
@STDERR_FILENO = global i64 0
define i32 @pty_setup_child(i32 %0, i16 zeroext %1, i16 zeroext %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i16
%7 = alloca i16
%8 = alloca %struct.termios
%9 = alloca %struct.winsize
store i32 %0, i32* %5
store i16 %1, i16* %6
store i16 %2, i16* %7
%10 = load i32, i32* %5
%11 = call i64 @tcgetattr(i32 %10, %struct.termios* %8)
%12 = icmp slt i64 %11, 0
br i1 %12, label %13, label %16
13: ; preds = %3
%14 = load i32, i32* @errno
%15 = sub nsw i32 0, %14
store i32 %15, i32* %4
br label %67
16: ; preds = %3
%17 = getelementptr inbounds %struct.termios, %struct.termios* %8, i32 0, i32 0
%18 = load i32*, i32** %17
%19 = load i64, i64* @VERASE
%20 = getelementptr inbounds i32, i32* %18, i64 %19
store i32 8, i32* %20
%21 = load i32, i32* @IUTF8
%22 = getelementptr inbounds %struct.termios, %struct.termios* %8, i32 0, i32 1
%23 = load i32, i32* %22
%24 = or i32 %23, %21
store i32 %24, i32* %22
%25 = load i32, i32* %5
%26 = load i32, i32* @TCSANOW
%27 = call i64 @tcsetattr(i32 %25, i32 %26, %struct.termios* %8)
%28 = icmp slt i64 %27, 0
br i1 %28, label %29, label %32
29: ; preds = %16
%30 = load i32, i32* @errno
%31 = sub nsw i32 0, %30
store i32 %31, i32* %4
br label %67
32: ; preds = %16
%33 = call i32 @memset(%struct.winsize* %9, i32 0, i32 4)
%34 = load i16, i16* %6
%35 = getelementptr inbounds %struct.winsize, %struct.winsize* %9, i32 0, i32 0
store i16 %34, i16* %35
%36 = load i16, i16* %7
%37 = getelementptr inbounds %struct.winsize, %struct.winsize* %9, i32 0, i32 1
store i16 %36, i16* %37
%38 = load i32, i32* %5
%39 = load i32, i32* @TIOCSWINSZ
%40 = call i64 @ioctl(i32 %38, i32 %39, %struct.winsize* %9)
%41 = icmp slt i64 %40, 0
br i1 %41, label %42, label %45
42: ; preds = %32
%43 = load i32, i32* @errno
%44 = sub nsw i32 0, %43
store i32 %44, i32* %4
br label %67
45: ; preds = %32
%46 = load i32, i32* %5
%47 = load i64, i64* @STDIN_FILENO
%48 = call i64 @dup2(i32 %46, i64 %47)
%49 = load i64, i64* @STDIN_FILENO
%50 = icmp ne i64 %48, %49
br i1 %50, label %63, label %51
51: ; preds = %45
%52 = load i32, i32* %5
%53 = load i64, i64* @STDOUT_FILENO
%54 = call i64 @dup2(i32 %52, i64 %53)
%55 = load i64, i64* @STDOUT_FILENO
%56 = icmp ne i64 %54, %55
br i1 %56, label %63, label %57
57: ; preds = %51
%58 = load i32, i32* %5
%59 = load i64, i64* @STDERR_FILENO
%60 = call i64 @dup2(i32 %58, i64 %59)
%61 = load i64, i64* @STDERR_FILENO
%62 = icmp ne i64 %60, %61
br i1 %62, label %63, label %66
63: ; preds = %57, %51, %45
%64 = load i32, i32* @errno
%65 = sub nsw i32 0, %64
store i32 %65, i32* %4
br label %67
66: ; preds = %57
store i32 0, i32* %4
br label %67
67: ; preds = %66, %63, %42, %29, %13
%68 = load i32, i32* %4
ret i32 %68
}

LLVM IR (-O2):
%struct.termios = type { i32*, i32 }
%struct.winsize = type { i16, i16 }
@errno = global i32 0
@VERASE = global i64 0
@IUTF8 = global i32 0
@TCSANOW = global i32 0
@TIOCSWINSZ = global i32 0
@STDIN_FILENO = global i64 0
@STDOUT_FILENO = global i64 0
@STDERR_FILENO = global i64 0
define i32 @pty_setup_child(i32 %0, i16 zeroext %1, i16 zeroext %2) {
%4 = alloca %struct.termios
%5 = alloca %struct.winsize
%6 = bitcast %struct.termios* %4 to i8*
%7 = bitcast %struct.winsize* %5 to i8*
%8 = call i64 @tcgetattr(i32 %0, %struct.termios* %4)
%9 = icmp slt i64 %8, 0
br i1 %9, label %10, label %13
10: ; preds = %3
%11 = load i32, i32* @errno
%12 = sub nsw i32 0, %11
br label %56
13: ; preds = %3
%14 = getelementptr inbounds %struct.termios, %struct.termios* %4, i64 0, i32 0
%15 = load i32*, i32** %14
%16 = load i64, i64* @VERASE
%17 = getelementptr inbounds i32, i32* %15, i64 %16
store i32 8, i32* %17
%18 = load i32, i32* @IUTF8
%19 = getelementptr inbounds %struct.termios, %struct.termios* %4, i64 0, i32 1
%20 = load i32, i32* %19
%21 = or i32 %20, %18
store i32 %21, i32* %19
%22 = load i32, i32* @TCSANOW
%23 = call i64 @tcsetattr(i32 %0, i32 %22, %struct.termios* %4)
%24 = icmp slt i64 %23, 0
br i1 %24, label %25, label %28
25: ; preds = %13
%26 = load i32, i32* @errno
%27 = sub nsw i32 0, %26
br label %56
28: ; preds = %13
%29 = call i32 @memset(%struct.winsize* %5, i32 0, i32 4)
%30 = getelementptr inbounds %struct.winsize, %struct.winsize* %5, i64 0, i32 0
store i16 %1, i16* %30
%31 = getelementptr inbounds %struct.winsize, %struct.winsize* %5, i64 0, i32 1
store i16 %2, i16* %31
%32 = load i32, i32* @TIOCSWINSZ
%33 = call i64 @ioctl(i32 %0, i32 %32, %struct.winsize* %5)
%34 = icmp slt i64 %33, 0
br i1 %34, label %35, label %38
35: ; preds = %28
%36 = load i32, i32* @errno
%37 = sub nsw i32 0, %36
br label %56
38: ; preds = %28
%39 = load i64, i64* @STDIN_FILENO
%40 = call i64 @dup2(i32 %0, i64 %39)
%41 = load i64, i64* @STDIN_FILENO
%42 = icmp eq i64 %40, %41
br i1 %42, label %43, label %53
43: ; preds = %38
%44 = load i64, i64* @STDOUT_FILENO
%45 = call i64 @dup2(i32 %0, i64 %44)
%46 = load i64, i64* @STDOUT_FILENO
%47 = icmp eq i64 %45, %46
br i1 %47, label %48, label %53
48: ; preds = %43
%49 = load i64, i64* @STDERR_FILENO
%50 = call i64 @dup2(i32 %0, i64 %49)
%51 = load i64, i64* @STDERR_FILENO
%52 = icmp eq i64 %50, %51
br i1 %52, label %56, label %53
53: ; preds = %48, %43, %38
%54 = load i32, i32* @errno
%55 = sub nsw i32 0, %54
br label %56
56: ; preds = %48, %53, %35, %25, %10
%57 = phi i32 [ %12, %10 ], [ %27, %25 ], [ %37, %35 ], [ %55, %53 ], [ 0, %48 ]
ret i32 %57
}