#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*/ close(int);
int connect_socket();
int open_pty();
int read(int, char *, int);
int /*<<< orphan*/ tcdrain(int);
int /*<<< orphan*/ write(int, char *, int);
int main() {
  int pty_fd;
  int sock_fd;
  char buf[256];
  int rd_size;
  sock_fd = connect_socket();
  pty_fd = open_pty();
  while ((rd_size = read(sock_fd, buf, sizeof(buf))) > 0) {
    write(pty_fd, buf, rd_size);
  }
  tcdrain(pty_fd);
  close(sock_fd);
  close(pty_fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x120,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x0,%al
call   1060 <connect_socket@plt>
mov    %eax,-0xc(%rbp)
mov    $0x0,%al
call   1030 <open_pty@plt>
mov    %eax,-0x8(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x110(%rbp),%rsi
mov    $0x100,%edx
call   1080 <read@plt>
mov    %eax,-0x114(%rbp)
cmp    $0x0,%eax
jle    11c3 <main+0x63>
mov    -0x8(%rbp),%edi
lea    -0x110(%rbp),%rsi
mov    -0x114(%rbp),%edx
call   1050 <write@plt>
jmp    1186 <main+0x26>
mov    -0x8(%rbp),%edi
call   1040 <tcdrain@plt>
mov    -0xc(%rbp),%edi
call   1070 <close@plt>
mov    -0x8(%rbp),%edi
call   1070 <close@plt>
xor    %eax,%eax
add    $0x120,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x100,%rsp
xor    %eax,%eax
call   1060 <connect_socket@plt>
mov    %eax,%r14d
xor    %eax,%eax
call   1030 <open_pty@plt>
mov    %eax,%ebp
mov    %rsp,%rsi
mov    %r14d,%edi
mov    $0x100,%edx
call   1080 <read@plt>
test   %eax,%eax
jle    11c0 <main+0x60>
mov    %rsp,%rbx
cs nopw 0x0(%rax,%rax,1)
nop
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1050 <write@plt>
mov    %r14d,%edi
mov    %rbx,%rsi
mov    $0x100,%edx
call   1080 <read@plt>
test   %eax,%eax
jg     11a0 <main+0x40>
mov    %ebp,%edi
call   1040 <tcdrain@plt>
mov    %r14d,%edi
call   1070 <close@plt>
mov    %ebp,%edi
call   1070 <close@plt>
xor    %eax,%eax
add    $0x100,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca [256 x i8]
%5 = alloca i32
store i32 0, i32* %1
%6 = call i32 (...) @connect_socket()
store i32 %6, i32* %3
%7 = call i32 (...) @open_pty()
store i32 %7, i32* %2
br label %8
8: ; preds = %13, %0
%9 = load i32, i32* %3
%10 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%11 = call i32 @read(i32 %9, i8* %10, i32 256)
store i32 %11, i32* %5
%12 = icmp sgt i32 %11, 0
br i1 %12, label %13, label %18
13: ; preds = %8
%14 = load i32, i32* %2
%15 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%16 = load i32, i32* %5
%17 = call i32 @write(i32 %14, i8* %15, i32 %16)
br label %8
18: ; preds = %8
%19 = load i32, i32* %2
%20 = call i32 @tcdrain(i32 %19)
%21 = load i32, i32* %3
%22 = call i32 @close(i32 %21)
%23 = load i32, i32* %2
%24 = call i32 @close(i32 %23)
ret i32 0
}

LLVM IR (-O2):
define i32 @main() {
%1 = alloca [256 x i8]
%2 = getelementptr inbounds [256 x i8], [256 x i8]* %1, i64 0, i64 0
%3 = tail call i32 (...) @connect_socket()
%4 = tail call i32 (...) @open_pty()
%5 = call i32 @read(i32 %3, i8* %2, i32 256)
%6 = icmp sgt i32 %5, 0
br i1 %6, label %7, label %12
7: ; preds = %0, %7
%8 = phi i32 [ %10, %7 ], [ %5, %0 ]
%9 = call i32 @write(i32 %4, i8* %2, i32 %8)
%10 = call i32 @read(i32 %3, i8* %2, i32 256)
%11 = icmp sgt i32 %10, 0
br i1 %11, label %7, label %12
12: ; preds = %7, %0
%13 = tail call i32 @tcdrain(i32 %4)
%14 = tail call i32 @close(i32 %3)
%15 = tail call i32 @close(i32 %4)
ret i32 0
}