#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 winsize {
  int dummy;
};
struct termios {
  int dummy;
};
typedef int pid_t;

/* Variables and functions */
int EOVERFLOW;
int /*<<< orphan*/ O_RDWR;
int STDERR_FILENO;
int STDIN_FILENO;
int STDOUT_FILENO;
int /*<<< orphan*/ TCSANOW;
int /*<<< orphan*/ TIOCSWINSZ;
int /*<<< orphan*/ close(int);
int dup2(int, int);
int errno;
int fork();
int ioctl(int, int /*<<< orphan*/, struct winsize const *);
int open(char *, int /*<<< orphan*/);
int pty_master_open(char *, int);
int setsid();
size_t strlen(char *);
int /*<<< orphan*/ strncpy(char *, char *, size_t);
int tcsetattr(int, int /*<<< orphan*/, struct termios const *);
pid_t pty_fork(int *master_fd, char *slave_name, size_t sn_len,
               const struct termios *slave_termios,
               const struct winsize *slave_ws) {
  int mfd, slave_fd, last_errno;
  pid_t child_pid;
  char slname[1000];
  mfd = pty_master_open(slname, 1000);
  if (mfd == -1) {
    return -1;
  }
  if (slave_name != NULL) {
    if (strlen(slname) < sn_len) {
      strncpy(slave_name, slname, sn_len);
    } else {
      close(mfd);
      errno = EOVERFLOW;
      return -1;
    }
  }
  child_pid = fork();
  if (child_pid == -1) {
    last_errno = errno;
    close(mfd);
    errno = last_errno;
    return -1;
  }
  if (child_pid != 0) {
    *master_fd = mfd;
    return child_pid;
  }
  if (setsid() == -1) {
    return -1;
  }
  close(mfd);
  slave_fd = open(slname, O_RDWR);
  if (slave_fd == -1) {
    return -1;
  }
  if (slave_termios != NULL) {
    if (tcsetattr(slave_fd, TCSANOW, slave_termios) == -1) {
      return -1;
    }
  }
  if (slave_ws != NULL) {
    if (ioctl(slave_fd, TIOCSWINSZ, slave_ws) == -1) {
      return -1;
    }
  }
  if (dup2(slave_fd, STDIN_FILENO) != STDIN_FILENO) {
    return -1;
  }
  if (dup2(slave_fd, STDOUT_FILENO) != STDOUT_FILENO) {
    return -1;
  }
  if (dup2(slave_fd, STDERR_FILENO) != STDERR_FILENO) {
    return -1;
  }
  if (slave_fd > STDERR_FILENO) {
    close(slave_fd);
  }
  return 0;
}


x86 assembly (clang, -O0):
<pty_fork>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    %r8,-0x30(%rbp)
lea    -0x430(%rbp),%rdi
mov    $0x3e8,%esi
call   1090 <pty_master_open@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0xffffffff,-0x34(%rbp)
jne    11e9 <pty_fork+0x49>
movl   $0xffffffff,-0x4(%rbp)
jmp    1417 <pty_fork+0x277>
cmpq   $0x0,-0x18(%rbp)
je     124e <pty_fork+0xae>
lea    -0x430(%rbp),%rdi
call   1040 <strlen@plt>
cmp    -0x20(%rbp),%rax
jae    1223 <pty_fork+0x83>
mov    -0x18(%rbp),%rdi
lea    -0x430(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1030 <strncpy@plt>
jmp    1249 <pty_fork+0xa9>
mov    -0x34(%rbp),%edi
call   1070 <close@plt>
mov    0x2dbe(%rip),%rax
mov    (%rax),%ecx
mov    0x2d7d(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    1417 <pty_fork+0x277>
jmp    124e <pty_fork+0xae>
mov    $0x0,%al
call   10c0 <fork@plt>
mov    %eax,-0x40(%rbp)
cmpl   $0xffffffff,-0x40(%rbp)
jne    128e <pty_fork+0xee>
mov    0x2d4f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x34(%rbp),%edi
call   1070 <close@plt>
mov    -0x3c(%rbp),%ecx
mov    0x2d38(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    1417 <pty_fork+0x277>
cmpl   $0x0,-0x40(%rbp)
je     12ac <pty_fork+0x10c>
mov    -0x34(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x40(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1417 <pty_fork+0x277>
mov    $0x0,%al
call   1080 <setsid@plt>
cmp    $0xffffffff,%eax
jne    12c8 <pty_fork+0x128>
movl   $0xffffffff,-0x4(%rbp)
jmp    1417 <pty_fork+0x277>
mov    -0x34(%rbp),%edi
call   1070 <close@plt>
lea    -0x430(%rbp),%rdi
mov    0x2cfa(%rip),%rax
mov    (%rax),%esi
call   10b0 <open@plt>
mov    %eax,-0x38(%rbp)
cmpl   $0xffffffff,-0x38(%rbp)
jne    12fe <pty_fork+0x15e>
movl   $0xffffffff,-0x4(%rbp)
jmp    1417 <pty_fork+0x277>
cmpq   $0x0,-0x28(%rbp)
je     1338 <pty_fork+0x198>
mov    -0x38(%rbp),%edi
mov    0x2cbd(%rip),%rax
mov    (%rax),%esi
mov    -0x28(%rbp),%rdx
call   10a0 <tcsetattr@plt>
cmp    $0xffffffff,%eax
jne    1333 <pty_fork+0x193>
movl   $0xffffffff,-0x4(%rbp)
jmp    1417 <pty_fork+0x277>
jmp    1338 <pty_fork+0x198>
cmpq   $0x0,-0x30(%rbp)
je     1372 <pty_fork+0x1d2>
mov    -0x38(%rbp),%edi
mov    0x2c7b(%rip),%rax
mov    (%rax),%esi
mov    -0x30(%rbp),%rdx
call   1060 <ioctl@plt>
cmp    $0xffffffff,%eax
jne    136d <pty_fork+0x1cd>
movl   $0xffffffff,-0x4(%rbp)
jmp    1417 <pty_fork+0x277>
jmp    1372 <pty_fork+0x1d2>
mov    -0x38(%rbp),%edi
mov    0x2c2c(%rip),%rax
mov    (%rax),%esi
call   1050 <dup2@plt>
mov    0x2c1e(%rip),%rcx
cmp    (%rcx),%eax
je     139e <pty_fork+0x1fe>
movl   $0xffffffff,-0x4(%rbp)
jmp    1417 <pty_fork+0x277>
mov    -0x38(%rbp),%edi
mov    0x2c40(%rip),%rax
mov    (%rax),%esi
call   1050 <dup2@plt>
mov    0x2c32(%rip),%rcx
cmp    (%rcx),%eax
je     13ca <pty_fork+0x22a>
movl   $0xffffffff,-0x4(%rbp)
jmp    1417 <pty_fork+0x277>
mov    -0x38(%rbp),%edi
mov    0x2bdc(%rip),%rax
mov    (%rax),%esi
call   1050 <dup2@plt>
mov    0x2bce(%rip),%rcx
cmp    (%rcx),%eax
je     13f6 <pty_fork+0x256>
movl   $0xffffffff,-0x4(%rbp)
jmp    1417 <pty_fork+0x277>
mov    -0x38(%rbp),%eax
mov    0x2bb0(%rip),%rcx
cmp    (%rcx),%eax
jle    1410 <pty_fork+0x270>
mov    -0x38(%rbp),%edi
call   1070 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pty_fork>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x3f8,%rsp
mov    %r8,0x8(%rsp)
mov    %rcx,%r15
mov    %rdx,%rbx
mov    %rsi,%r14
mov    %rdi,%r13
lea    0x10(%rsp),%rdi
mov    $0x3e8,%esi
call   1090 <pty_master_open@plt>
mov    $0xffffffff,%r12d
cmp    $0xffffffff,%eax
je     1315 <pty_fork+0x175>
mov    %eax,%ebp
test   %r14,%r14
je     1206 <pty_fork+0x66>
lea    0x10(%rsp),%rdi
call   1040 <strlen@plt>
cmp    %rbx,%rax
jae    1233 <pty_fork+0x93>
lea    0x10(%rsp),%rsi
mov    %r14,%rdi
mov    %rbx,%rdx
call   1030 <strncpy@plt>
xor    %eax,%eax
call   10c0 <fork@plt>
test   %eax,%eax
je     1251 <pty_fork+0xb1>
cmp    $0xffffffff,%eax
jne    130e <pty_fork+0x16e>
mov    0x2d97(%rip),%r14
mov    (%r14),%ebx
mov    %ebp,%edi
call   1070 <close@plt>
mov    %ebx,(%r14)
jmp    1315 <pty_fork+0x175>
mov    %ebp,%edi
call   1070 <close@plt>
mov    0x2daf(%rip),%rax
mov    (%rax),%eax
mov    0x2d6e(%rip),%rcx
mov    %eax,(%rcx)
jmp    1315 <pty_fork+0x175>
xor    %eax,%eax
call   1080 <setsid@plt>
cmp    $0xffffffff,%eax
je     1315 <pty_fork+0x175>
mov    %ebp,%edi
call   1070 <close@plt>
mov    0x2d69(%rip),%rax
mov    (%rax),%esi
lea    0x10(%rsp),%rdi
call   10b0 <open@plt>
cmp    $0xffffffff,%eax
je     1315 <pty_fork+0x175>
mov    %eax,%ebp
test   %r15,%r15
je     12a3 <pty_fork+0x103>
mov    0x2d3e(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
mov    %r15,%rdx
call   10a0 <tcsetattr@plt>
cmp    $0xffffffff,%eax
je     1315 <pty_fork+0x175>
mov    0x8(%rsp),%rdx
test   %rdx,%rdx
je     12c2 <pty_fork+0x122>
mov    0x2d14(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
call   1060 <ioctl@plt>
cmp    $0xffffffff,%eax
je     1315 <pty_fork+0x175>
mov    0x2cdf(%rip),%rbx
mov    (%rbx),%esi
mov    %ebp,%edi
call   1050 <dup2@plt>
cmp    (%rbx),%eax
jne    1315 <pty_fork+0x175>
mov    0x2d0b(%rip),%rbx
mov    (%rbx),%esi
mov    %ebp,%edi
call   1050 <dup2@plt>
cmp    (%rbx),%eax
jne    1315 <pty_fork+0x175>
mov    0x2cbf(%rip),%rbx
mov    (%rbx),%esi
mov    %ebp,%edi
call   1050 <dup2@plt>
cmp    (%rbx),%eax
jne    1315 <pty_fork+0x175>
xor    %r12d,%r12d
cmp    %eax,%ebp
jle    1315 <pty_fork+0x175>
mov    %ebp,%edi
call   1070 <close@plt>
jmp    1315 <pty_fork+0x175>
mov    %ebp,0x0(%r13)
mov    %eax,%r12d
mov    %r12d,%eax
add    $0x3f8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.termios = type { i32 }
%struct.winsize = type { i32 }
@EOVERFLOW = global i32 0
@errno = global i32 0
@O_RDWR = global i32 0
@TCSANOW = global i32 0
@TIOCSWINSZ = global i32 0
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
define i32 @pty_fork(i32* %0, i8* %1, i64 %2, %struct.termios* %3, %struct.winsize* %4) {
%6 = alloca i32
%7 = alloca i32*
%8 = alloca i8*
%9 = alloca i64
%10 = alloca %struct.termios*
%11 = alloca %struct.winsize*
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
%15 = alloca i32
%16 = alloca [1000 x i8]
store i32* %0, i32** %7
store i8* %1, i8** %8
store i64 %2, i64* %9
store %struct.termios* %3, %struct.termios** %10
store %struct.winsize* %4, %struct.winsize** %11
%17 = getelementptr inbounds [1000 x i8], [1000 x i8]* %16, i64 0, i64 0
%18 = call i32 @pty_master_open(i8* %17, i32 1000)
store i32 %18, i32* %12
%19 = load i32, i32* %12
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %22
21: ; preds = %5
store i32 -1, i32* %6
br label %120
22: ; preds = %5
%23 = load i8*, i8** %8
%24 = icmp ne i8* %23, null
br i1 %24, label %25, label %40
25: ; preds = %22
%26 = getelementptr inbounds [1000 x i8], [1000 x i8]* %16, i64 0, i64 0
%27 = call i64 @strlen(i8* %26)
%28 = load i64, i64* %9
%29 = icmp ult i64 %27, %28
br i1 %29, label %30, label %35
30: ; preds = %25
%31 = load i8*, i8** %8
%32 = getelementptr inbounds [1000 x i8], [1000 x i8]* %16, i64 0, i64 0
%33 = load i64, i64* %9
%34 = call i32 @strncpy(i8* %31, i8* %32, i64 %33)
br label %39
35: ; preds = %25
%36 = load i32, i32* %12
%37 = call i32 @close(i32 %36)
%38 = load i32, i32* @EOVERFLOW
store i32 %38, i32* @errno
store i32 -1, i32* %6
br label %120
39: ; preds = %30
br label %40
40: ; preds = %39, %22
%41 = call i32 (...) @fork()
store i32 %41, i32* %15
%42 = load i32, i32* %15
%43 = icmp eq i32 %42, -1
br i1 %43, label %44, label %49
44: ; preds = %40
%45 = load i32, i32* @errno
store i32 %45, i32* %14
%46 = load i32, i32* %12
%47 = call i32 @close(i32 %46)
%48 = load i32, i32* %14
store i32 %48, i32* @errno
store i32 -1, i32* %6
br label %120
49: ; preds = %40
%50 = load i32, i32* %15
%51 = icmp ne i32 %50, 0
br i1 %51, label %52, label %56
52: ; preds = %49
%53 = load i32, i32* %12
%54 = load i32*, i32** %7
store i32 %53, i32* %54
%55 = load i32, i32* %15
store i32 %55, i32* %6
br label %120
56: ; preds = %49
%57 = call i32 (...) @setsid()
%58 = icmp eq i32 %57, -1
br i1 %58, label %59, label %60
59: ; preds = %56
store i32 -1, i32* %6
br label %120
60: ; preds = %56
%61 = load i32, i32* %12
%62 = call i32 @close(i32 %61)
%63 = getelementptr inbounds [1000 x i8], [1000 x i8]* %16, i64 0, i64 0
%64 = load i32, i32* @O_RDWR
%65 = call i32 @open(i8* %63, i32 %64)
store i32 %65, i32* %13
%66 = load i32, i32* %13
%67 = icmp eq i32 %66, -1
br i1 %67, label %68, label %69
68: ; preds = %60
store i32 -1, i32* %6
br label %120
69: ; preds = %60
%70 = load %struct.termios*, %struct.termios** %10
%71 = icmp ne %struct.termios* %70, null
br i1 %71, label %72, label %80
72: ; preds = %69
%73 = load i32, i32* %13
%74 = load i32, i32* @TCSANOW
%75 = load %struct.termios*, %struct.termios** %10
%76 = call i32 @tcsetattr(i32 %73, i32 %74, %struct.termios* %75)
%77 = icmp eq i32 %76, -1
br i1 %77, label %78, label %79
78: ; preds = %72
store i32 -1, i32* %6
br label %120
79: ; preds = %72
br label %80
80: ; preds = %79, %69
%81 = load %struct.winsize*, %struct.winsize** %11
%82 = icmp ne %struct.winsize* %81, null
br i1 %82, label %83, label %91
83: ; preds = %80
%84 = load i32, i32* %13
%85 = load i32, i32* @TIOCSWINSZ
%86 = load %struct.winsize*, %struct.winsize** %11
%87 = call i32 @ioctl(i32 %84, i32 %85, %struct.winsize* %86)
%88 = icmp eq i32 %87, -1
br i1 %88, label %89, label %90
89: ; preds = %83
store i32 -1, i32* %6
br label %120
90: ; preds = %83
br label %91
91: ; preds = %90, %80
%92 = load i32, i32* %13
%93 = load i32, i32* @STDIN_FILENO
%94 = call i32 @dup2(i32 %92, i32 %93)
%95 = load i32, i32* @STDIN_FILENO
%96 = icmp ne i32 %94, %95
br i1 %96, label %97, label %98
97: ; preds = %91
store i32 -1, i32* %6
br label %120
98: ; preds = %91
%99 = load i32, i32* %13
%100 = load i32, i32* @STDOUT_FILENO
%101 = call i32 @dup2(i32 %99, i32 %100)
%102 = load i32, i32* @STDOUT_FILENO
%103 = icmp ne i32 %101, %102
br i1 %103, label %104, label %105
104: ; preds = %98
store i32 -1, i32* %6
br label %120
105: ; preds = %98
%106 = load i32, i32* %13
%107 = load i32, i32* @STDERR_FILENO
%108 = call i32 @dup2(i32 %106, i32 %107)
%109 = load i32, i32* @STDERR_FILENO
%110 = icmp ne i32 %108, %109
br i1 %110, label %111, label %112
111: ; preds = %105
store i32 -1, i32* %6
br label %120
112: ; preds = %105
%113 = load i32, i32* %13
%114 = load i32, i32* @STDERR_FILENO
%115 = icmp sgt i32 %113, %114
br i1 %115, label %116, label %119
116: ; preds = %112
%117 = load i32, i32* %13
%118 = call i32 @close(i32 %117)
br label %119
119: ; preds = %116, %112
store i32 0, i32* %6
br label %120
120: ; preds = %119, %111, %104, %97, %89, %78, %68, %59, %52, %44, %35, %21
%121 = load i32, i32* %6
ret i32 %121
}

LLVM IR (-O2):
%struct.termios = type { i32 }
%struct.winsize = type { i32 }
@EOVERFLOW = global i32 0
@errno = global i32 0
@O_RDWR = global i32 0
@TCSANOW = global i32 0
@TIOCSWINSZ = global i32 0
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
define i32 @pty_fork(i32* nocapture writeonly %0, i8* %1, i64 %2, %struct.termios* %3, %struct.winsize* %4) {
%6 = alloca [1000 x i8]
%7 = getelementptr inbounds [1000 x i8], [1000 x i8]* %6, i64 0, i64 0
%8 = call i32 @pty_master_open(i8* %7, i32 1000)
%9 = icmp eq i32 %8, -1
br i1 %9, label %65, label %10
10: ; preds = %5
%11 = icmp eq i8* %1, null
br i1 %11, label %20, label %12
12: ; preds = %10
%13 = call i64 @strlen(i8* %7)
%14 = icmp ult i64 %13, %2
br i1 %14, label %15, label %17
15: ; preds = %12
%16 = call i32 @strncpy(i8* %1, i8* %7, i64 %2)
br label %20
17: ; preds = %12
%18 = call i32 @close(i32 %8)
%19 = load i32, i32* @EOVERFLOW
store i32 %19, i32* @errno
br label %65
20: ; preds = %15, %10
%21 = call i32 (...) @fork()
switch i32 %21, label %25 [
i32 -1, label %22
i32 0, label %26
]
22: ; preds = %20
%23 = load i32, i32* @errno
%24 = call i32 @close(i32 %8)
store i32 %23, i32* @errno
br label %65
25: ; preds = %20
store i32 %8, i32* %0
br label %65
26: ; preds = %20
%27 = call i32 (...) @setsid()
%28 = icmp eq i32 %27, -1
br i1 %28, label %65, label %29
29: ; preds = %26
%30 = call i32 @close(i32 %8)
%31 = load i32, i32* @O_RDWR
%32 = call i32 @open(i8* %7, i32 %31)
%33 = icmp eq i32 %32, -1
br i1 %33, label %65, label %34
34: ; preds = %29
%35 = icmp eq %struct.termios* %3, null
br i1 %35, label %40, label %36
36: ; preds = %34
%37 = load i32, i32* @TCSANOW
%38 = call i32 @tcsetattr(i32 %32, i32 %37, %struct.termios* %3)
%39 = icmp eq i32 %38, -1
br i1 %39, label %65, label %40
40: ; preds = %36, %34
%41 = icmp eq %struct.winsize* %4, null
br i1 %41, label %46, label %42
42: ; preds = %40
%43 = load i32, i32* @TIOCSWINSZ
%44 = call i32 @ioctl(i32 %32, i32 %43, %struct.winsize* %4)
%45 = icmp eq i32 %44, -1
br i1 %45, label %65, label %46
46: ; preds = %42, %40
%47 = load i32, i32* @STDIN_FILENO
%48 = call i32 @dup2(i32 %32, i32 %47)
%49 = load i32, i32* @STDIN_FILENO
%50 = icmp eq i32 %48, %49
br i1 %50, label %51, label %65
51: ; preds = %46
%52 = load i32, i32* @STDOUT_FILENO
%53 = call i32 @dup2(i32 %32, i32 %52)
%54 = load i32, i32* @STDOUT_FILENO
%55 = icmp eq i32 %53, %54
br i1 %55, label %56, label %65
56: ; preds = %51
%57 = load i32, i32* @STDERR_FILENO
%58 = call i32 @dup2(i32 %32, i32 %57)
%59 = load i32, i32* @STDERR_FILENO
%60 = icmp eq i32 %58, %59
br i1 %60, label %61, label %65
61: ; preds = %56
%62 = icmp sgt i32 %32, %58
br i1 %62, label %63, label %65
63: ; preds = %61
%64 = call i32 @close(i32 %32)
br label %65
65: ; preds = %61, %63, %56, %51, %46, %42, %36, %29, %26, %5, %25, %22, %17
%66 = phi i32 [ -1, %22 ], [ %21, %25 ], [ -1, %17 ], [ -1, %5 ], [ -1, %26 ], [ -1, %29 ], [ -1, %36 ], [ -1, %42 ], [ -1, %46 ], [ -1, %51 ], [ -1, %56 ], [ 0, %63 ], [ 0, %61 ]
ret i32 %66
}