#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 /*<<< orphan*/ EXIT_FAILURE;
int close(int);
int connect_to_gateway(char *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ free(char *);
char *malloc(int);
int /*<<< orphan*/ perror(char *);
char *read_line(int);
int /*<<< orphan*/ snprintf(char *, int, char *, char *, char *);
int /*<<< orphan*/ stderr;
int strlen(char *);
int /*<<< orphan*/ write_line(int, char *);
void book(char *hostname, char *type, char *guest) {
  int server_socket;
  char *request =
      malloc(strlen("book\n%s\n%s\n\n") + strlen(type) + strlen(guest) + 1);
  char *line;
  if (request == NULL) {
    perror("Unable to allocate necessary memory");
    exit(EXIT_FAILURE);
  }
  snprintf(request,
           strlen("book\n%s\n%s\n\n") + strlen(type) + strlen(guest) + 1,
           "book\n%s\n%s\n\n", type, guest);
  server_socket = connect_to_gateway(hostname);
  write_line(server_socket, request);
  line = read_line(server_socket);
  if (!line) {
    fprintf(stderr, "Cannot read response\n");
    exit(EXIT_FAILURE);
  }
  if (*line != '0') {
    if (strlen(line) > 2) {
      if (*line == '1') {
        fprintf(stderr, "%s\n", line + 2);
      } else {
        fprintf(stderr, "Error response: %s\n", line + 2);
      }
    } else {
      fprintf(stderr, "Cannot read response\n");
    }
    exit(EXIT_FAILURE);
  }
  free(line);
  line = read_line(server_socket);
  if (strlen(line) != 0) {
    fprintf(stderr, "Cannot read response\n");
    exit(EXIT_FAILURE);
  }
  free(line);
  if (close(server_socket) == -1) {
    perror("Error closing connection");
    exit(EXIT_FAILURE);
  }
}


x86 assembly (clang, -O0):
<book>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
lea    0xe35(%rip),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x38(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x38(%rbp),%eax
add    %ecx,%eax
mov    %eax,-0x34(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x34(%rbp),%edi
add    %eax,%edi
add    $0x1,%edi
call   1080 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1225 <book+0x75>
lea    0xdfb(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2dba(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x40(%rbp)
lea    0xdcc(%rip),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x48(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x48(%rbp),%eax
add    %ecx,%eax
mov    %eax,-0x44(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x44(%rbp),%esi
mov    -0x40(%rbp),%rdi
add    %eax,%esi
add    $0x1,%esi
mov    -0x10(%rbp),%rcx
mov    -0x18(%rbp),%r8
lea    0xd8d(%rip),%rdx
call   1050 <snprintf@plt>
mov    -0x8(%rbp),%rdi
call   10b0 <connect_to_gateway@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%edi
mov    -0x28(%rbp),%rsi
call   10d0 <write_line@plt>
mov    -0x1c(%rbp),%edi
call   1090 <read_line@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    12cc <book+0x11c>
mov    0x2d32(%rip),%rax
mov    (%rax),%edi
lea    0xd7a(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    0x2d13(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
mov    -0x30(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x30,%eax
je     136b <book+0x1bb>
mov    -0x30(%rbp),%rdi
call   1040 <strlen@plt>
cmp    $0x2,%eax
jle    1346 <book+0x196>
mov    -0x30(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x31,%eax
jne    1322 <book+0x172>
mov    0x2cdb(%rip),%rax
mov    (%rax),%edi
mov    -0x30(%rbp),%rdx
add    $0x2,%rdx
lea    0xd41(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
jmp    1341 <book+0x191>
mov    0x2cb7(%rip),%rax
mov    (%rax),%edi
mov    -0x30(%rbp),%rdx
add    $0x2,%rdx
lea    0xd0d(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
jmp    135d <book+0x1ad>
mov    0x2c93(%rip),%rax
mov    (%rax),%edi
lea    0xcdb(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    0x2c74(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    -0x1c(%rbp),%edi
call   1090 <read_line@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
call   1040 <strlen@plt>
cmp    $0x0,%eax
je     13b7 <book+0x207>
mov    0x2c47(%rip),%rax
mov    (%rax),%edi
lea    0xc8f(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    0x2c28(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    -0x1c(%rbp),%edi
call   1060 <close@plt>
cmp    $0xffffffff,%eax
jne    13eb <book+0x23b>
lea    0xc83(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2bf4(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<book>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r15
mov    %rsi,%r13
mov    %rdi,(%rsp)
lea    0xe34(%rip),%rdi
call   1040 <strlen@plt>
mov    %eax,%ebp
mov    %r13,%rdi
call   1040 <strlen@plt>
mov    %eax,%ebx
mov    %r15,%rdi
call   1040 <strlen@plt>
add    %ebp,%ebx
lea    (%rax,%rbx,1),%edi
add    $0x1,%edi
call   1080 <malloc@plt>
test   %rax,%rax
je     12ab <book+0xfb>
mov    %rax,%r12
lea    0xdfb(%rip),%rbx
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %eax,%ebp
mov    %r13,%rdi
call   1040 <strlen@plt>
mov    %eax,%r14d
mov    %r15,%rdi
call   1040 <strlen@plt>
add    %ebp,%r14d
lea    (%rax,%r14,1),%esi
add    $0x1,%esi
mov    %r12,%rdi
mov    %rbx,%rdx
mov    %r13,%rcx
mov    %r15,%r8
call   1050 <snprintf@plt>
mov    (%rsp),%rdi
call   10b0 <connect_to_gateway@plt>
mov    %eax,%ebp
mov    %eax,%edi
mov    %r12,%rsi
call   10d0 <write_line@plt>
mov    %ebp,%edi
call   1090 <read_line@plt>
test   %rax,%rax
je     12e4 <book+0x134>
mov    %rax,%rbx
cmpb   $0x30,(%rax)
jne    12b9 <book+0x109>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %ebp,%edi
call   1090 <read_line@plt>
mov    %rax,%rbx
mov    %rax,%rdi
call   1040 <strlen@plt>
test   %eax,%eax
jne    12e4 <book+0x134>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %ebp,%edi
call   1060 <close@plt>
cmp    $0xffffffff,%eax
je     12fd <book+0x14d>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd5b(%rip),%rdi
call   10a0 <perror@plt>
jmp    1319 <book+0x169>
mov    %rbx,%rdi
call   1040 <strlen@plt>
cmp    $0x3,%eax
jl     12e4 <book+0x134>
mov    %rbx,%rdx
add    $0x2,%rdx
cmpb   $0x31,(%rbx)
mov    0x2d09(%rip),%rax
mov    (%rax),%edi
jne    130b <book+0x15b>
lea    0xd75(%rip),%rsi
jmp    1312 <book+0x162>
mov    0x2cf5(%rip),%rax
mov    (%rax),%edi
lea    0xd3d(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
jmp    1319 <book+0x169>
lea    0xd57(%rip),%rdi
call   10a0 <perror@plt>
jmp    1319 <book+0x169>
lea    0xd35(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    0x2cb8(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
@.str = [13 x i8] c"book\0A%s\0A%s\0A\0A\00"
@.str.1 = [36 x i8] c"Unable to allocate necessary memory\00"
@EXIT_FAILURE = global i32 0
@stderr = global i32 0
@.str.2 = [22 x i8] c"Cannot read response\0A\00"
@.str.3 = [4 x i8] c"%s\0A\00"
@.str.4 = [20 x i8] c"Error response: %s\0A\00"
@.str.5 = [25 x i8] c"Error closing connection\00"
define void @book(i8* %0, i8* %1, i8* %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i8*
store i8* %0, i8** %4
store i8* %1, i8** %5
store i8* %2, i8** %6
%10 = call i32 @strlen(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%11 = load i8*, i8** %5
%12 = call i32 @strlen(i8* %11)
%13 = add nsw i32 %10, %12
%14 = load i8*, i8** %6
%15 = call i32 @strlen(i8* %14)
%16 = add nsw i32 %13, %15
%17 = add nsw i32 %16, 1
%18 = call i8* @malloc(i32 %17)
store i8* %18, i8** %8
%19 = load i8*, i8** %8
%20 = icmp eq i8* %19, null
br i1 %20, label %21, label %25
21: ; preds = %3
%22 = call i32 @perror(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0))
%23 = load i32, i32* @EXIT_FAILURE
%24 = call i32 @exit(i32 %23)
unreachable
25: ; preds = %3
%26 = load i8*, i8** %8
%27 = call i32 @strlen(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%28 = load i8*, i8** %5
%29 = call i32 @strlen(i8* %28)
%30 = add nsw i32 %27, %29
%31 = load i8*, i8** %6
%32 = call i32 @strlen(i8* %31)
%33 = add nsw i32 %30, %32
%34 = add nsw i32 %33, 1
%35 = load i8*, i8** %5
%36 = load i8*, i8** %6
%37 = call i32 @snprintf(i8* %26, i32 %34, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8* %35, i8* %36)
%38 = load i8*, i8** %4
%39 = call i32 @connect_to_gateway(i8* %38)
store i32 %39, i32* %7
%40 = load i32, i32* %7
%41 = load i8*, i8** %8
%42 = call i32 @write_line(i32 %40, i8* %41)
%43 = load i32, i32* %7
%44 = call i8* @read_line(i32 %43)
store i8* %44, i8** %9
%45 = load i8*, i8** %9
%46 = icmp ne i8* %45, null
br i1 %46, label %52, label %47
47: ; preds = %25
%48 = load i32, i32* @stderr
%49 = call i32 (i32, i8*, ...) @fprintf(i32 %48, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0))
%50 = load i32, i32* @EXIT_FAILURE
%51 = call i32 @exit(i32 %50)
unreachable
52: ; preds = %25
%53 = load i8*, i8** %9
%54 = load i8, i8* %53
%55 = sext i8 %54 to i32
%56 = icmp ne i32 %55, 48
br i1 %56, label %57, label %83
57: ; preds = %52
%58 = load i8*, i8** %9
%59 = call i32 @strlen(i8* %58)
%60 = icmp sgt i32 %59, 2
br i1 %60, label %61, label %77
61: ; preds = %57
%62 = load i8*, i8** %9
%63 = load i8, i8* %62
%64 = sext i8 %63 to i32
%65 = icmp eq i32 %64, 49
br i1 %65, label %66, label %71
66: ; preds = %61
%67 = load i32, i32* @stderr
%68 = load i8*, i8** %9
%69 = getelementptr inbounds i8, i8* %68, i64 2
%70 = call i32 (i32, i8*, ...) @fprintf(i32 %67, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8* %69)
br label %76
71: ; preds = %61
%72 = load i32, i32* @stderr
%73 = load i8*, i8** %9
%74 = getelementptr inbounds i8, i8* %73, i64 2
%75 = call i32 (i32, i8*, ...) @fprintf(i32 %72, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0), i8* %74)
br label %76
76: ; preds = %71, %66
br label %80
77: ; preds = %57
%78 = load i32, i32* @stderr
%79 = call i32 (i32, i8*, ...) @fprintf(i32 %78, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0))
br label %80
80: ; preds = %77, %76
%81 = load i32, i32* @EXIT_FAILURE
%82 = call i32 @exit(i32 %81)
unreachable
83: ; preds = %52
%84 = load i8*, i8** %9
%85 = call i32 @free(i8* %84)
%86 = load i32, i32* %7
%87 = call i8* @read_line(i32 %86)
store i8* %87, i8** %9
%88 = load i8*, i8** %9
%89 = call i32 @strlen(i8* %88)
%90 = icmp ne i32 %89, 0
br i1 %90, label %91, label %96
91: ; preds = %83
%92 = load i32, i32* @stderr
%93 = call i32 (i32, i8*, ...) @fprintf(i32 %92, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0))
%94 = load i32, i32* @EXIT_FAILURE
%95 = call i32 @exit(i32 %94)
unreachable
96: ; preds = %83
%97 = load i8*, i8** %9
%98 = call i32 @free(i8* %97)
%99 = load i32, i32* %7
%100 = call i32 @close(i32 %99)
%101 = icmp eq i32 %100, -1
br i1 %101, label %102, label %106
102: ; preds = %96
%103 = call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.5, i64 0, i64 0))
%104 = load i32, i32* @EXIT_FAILURE
%105 = call i32 @exit(i32 %104)
unreachable
106: ; preds = %96
ret void
}

LLVM IR (-O2):
@.str = [13 x i8] c"book\0A%s\0A%s\0A\0A\00"
@.str.1 = [36 x i8] c"Unable to allocate necessary memory\00"
@EXIT_FAILURE = global i32 0
@stderr = global i32 0
@.str.2 = [22 x i8] c"Cannot read response\0A\00"
@.str.3 = [4 x i8] c"%s\0A\00"
@.str.4 = [20 x i8] c"Error response: %s\0A\00"
@.str.5 = [25 x i8] c"Error closing connection\00"
define void @book(i8* %0, i8* %1, i8* %2) {
%4 = tail call i32 @strlen(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%5 = tail call i32 @strlen(i8* %1)
%6 = tail call i32 @strlen(i8* %2)
%7 = add i32 %4, 1
%8 = add i32 %7, %5
%9 = add i32 %8, %6
%10 = tail call i8* @malloc(i32 %9)
%11 = icmp eq i8* %10, null
br i1 %11, label %12, label %16
12: ; preds = %3
%13 = tail call i32 @perror(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0))
%14 = load i32, i32* @EXIT_FAILURE
%15 = tail call i32 @exit(i32 %14)
unreachable
16: ; preds = %3
%17 = tail call i32 @strlen(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%18 = tail call i32 @strlen(i8* %1)
%19 = tail call i32 @strlen(i8* %2)
%20 = add i32 %17, 1
%21 = add i32 %20, %18
%22 = add i32 %21, %19
%23 = tail call i32 @snprintf(i8* %10, i32 %22, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8* %1, i8* %2)
%24 = tail call i32 @connect_to_gateway(i8* %0)
%25 = tail call i32 @write_line(i32 %24, i8* %10)
%26 = tail call i8* @read_line(i32 %24)
%27 = icmp eq i8* %26, null
br i1 %27, label %28, label %33
28: ; preds = %16
%29 = load i32, i32* @stderr
%30 = tail call i32 (i32, i8*, ...) @fprintf(i32 %29, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0))
%31 = load i32, i32* @EXIT_FAILURE
%32 = tail call i32 @exit(i32 %31)
unreachable
33: ; preds = %16
%34 = load i8, i8* %26
%35 = icmp eq i8 %34, 48
br i1 %35, label %54, label %36
36: ; preds = %33
%37 = tail call i32 @strlen(i8* %26)
%38 = icmp sgt i32 %37, 2
br i1 %38, label %39, label %48
39: ; preds = %36
%40 = load i8, i8* %26
%41 = icmp eq i8 %40, 49
%42 = load i32, i32* @stderr
%43 = getelementptr inbounds i8, i8* %26, i64 2
br i1 %41, label %44, label %46
44: ; preds = %39
%45 = tail call i32 (i32, i8*, ...) @fprintf(i32 %42, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8* %43)
br label %51
46: ; preds = %39
%47 = tail call i32 (i32, i8*, ...) @fprintf(i32 %42, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0), i8* %43)
br label %51
48: ; preds = %36
%49 = load i32, i32* @stderr
%50 = tail call i32 (i32, i8*, ...) @fprintf(i32 %49, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0))
br label %51
51: ; preds = %44, %46, %48
%52 = load i32, i32* @EXIT_FAILURE
%53 = tail call i32 @exit(i32 %52)
unreachable
54: ; preds = %33
%55 = tail call i32 @free(i8* %26)
%56 = tail call i8* @read_line(i32 %24)
%57 = tail call i32 @strlen(i8* %56)
%58 = icmp eq i32 %57, 0
br i1 %58, label %64, label %59
59: ; preds = %54
%60 = load i32, i32* @stderr
%61 = tail call i32 (i32, i8*, ...) @fprintf(i32 %60, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0))
%62 = load i32, i32* @EXIT_FAILURE
%63 = tail call i32 @exit(i32 %62)
unreachable
64: ; preds = %54
%65 = tail call i32 @free(i8* %56)
%66 = tail call i32 @close(i32 %24)
%67 = icmp eq i32 %66, -1
br i1 %67, label %68, label %72
68: ; preds = %64
%69 = tail call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.5, i64 0, i64 0))
%70 = load i32, i32* @EXIT_FAILURE
%71 = tail call i32 @exit(i32 %70)
unreachable
72: ; preds = %64
ret void
}