#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 stat {
  int dummy;
};

/* Variables and functions */
int _recvfd(int, size_t *, int /*<<< orphan*/ *);
int /*<<< orphan*/ fstat(int, struct stat *);
int read_msg(int, int, char *, int);
int /*<<< orphan*/ write_msg(int, char, char *, int /*<<< orphan*/);
int get_pty(int socket) {
  char buf[1024];
  char *dummy = NULL;
  size_t len;
  int rsock;
  buf[0] = 0;
  write_msg(socket, (char)0, buf, 0);
  if (read_msg(socket, 1, buf, 1024) == -1) {
    return -1;
  }
  len = 0;
  rsock = _recvfd(socket, &len, NULL);
  struct stat statbuf;
  fstat(rsock, &statbuf);
  return rsock;
}


x86 assembly (clang, -O0):
<get_pty>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
mov    %edi,-0x8(%rbp)
movq   $0x0,-0x418(%rbp)
movb   $0x0,-0x410(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x410(%rbp),%rdx
xor    %ecx,%ecx
mov    %ecx,%esi
call   1030 <write_msg@plt>
mov    -0x8(%rbp),%edi
lea    -0x410(%rbp),%rdx
mov    $0x1,%esi
mov    $0x400,%ecx
call   1050 <read_msg@plt>
cmp    $0xffffffff,%eax
jne    11a1 <get_pty+0x61>
movl   $0xffffffff,-0x4(%rbp)
jmp    11e0 <get_pty+0xa0>
movq   $0x0,-0x420(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x420(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1040 <_recvfd@plt>
mov    %eax,-0x424(%rbp)
mov    -0x424(%rbp),%edi
lea    -0x428(%rbp),%rsi
call   1060 <fstat@plt>
mov    -0x424(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_pty>:
push   %r14
push   %rbx
sub    $0x418,%rsp
mov    %edi,%ebx
movb   $0x0,0x10(%rsp)
lea    0x10(%rsp),%r14
xor    %esi,%esi
mov    %r14,%rdx
xor    %ecx,%ecx
call   1030 <write_msg@plt>
mov    %ebx,%edi
mov    $0x1,%esi
mov    %r14,%rdx
mov    $0x400,%ecx
call   1050 <read_msg@plt>
cmp    $0xffffffff,%eax
je     11a0 <get_pty+0x60>
movq   $0x0,0x8(%rsp)
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
xor    %edx,%edx
call   1040 <_recvfd@plt>
mov    %eax,%ebx
mov    %rsp,%rsi
mov    %eax,%edi
call   1060 <fstat@plt>
jmp    11a5 <get_pty+0x65>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x418,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
define i32 @get_pty(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca [1024 x i8]
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i32
%8 = alloca %struct.stat
store i32 %0, i32* %3
store i8* null, i8** %5
%9 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
store i8 0, i8* %9
%10 = load i32, i32* %3
%11 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%12 = call i32 @write_msg(i32 %10, i8 signext 0, i8* %11, i32 0)
%13 = load i32, i32* %3
%14 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%15 = call i32 @read_msg(i32 %13, i32 1, i8* %14, i32 1024)
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %18
17: ; preds = %1
store i32 -1, i32* %2
br label %24
18: ; preds = %1
store i64 0, i64* %6
%19 = load i32, i32* %3
%20 = call i32 @_recvfd(i32 %19, i64* %6, i32* null)
store i32 %20, i32* %7
%21 = load i32, i32* %7
%22 = call i32 @fstat(i32 %21, %struct.stat* %8)
%23 = load i32, i32* %7
store i32 %23, i32* %2
br label %24
24: ; preds = %18, %17
%25 = load i32, i32* %2
ret i32 %25
}

LLVM IR (-O2):
%struct.stat = type { i32 }
define i32 @get_pty(i32 %0) {
%2 = alloca [1024 x i8]
%3 = alloca i64
%4 = alloca %struct.stat
%5 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%6 = bitcast i64* %3 to i8*
store i8 0, i8* %5
%7 = call i32 @write_msg(i32 %0, i8 signext 0, i8* %5, i32 0)
%8 = call i32 @read_msg(i32 %0, i32 1, i8* %5, i32 1024)
%9 = icmp eq i32 %8, -1
br i1 %9, label %14, label %10
10: ; preds = %1
store i64 0, i64* %3
%11 = call i32 @_recvfd(i32 %0, i64* %3, i32* null)
%12 = bitcast %struct.stat* %4 to i8*
%13 = call i32 @fstat(i32 %11, %struct.stat* %4)
br label %14
14: ; preds = %1, %10
%15 = phi i32 [ %11, %10 ], [ -1, %1 ]
ret i32 %15
}