#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 /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ _exit(int /*<<< orphan*/);
scalar_t__ _internal_get_pty(int *, char **);
scalar_t__ _internal_login_tty(int, char *, struct termios *, struct winsize *);
int /*<<< orphan*/ close(int);
int fork();
int /*<<< orphan*/ strcpy(char *, char *);
int forkpty(int *amaster, char *name, struct termios *termp,
            struct winsize *winp) {
  char *path;
  int master = -1;
  pid_t pid;
  if (amaster == NULL)
    return -1;
  if (_internal_get_pty(&master, &path) != 0)
    goto out;
  if (name != NULL)
    strcpy(name, path);
  if ((pid = fork()) == -1)
    goto out;
  if (pid == 0) {
    close(master);
    if (_internal_login_tty(-1, path, termp, winp) != 0)
      _exit(EXIT_FAILURE);
    return 0;
  }
  *amaster = master;
  return pid;
out:
  if (master != -1)
    close(master);
  return -1;
}


x86 assembly (clang, -O0):
<forkpty>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
movl   $0xffffffff,-0x34(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1196 <forkpty+0x36>
movl   $0xffffffff,-0x4(%rbp)
jmp    125b <forkpty+0xfb>
lea    -0x34(%rbp),%rdi
lea    -0x30(%rbp),%rsi
call   1060 <_internal_get_pty@plt>
cmp    $0x0,%rax
je     11b2 <forkpty+0x52>
jmp    1242 <forkpty+0xe2>
cmpq   $0x0,-0x18(%rbp)
je     11ca <forkpty+0x6a>
mov    -0x18(%rbp),%rdi
mov    -0x30(%rbp),%rsi
call   1040 <strcpy@plt>
mov    $0x0,%al
call   1080 <fork@plt>
mov    %eax,-0x38(%rbp)
cmp    $0xffffffff,%eax
jne    11e2 <forkpty+0x82>
jmp    1242 <forkpty+0xe2>
cmpl   $0x0,-0x38(%rbp)
jne    122e <forkpty+0xce>
mov    -0x34(%rbp),%edi
call   1050 <close@plt>
mov    -0x30(%rbp),%rsi
mov    -0x20(%rbp),%rdx
mov    -0x28(%rbp),%rcx
mov    $0xffffffff,%edi
call   1070 <_internal_login_tty@plt>
cmp    $0x0,%rax
je     1222 <forkpty+0xc2>
mov    0x2dc5(%rip),%rax
mov    (%rax),%edi
call   1030 <_exit@plt>
movl   $0x0,-0x4(%rbp)
jmp    125b <forkpty+0xfb>
mov    -0x34(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x38(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    125b <forkpty+0xfb>
cmpl   $0xffffffff,-0x34(%rbp)
je     1254 <forkpty+0xf4>
mov    -0x34(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<forkpty>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
movl   $0xffffffff,0x4(%rsp)
ff
mov    $0xffffffff,%r12d
test   %rdi,%rdi
je     1212 <forkpty+0xb2>
mov    %rcx,%r14
mov    %rdx,%r15
mov    %rsi,%rbp
mov    %rdi,%rbx
lea    0x4(%rsp),%rdi
lea    0x8(%rsp),%rsi
call   1060 <_internal_get_pty@plt>
test   %rax,%rax
je     11b3 <forkpty+0x53>
mov    0x4(%rsp),%edi
cmp    $0xffffffff,%edi
je     1212 <forkpty+0xb2>
call   1050 <close@plt>
jmp    1212 <forkpty+0xb2>
test   %rbp,%rbp
je     11c5 <forkpty+0x65>
mov    0x8(%rsp),%rsi
mov    %rbp,%rdi
call   1040 <strcpy@plt>
xor    %eax,%eax
call   1080 <fork@plt>
cmp    $0xffffffff,%eax
je     11a3 <forkpty+0x43>
test   %eax,%eax
jne    1209 <forkpty+0xa9>
mov    0x4(%rsp),%edi
call   1050 <close@plt>
mov    0x8(%rsp),%rsi
mov    $0xffffffff,%edi
mov    %r15,%rdx
mov    %r14,%rcx
call   1070 <_internal_login_tty@plt>
xor    %r12d,%r12d
test   %rax,%rax
je     1212 <forkpty+0xb2>
mov    0x2dde(%rip),%rax
mov    (%rax),%edi
call   1030 <_exit@plt>
mov    0x4(%rsp),%ecx
mov    %ecx,(%rbx)
mov    %eax,%r12d
mov    %r12d,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.termios = type { i32 }
%struct.winsize = type { i32 }
@EXIT_FAILURE = global i32 0
define i32 @forkpty(i32* %0, i8* %1, %struct.termios* %2, %struct.winsize* %3) {
%5 = alloca i32
%6 = alloca i32*
%7 = alloca i8*
%8 = alloca %struct.termios*
%9 = alloca %struct.winsize*
%10 = alloca i8*
%11 = alloca i32
%12 = alloca i32
store i32* %0, i32** %6
store i8* %1, i8** %7
store %struct.termios* %2, %struct.termios** %8
store %struct.winsize* %3, %struct.winsize** %9
store i32 -1, i32* %11
%13 = load i32*, i32** %6
%14 = icmp eq i32* %13, null
br i1 %14, label %15, label %16
15: ; preds = %4
store i32 -1, i32* %5
br label %57
16: ; preds = %4
%17 = call i64 @_internal_get_pty(i32* %11, i8** %10)
%18 = icmp ne i64 %17, 0
br i1 %18, label %19, label %20
19: ; preds = %16
br label %50
20: ; preds = %16
%21 = load i8*, i8** %7
%22 = icmp ne i8* %21, null
br i1 %22, label %23, label %27
23: ; preds = %20
%24 = load i8*, i8** %7
%25 = load i8*, i8** %10
%26 = call i32 @strcpy(i8* %24, i8* %25)
br label %27
27: ; preds = %23, %20
%28 = call i32 (...) @fork()
store i32 %28, i32* %12
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %31
30: ; preds = %27
br label %50
31: ; preds = %27
%32 = load i32, i32* %12
%33 = icmp eq i32 %32, 0
br i1 %33, label %34, label %46
34: ; preds = %31
%35 = load i32, i32* %11
%36 = call i32 @close(i32 %35)
%37 = load i8*, i8** %10
%38 = load %struct.termios*, %struct.termios** %8
%39 = load %struct.winsize*, %struct.winsize** %9
%40 = call i64 @_internal_login_tty(i32 -1, i8* %37, %struct.termios* %38, %struct.winsize* %39)
%41 = icmp ne i64 %40, 0
br i1 %41, label %42, label %45
42: ; preds = %34
%43 = load i32, i32* @EXIT_FAILURE
%44 = call i32 @_exit(i32 %43)
unreachable
45: ; preds = %34
store i32 0, i32* %5
br label %57
46: ; preds = %31
%47 = load i32, i32* %11
%48 = load i32*, i32** %6
store i32 %47, i32* %48
%49 = load i32, i32* %12
store i32 %49, i32* %5
br label %57
50: ; preds = %30, %19
%51 = load i32, i32* %11
%52 = icmp ne i32 %51, -1
br i1 %52, label %53, label %56
53: ; preds = %50
%54 = load i32, i32* %11
%55 = call i32 @close(i32 %54)
br label %56
56: ; preds = %53, %50
store i32 -1, i32* %5
br label %57
57: ; preds = %56, %46, %45, %15
%58 = load i32, i32* %5
ret i32 %58
}

LLVM IR (-O2):
%struct.termios = type { i32 }
%struct.winsize = type { i32 }
@EXIT_FAILURE = global i32 0
define i32 @forkpty(i32* writeonly %0, i8* %1, %struct.termios* %2, %struct.winsize* %3) {
%5 = alloca i8*
%6 = alloca i32
%7 = bitcast i8** %5 to i8*
%8 = bitcast i32* %6 to i8*
store i32 -1, i32* %6
%9 = icmp eq i32* %0, null
br i1 %9, label %36, label %10
10: ; preds = %4
%11 = call i64 @_internal_get_pty(i32* %6, i8** %5)
%12 = icmp eq i64 %11, 0
br i1 %12, label %13, label %31
13: ; preds = %10
%14 = icmp eq i8* %1, null
br i1 %14, label %18, label %15
15: ; preds = %13
%16 = load i8*, i8** %5
%17 = call i32 @strcpy(i8* %1, i8* %16)
br label %18
18: ; preds = %15, %13
%19 = call i32 (...) @fork()
switch i32 %19, label %29 [
i32 -1, label %31
i32 0, label %20
]
20: ; preds = %18
%21 = load i32, i32* %6
%22 = call i32 @close(i32 %21)
%23 = load i8*, i8** %5
%24 = call i64 @_internal_login_tty(i32 -1, i8* %23, %struct.termios* %2, %struct.winsize* %3)
%25 = icmp eq i64 %24, 0
br i1 %25, label %36, label %26
26: ; preds = %20
%27 = load i32, i32* @EXIT_FAILURE
%28 = call i32 @_exit(i32 %27)
unreachable
29: ; preds = %18
%30 = load i32, i32* %6
store i32 %30, i32* %0
br label %36
31: ; preds = %18, %10
%32 = load i32, i32* %6
%33 = icmp eq i32 %32, -1
br i1 %33, label %36, label %34
34: ; preds = %31
%35 = call i32 @close(i32 %32)
br label %36
36: ; preds = %31, %34, %20, %4, %29
%37 = phi i32 [ %19, %29 ], [ -1, %4 ], [ 0, %20 ], [ -1, %34 ], [ -1, %31 ]
ret i32 %37
}