#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 */
typedef int /*<<< orphan*/ pts_name;
typedef int pid_t;

/* Variables and functions */
scalar_t__ STDERR_FILENO;
scalar_t__ STDIN_FILENO;
scalar_t__ STDOUT_FILENO;
int /*<<< orphan*/ close(int);
scalar_t__ dup2(int, scalar_t__);
int fork();
int getpid();
int /*<<< orphan*/ printf(char *, ...);
int ptym_open(char *, int);
int ptys_open(char *);
int /*<<< orphan*/ set_raw_tty(int);
scalar_t__ setsid();
pid_t pty_fork(int *ptrfdm, int mode) {
  int fdm, fds;
  pid_t pid;
  char pts_name[20];
  if ((fdm = ptym_open(pts_name, sizeof(pts_name))) < 0) {
    printf("ERROR: can't open master pty: %s, error %d", pts_name, fdm);
    return (-1);
  }
  if (mode == 0) {
    set_raw_tty(fdm);
  }
  if ((pid = fork()) < 0) {
    return (-1);
  } else if (pid == 0) {
    printf("[%d] pty fork child\n", getpid());
    close(fdm);
    if (setsid() < 0)
      printf("ERROR: setsid error");
    if ((fds = ptys_open(pts_name)) < 0)
      printf("ERROR: can't open slave pty");
    if (mode == 0) {
      set_raw_tty(fds);
    }
    if (dup2(fds, STDIN_FILENO) != STDIN_FILENO)
      printf("ERROR: dup2 error to stdin");
    if (dup2(fds, STDOUT_FILENO) != STDOUT_FILENO)
      printf("ERROR: dup2 error to stdout");
    if (dup2(fds, STDERR_FILENO) != STDERR_FILENO)
      printf("ERROR: dup2 error to stderr");
    if (fds != STDIN_FILENO && fds != STDOUT_FILENO && fds != STDERR_FILENO)
      close(fds);
    return (0);
  } else {
    printf("[%d] pty fork parent\n", getpid());
    *ptrfdm = fdm;
    return (pid);
  }
}


x86 assembly (clang, -O0):
<pty_fork>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
lea    -0x40(%rbp),%rdi
mov    $0x14,%esi
call   1090 <ptym_open@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jge    11da <pty_fork+0x4a>
lea    -0x40(%rbp),%rsi
mov    -0x18(%rbp),%edx
lea    0xe39(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    138e <pty_fork+0x1fe>
cmpl   $0x0,-0x14(%rbp)
jne    11ec <pty_fork+0x5c>
mov    -0x18(%rbp),%edi
call   1040 <set_raw_tty@plt>
mov    $0x0,%al
call   10b0 <fork@plt>
mov    %eax,-0x20(%rbp)
cmp    $0x0,%eax
jge    120b <pty_fork+0x7b>
movl   $0xffffffff,-0x4(%rbp)
jmp    138e <pty_fork+0x1fe>
cmpl   $0x0,-0x20(%rbp)
jne    1368 <pty_fork+0x1d8>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xe06(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x18(%rbp),%edi
call   1070 <close@plt>
mov    $0x0,%al
call   1080 <setsid@plt>
cmp    $0x0,%rax
jge    1253 <pty_fork+0xc3>
lea    0xdf4(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
lea    -0x40(%rbp),%rdi
call   10a0 <ptys_open@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jge    1276 <pty_fork+0xe6>
lea    0xde5(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
cmpl   $0x0,-0x14(%rbp)
jne    1288 <pty_fork+0xf8>
mov    -0x1c(%rbp),%edi
call   1040 <set_raw_tty@plt>
mov    -0x1c(%rbp),%edi
mov    0x2d3e(%rip),%rax
mov    (%rax),%rsi
call   1050 <dup2@plt>
mov    0x2d2f(%rip),%rcx
cmp    (%rcx),%rax
je     12b8 <pty_fork+0x128>
lea    0xdbf(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x1c(%rbp),%edi
mov    0x2d2e(%rip),%rax
mov    (%rax),%rsi
call   1050 <dup2@plt>
mov    0x2d1f(%rip),%rcx
cmp    (%rcx),%rax
je     12e8 <pty_fork+0x158>
lea    0xdaa(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x1c(%rbp),%edi
mov    0x2ce6(%rip),%rax
mov    (%rax),%rsi
call   1050 <dup2@plt>
mov    0x2cd7(%rip),%rcx
cmp    (%rcx),%rax
je     1318 <pty_fork+0x188>
lea    0xd96(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movslq -0x1c(%rbp),%rax
mov    0x2cad(%rip),%rcx
cmp    (%rcx),%rax
je     135c <pty_fork+0x1cc>
movslq -0x1c(%rbp),%rax
mov    0x2cb9(%rip),%rcx
cmp    (%rcx),%rax
je     135c <pty_fork+0x1cc>
movslq -0x1c(%rbp),%rax
mov    0x2c8d(%rip),%rcx
cmp    (%rcx),%rax
je     135c <pty_fork+0x1cc>
mov    -0x1c(%rbp),%edi
call   1070 <close@plt>
movl   $0x0,-0x4(%rbp)
jmp    138e <pty_fork+0x1fe>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xd4b(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x18(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pty_fork>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %esi,%r14d
mov    %rdi,%r15
mov    %rsp,%rdi
mov    $0x14,%esi
call   1090 <ptym_open@plt>
mov    %eax,%ebx
test   %eax,%eax
js     11ec <pty_fork+0x5c>
test   %r14d,%r14d
jne    11bf <pty_fork+0x2f>
mov    %ebx,%edi
call   1040 <set_raw_tty@plt>
xor    %eax,%eax
call   10b0 <fork@plt>
test   %eax,%eax
js     11ff <pty_fork+0x6f>
mov    %eax,%ebp
xor    %eax,%eax
call   1030 <getpid@plt>
test   %ebp,%ebp
je     1211 <pty_fork+0x81>
lea    0xee5(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %ebx,(%r15)
jmp    1204 <pty_fork+0x74>
lea    0xe0d(%rip),%rdi
mov    %rsp,%rsi
mov    %ebx,%edx
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe13(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %ebx,%edi
call   1070 <close@plt>
xor    %eax,%eax
call   1080 <setsid@plt>
test   %rax,%rax
jns    1242 <pty_fork+0xb2>
lea    0xe05(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %rsp,%rdi
call   10a0 <ptys_open@plt>
mov    %eax,%ebx
test   %eax,%eax
js     12fb <pty_fork+0x16b>
test   %r14d,%r14d
je     1312 <pty_fork+0x182>
mov    0x2d6c(%rip),%r14
mov    (%r14),%rsi
mov    %ebx,%edi
call   1050 <dup2@plt>
cmp    (%r14),%rax
je     1281 <pty_fork+0xf1>
lea    0xdf6(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    0x2d68(%rip),%r15
mov    (%r15),%rsi
mov    %ebx,%edi
call   1050 <dup2@plt>
cmp    (%r15),%rax
je     12a5 <pty_fork+0x115>
lea    0xded(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    0x2d2c(%rip),%rbp
mov    0x0(%rbp),%rsi
mov    %ebx,%edi
call   1050 <dup2@plt>
cmp    0x0(%rbp),%rax
je     12cf <pty_fork+0x13f>
lea    0xde3(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    0x0(%rbp),%rax
movslq %ebx,%rcx
xor    %ebp,%ebp
cmp    %rcx,(%r14)
je     1204 <pty_fork+0x74>
cmp    %rcx,(%r15)
je     1204 <pty_fork+0x74>
cmp    %rcx,%rax
je     1204 <pty_fork+0x74>
mov    %ebx,%edi
call   1070 <close@plt>
jmp    1204 <pty_fork+0x74>
lea    0xd52(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
test   %r14d,%r14d
jne    125d <pty_fork+0xcd>
mov    %ebx,%edi
call   1040 <set_raw_tty@plt>
jmp    125d <pty_fork+0xcd>

LLVM IR (-O0):
@.str = [43 x i8] c"ERROR: can't open master pty: %s, error %d\00"
@.str.1 = [21 x i8] c"[%d] pty fork child\0A\00"
@.str.2 = [20 x i8] c"ERROR: setsid error\00"
@.str.3 = [28 x i8] c"ERROR: can't open slave pty\00"
@STDIN_FILENO = global i64 0
@.str.4 = [27 x i8] c"ERROR: dup2 error to stdin\00"
@STDOUT_FILENO = global i64 0
@.str.5 = [28 x i8] c"ERROR: dup2 error to stdout\00"
@STDERR_FILENO = global i64 0
@.str.6 = [28 x i8] c"ERROR: dup2 error to stderr\00"
@.str.7 = [22 x i8] c"[%d] pty fork parent\0A\00"
define i32 @pty_fork(i32* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca [20 x i8]
store i32* %0, i32** %4
store i32 %1, i32* %5
%10 = getelementptr inbounds [20 x i8], [20 x i8]* %9, i64 0, i64 0
%11 = call i32 @ptym_open(i8* %10, i32 20)
store i32 %11, i32* %6
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %17
13: ; preds = %2
%14 = getelementptr inbounds [20 x i8], [20 x i8]* %9, i64 0, i64 0
%15 = load i32, i32* %6
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str, i64 0, i64 0), i8* %14, i32 %15)
store i32 -1, i32* %3
br label %100
17: ; preds = %2
%18 = load i32, i32* %5
%19 = icmp eq i32 %18, 0
br i1 %19, label %20, label %23
20: ; preds = %17
%21 = load i32, i32* %6
%22 = call i32 @set_raw_tty(i32 %21)
br label %23
23: ; preds = %20, %17
%24 = call i32 (...) @fork()
store i32 %24, i32* %8
%25 = icmp slt i32 %24, 0
br i1 %25, label %26, label %27
26: ; preds = %23
store i32 -1, i32* %3
br label %100
27: ; preds = %23
%28 = load i32, i32* %8
%29 = icmp eq i32 %28, 0
br i1 %29, label %30, label %94
30: ; preds = %27
%31 = call i32 (...) @getpid()
%32 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i32 %31)
%33 = load i32, i32* %6
%34 = call i32 @close(i32 %33)
%35 = call i64 (...) @setsid()
%36 = icmp slt i64 %35, 0
br i1 %36, label %37, label %39
37: ; preds = %30
%38 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0))
br label %39
39: ; preds = %37, %30
%40 = getelementptr inbounds [20 x i8], [20 x i8]* %9, i64 0, i64 0
%41 = call i32 @ptys_open(i8* %40)
store i32 %41, i32* %7
%42 = icmp slt i32 %41, 0
br i1 %42, label %43, label %45
43: ; preds = %39
%44 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0))
br label %45
45: ; preds = %43, %39
%46 = load i32, i32* %5
%47 = icmp eq i32 %46, 0
br i1 %47, label %48, label %51
48: ; preds = %45
%49 = load i32, i32* %7
%50 = call i32 @set_raw_tty(i32 %49)
br label %51
51: ; preds = %48, %45
%52 = load i32, i32* %7
%53 = load i64, i64* @STDIN_FILENO
%54 = call i64 @dup2(i32 %52, i64 %53)
%55 = load i64, i64* @STDIN_FILENO
%56 = icmp ne i64 %54, %55
br i1 %56, label %57, label %59
57: ; preds = %51
%58 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.4, i64 0, i64 0))
br label %59
59: ; preds = %57, %51
%60 = load i32, i32* %7
%61 = load i64, i64* @STDOUT_FILENO
%62 = call i64 @dup2(i32 %60, i64 %61)
%63 = load i64, i64* @STDOUT_FILENO
%64 = icmp ne i64 %62, %63
br i1 %64, label %65, label %67
65: ; preds = %59
%66 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.5, i64 0, i64 0))
br label %67
67: ; preds = %65, %59
%68 = load i32, i32* %7
%69 = load i64, i64* @STDERR_FILENO
%70 = call i64 @dup2(i32 %68, i64 %69)
%71 = load i64, i64* @STDERR_FILENO
%72 = icmp ne i64 %70, %71
br i1 %72, label %73, label %75
73: ; preds = %67
%74 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.6, i64 0, i64 0))
br label %75
75: ; preds = %73, %67
%76 = load i32, i32* %7
%77 = sext i32 %76 to i64
%78 = load i64, i64* @STDIN_FILENO
%79 = icmp ne i64 %77, %78
br i1 %79, label %80, label %93
80: ; preds = %75
%81 = load i32, i32* %7
%82 = sext i32 %81 to i64
%83 = load i64, i64* @STDOUT_FILENO
%84 = icmp ne i64 %82, %83
br i1 %84, label %85, label %93
85: ; preds = %80
%86 = load i32, i32* %7
%87 = sext i32 %86 to i64
%88 = load i64, i64* @STDERR_FILENO
%89 = icmp ne i64 %87, %88
br i1 %89, label %90, label %93
90: ; preds = %85
%91 = load i32, i32* %7
%92 = call i32 @close(i32 %91)
br label %93
93: ; preds = %90, %85, %80, %75
store i32 0, i32* %3
br label %100
94: ; preds = %27
%95 = call i32 (...) @getpid()
%96 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.7, i64 0, i64 0), i32 %95)
%97 = load i32, i32* %6
%98 = load i32*, i32** %4
store i32 %97, i32* %98
%99 = load i32, i32* %8
store i32 %99, i32* %3
br label %100
100: ; preds = %94, %93, %26, %13
%101 = load i32, i32* %3
ret i32 %101
}

LLVM IR (-O2):
@.str = [43 x i8] c"ERROR: can't open master pty: %s, error %d\00"
@.str.1 = [21 x i8] c"[%d] pty fork child\0A\00"
@.str.2 = [20 x i8] c"ERROR: setsid error\00"
@.str.3 = [28 x i8] c"ERROR: can't open slave pty\00"
@STDIN_FILENO = global i64 0
@.str.4 = [27 x i8] c"ERROR: dup2 error to stdin\00"
@STDOUT_FILENO = global i64 0
@.str.5 = [28 x i8] c"ERROR: dup2 error to stdout\00"
@STDERR_FILENO = global i64 0
@.str.6 = [28 x i8] c"ERROR: dup2 error to stderr\00"
@.str.7 = [22 x i8] c"[%d] pty fork parent\0A\00"
define i32 @pty_fork(i32* nocapture writeonly %0, i32 %1) {
%3 = alloca [20 x i8]
%4 = getelementptr inbounds [20 x i8], [20 x i8]* %3, i64 0, i64 0
%5 = call i32 @ptym_open(i8* %4, i32 20)
%6 = icmp slt i32 %5, 0
br i1 %6, label %7, label %9
7: ; preds = %2
%8 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([43 x i8], [43 x i8]* @.str, i64 0, i64 0), i8* %4, i32 %5)
br label %70
9: ; preds = %2
%10 = icmp eq i32 %1, 0
br i1 %10, label %11, label %13
11: ; preds = %9
%12 = call i32 @set_raw_tty(i32 %5)
br label %13
13: ; preds = %11, %9
%14 = call i32 (...) @fork()
%15 = icmp slt i32 %14, 0
br i1 %15, label %70, label %16
16: ; preds = %13
%17 = icmp eq i32 %14, 0
%18 = call i32 (...) @getpid()
br i1 %17, label %19, label %68
19: ; preds = %16
%20 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i32 %18)
%21 = call i32 @close(i32 %5)
%22 = call i64 (...) @setsid()
%23 = icmp slt i64 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %19
%25 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0))
br label %26
26: ; preds = %24, %19
%27 = call i32 @ptys_open(i8* %4)
%28 = icmp slt i32 %27, 0
br i1 %28, label %29, label %31
29: ; preds = %26
%30 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0))
br label %31
31: ; preds = %29, %26
br i1 %10, label %32, label %34
32: ; preds = %31
%33 = call i32 @set_raw_tty(i32 %27)
br label %34
34: ; preds = %32, %31
%35 = load i64, i64* @STDIN_FILENO
%36 = call i64 @dup2(i32 %27, i64 %35)
%37 = load i64, i64* @STDIN_FILENO
%38 = icmp eq i64 %36, %37
br i1 %38, label %41, label %39
39: ; preds = %34
%40 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @.str.4, i64 0, i64 0))
br label %41
41: ; preds = %39, %34
%42 = load i64, i64* @STDOUT_FILENO
%43 = call i64 @dup2(i32 %27, i64 %42)
%44 = load i64, i64* @STDOUT_FILENO
%45 = icmp eq i64 %43, %44
br i1 %45, label %48, label %46
46: ; preds = %41
%47 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @.str.5, i64 0, i64 0))
br label %48
48: ; preds = %46, %41
%49 = load i64, i64* @STDERR_FILENO
%50 = call i64 @dup2(i32 %27, i64 %49)
%51 = load i64, i64* @STDERR_FILENO
%52 = icmp eq i64 %50, %51
br i1 %52, label %56, label %53
53: ; preds = %48
%54 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @.str.6, i64 0, i64 0))
%55 = load i64, i64* @STDERR_FILENO
br label %56
56: ; preds = %53, %48
%57 = phi i64 [ %55, %53 ], [ %50, %48 ]
%58 = sext i32 %27 to i64
%59 = load i64, i64* @STDIN_FILENO
%60 = icmp eq i64 %59, %58
%61 = load i64, i64* @STDOUT_FILENO
%62 = icmp eq i64 %61, %58
%63 = select i1 %60, i1 true, i1 %62
%64 = icmp eq i64 %57, %58
%65 = select i1 %63, i1 true, i1 %64
br i1 %65, label %70, label %66
66: ; preds = %56
%67 = call i32 @close(i32 %27)
br label %70
68: ; preds = %16
%69 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.7, i64 0, i64 0), i32 %18)
store i32 %5, i32* %0
br label %70
70: ; preds = %56, %66, %13, %68, %7
%71 = phi i32 [ -1, %7 ], [ %14, %68 ], [ -1, %13 ], [ 0, %66 ], [ 0, %56 ]
ret i32 %71
}