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

/* Variables and functions */
scalar_t__ FD_ISSET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int atoi(char const *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ fwrite(char *, int, int, int /*<<< orphan*/);
int mysock_connect_socket(char *, int);
int mysock_read_data(int, char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ mysock_write_persist(int, char *, int /*<<< orphan*/);
int /*<<< orphan*/ select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                          int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ stdout;
int /*<<< orphan*/ strlen(char *);
int main(int argc, const char *argv[]) {
  int portno;
  int sock;
  fd_set fs;
  if (argc != 2) {
    fprintf(stderr, "Usage: debug_client <portno>\n");
    exit(1);
  }
  portno = atoi(argv[1]);
  sock = mysock_connect_socket("127.0.0.1", portno);
  if (sock <= 0) {
    fprintf(stderr, "Connect failed\n");
    exit(1);
  }
  while (1) {
    FD_ZERO(&fs);
    FD_SET(sock, &fs);
    FD_SET(0, &fs);
    select(sock + 1, &fs, NULL, NULL, NULL);
    if (FD_ISSET(sock, &fs)) {
      char tmp[1024];
      int n_read;
      n_read = mysock_read_data(sock, tmp, 1024, 0);
      if (n_read <= 0) {
        fprintf(stderr, "Connection terminated.\n");
        exit(0);
      }
      fwrite(tmp, sizeof(char), n_read, stdout);
      fflush(stdout);
    }
    if (FD_ISSET(0, &fs)) {
      char tmp[1024];
      int n_read;
      fgets(tmp, 1024, stdin);
      mysock_write_persist(sock, tmp, strlen(tmp));
    }
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x850,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     1222 <main+0x42>
mov    0x2dce(%rip),%rax
mov    (%rax),%edi
lea    0xded(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   10a0 <atoi@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%esi
lea    0xde2(%rip),%rdi
call   10b0 <mysock_connect_socket@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jg     126d <main+0x8d>
mov    0x2d83(%rip),%rax
mov    (%rax),%edi
lea    0xdca(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
jmp    1272 <main+0x92>
lea    -0x1c(%rbp),%rdi
call   1070 <FD_ZERO@plt>
mov    -0x18(%rbp),%edi
lea    -0x1c(%rbp),%rsi
call   10f0 <FD_SET@plt>
xor    %edi,%edi
lea    -0x1c(%rbp),%rsi
call   10f0 <FD_SET@plt>
mov    -0x18(%rbp),%edi
add    $0x1,%edi
lea    -0x1c(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%r8d
mov    %r8,%rdx
mov    %r8,%rcx
call   1080 <select@plt>
mov    -0x18(%rbp),%edi
lea    -0x1c(%rbp),%rsi
call   1030 <FD_ISSET@plt>
cmp    $0x0,%rax
je     1335 <main+0x155>
mov    -0x18(%rbp),%edi
lea    -0x420(%rbp),%rsi
mov    $0x400,%edx
xor    %ecx,%ecx
call   10e0 <mysock_read_data@plt>
mov    %eax,-0x424(%rbp)
cmpl   $0x0,-0x424(%rbp)
jg     1307 <main+0x127>
mov    0x2ce6(%rip),%rax
mov    (%rax),%edi
lea    0xd3d(%rip),%rsi
call   1060 <fprintf@plt>
xor    %edi,%edi
call   10c0 <exit@plt>
lea    -0x420(%rbp),%rdi
mov    -0x424(%rbp),%edx
mov    0x2cb5(%rip),%rax
mov    (%rax),%ecx
mov    $0x1,%esi
call   10d0 <fwrite@plt>
mov    0x2ca2(%rip),%rax
mov    (%rax),%edi
call   1090 <fflush@plt>
xor    %edi,%edi
lea    -0x1c(%rbp),%rsi
call   1030 <FD_ISSET@plt>
cmp    $0x0,%rax
je     139b <main+0x1bb>
lea    -0x830(%rbp),%rdi
mov    0x2c90(%rip),%rax
mov    (%rax),%edx
mov    $0x400,%esi
call   1050 <fgets@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x844(%rbp)
lea    -0x830(%rbp),%rax
mov    %rax,-0x840(%rbp)
lea    -0x830(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x844(%rbp),%edi
mov    -0x840(%rbp),%rsi
mov    %eax,%edx
call   1100 <mysock_write_persist@plt>
jmp    1272 <main+0x92>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x418,%rsp
cmp    $0x2,%edi
jne    1223 <main+0x43>
mov    0x8(%rsi),%rdi
call   10a0 <atoi@plt>
lea    0xe18(%rip),%rdi
mov    %eax,%esi
call   10b0 <mysock_connect_socket@plt>
test   %eax,%eax
jg     1242 <main+0x62>
mov    0x2dc0(%rip),%rax
mov    (%rax),%edi
lea    0xe07(%rip),%rsi
jmp    1233 <main+0x53>
mov    0x2dae(%rip),%rax
mov    (%rax),%edi
lea    0xdcd(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    %eax,%ebx
mov    %eax,%r14d
add    $0x1,%r14d
lea    0xc(%rsp),%rbp
mov    0x2d91(%rip),%r12
lea    0x10(%rsp),%r15
mov    0x2d6d(%rip),%r13
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %rbp,%rdi
call   1070 <FD_ZERO@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
call   10f0 <FD_SET@plt>
xor    %edi,%edi
mov    %rbp,%rsi
call   10f0 <FD_SET@plt>
mov    %r14d,%edi
mov    %rbp,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
call   1080 <select@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
call   1030 <FD_ISSET@plt>
test   %rax,%rax
je     12de <main+0xfe>
mov    %ebx,%edi
mov    %r15,%rsi
mov    $0x400,%edx
xor    %ecx,%ecx
call   10e0 <mysock_read_data@plt>
test   %eax,%eax
jle    1317 <main+0x137>
mov    0x0(%r13),%ecx
mov    %r15,%rdi
mov    $0x1,%esi
mov    %eax,%edx
call   10d0 <fwrite@plt>
mov    0x0(%r13),%edi
call   1090 <fflush@plt>
xor    %edi,%edi
mov    %rbp,%rsi
call   1030 <FD_ISSET@plt>
test   %rax,%rax
je     1270 <main+0x90>
mov    (%r12),%edx
mov    %r15,%rdi
mov    $0x400,%esi
call   1050 <fgets@plt>
mov    %r15,%rdi
call   1040 <strlen@plt>
mov    %ebx,%edi
mov    %r15,%rsi
mov    %eax,%edx
call   1100 <mysock_write_persist@plt>
jmp    1270 <main+0x90>
mov    0x2cba(%rip),%rax
mov    (%rax),%edi
lea    0xd11(%rip),%rsi
call   1060 <fprintf@plt>
xor    %edi,%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [30 x i8] c"Usage: debug_client <portno>\0A\00"
@.str.1 = [10 x i8] c"127.0.0.1\00"
@.str.2 = [16 x i8] c"Connect failed\0A\00"
@.str.3 = [24 x i8] c"Connection terminated.\0A\00"
@stdout = global i32 0
@stdin = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca [1024 x i8]
%10 = alloca i32
%11 = alloca [1024 x i8]
%12 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%13 = load i32, i32* %4
%14 = icmp ne i32 %13, 2
br i1 %14, label %15, label %19
15: ; preds = %2
%16 = load i32, i32* @stderr
%17 = call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0))
%18 = call i32 @exit(i32 1)
unreachable
19: ; preds = %2
%20 = load i8**, i8*** %5
%21 = getelementptr inbounds i8*, i8** %20, i64 1
%22 = load i8*, i8** %21
%23 = call i32 @atoi(i8* %22)
store i32 %23, i32* %6
%24 = load i32, i32* %6
%25 = call i32 @mysock_connect_socket(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 %24)
store i32 %25, i32* %7
%26 = load i32, i32* %7
%27 = icmp sle i32 %26, 0
br i1 %27, label %28, label %32
28: ; preds = %19
%29 = load i32, i32* @stderr
%30 = call i32 @fprintf(i32 %29, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
%31 = call i32 @exit(i32 1)
unreachable
32: ; preds = %19
br label %33
33: ; preds = %32, %73
%34 = call i32 @FD_ZERO(i32* %8)
%35 = load i32, i32* %7
%36 = call i32 @FD_SET(i32 %35, i32* %8)
%37 = call i32 @FD_SET(i32 0, i32* %8)
%38 = load i32, i32* %7
%39 = add nsw i32 %38, 1
%40 = call i32 @select(i32 %39, i32* %8, i32* null, i32* null, i32* null)
%41 = load i32, i32* %7
%42 = call i64 @FD_ISSET(i32 %41, i32* %8)
%43 = icmp ne i64 %42, 0
br i1 %43, label %44, label %61
44: ; preds = %33
%45 = load i32, i32* %7
%46 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%47 = call i32 @mysock_read_data(i32 %45, i8* %46, i32 1024, i32 0)
store i32 %47, i32* %10
%48 = load i32, i32* %10
%49 = icmp sle i32 %48, 0
br i1 %49, label %50, label %54
50: ; preds = %44
%51 = load i32, i32* @stderr
%52 = call i32 @fprintf(i32 %51, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0))
%53 = call i32 @exit(i32 0)
unreachable
54: ; preds = %44
%55 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%56 = load i32, i32* %10
%57 = load i32, i32* @stdout
%58 = call i32 @fwrite(i8* %55, i32 1, i32 %56, i32 %57)
%59 = load i32, i32* @stdout
%60 = call i32 @fflush(i32 %59)
br label %61
61: ; preds = %54, %33
%62 = call i64 @FD_ISSET(i32 0, i32* %8)
%63 = icmp ne i64 %62, 0
br i1 %63, label %64, label %73
64: ; preds = %61
%65 = getelementptr inbounds [1024 x i8], [1024 x i8]* %11, i64 0, i64 0
%66 = load i32, i32* @stdin
%67 = call i32 @fgets(i8* %65, i32 1024, i32 %66)
%68 = load i32, i32* %7
%69 = getelementptr inbounds [1024 x i8], [1024 x i8]* %11, i64 0, i64 0
%70 = getelementptr inbounds [1024 x i8], [1024 x i8]* %11, i64 0, i64 0
%71 = call i32 @strlen(i8* %70)
%72 = call i32 @mysock_write_persist(i32 %68, i8* %69, i32 %71)
br label %73
73: ; preds = %64, %61
br label %33
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [30 x i8] c"Usage: debug_client <portno>\0A\00"
@.str.1 = [10 x i8] c"127.0.0.1\00"
@.str.2 = [16 x i8] c"Connect failed\0A\00"
@.str.3 = [24 x i8] c"Connection terminated.\0A\00"
@stdout = global i32 0
@stdin = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i32
%4 = alloca [1024 x i8]
%5 = alloca [1024 x i8]
%6 = bitcast i32* %3 to i8*
%7 = icmp eq i32 %0, 2
br i1 %7, label %12, label %8
8: ; preds = %2
%9 = load i32, i32* @stderr
%10 = tail call i32 @fprintf(i32 %9, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0))
%11 = tail call i32 @exit(i32 1)
unreachable
12: ; preds = %2
%13 = getelementptr inbounds i8*, i8** %1, i64 1
%14 = load i8*, i8** %13
%15 = tail call i32 @atoi(i8* %14)
%16 = tail call i32 @mysock_connect_socket(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 %15)
%17 = icmp slt i32 %16, 1
br i1 %17, label %22, label %18
18: ; preds = %12
%19 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%20 = add nuw nsw i32 %16, 1
%21 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
br label %26
22: ; preds = %12
%23 = load i32, i32* @stderr
%24 = tail call i32 @fprintf(i32 %23, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
%25 = tail call i32 @exit(i32 1)
unreachable
26: ; preds = %53, %18
%27 = call i32 @FD_ZERO(i32* %3)
%28 = call i32 @FD_SET(i32 %16, i32* %3)
%29 = call i32 @FD_SET(i32 0, i32* %3)
%30 = call i32 @select(i32 %20, i32* %3, i32* null, i32* null, i32* null)
%31 = call i64 @FD_ISSET(i32 %16, i32* %3)
%32 = icmp eq i64 %31, 0
br i1 %32, label %45, label %33
33: ; preds = %26
%34 = call i32 @mysock_read_data(i32 %16, i8* %19, i32 1024, i32 0)
%35 = icmp slt i32 %34, 1
br i1 %35, label %36, label %40
36: ; preds = %33
%37 = load i32, i32* @stderr
%38 = call i32 @fprintf(i32 %37, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0))
%39 = call i32 @exit(i32 0)
unreachable
40: ; preds = %33
%41 = load i32, i32* @stdout
%42 = call i32 @fwrite(i8* %19, i32 1, i32 %34, i32 %41)
%43 = load i32, i32* @stdout
%44 = call i32 @fflush(i32 %43)
br label %45
45: ; preds = %40, %26
%46 = call i64 @FD_ISSET(i32 0, i32* %3)
%47 = icmp eq i64 %46, 0
br i1 %47, label %53, label %48
48: ; preds = %45
%49 = load i32, i32* @stdin
%50 = call i32 @fgets(i8* %21, i32 1024, i32 %49)
%51 = call i32 @strlen(i8* %21)
%52 = call i32 @mysock_write_persist(i32 %16, i8* %21, i32 %51)
br label %53
53: ; preds = %48, %45
br label %26
}