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

/* Variables and functions */
int EINVAL;
int O_CREAT;
int O_RDWR;
int /*<<< orphan*/ close(int);
int errno;
int grantpt(int);
int open(char const *, int, int);
int /*<<< orphan*/ printf(char *, ...);
char *strerror(int);
int /*<<< orphan*/ unlink(char const *);
int test_einval(void) {
  int fd, ret, err;
  const char file[] = "./grantpt-einval";
  fd = open(file, O_RDWR | O_CREAT, 0600);
  if (fd == -1) {
    printf("open(\"%s\", O_RDWR) failed\nerrno %d (%s)\n", file, errno,
           strerror(errno));
    return 0;
  }
  unlink(file);
  ret = grantpt(fd);
  err = errno;
  if (ret != -1 || err != EINVAL) {
    printf("grantpt(): expected: return = %d, errno = %d\n", -1, EINVAL);
    printf("           got: return = %d, errno = %d\n", ret, err);
    ret = 1;
  } else
    ret = 0;
  close(fd);
  return ret;
}


x86 assembly (clang, -O0):
<test_einval>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    0xe91(%rip),%rax
mov    %rax,-0x30(%rbp)
mov    0xe8e(%rip),%rax
mov    %rax,-0x28(%rbp)
mov    0xe8c(%rip),%al
mov    %al,-0x20(%rbp)
lea    -0x30(%rbp),%rdi
mov    0x2e4e(%rip),%rax
mov    (%rax),%esi
mov    0x2e5d(%rip),%rax
or     (%rax),%esi
mov    $0x180,%edx
call   1060 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    11fa <test_einval+0x9a>
lea    -0x30(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    0x2e0d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    0x2e01(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x40(%rbp),%rsi
mov    -0x34(%rbp),%edx
mov    %rax,%rcx
lea    0xe2a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1287 <test_einval+0x127>
lea    -0x30(%rbp),%rdi
call   1030 <unlink@plt>
mov    -0x8(%rbp),%edi
call   1070 <grantpt@plt>
mov    %eax,-0xc(%rbp)
mov    0x2dbb(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    1236 <test_einval+0xd6>
mov    -0x10(%rbp),%eax
mov    0x2d9a(%rip),%rcx
cmp    (%rcx),%eax
je     1272 <test_einval+0x112>
mov    0x2d8b(%rip),%rax
mov    (%rax),%edx
lea    0xdf4(%rip),%rdi
mov    $0xffffffff,%esi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0xc(%rbp),%esi
mov    -0x10(%rbp),%edx
lea    0xe09(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0xc(%rbp)
jmp    1279 <test_einval+0x119>
movl   $0x0,-0xc(%rbp)
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_einval>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
movaps 0xe8f(%rip),%xmm0
movaps %xmm0,(%rsp)
movb   $0x0,0x10(%rsp)
mov    0x2e5f(%rip),%rax
mov    0x2e70(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %rsp,%rdi
mov    $0x180,%edx
call   1060 <open@plt>
cmp    $0xffffffff,%eax
je     1207 <test_einval+0xa7>
mov    %eax,%ebx
mov    %rsp,%rdi
call   1030 <unlink@plt>
mov    %ebx,%edi
call   1070 <grantpt@plt>
mov    %eax,%r15d
mov    0x2e17(%rip),%rax
mov    (%rax),%r14d
mov    0x2e05(%rip),%rax
mov    (%rax),%edx
cmp    $0xffffffff,%r15d
jne    11d2 <test_einval+0x72>
xor    %ebp,%ebp
cmp    %edx,%r14d
je     11fe <test_einval+0x9e>
lea    0xe61(%rip),%rdi
mov    $0xffffffff,%esi
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xe7c(%rip),%rdi
mov    %r15d,%esi
mov    %r14d,%edx
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0x1,%ebp
mov    %ebx,%edi
call   1050 <close@plt>
jmp    122f <test_einval+0xcf>
mov    0x2dc2(%rip),%rax
mov    (%rax),%ebx
mov    %ebx,%edi
call   1080 <strerror@plt>
lea    0xdf3(%rip),%rdi
xor    %ebp,%ebp
mov    %rsp,%rsi
mov    %ebx,%edx
mov    %rax,%rcx
xor    %eax,%eax
call   1040 <printf@plt>
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@__const.test_einval.file = [17 x i8] c"./grantpt-einval\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@.str = [41 x i8] c"open(\22%s\22, O_RDWR) failed\0Aerrno %d (%s)\0A\00"
@errno = global i32 0
@EINVAL = global i32 0
@.str.1 = [46 x i8] c"grantpt(): expected: return = %d, errno = %d\0A\00"
@.str.2 = [41 x i8] c" got: return = %d, errno = %d\0A\00"
define i32 @test_einval() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca [17 x i8]
%6 = bitcast [17 x i8]* %5 to i8*
%7 = getelementptr inbounds [17 x i8], [17 x i8]* %5, i64 0, i64 0
%8 = load i32, i32* @O_RDWR
%9 = load i32, i32* @O_CREAT
%10 = or i32 %8, %9
%11 = call i32 @open(i8* %7, i32 %10, i32 384)
store i32 %11, i32* %2
%12 = load i32, i32* %2
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %20
14: ; preds = %0
%15 = getelementptr inbounds [17 x i8], [17 x i8]* %5, i64 0, i64 0
%16 = load i32, i32* @errno
%17 = load i32, i32* @errno
%18 = call i8* @strerror(i32 %17)
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0), i8* %15, i32 %16, i8* %18)
store i32 0, i32* %1
br label %43
20: ; preds = %0
%21 = getelementptr inbounds [17 x i8], [17 x i8]* %5, i64 0, i64 0
%22 = call i32 @unlink(i8* %21)
%23 = load i32, i32* %2
%24 = call i32 @grantpt(i32 %23)
store i32 %24, i32* %3
%25 = load i32, i32* @errno
store i32 %25, i32* %4
%26 = load i32, i32* %3
%27 = icmp ne i32 %26, -1
br i1 %27, label %32, label %28
28: ; preds = %20
%29 = load i32, i32* %4
%30 = load i32, i32* @EINVAL
%31 = icmp ne i32 %29, %30
br i1 %31, label %32, label %38
32: ; preds = %28, %20
%33 = load i32, i32* @EINVAL
%34 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.1, i64 0, i64 0), i32 -1, i32 %33)
%35 = load i32, i32* %3
%36 = load i32, i32* %4
%37 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.2, i64 0, i64 0), i32 %35, i32 %36)
store i32 1, i32* %3
br label %39
38: ; preds = %28
store i32 0, i32* %3
br label %39
39: ; preds = %38, %32
%40 = load i32, i32* %2
%41 = call i32 @close(i32 %40)
%42 = load i32, i32* %3
store i32 %42, i32* %1
br label %43
43: ; preds = %39, %14
%44 = load i32, i32* %1
ret i32 %44
}

LLVM IR (-O2):
@__const.test_einval.file = [17 x i8] c"./grantpt-einval\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@.str = [41 x i8] c"open(\22%s\22, O_RDWR) failed\0Aerrno %d (%s)\0A\00"
@errno = global i32 0
@EINVAL = global i32 0
@.str.1 = [46 x i8] c"grantpt(): expected: return = %d, errno = %d\0A\00"
@.str.2 = [41 x i8] c" got: return = %d, errno = %d\0A\00"
define i32 @test_einval() {
%1 = alloca [17 x i8]
%2 = getelementptr inbounds [17 x i8], [17 x i8]* %1, i64 0, i64 0
%3 = load i32, i32* @O_RDWR
%4 = load i32, i32* @O_CREAT
%5 = or i32 %4, %3
%6 = call i32 @open(i8* %2, i32 %5, i32 384)
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %12
8: ; preds = %0
%9 = load i32, i32* @errno
%10 = tail call i8* @strerror(i32 %9)
%11 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0), i8* %2, i32 %9, i8* %10)
br label %26
12: ; preds = %0
%13 = call i32 @unlink(i8* %2)
%14 = tail call i32 @grantpt(i32 %6)
%15 = load i32, i32* @errno
%16 = icmp eq i32 %14, -1
%17 = load i32, i32* @EINVAL
%18 = icmp eq i32 %15, %17
%19 = select i1 %16, i1 %18, i1 false
br i1 %19, label %23, label %20
20: ; preds = %12
%21 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([46 x i8], [46 x i8]* @.str.1, i64 0, i64 0), i32 -1, i32 %17)
%22 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([41 x i8], [41 x i8]* @.str.2, i64 0, i64 0), i32 %14, i32 %15)
br label %23
23: ; preds = %12, %20
%24 = phi i32 [ 1, %20 ], [ 0, %12 ]
%25 = tail call i32 @close(i32 %6)
br label %26
26: ; preds = %23, %8
%27 = phi i32 [ 0, %8 ], [ %24, %23 ]
ret i32 %27
}