#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 writer {
  int dummy;
};
struct string_buffer {
  int dummy;
};
struct socket {
  int dummy;
};
struct reader {
  int dummy;
};

/* Variables and functions */
struct socket *create_client_socket(int);
struct string_buffer *create_string_buffer();
int /*<<< orphan*/ reader_read_line(struct reader *, struct string_buffer *);
int /*<<< orphan*/ socket_close(struct socket *);
struct reader *socket_get_reader(struct socket *);
struct writer *socket_get_writer(struct socket *);
int /*<<< orphan*/ string_buffer_dispose(struct string_buffer *);
int string_buffer_equals_string(struct string_buffer *, char *);
int string_buffer_split(struct string_buffer *, char *, struct string_buffer *,
                        struct string_buffer *);
int /*<<< orphan*/ writer_write_string(struct writer *, char *);
int /*<<< orphan*/ writer_write_string_buffer(struct writer *,
                                              struct string_buffer *);
int main() {
  struct socket *s = create_client_socket(12345);
  struct reader *r = socket_get_reader(s);
  struct writer *w = socket_get_writer(s);
  bool stop = false;
  struct string_buffer *line = create_string_buffer();
  struct string_buffer *nick = create_string_buffer();
  struct string_buffer *text = create_string_buffer();
  reader_read_line(r, line);
  reader_read_line(r, line);
  writer_write_string(w, "BoT\r\n");
  while (!stop) {
    bool test = true;
    bool result = false;
    reader_read_line(r, line);
    result = string_buffer_split(line, " says: ", nick, text);
    test = string_buffer_equals_string(nick, "BoT");
    if (result && !test) {
      test = string_buffer_equals_string(text, "!hello");
      if (test) {
        writer_write_string(w, "Hello ");
        writer_write_string_buffer(w, nick);
        writer_write_string(w, "!\r\n");
      } else {
        test = string_buffer_equals_string(text, "!quit");
        if (test) {
          writer_write_string(w, "Byebye!\r\n");
          stop = true;
          string_buffer_dispose(line);
          string_buffer_dispose(nick);
          string_buffer_dispose(text);
        }
      }
    }
  }
  socket_close(s);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x3039,%edi
call   1050 <create_client_socket@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <socket_get_reader@plt>
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   10b0 <socket_get_writer@plt>
mov    %rax,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
mov    $0x0,%al
call   10c0 <create_string_buffer@plt>
mov    %rax,-0x30(%rbp)
mov    $0x0,%al
call   10c0 <create_string_buffer@plt>
mov    %rax,-0x38(%rbp)
mov    $0x0,%al
call   10c0 <create_string_buffer@plt>
mov    %rax,-0x40(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x30(%rbp),%rsi
call   1070 <reader_read_line@plt>
mov    -0x18(%rbp),%rdi
mov    -0x30(%rbp),%rsi
call   1070 <reader_read_line@plt>
mov    -0x20(%rbp),%rdi
lea    0xdcc(%rip),%rsi
call   1090 <writer_write_string@plt>
cmpl   $0x0,-0x24(%rbp)
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    124f <main+0x9f>
jmp    1359 <main+0x1a9>
movl   $0x1,-0x44(%rbp)
movl   $0x0,-0x48(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x30(%rbp),%rsi
call   1070 <reader_read_line@plt>
mov    -0x30(%rbp),%rdi
mov    -0x38(%rbp),%rdx
mov    -0x40(%rbp),%rcx
lea    0xd89(%rip),%rsi
call   1060 <string_buffer_split@plt>
mov    %eax,-0x48(%rbp)
mov    -0x38(%rbp),%rdi
lea    0xd7e(%rip),%rsi
call   1030 <string_buffer_equals_string@plt>
mov    %eax,-0x44(%rbp)
cmpl   $0x0,-0x48(%rbp)
je     1354 <main+0x1a4>
cmpl   $0x0,-0x44(%rbp)
jne    1354 <main+0x1a4>
mov    -0x40(%rbp),%rdi
lea    0xd5b(%rip),%rsi
call   1030 <string_buffer_equals_string@plt>
mov    %eax,-0x44(%rbp)
cmpl   $0x0,-0x44(%rbp)
je     12fb <main+0x14b>
mov    -0x20(%rbp),%rdi
lea    0xd45(%rip),%rsi
call   1090 <writer_write_string@plt>
mov    -0x20(%rbp),%rdi
mov    -0x38(%rbp),%rsi
call   10d0 <writer_write_string_buffer@plt>
mov    -0x20(%rbp),%rdi
lea    0xd3b(%rip),%rsi
call   1090 <writer_write_string@plt>
jmp    134f <main+0x19f>
mov    -0x40(%rbp),%rdi
lea    0xd1a(%rip),%rsi
call   1030 <string_buffer_equals_string@plt>
mov    %eax,-0x44(%rbp)
cmpl   $0x0,-0x44(%rbp)
je     134a <main+0x19a>
mov    -0x20(%rbp),%rdi
lea    0xd03(%rip),%rsi
call   1090 <writer_write_string@plt>
movl   $0x1,-0x24(%rbp)
mov    -0x30(%rbp),%rdi
call   1080 <string_buffer_dispose@plt>
mov    -0x38(%rbp),%rdi
call   1080 <string_buffer_dispose@plt>
mov    -0x40(%rbp),%rdi
call   1080 <string_buffer_dispose@plt>
jmp    134f <main+0x19f>
jmp    1354 <main+0x1a4>
jmp    1239 <main+0x89>
mov    -0x10(%rbp),%rdi
call   10a0 <socket_close@plt>
xor    %eax,%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    $0x3039,%edi
call   1050 <create_client_socket@plt>
mov    %rax,%rbx
mov    %rax,%rdi
call   1040 <socket_get_reader@plt>
mov    %rax,%r12
mov    %rbx,0x10(%rsp)
mov    %rbx,%rdi
call   10b0 <socket_get_writer@plt>
mov    %rax,%r14
xor    %eax,%eax
call   10c0 <create_string_buffer@plt>
mov    %rax,%r13
xor    %eax,%eax
call   10c0 <create_string_buffer@plt>
mov    %rax,%rbx
xor    %eax,%eax
call   10c0 <create_string_buffer@plt>
mov    %rax,%rbp
mov    %r12,%rdi
mov    %r13,%rsi
call   1070 <reader_read_line@plt>
mov    %r12,%rdi
mov    %r13,%rsi
call   1070 <reader_read_line@plt>
lea    0xddf(%rip),%rsi
lea    0xde6(%rip),%r15
mov    %r14,0x8(%rsp)
mov    %r14,%rdi
call   1090 <writer_write_string@plt>
cs nopw 0x0(%rax,%rax,1)
nop
mov    %r12,%rdi
mov    %r13,%rsi
call   1070 <reader_read_line@plt>
mov    %r13,%rdi
lea    0xdb1(%rip),%rsi
mov    %rbx,%rdx
mov    %rbp,%rcx
call   1060 <string_buffer_split@plt>
mov    %eax,%r14d
mov    %rbx,%rdi
mov    %r15,%rsi
call   1030 <string_buffer_equals_string@plt>
test   %r14d,%r14d
je     1240 <main+0x90>
test   %eax,%eax
jne    1240 <main+0x90>
mov    %rbp,%rdi
lea    0xd91(%rip),%rsi
call   1030 <string_buffer_equals_string@plt>
test   %eax,%eax
jne    12a0 <main+0xf0>
mov    %rbp,%rdi
lea    0xd8c(%rip),%rsi
call   1030 <string_buffer_equals_string@plt>
test   %eax,%eax
je     1240 <main+0x90>
jmp    12cb <main+0x11b>
nop
mov    0x8(%rsp),%r14
mov    %r14,%rdi
lea    0xd6a(%rip),%rsi
call   1090 <writer_write_string@plt>
mov    %r14,%rdi
mov    %rbx,%rsi
call   10d0 <writer_write_string_buffer@plt>
lea    0xd66(%rip),%rsi
jmp    122d <main+0x7d>
lea    0xd54(%rip),%rsi
mov    0x8(%rsp),%rdi
call   1090 <writer_write_string@plt>
mov    %r13,%rdi
call   1080 <string_buffer_dispose@plt>
mov    %rbx,%rdi
call   1080 <string_buffer_dispose@plt>
mov    %rbp,%rdi
call   1080 <string_buffer_dispose@plt>
mov    0x10(%rsp),%rdi
call   10a0 <socket_close@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.socket = type { i32 }
%struct.reader = type { i32 }
%struct.writer = type { i32 }
%struct.string_buffer = type { i32 }
@.str = [6 x i8] c"BoT\0D\0A\00"
@.str.1 = [8 x i8] c" says: \00"
@.str.2 = [4 x i8] c"BoT\00"
@.str.3 = [7 x i8] c"!hello\00"
@.str.4 = [7 x i8] c"Hello \00"
@.str.5 = [4 x i8] c"!\0D\0A\00"
@.str.6 = [6 x i8] c"!quit\00"
@.str.7 = [10 x i8] c"Byebye!\0D\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca %struct.socket*
%3 = alloca %struct.reader*
%4 = alloca %struct.writer*
%5 = alloca i32
%6 = alloca %struct.string_buffer*
%7 = alloca %struct.string_buffer*
%8 = alloca %struct.string_buffer*
%9 = alloca i32
%10 = alloca i32
store i32 0, i32* %1
%11 = call %struct.socket* @create_client_socket(i32 12345)
store %struct.socket* %11, %struct.socket** %2
%12 = load %struct.socket*, %struct.socket** %2
%13 = call %struct.reader* @socket_get_reader(%struct.socket* %12)
store %struct.reader* %13, %struct.reader** %3
%14 = load %struct.socket*, %struct.socket** %2
%15 = call %struct.writer* @socket_get_writer(%struct.socket* %14)
store %struct.writer* %15, %struct.writer** %4
store i32 0, i32* %5
%16 = call %struct.string_buffer* (...) @create_string_buffer()
store %struct.string_buffer* %16, %struct.string_buffer** %6
%17 = call %struct.string_buffer* (...) @create_string_buffer()
store %struct.string_buffer* %17, %struct.string_buffer** %7
%18 = call %struct.string_buffer* (...) @create_string_buffer()
store %struct.string_buffer* %18, %struct.string_buffer** %8
%19 = load %struct.reader*, %struct.reader** %3
%20 = load %struct.string_buffer*, %struct.string_buffer** %6
%21 = call i32 @reader_read_line(%struct.reader* %19, %struct.string_buffer* %20)
%22 = load %struct.reader*, %struct.reader** %3
%23 = load %struct.string_buffer*, %struct.string_buffer** %6
%24 = call i32 @reader_read_line(%struct.reader* %22, %struct.string_buffer* %23)
%25 = load %struct.writer*, %struct.writer** %4
%26 = call i32 @writer_write_string(%struct.writer* %25, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
br label %27
27: ; preds = %75, %0
%28 = load i32, i32* %5
%29 = icmp ne i32 %28, 0
%30 = xor i1 %29, true
br i1 %30, label %31, label %76
31: ; preds = %27
store i32 1, i32* %9
store i32 0, i32* %10
%32 = load %struct.reader*, %struct.reader** %3
%33 = load %struct.string_buffer*, %struct.string_buffer** %6
%34 = call i32 @reader_read_line(%struct.reader* %32, %struct.string_buffer* %33)
%35 = load %struct.string_buffer*, %struct.string_buffer** %6
%36 = load %struct.string_buffer*, %struct.string_buffer** %7
%37 = load %struct.string_buffer*, %struct.string_buffer** %8
%38 = call i32 @string_buffer_split(%struct.string_buffer* %35, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), %struct.string_buffer* %36, %struct.string_buffer* %37)
store i32 %38, i32* %10
%39 = load %struct.string_buffer*, %struct.string_buffer** %7
%40 = call i32 @string_buffer_equals_string(%struct.string_buffer* %39, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0))
store i32 %40, i32* %9
%41 = load i32, i32* %10
%42 = icmp ne i32 %41, 0
br i1 %42, label %43, label %75
43: ; preds = %31
%44 = load i32, i32* %9
%45 = icmp ne i32 %44, 0
br i1 %45, label %75, label %46
46: ; preds = %43
%47 = load %struct.string_buffer*, %struct.string_buffer** %8
%48 = call i32 @string_buffer_equals_string(%struct.string_buffer* %47, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
store i32 %48, i32* %9
%49 = load i32, i32* %9
%50 = icmp ne i32 %49, 0
br i1 %50, label %51, label %59
51: ; preds = %46
%52 = load %struct.writer*, %struct.writer** %4
%53 = call i32 @writer_write_string(%struct.writer* %52, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
%54 = load %struct.writer*, %struct.writer** %4
%55 = load %struct.string_buffer*, %struct.string_buffer** %7
%56 = call i32 @writer_write_string_buffer(%struct.writer* %54, %struct.string_buffer* %55)
%57 = load %struct.writer*, %struct.writer** %4
%58 = call i32 @writer_write_string(%struct.writer* %57, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0))
br label %74
59: ; preds = %46
%60 = load %struct.string_buffer*, %struct.string_buffer** %8
%61 = call i32 @string_buffer_equals_string(%struct.string_buffer* %60, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.6, i64 0, i64 0))
store i32 %61, i32* %9
%62 = load i32, i32* %9
%63 = icmp ne i32 %62, 0
br i1 %63, label %64, label %73
64: ; preds = %59
%65 = load %struct.writer*, %struct.writer** %4
%66 = call i32 @writer_write_string(%struct.writer* %65, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.7, i64 0, i64 0))
store i32 1, i32* %5
%67 = load %struct.string_buffer*, %struct.string_buffer** %6
%68 = call i32 @string_buffer_dispose(%struct.string_buffer* %67)
%69 = load %struct.string_buffer*, %struct.string_buffer** %7
%70 = call i32 @string_buffer_dispose(%struct.string_buffer* %69)
%71 = load %struct.string_buffer*, %struct.string_buffer** %8
%72 = call i32 @string_buffer_dispose(%struct.string_buffer* %71)
br label %73
73: ; preds = %64, %59
br label %74
74: ; preds = %73, %51
br label %75
75: ; preds = %74, %43, %31
br label %27
76: ; preds = %27
%77 = load %struct.socket*, %struct.socket** %2
%78 = call i32 @socket_close(%struct.socket* %77)
ret i32 0
}

LLVM IR (-O2):
%struct.socket = type { i32 }
%struct.reader = type { i32 }
%struct.writer = type { i32 }
%struct.string_buffer = type { i32 }
@.str = [6 x i8] c"BoT\0D\0A\00"
@.str.1 = [8 x i8] c" says: \00"
@.str.2 = [4 x i8] c"BoT\00"
@.str.3 = [7 x i8] c"!hello\00"
@.str.4 = [7 x i8] c"Hello \00"
@.str.5 = [4 x i8] c"!\0D\0A\00"
@.str.6 = [6 x i8] c"!quit\00"
@.str.7 = [10 x i8] c"Byebye!\0D\0A\00"
define i32 @main() {
%1 = tail call %struct.socket* @create_client_socket(i32 12345)
%2 = tail call %struct.reader* @socket_get_reader(%struct.socket* %1)
%3 = tail call %struct.writer* @socket_get_writer(%struct.socket* %1)
%4 = tail call %struct.string_buffer* (...) @create_string_buffer()
%5 = tail call %struct.string_buffer* (...) @create_string_buffer()
%6 = tail call %struct.string_buffer* (...) @create_string_buffer()
%7 = tail call i32 @reader_read_line(%struct.reader* %2, %struct.string_buffer* %4)
%8 = tail call i32 @reader_read_line(%struct.reader* %2, %struct.string_buffer* %4)
br label %9
9: ; preds = %0, %22
%10 = phi i8* [ getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0), %22 ], [ getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), %0 ]
%11 = tail call i32 @writer_write_string(%struct.writer* %3, i8* %10)
br label %12
12: ; preds = %28, %9
%13 = tail call i32 @reader_read_line(%struct.reader* %2, %struct.string_buffer* %4)
%14 = tail call i32 @string_buffer_split(%struct.string_buffer* %4, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), %struct.string_buffer* %5, %struct.string_buffer* %6)
%15 = tail call i32 @string_buffer_equals_string(%struct.string_buffer* %5, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0))
%16 = icmp eq i32 %14, 0
%17 = icmp ne i32 %15, 0
%18 = select i1 %16, i1 true, i1 %17
br i1 %18, label %28, label %19
19: ; preds = %12
%20 = tail call i32 @string_buffer_equals_string(%struct.string_buffer* %6, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
%21 = icmp eq i32 %20, 0
br i1 %21, label %25, label %22
22: ; preds = %19
%23 = tail call i32 @writer_write_string(%struct.writer* %3, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
%24 = tail call i32 @writer_write_string_buffer(%struct.writer* %3, %struct.string_buffer* %5)
br label %9
25: ; preds = %19
%26 = tail call i32 @string_buffer_equals_string(%struct.string_buffer* %6, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.6, i64 0, i64 0))
%27 = icmp eq i32 %26, 0
br i1 %27, label %28, label %29
28: ; preds = %25, %12
br label %12
29: ; preds = %25
%30 = tail call i32 @writer_write_string(%struct.writer* %3, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.7, i64 0, i64 0))
%31 = tail call i32 @string_buffer_dispose(%struct.string_buffer* %4)
%32 = tail call i32 @string_buffer_dispose(%struct.string_buffer* %5)
%33 = tail call i32 @string_buffer_dispose(%struct.string_buffer* %6)
%34 = tail call i32 @socket_close(%struct.socket* %1)
ret i32 0
}