#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 group {
  scalar_t__ gr_gid;
};
typedef scalar_t__ gid_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ O_RDWR;
int S_IRUSR;
int S_IWUSR;
scalar_t__ chmod(char *, int);
scalar_t__ chown(char *, int /*<<< orphan*/, scalar_t__);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, char *);
struct group *getgrnam(char *);
int /*<<< orphan*/ getuid();
int open(char *, int /*<<< orphan*/);
scalar_t__ openpty(int *, int *, char *, int /*<<< orphan*/ *,
                   int /*<<< orphan*/ *);
char *strerror(int /*<<< orphan*/);
int setup_slave_pty(FILE *fp) {
  int pty_master = -1;
  int pty_slave = -1;
  char pty_name[256];
  struct group *gptr;
  gid_t gid;
  int slave = -1;
  if (openpty(&pty_master, &pty_slave, pty_name, NULL, NULL) < 0) {
    fprintf(fp, "ERROR: openpty() failed with: %s.\n", strerror(errno));
    return -1;
  }
  if ((gptr = getgrnam(pty_name)) != 0) {
    gid = gptr->gr_gid;
  } else {
    gid = (gid_t)-1;
  }
  if (chown(pty_name, getuid(), gid) != 0) {
    fprintf(fp, "ERROR; chown() failed with: %s.\n", strerror(errno));
  }
  if (chmod(pty_name, S_IRUSR | S_IWUSR) != 0) {
    fprintf(fp, "ERROR: chmod() failed with: %s.\n", strerror(errno));
  }
  if ((slave = open(pty_name, O_RDWR)) == -1) {
    fprintf(fp, "ERROR: open() failed with: %s.\n", strerror(errno));
  }
  return (slave);
}


x86 assembly (clang, -O0):
<setup_slave_pty>:
push   %rbp
mov    %rsp,%rbp
sub    $0x160,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0xffffffff,-0x14(%rbp)
movl   $0xffffffff,-0x18(%rbp)
movl   $0xffffffff,-0x134(%rbp)
ff ff ff
lea    -0x120(%rbp),%rdx
lea    -0x14(%rbp),%rdi
lea    -0x18(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%r8d
mov    %r8,%rcx
call   1060 <openpty@plt>
cmp    $0x0,%rax
jge    1208 <setup_slave_pty+0x88>
mov    -0x10(%rbp),%rax
mov    %rax,-0x140(%rbp)
mov    0x2df1(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x140(%rbp),%rdi
mov    %rax,%rdx
lea    0xe09(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    135c <setup_slave_pty+0x1dc>
lea    -0x120(%rbp),%rdi
call   1050 <getgrnam@plt>
mov    %rax,-0x128(%rbp)
cmp    $0x0,%rax
je     123b <setup_slave_pty+0xbb>
mov    -0x128(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x130(%rbp)
jmp    1246 <setup_slave_pty+0xc6>
movq   $0xffffffffffffffff,-0x130(%rbp)
ff ff ff ff
lea    -0x120(%rbp),%rax
mov    %rax,-0x148(%rbp)
mov    $0x0,%al
call   1030 <getuid@plt>
mov    -0x148(%rbp),%rdi
mov    %eax,%esi
mov    -0x130(%rbp),%rdx
call   1070 <chown@plt>
cmp    $0x0,%rax
je     12a9 <setup_slave_pty+0x129>
mov    -0x10(%rbp),%rax
mov    %rax,-0x150(%rbp)
mov    0x2d44(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x150(%rbp),%rdi
mov    %rax,%rdx
lea    0xd7f(%rip),%rsi
call   1040 <fprintf@plt>
lea    -0x120(%rbp),%rdi
mov    0x2d11(%rip),%rax
mov    (%rax),%esi
mov    0x2d20(%rip),%rax
or     (%rax),%esi
call   1080 <chmod@plt>
cmp    $0x0,%rax
je     1300 <setup_slave_pty+0x180>
mov    -0x10(%rbp),%rax
mov    %rax,-0x158(%rbp)
mov    0x2ced(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x158(%rbp),%rdi
mov    %rax,%rdx
lea    0xd49(%rip),%rsi
call   1040 <fprintf@plt>
lea    -0x120(%rbp),%rdi
mov    0x2cda(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
mov    %eax,-0x134(%rbp)
cmp    $0xffffffff,%eax
jne    1353 <setup_slave_pty+0x1d3>
mov    -0x10(%rbp),%rax
mov    %rax,-0x160(%rbp)
mov    0x2c9a(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x160(%rbp),%rdi
mov    %rax,%rdx
lea    0xd17(%rip),%rsi
call   1040 <fprintf@plt>
mov    -0x134(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x160,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<setup_slave_pty>:
push   %r14
push   %rbx
sub    $0x118,%rsp
mov    %rdi,%r14
movl   $0xffffffff,0xc(%rsp)
ff
movl   $0xffffffff,0x8(%rsp)
ff
lea    0xc(%rsp),%rdi
lea    0x8(%rsp),%rsi
lea    0x10(%rsp),%rdx
xor    %ecx,%ecx
xor    %r8d,%r8d
call   1060 <openpty@plt>
test   %rax,%rax
js     11cf <setup_slave_pty+0x4f>
lea    0x10(%rsp),%rdi
call   1050 <getgrnam@plt>
test   %rax,%rax
je     11db <setup_slave_pty+0x5b>
mov    (%rax),%rbx
jmp    11e2 <setup_slave_pty+0x62>
lea    0xe2a(%rip),%rbx
jmp    127d <setup_slave_pty+0xfd>
mov    $0xffffffffffffffff,%rbx
xor    %eax,%eax
call   1030 <getuid@plt>
lea    0x10(%rsp),%rdi
mov    %eax,%esi
mov    %rbx,%rdx
call   1070 <chown@plt>
test   %rax,%rax
je     121d <setup_slave_pty+0x9d>
mov    0x2dcc(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xe11(%rip),%rsi
mov    %r14,%rdi
mov    %rax,%rdx
call   1040 <fprintf@plt>
mov    0x2da4(%rip),%rax
mov    0x2db5(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0x10(%rsp),%rdi
call   1080 <chmod@plt>
test   %rax,%rax
je     125e <setup_slave_pty+0xde>
mov    0x2d8b(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xdf1(%rip),%rsi
mov    %r14,%rdi
mov    %rax,%rdx
call   1040 <fprintf@plt>
mov    0x2d83(%rip),%rax
mov    (%rax),%esi
lea    0x10(%rsp),%rdi
call   1090 <open@plt>
cmp    $0xffffffff,%eax
jne    129e <setup_slave_pty+0x11e>
lea    0xde8(%rip),%rbx
mov    0x2d4c(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    %r14,%rdi
mov    %rbx,%rsi
mov    %rax,%rdx
call   1040 <fprintf@plt>
mov    $0xffffffff,%eax
add    $0x118,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.group = type { i64 }
@.str = [35 x i8] c"ERROR: openpty() failed with: %s.\0A\00"
@errno = global i32 0
@.str.1 = [33 x i8] c"ERROR; chown() failed with: %s.\0A\00"
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@.str.2 = [33 x i8] c"ERROR: chmod() failed with: %s.\0A\00"
@O_RDWR = global i32 0
@.str.3 = [32 x i8] c"ERROR: open() failed with: %s.\0A\00"
define i32 @setup_slave_pty(i32* %0) {
%2 = alloca i32
%3 = alloca i32*
%4 = alloca i32
%5 = alloca i32
%6 = alloca [256 x i8]
%7 = alloca %struct.group*
%8 = alloca i64
%9 = alloca i32
store i32* %0, i32** %3
store i32 -1, i32* %4
store i32 -1, i32* %5
store i32 -1, i32* %9
%10 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%11 = call i64 @openpty(i32* %4, i32* %5, i8* %10, i32* null, i32* null)
%12 = icmp slt i64 %11, 0
br i1 %12, label %13, label %18
13: ; preds = %1
%14 = load i32*, i32** %3
%15 = load i32, i32* @errno
%16 = call i8* @strerror(i32 %15)
%17 = call i32 @fprintf(i32* %14, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0), i8* %16)
store i32 -1, i32* %2
br label %62
18: ; preds = %1
%19 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%20 = call %struct.group* @getgrnam(i8* %19)
store %struct.group* %20, %struct.group** %7
%21 = icmp ne %struct.group* %20, null
br i1 %21, label %22, label %26
22: ; preds = %18
%23 = load %struct.group*, %struct.group** %7
%24 = getelementptr inbounds %struct.group, %struct.group* %23, i32 0, i32 0
%25 = load i64, i64* %24
store i64 %25, i64* %8
br label %27
26: ; preds = %18
store i64 -1, i64* %8
br label %27
27: ; preds = %26, %22
%28 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%29 = call i32 (...) @getuid()
%30 = load i64, i64* %8
%31 = call i64 @chown(i8* %28, i32 %29, i64 %30)
%32 = icmp ne i64 %31, 0
br i1 %32, label %33, label %38
33: ; preds = %27
%34 = load i32*, i32** %3
%35 = load i32, i32* @errno
%36 = call i8* @strerror(i32 %35)
%37 = call i32 @fprintf(i32* %34, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i8* %36)
br label %38
38: ; preds = %33, %27
%39 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%40 = load i32, i32* @S_IRUSR
%41 = load i32, i32* @S_IWUSR
%42 = or i32 %40, %41
%43 = call i64 @chmod(i8* %39, i32 %42)
%44 = icmp ne i64 %43, 0
br i1 %44, label %45, label %50
45: ; preds = %38
%46 = load i32*, i32** %3
%47 = load i32, i32* @errno
%48 = call i8* @strerror(i32 %47)
%49 = call i32 @fprintf(i32* %46, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0), i8* %48)
br label %50
50: ; preds = %45, %38
%51 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%52 = load i32, i32* @O_RDWR
%53 = call i32 @open(i8* %51, i32 %52)
store i32 %53, i32* %9
%54 = icmp eq i32 %53, -1
br i1 %54, label %55, label %60
55: ; preds = %50
%56 = load i32*, i32** %3
%57 = load i32, i32* @errno
%58 = call i8* @strerror(i32 %57)
%59 = call i32 @fprintf(i32* %56, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0), i8* %58)
br label %60
60: ; preds = %55, %50
%61 = load i32, i32* %9
store i32 %61, i32* %2
br label %62
62: ; preds = %60, %13
%63 = load i32, i32* %2
ret i32 %63
}

LLVM IR (-O2):
%struct.group = type { i64 }
@.str = [35 x i8] c"ERROR: openpty() failed with: %s.\0A\00"
@errno = global i32 0
@.str.1 = [33 x i8] c"ERROR; chown() failed with: %s.\0A\00"
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@.str.2 = [33 x i8] c"ERROR: chmod() failed with: %s.\0A\00"
@O_RDWR = global i32 0
@.str.3 = [32 x i8] c"ERROR: open() failed with: %s.\0A\00"
define i32 @setup_slave_pty(i32* nocapture %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca [256 x i8]
%5 = bitcast i32* %2 to i8*
store i32 -1, i32* %2
%6 = bitcast i32* %3 to i8*
store i32 -1, i32* %3
%7 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%8 = call i64 @openpty(i32* %2, i32* %3, i8* %7, i32* null, i32* null)
%9 = icmp slt i64 %8, 0
br i1 %9, label %39, label %10
10: ; preds = %1
%11 = call %struct.group* @getgrnam(i8* %7)
%12 = icmp eq %struct.group* %11, null
br i1 %12, label %16, label %13
13: ; preds = %10
%14 = getelementptr inbounds %struct.group, %struct.group* %11, i64 0, i32 0
%15 = load i64, i64* %14
br label %16
16: ; preds = %10, %13
%17 = phi i64 [ %15, %13 ], [ -1, %10 ]
%18 = call i32 (...) @getuid()
%19 = call i64 @chown(i8* %7, i32 %18, i64 %17)
%20 = icmp eq i64 %19, 0
br i1 %20, label %25, label %21
21: ; preds = %16
%22 = load i32, i32* @errno
%23 = call i8* @strerror(i32 %22)
%24 = call i32 @fprintf(i32* %0, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i8* %23)
br label %25
25: ; preds = %21, %16
%26 = load i32, i32* @S_IRUSR
%27 = load i32, i32* @S_IWUSR
%28 = or i32 %27, %26
%29 = call i64 @chmod(i8* %7, i32 %28)
%30 = icmp eq i64 %29, 0
br i1 %30, label %35, label %31
31: ; preds = %25
%32 = load i32, i32* @errno
%33 = call i8* @strerror(i32 %32)
%34 = call i32 @fprintf(i32* %0, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0), i8* %33)
br label %35
35: ; preds = %31, %25
%36 = load i32, i32* @O_RDWR
%37 = call i32 @open(i8* %7, i32 %36)
%38 = icmp eq i32 %37, -1
br i1 %38, label %39, label %44
39: ; preds = %35, %1
%40 = phi i8* [ getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0), %1 ], [ getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0), %35 ]
%41 = load i32, i32* @errno
%42 = call i8* @strerror(i32 %41)
%43 = call i32 @fprintf(i32* %0, i8* %40, i8* %42)
br label %44
44: ; preds = %39, %35
%45 = phi i32 [ %37, %35 ], [ -1, %39 ]
ret i32 %45
}