#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 */
scalar_t__ close(int);
int fork();
int /*<<< orphan*/ login_tty(int);
scalar_t__ openpty(int *, int *, char *, struct termios const *,
                   struct winsize const *);
pid_t forkpty(int *amaster, char *name, const struct termios *termp,
              const struct winsize *winp) {
  int pts_fd;
  pid_t child;
  if (openpty(amaster, &pts_fd, name, termp, winp) < 0)
    return -1;
  child = fork();
  if (child < 0)
    return -1;
  if (!child) {
    login_tty(pts_fd);
  } else {
    if (close(pts_fd) < 0)
      return -1;
  }
  return child;
}


x86 assembly (clang, -O0):
<forkpty>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    -0x20(%rbp),%rcx
mov    -0x28(%rbp),%r8
lea    -0x2c(%rbp),%rsi
call   1050 <openpty@plt>
cmp    $0x0,%rax
jge    1187 <forkpty+0x47>
movl   $0xffffffff,-0x4(%rbp)
jmp    11e7 <forkpty+0xa7>
mov    $0x0,%al
call   1060 <fork@plt>
mov    %eax,-0x30(%rbp)
cmpl   $0x0,-0x30(%rbp)
jge    11a7 <forkpty+0x67>
movl   $0xffffffff,-0x4(%rbp)
jmp    11e7 <forkpty+0xa7>
cmpl   $0x0,-0x30(%rbp)
jne    11be <forkpty+0x7e>
mov    -0x2c(%rbp),%edi
call   1040 <login_tty@plt>
jmp    11e1 <forkpty+0xa1>
mov    -0x2c(%rbp),%edi
call   1030 <close@plt>
cmp    $0x0,%rax
jge    11dc <forkpty+0x9c>
movl   $0xffffffff,-0x4(%rbp)
jmp    11e7 <forkpty+0xa7>
jmp    11e1 <forkpty+0xa1>
mov    -0x30(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<forkpty>:
push   %rbp
push   %rbx
push   %rax
mov    %rcx,%r8
mov    %rdx,%rcx
mov    %rsi,%rdx
lea    0x4(%rsp),%rsi
call   1050 <openpty@plt>
mov    $0xffffffff,%ebx
test   %rax,%rax
js     1186 <forkpty+0x46>
xor    %eax,%eax
call   1060 <fork@plt>
test   %eax,%eax
js     1186 <forkpty+0x46>
mov    %eax,%ebp
mov    0x4(%rsp),%edi
je     117f <forkpty+0x3f>
call   1030 <close@plt>
test   %rax,%rax
jns    1184 <forkpty+0x44>
jmp    1186 <forkpty+0x46>
call   1040 <login_tty@plt>
mov    %ebp,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.termios = type { i32 }
%struct.winsize = type { i32 }
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 i32
%11 = 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
%12 = load i32*, i32** %6
%13 = load i8*, i8** %7
%14 = load %struct.termios*, %struct.termios** %8
%15 = load %struct.winsize*, %struct.winsize** %9
%16 = call i64 @openpty(i32* %12, i32* %10, i8* %13, %struct.termios* %14, %struct.winsize* %15)
%17 = icmp slt i64 %16, 0
br i1 %17, label %18, label %19
18: ; preds = %4
store i32 -1, i32* %5
br label %38
19: ; preds = %4
%20 = call i32 (...) @fork()
store i32 %20, i32* %11
%21 = load i32, i32* %11
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %24
23: ; preds = %19
store i32 -1, i32* %5
br label %38
24: ; preds = %19
%25 = load i32, i32* %11
%26 = icmp ne i32 %25, 0
br i1 %26, label %30, label %27
27: ; preds = %24
%28 = load i32, i32* %10
%29 = call i32 @login_tty(i32 %28)
br label %36
30: ; preds = %24
%31 = load i32, i32* %10
%32 = call i64 @close(i32 %31)
%33 = icmp slt i64 %32, 0
br i1 %33, label %34, label %35
34: ; preds = %30
store i32 -1, i32* %5
br label %38
35: ; preds = %30
br label %36
36: ; preds = %35, %27
%37 = load i32, i32* %11
store i32 %37, i32* %5
br label %38
38: ; preds = %36, %34, %23, %18
%39 = load i32, i32* %5
ret i32 %39
}

LLVM IR (-O2):
%struct.termios = type { i32 }
%struct.winsize = type { i32 }
define i32 @forkpty(i32* %0, i8* %1, %struct.termios* %2, %struct.winsize* %3) {
%5 = alloca i32
%6 = bitcast i32* %5 to i8*
%7 = call i64 @openpty(i32* %0, i32* %5, i8* %1, %struct.termios* %2, %struct.winsize* %3)
%8 = icmp slt i64 %7, 0
br i1 %8, label %21, label %9
9: ; preds = %4
%10 = call i32 (...) @fork()
%11 = icmp slt i32 %10, 0
br i1 %11, label %21, label %12
12: ; preds = %9
%13 = icmp eq i32 %10, 0
%14 = load i32, i32* %5
br i1 %13, label %15, label %17
15: ; preds = %12
%16 = call i32 @login_tty(i32 %14)
br label %20
17: ; preds = %12
%18 = call i64 @close(i32 %14)
%19 = icmp slt i64 %18, 0
br i1 %19, label %21, label %20
20: ; preds = %17, %15
br label %21
21: ; preds = %17, %9, %4, %20
%22 = phi i32 [ %10, %20 ], [ -1, %4 ], [ -1, %9 ], [ -1, %17 ]
ret i32 %22
}