#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*/ close(int);
int fork();
int /*<<< orphan*/ login_tty(int);
int openpty(int *, int *, char *, struct termios const *,
            struct winsize const *);
int forkpty(int *amaster, char *name, const struct termios *termp,
            const 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);
    login_tty(slave);
    return 0;
  }
  if (amaster)
    *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   1050 <openpty@plt>
cmp    $0xffffffff,%eax
jne    1186 <forkpty+0x46>
movl   $0xffffffff,-0x4(%rbp)
jmp    11fd <forkpty+0xbd>
xor    %eax,%eax
call   1060 <fork@plt>
mov    %eax,-0x34(%rbp)
mov    %eax,%ecx
mov    %ecx,-0x38(%rbp)
sub    $0xffffffff,%eax
je     11b3 <forkpty+0x73>
jmp    11a3 <forkpty+0x63>
mov    -0x38(%rbp),%eax
test   %eax,%eax
je     11bf <forkpty+0x7f>
jmp    11db <forkpty+0x9b>
movl   $0xffffffff,-0x4(%rbp)
jmp    11fd <forkpty+0xbd>
mov    -0x2c(%rbp),%edi
call   1030 <close@plt>
mov    -0x30(%rbp),%edi
call   1040 <login_tty@plt>
movl   $0x0,-0x4(%rbp)
jmp    11fd <forkpty+0xbd>
cmpq   $0x0,-0x10(%rbp)
je     11ef <forkpty+0xaf>
mov    -0x2c(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x30(%rbp),%edi
call   1030 <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   1050 <openpty@plt>
cmp    $0xffffffff,%eax
je     1188 <forkpty+0x48>
xor    %eax,%eax
call   1060 <fork@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
je     11a2 <forkpty+0x62>
test   %ebp,%ebp
jne    118f <forkpty+0x4f>
mov    0x4(%rsp),%edi
call   1030 <close@plt>
mov    (%rsp),%edi
call   1040 <login_tty@plt>
xor    %ebp,%ebp
jmp    11a2 <forkpty+0x62>
mov    $0xffffffff,%ebp
jmp    11a2 <forkpty+0x62>
test   %rbx,%rbx
je     119a <forkpty+0x5a>
mov    0x4(%rsp),%eax
mov    %eax,(%rbx)
mov    (%rsp),%edi
call   1030 <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 %27 [
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)
store i32 0, i32* %5
br label %37
27: ; preds = %19
%28 = load i32*, i32** %6
%29 = icmp ne i32* %28, null
br i1 %29, label %30, label %33
30: ; preds = %27
%31 = load i32, i32* %10
%32 = load i32*, i32** %6
store i32 %31, i32* %32
br label %33
33: ; preds = %30, %27
%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 = %33, %22, %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* 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 %18 [
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)
br label %25
18: ; preds = %11
%19 = icmp eq i32* %0, null
br i1 %19, label %22, label %20
20: ; preds = %18
%21 = load i32, i32* %5
store i32 %21, i32* %0
br label %22
22: ; preds = %20, %18
%23 = load i32, i32* %6
%24 = call i32 @close(i32 %23)
br label %25
25: ; preds = %11, %4, %22, %13
%26 = phi i32 [ %12, %22 ], [ 0, %13 ], [ -1, %4 ], [ %12, %11 ]
ret i32 %26
}