#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 errno;
int /*<<< orphan*/ printf(char *, ...);
int ptsname_r(int, char *, size_t);
char *strerror(int);
int do_single_test(int fd, char *buf, size_t buflen, int expected_err) {
  int ret = ptsname_r(fd, buf, buflen);
  int err = errno;
  if (expected_err == 0) {
    if (ret != 0) {
      printf("ptsname_r: expected: return = 0\n");
      printf("           got: return = %d, errno = %d (%s)\n", ret, err,
             strerror(err));
      return 1;
    }
  } else {
    if (ret == 0 || errno != expected_err) {
      printf("ptsname_r: expected: return = %d, errno = %d (%s)\n", -1,
             expected_err, strerror(expected_err));
      printf("           got: return = %d, errno = %d (%s)\n", ret, err,
             strerror(err));
      return 1;
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<do_single_test>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1040 <ptsname_r@plt>
mov    %eax,-0x20(%rbp)
mov    0x2e80(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jne    11c3 <do_single_test+0x93>
cmpl   $0x0,-0x20(%rbp)
je     11be <do_single_test+0x8e>
lea    0xe80(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x20(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x24(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0x24(%rbp),%edi
call   1050 <strerror@plt>
mov    -0x2c(%rbp),%esi
mov    -0x28(%rbp),%edx
mov    %rax,%rcx
lea    0xe76(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1249 <do_single_test+0x119>
jmp    1242 <do_single_test+0x112>
cmpl   $0x0,-0x20(%rbp)
je     11df <do_single_test+0xaf>
mov    0x2e0c(%rip),%rax
mov    (%rax),%eax
cmp    -0x1c(%rbp),%eax
je     123d <do_single_test+0x10d>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    -0x1c(%rbp),%edi
call   1050 <strerror@plt>
mov    -0x38(%rbp),%edx
mov    %rax,%rcx
lea    0xe55(%rip),%rdi
mov    $0xffffffff,%esi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x20(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    -0x24(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    -0x24(%rbp),%edi
call   1050 <strerror@plt>
mov    -0x34(%rbp),%esi
mov    -0x30(%rbp),%edx
mov    %rax,%rcx
lea    0xdf7(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1249 <do_single_test+0x119>
jmp    1242 <do_single_test+0x112>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_single_test>:
push   %rbp
push   %r14
push   %rbx
mov    %ecx,%ebx
call   1050 <ptsname_r@plt>
mov    %eax,%r14d
mov    0x2e8b(%rip),%rax
mov    (%rax),%ebp
test   %ebx,%ebx
je     1187 <do_single_test+0x47>
test   %r14d,%r14d
je     1166 <do_single_test+0x26>
xor    %eax,%eax
cmp    %ebx,%ebp
je     11be <do_single_test+0x7e>
mov    %ebx,%edi
call   1060 <strerror@plt>
lea    0xeba(%rip),%rdi
mov    $0xffffffff,%esi
mov    %ebx,%edx
mov    %rax,%rcx
xor    %eax,%eax
call   1040 <printf@plt>
jmp    1198 <do_single_test+0x58>
test   %r14d,%r14d
je     11bc <do_single_test+0x7c>
lea    0xece(%rip),%rdi
call   1030 <puts@plt>
mov    %ebp,%edi
call   1060 <strerror@plt>
lea    0xe5a(%rip),%rdi
mov    %r14d,%esi
mov    %ebp,%edx
mov    %rax,%rcx
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0x1,%eax
jmp    11be <do_single_test+0x7e>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@errno = global i32 0
@.str = [33 x i8] c"ptsname_r: expected: return = 0\0A\00"
@.str.1 = [46 x i8] c" got: return = %d, errno = %d (%s)\0A\00"
@.str.2 = [51 x i8] c"ptsname_r: expected: return = %d, errno = %d (%s)\0A\00"
define i32 @do_single_test(i32 %0, i8* %1, i64 %2, i32 %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
store i32 %0, i32* %6
store i8* %1, i8** %7
store i64 %2, i64* %8
store i32 %3, i32* %9
%12 = load i32, i32* %6
%13 = load i8*, i8** %7
%14 = load i64, i64* %8
%15 = call i32 @ptsname_r(i32 %12, i8* %13, i64 %14)
store i32 %15, i32* %10
%16 = load i32, i32* @errno
store i32 %16, i32* %11
%17 = load i32, i32* %9
%18 = icmp eq i32 %17, 0
br i1 %18, label %19, label %30
19: ; preds = %4
%20 = load i32, i32* %10
%21 = icmp ne i32 %20, 0
br i1 %21, label %22, label %29
22: ; preds = %19
%23 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0))
%24 = load i32, i32* %10
%25 = load i32, i32* %11
%26 = load i32, i32* %11
%27 = call i8* @strerror(i32 %26)
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.1, i64 0, i64 0), i32 %24, i32 %25, i8* %27)
store i32 1, i32* %5
br label %49
29: ; preds = %19
br label %48
30: ; preds = %4
%31 = load i32, i32* %10
%32 = icmp eq i32 %31, 0
br i1 %32, label %37, label %33
33: ; preds = %30
%34 = load i32, i32* @errno
%35 = load i32, i32* %9
%36 = icmp ne i32 %34, %35
br i1 %36, label %37, label %47
37: ; preds = %33, %30
%38 = load i32, i32* %9
%39 = load i32, i32* %9
%40 = call i8* @strerror(i32 %39)
%41 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.2, i64 0, i64 0), i32 -1, i32 %38, i8* %40)
%42 = load i32, i32* %10
%43 = load i32, i32* %11
%44 = load i32, i32* %11
%45 = call i8* @strerror(i32 %44)
%46 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.1, i64 0, i64 0), i32 %42, i32 %43, i8* %45)
store i32 1, i32* %5
br label %49
47: ; preds = %33
br label %48
48: ; preds = %47, %29
store i32 0, i32* %5
br label %49
49: ; preds = %48, %37, %22
%50 = load i32, i32* %5
ret i32 %50
}

LLVM IR (-O2):
@errno = global i32 0
@.str.1 = [46 x i8] c" got: return = %d, errno = %d (%s)\0A\00"
@.str.2 = [51 x i8] c"ptsname_r: expected: return = %d, errno = %d (%s)\0A\00"
@str = [32 x i8] c"ptsname_r: expected: return = 0\00"
define i32 @do_single_test(i32 %0, i8* %1, i64 %2, i32 %3) {
%5 = tail call i32 @ptsname_r(i32 %0, i8* %1, i64 %2)
%6 = load i32, i32* @errno
%7 = icmp eq i32 %3, 0
br i1 %7, label %8, label %12
8: ; preds = %4
%9 = icmp eq i32 %5, 0
br i1 %9, label %22, label %10
10: ; preds = %8
%11 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @str, i64 0, i64 0))
br label %19
12: ; preds = %4
%13 = icmp ne i32 %5, 0
%14 = icmp eq i32 %6, %3
%15 = select i1 %13, i1 %14, i1 false
br i1 %15, label %22, label %16
16: ; preds = %12
%17 = tail call i8* @strerror(i32 %3)
%18 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([51 x i8], [51 x i8]* @.str.2, i64 0, i64 0), i32 -1, i32 %3, i8* %17)
br label %19
19: ; preds = %10, %16
%20 = tail call i8* @strerror(i32 %6)
%21 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([46 x i8], [46 x i8]* @.str.1, i64 0, i64 0), i32 %5, i32 %6, i8* %20)
br label %22
22: ; preds = %19, %8, %12
%23 = phi i32 [ 0, %12 ], [ 0, %8 ], [ 1, %19 ]
ret i32 %23
}