#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*/ buf;

/* Variables and functions */
int /*<<< orphan*/ EINVAL;
int /*<<< orphan*/ ENOTTY;
int /*<<< orphan*/ ERANGE;
int O_CREAT;
int /*<<< orphan*/ O_RDONLY;
int O_RDWR;
int /*<<< orphan*/ close(int);
int do_single_test(int, char *, int, int /*<<< orphan*/);
int errno;
int open(char *, int, ...);
int posix_openpt(int);
int /*<<< orphan*/ printf(char *, ...);
char *strerror(int);
int /*<<< orphan*/ unlink(char *);
int do_test(void) {
  char buf[512];
  int result = 0;
  int fd = posix_openpt(O_RDWR);
  if (fd != -1) {
    result |= do_single_test(fd, buf, sizeof(buf), 0);
    result |= do_single_test(fd, NULL, sizeof(buf), EINVAL);
    result |= do_single_test(fd, buf, 1, ERANGE);
    close(fd);
  } else
    printf("posix_openpt (O_RDWR) failed\nerrno %d (%s)\n", errno,
           strerror(errno));
  fd = open("/dev/tty", O_RDONLY);
  if (fd != -1) {
    result |= do_single_test(fd, buf, sizeof(buf), ENOTTY);
    close(fd);
  } else
    printf("open (\"%s\", O_RDWR) failed\nerrno %d (%s)\n", "/dev/tty", errno,
           strerror(errno));
  fd = open("./ptsname-einval", O_RDWR | O_CREAT, 0600);
  if (fd != -1) {
    result |= do_single_test(fd, buf, sizeof(buf), ENOTTY);
    close(fd);
    unlink("./ptsname-einval");
  } else
    printf("open (\"%s\", O_RDWR | OCREAT) failed\nerrno %d (%s)\n",
           "./ptsname-einval", errno, strerror(errno));
  return result;
}


x86 assembly (clang, -O0):
<do_test>:
push   %rbp
mov    %rsp,%rbp
sub    $0x220,%rsp
movl   $0x0,-0x204(%rbp)
mov    0x2e54(%rip),%rax
mov    (%rax),%edi
call   1090 <posix_openpt@plt>
mov    %eax,-0x208(%rbp)
cmpl   $0xffffffff,-0x208(%rbp)
je     1230 <do_test+0xc0>
mov    -0x208(%rbp),%edi
lea    -0x200(%rbp),%rsi
mov    $0x200,%edx
xor    %ecx,%ecx
call   1030 <do_single_test@plt>
or     -0x204(%rbp),%eax
mov    %eax,-0x204(%rbp)
mov    -0x208(%rbp),%edi
mov    0x2dd8(%rip),%rax
mov    (%rax),%ecx
xor    %eax,%eax
mov    %eax,%esi
mov    $0x200,%edx
call   1030 <do_single_test@plt>
or     -0x204(%rbp),%eax
mov    %eax,-0x204(%rbp)
mov    -0x208(%rbp),%edi
lea    -0x200(%rbp),%rsi
mov    0x2dc0(%rip),%rax
mov    (%rax),%ecx
mov    $0x1,%edx
call   1030 <do_single_test@plt>
or     -0x204(%rbp),%eax
mov    %eax,-0x204(%rbp)
mov    -0x208(%rbp),%edi
call   1060 <close@plt>
jmp    1264 <do_test+0xf4>
mov    0x2d81(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20c(%rbp)
mov    0x2d72(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x20c(%rbp),%esi
mov    %rax,%rdx
lea    0xda3(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    0x2d65(%rip),%rax
mov    (%rax),%esi
lea    0xdb8(%rip),%rdi
mov    $0x0,%al
call   1070 <open@plt>
mov    %eax,-0x208(%rbp)
cmpl   $0xffffffff,-0x208(%rbp)
je     12ca <do_test+0x15a>
mov    -0x208(%rbp),%edi
lea    -0x200(%rbp),%rsi
mov    0x2d36(%rip),%rax
mov    (%rax),%ecx
mov    $0x200,%edx
call   1030 <do_single_test@plt>
or     -0x204(%rbp),%eax
mov    %eax,-0x204(%rbp)
mov    -0x208(%rbp),%edi
call   1060 <close@plt>
jmp    1305 <do_test+0x195>
mov    0x2ce7(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x210(%rbp)
mov    0x2cd8(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x210(%rbp),%edx
mov    %rax,%rcx
lea    0xd3e(%rip),%rdi
lea    0xd2e(%rip),%rsi
mov    $0x0,%al
call   1050 <printf@plt>
mov    0x2cd4(%rip),%rax
mov    (%rax),%esi
mov    0x2ce3(%rip),%rax
or     (%rax),%esi
lea    0xd41(%rip),%rdi
mov    $0x180,%edx
mov    $0x0,%al
call   1070 <open@plt>
mov    %eax,-0x208(%rbp)
cmpl   $0xffffffff,-0x208(%rbp)
je     1385 <do_test+0x215>
mov    -0x208(%rbp),%edi
lea    -0x200(%rbp),%rsi
mov    0x2c87(%rip),%rax
mov    (%rax),%ecx
mov    $0x200,%edx
call   1030 <do_single_test@plt>
or     -0x204(%rbp),%eax
mov    %eax,-0x204(%rbp)
mov    -0x208(%rbp),%edi
call   1060 <close@plt>
lea    0xce4(%rip),%rdi
call   1040 <unlink@plt>
jmp    13c0 <do_test+0x250>
mov    0x2c2c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x214(%rbp)
mov    0x2c1d(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x214(%rbp),%edx
mov    %rax,%rcx
lea    0xcbe(%rip),%rdi
lea    0xca6(%rip),%rsi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x204(%rbp),%eax
add    $0x220,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_test>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x200,%rsp
mov    0x2e5a(%rip),%r12
mov    (%r12),%edi
call   1090 <posix_openpt@plt>
cmp    $0xffffffff,%eax
je     11f0 <do_test+0x80>
mov    %eax,%ebp
mov    %rsp,%r14
mov    %eax,%edi
mov    %r14,%rsi
mov    $0x200,%edx
xor    %ecx,%ecx
call   1030 <do_single_test@plt>
mov    %eax,%r15d
mov    0x2dfc(%rip),%rax
mov    (%rax),%ecx
mov    %ebp,%edi
xor    %esi,%esi
mov    $0x200,%edx
call   1030 <do_single_test@plt>
mov    %eax,%ebx
or     %r15d,%ebx
mov    0x2df8(%rip),%rax
mov    (%rax),%ecx
mov    %ebp,%edi
mov    %r14,%rsi
mov    $0x1,%edx
call   1030 <do_single_test@plt>
mov    %eax,%r14d
or     %ebx,%r14d
mov    %ebp,%edi
call   1060 <close@plt>
jmp    1216 <do_test+0xa6>
mov    0x2dc1(%rip),%rax
mov    (%rax),%ebx
mov    %ebx,%edi
call   1080 <strerror@plt>
lea    0xdf9(%rip),%rdi
xor    %r14d,%r14d
mov    %ebx,%esi
mov    %rax,%rdx
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x2db3(%rip),%rax
mov    (%rax),%esi
lea    0xe06(%rip),%rdi
xor    %eax,%eax
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     1258 <do_test+0xe8>
mov    %eax,%ebp
mov    0x2d9d(%rip),%rax
mov    (%rax),%ecx
mov    %rsp,%rsi
mov    %ebp,%edi
mov    $0x200,%edx
call   1030 <do_single_test@plt>
or     %eax,%r14d
mov    %ebp,%edi
call   1060 <close@plt>
jmp    1282 <do_test+0x112>
mov    0x2d59(%rip),%rax
mov    (%rax),%ebx
mov    %ebx,%edi
call   1080 <strerror@plt>
lea    0xdc6(%rip),%rdi
lea    0xdb6(%rip),%rsi
mov    %ebx,%edx
mov    %rax,%rcx
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x2d6f(%rip),%rax
mov    (%rax),%esi
or     (%r12),%esi
lea    0xdc9(%rip),%rdi
mov    $0x180,%edx
xor    %eax,%eax
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     12d9 <do_test+0x169>
mov    %eax,%ebp
mov    0x2d28(%rip),%rax
mov    (%rax),%ecx
mov    %rsp,%rsi
mov    %ebp,%edi
mov    $0x200,%edx
call   1030 <do_single_test@plt>
or     %eax,%r14d
mov    %ebp,%edi
call   1060 <close@plt>
lea    0xd8d(%rip),%rdi
call   1040 <unlink@plt>
jmp    1303 <do_test+0x193>
mov    0x2cd8(%rip),%rax
mov    (%rax),%ebx
mov    %ebx,%edi
call   1080 <strerror@plt>
lea    0xd80(%rip),%rdi
lea    0xd68(%rip),%rsi
mov    %ebx,%edx
mov    %rax,%rcx
xor    %eax,%eax
call   1050 <printf@plt>
mov    %r14d,%eax
add    $0x200,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@O_RDWR = global i32 0
@EINVAL = global i32 0
@ERANGE = global i32 0
@.str = [44 x i8] c"posix_openpt (O_RDWR) failed\0Aerrno %d (%s)\0A\00"
@errno = global i32 0
@.str.1 = [9 x i8] c"/dev/tty\00"
@O_RDONLY = global i32 0
@ENOTTY = global i32 0
@.str.2 = [42 x i8] c"open (\22%s\22, O_RDWR) failed\0Aerrno %d (%s)\0A\00"
@.str.3 = [17 x i8] c"./ptsname-einval\00"
@O_CREAT = global i32 0
@.str.4 = [51 x i8] c"open (\22%s\22, O_RDWR | OCREAT) failed\0Aerrno %d (%s)\0A\00"
define i32 @do_test() {
%1 = alloca [512 x i8]
%2 = alloca i32
%3 = alloca i32
store i32 0, i32* %2
%4 = load i32, i32* @O_RDWR
%5 = call i32 @posix_openpt(i32 %4)
store i32 %5, i32* %3
%6 = load i32, i32* %3
%7 = icmp ne i32 %6, -1
br i1 %7, label %8, label %27
8: ; preds = %0
%9 = load i32, i32* %3
%10 = getelementptr inbounds [512 x i8], [512 x i8]* %1, i64 0, i64 0
%11 = call i32 @do_single_test(i32 %9, i8* %10, i32 512, i32 0)
%12 = load i32, i32* %2
%13 = or i32 %12, %11
store i32 %13, i32* %2
%14 = load i32, i32* %3
%15 = load i32, i32* @EINVAL
%16 = call i32 @do_single_test(i32 %14, i8* null, i32 512, i32 %15)
%17 = load i32, i32* %2
%18 = or i32 %17, %16
store i32 %18, i32* %2
%19 = load i32, i32* %3
%20 = getelementptr inbounds [512 x i8], [512 x i8]* %1, i64 0, i64 0
%21 = load i32, i32* @ERANGE
%22 = call i32 @do_single_test(i32 %19, i8* %20, i32 1, i32 %21)
%23 = load i32, i32* %2
%24 = or i32 %23, %22
store i32 %24, i32* %2
%25 = load i32, i32* %3
%26 = call i32 @close(i32 %25)
br label %32
27: ; preds = %0
%28 = load i32, i32* @errno
%29 = load i32, i32* @errno
%30 = call i8* @strerror(i32 %29)
%31 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str, i64 0, i64 0), i32 %28, i8* %30)
br label %32
32: ; preds = %27, %8
%33 = load i32, i32* @O_RDONLY
%34 = call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i32 %33)
store i32 %34, i32* %3
%35 = load i32, i32* %3
%36 = icmp ne i32 %35, -1
br i1 %36, label %37, label %46
37: ; preds = %32
%38 = load i32, i32* %3
%39 = getelementptr inbounds [512 x i8], [512 x i8]* %1, i64 0, i64 0
%40 = load i32, i32* @ENOTTY
%41 = call i32 @do_single_test(i32 %38, i8* %39, i32 512, i32 %40)
%42 = load i32, i32* %2
%43 = or i32 %42, %41
store i32 %43, i32* %2
%44 = load i32, i32* %3
%45 = call i32 @close(i32 %44)
br label %51
46: ; preds = %32
%47 = load i32, i32* @errno
%48 = load i32, i32* @errno
%49 = call i8* @strerror(i32 %48)
%50 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i32 %47, i8* %49)
br label %51
51: ; preds = %46, %37
%52 = load i32, i32* @O_RDWR
%53 = load i32, i32* @O_CREAT
%54 = or i32 %52, %53
%55 = call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0), i32 %54, i32 384)
store i32 %55, i32* %3
%56 = load i32, i32* %3
%57 = icmp ne i32 %56, -1
br i1 %57, label %58, label %68
58: ; preds = %51
%59 = load i32, i32* %3
%60 = getelementptr inbounds [512 x i8], [512 x i8]* %1, i64 0, i64 0
%61 = load i32, i32* @ENOTTY
%62 = call i32 @do_single_test(i32 %59, i8* %60, i32 512, i32 %61)
%63 = load i32, i32* %2
%64 = or i32 %63, %62
store i32 %64, i32* %2
%65 = load i32, i32* %3
%66 = call i32 @close(i32 %65)
%67 = call i32 @unlink(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0))
br label %73
68: ; preds = %51
%69 = load i32, i32* @errno
%70 = load i32, i32* @errno
%71 = call i8* @strerror(i32 %70)
%72 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0), i32 %69, i8* %71)
br label %73
73: ; preds = %68, %58
%74 = load i32, i32* %2
ret i32 %74
}

LLVM IR (-O2):
@O_RDWR = global i32 0
@EINVAL = global i32 0
@ERANGE = global i32 0
@.str = [44 x i8] c"posix_openpt (O_RDWR) failed\0Aerrno %d (%s)\0A\00"
@errno = global i32 0
@.str.1 = [9 x i8] c"/dev/tty\00"
@O_RDONLY = global i32 0
@ENOTTY = global i32 0
@.str.2 = [42 x i8] c"open (\22%s\22, O_RDWR) failed\0Aerrno %d (%s)\0A\00"
@.str.3 = [17 x i8] c"./ptsname-einval\00"
@O_CREAT = global i32 0
@.str.4 = [51 x i8] c"open (\22%s\22, O_RDWR | OCREAT) failed\0Aerrno %d (%s)\0A\00"
define i32 @do_test() {
%1 = alloca [512 x i8]
%2 = getelementptr inbounds [512 x i8], [512 x i8]* %1, i64 0, i64 0
%3 = load i32, i32* @O_RDWR
%4 = tail call i32 @posix_openpt(i32 %3)
%5 = icmp eq i32 %4, -1
br i1 %5, label %15, label %6
6: ; preds = %0
%7 = call i32 @do_single_test(i32 %4, i8* %2, i32 512, i32 0)
%8 = load i32, i32* @EINVAL
%9 = call i32 @do_single_test(i32 %4, i8* null, i32 512, i32 %8)
%10 = or i32 %9, %7
%11 = load i32, i32* @ERANGE
%12 = call i32 @do_single_test(i32 %4, i8* %2, i32 1, i32 %11)
%13 = or i32 %10, %12
%14 = call i32 @close(i32 %4)
br label %19
15: ; preds = %0
%16 = load i32, i32* @errno
%17 = tail call i8* @strerror(i32 %16)
%18 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([44 x i8], [44 x i8]* @.str, i64 0, i64 0), i32 %16, i8* %17)
br label %19
19: ; preds = %15, %6
%20 = phi i32 [ %13, %6 ], [ 0, %15 ]
%21 = load i32, i32* @O_RDONLY
%22 = call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i32 %21)
%23 = icmp eq i32 %22, -1
br i1 %23, label %29, label %24
24: ; preds = %19
%25 = load i32, i32* @ENOTTY
%26 = call i32 @do_single_test(i32 %22, i8* %2, i32 512, i32 %25)
%27 = or i32 %26, %20
%28 = call i32 @close(i32 %22)
br label %33
29: ; preds = %19
%30 = load i32, i32* @errno
%31 = call i8* @strerror(i32 %30)
%32 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([42 x i8], [42 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i32 %30, i8* %31)
br label %33
33: ; preds = %29, %24
%34 = phi i32 [ %27, %24 ], [ %20, %29 ]
%35 = load i32, i32* @O_RDWR
%36 = load i32, i32* @O_CREAT
%37 = or i32 %36, %35
%38 = call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0), i32 %37, i32 384)
%39 = icmp eq i32 %38, -1
br i1 %39, label %46, label %40
40: ; preds = %33
%41 = load i32, i32* @ENOTTY
%42 = call i32 @do_single_test(i32 %38, i8* %2, i32 512, i32 %41)
%43 = or i32 %42, %34
%44 = call i32 @close(i32 %38)
%45 = call i32 @unlink(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0))
br label %50
46: ; preds = %33
%47 = load i32, i32* @errno
%48 = call i8* @strerror(i32 %47)
%49 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([51 x i8], [51 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0), i32 %47, i8* %48)
br label %50
50: ; preds = %46, %40
%51 = phi i32 [ %43, %40 ], [ %34, %46 ]
ret i32 %51
}