#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;
};

/* Variables and functions */
int /*<<< orphan*/ _exit(int);
int /*<<< orphan*/ close(int);
int fork();
int /*<<< orphan*/ login_tty(int);
int openpty(int *, int *, char *, struct termios *, struct winsize *);
int forkpty(int *amaster, char *name, struct termios *termp,
            struct winsize *winp) {
  int master, slave, pid;
  if (openpty(&master, &slave, name, termp, winp) == -1)
    return -1;
  switch (pid = fork()) {
  case -1:
    return -1;
  case 0:
    close(master);
    if (login_tty(slave))
      _exit(1);
    return 0;
  default:
    *amaster = master;
    close(slave);
    return pid;
  }
}


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)
mov    -0x18(%rbp),%rdx
mov    -0x20(%rbp),%rcx
mov    -0x28(%rbp),%r8
lea    -0x2c(%rbp),%rdi
lea    -0x30(%rbp),%rsi
call   1060 <openpty@plt>
cmp    $0xffffffff,%eax
jne    1196 <forkpty+0x46>
movl   $0xffffffff,-0x4(%rbp)
jmp    1215 <forkpty+0xc5>
xor    %eax,%eax
call   1070 <fork@plt>
mov    %eax,-0x34(%rbp)
mov    %eax,%ecx
mov    %ecx,-0x38(%rbp)
sub    $0xffffffff,%eax
je     11c3 <forkpty+0x73>
jmp    11b3 <forkpty+0x63>
mov    -0x38(%rbp),%eax
test   %eax,%eax
je     11cf <forkpty+0x7f>
jmp    11fe <forkpty+0xae>
movl   $0xffffffff,-0x4(%rbp)
jmp    1215 <forkpty+0xc5>
mov    -0x2c(%rbp),%edi
call   1040 <close@plt>
mov    -0x30(%rbp),%edi
call   1050 <login_tty@plt>
cmp    $0x0,%eax
je     11f2 <forkpty+0xa2>
mov    $0x1,%edi
call   1030 <_exit@plt>
movl   $0x0,-0x4(%rbp)
jmp    1215 <forkpty+0xc5>
mov    -0x2c(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x30(%rbp),%edi
call   1040 <close@plt>
mov    -0x34(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<forkpty>:
push   %rbp
push   %rbx
push   %rax
mov    %rcx,%r8
mov    %rdx,%rcx
mov    %rsi,%rdx
mov    %rdi,%rbx
lea    0x4(%rsp),%rdi
mov    %rsp,%rsi
call   1060 <openpty@plt>
cmp    $0xffffffff,%eax
je     11a4 <forkpty+0x54>
xor    %eax,%eax
call   1070 <fork@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
je     11b9 <forkpty+0x69>
test   %ebp,%ebp
jne    11ab <forkpty+0x5b>
mov    0x4(%rsp),%edi
call   1040 <close@plt>
mov    (%rsp),%edi
call   1050 <login_tty@plt>
xor    %ebp,%ebp
test   %eax,%eax
je     11b9 <forkpty+0x69>
mov    $0x1,%edi
call   1030 <_exit@plt>
mov    $0xffffffff,%ebp
jmp    11b9 <forkpty+0x69>
mov    0x4(%rsp),%eax
mov    %eax,(%rbx)
mov    (%rsp),%edi
call   1040 <close@plt>
mov    %ebp,%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
%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
%13 = load i8*, i8** %7
%14 = load %struct.termios*, %struct.termios** %8
%15 = load %struct.winsize*, %struct.winsize** %9
%16 = call i32 @openpty(i32* %10, i32* %11, i8* %13, %struct.termios* %14, %struct.winsize* %15)
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %19
18: ; preds = %4
store i32 -1, i32* %5
br label %37
19: ; preds = %4
%20 = call i32 (...) @fork()
store i32 %20, i32* %12
switch i32 %20, label %31 [
i32 -1, label %21
i32 0, label %22
]
21: ; preds = %19
store i32 -1, i32* %5
br label %37
22: ; preds = %19
%23 = load i32, i32* %10
%24 = call i32 @close(i32 %23)
%25 = load i32, i32* %11
%26 = call i32 @login_tty(i32 %25)
%27 = icmp ne i32 %26, 0
br i1 %27, label %28, label %30
28: ; preds = %22
%29 = call i32 @_exit(i32 1)
unreachable
30: ; preds = %22
store i32 0, i32* %5
br label %37
31: ; preds = %19
%32 = load i32, i32* %10
%33 = load i32*, i32** %6
store i32 %32, i32* %33
%34 = load i32, i32* %11
%35 = call i32 @close(i32 %34)
%36 = load i32, i32* %12
store i32 %36, i32* %5
br label %37
37: ; preds = %31, %30, %21, %18
%38 = load i32, i32* %5
ret i32 %38
}

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