#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 *);
int /*<<< orphan*/ stderr;
int strlen(char *);
int /*<<< orphan*/ write_line(int, char *);
void book_any(char *hostname, char *guest) {
  int server_socket;
  char *request = malloc(strlen("book\n%s\n\n") + strlen(guest) + 1);
  char *line;
  if (request == NULL) {
    perror("Unable to allocate necessary memory");
    exit(EXIT_FAILURE);
  }
  snprintf(request, strlen("book\n%s\n\n") + strlen(guest) + 1, "book\n%s\n\n",
           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_any>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xe39(%rip),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x2c(%rbp),%edi
add    %eax,%edi
add    $0x1,%edi
call   1080 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    120e <book_any+0x5e>
lea    0xe0f(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2dd1(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x38(%rbp)
lea    0xde3(%rip),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x3c(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x3c(%rbp),%esi
mov    -0x38(%rbp),%rdi
add    %eax,%esi
add    $0x1,%esi
mov    -0x10(%rbp),%rcx
lea    0xdbb(%rip),%rdx
call   1050 <snprintf@plt>
mov    -0x8(%rbp),%rdi
call   10b0 <connect_to_gateway@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%edi
mov    -0x20(%rbp),%rsi
call   10d0 <write_line@plt>
mov    -0x14(%rbp),%edi
call   1090 <read_line@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    129e <book_any+0xee>
mov    0x2d60(%rip),%rax
mov    (%rax),%edi
lea    0xda5(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    0x2d41(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
mov    -0x28(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x30,%eax
je     133d <book_any+0x18d>
mov    -0x28(%rbp),%rdi
call   1040 <strlen@plt>
cmp    $0x2,%eax
jle    1318 <book_any+0x168>
mov    -0x28(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x31,%eax
jne    12f4 <book_any+0x144>
mov    0x2d09(%rip),%rax
mov    (%rax),%edi
mov    -0x28(%rbp),%rdx
add    $0x2,%rdx
lea    0xd6c(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
jmp    1313 <book_any+0x163>
mov    0x2ce5(%rip),%rax
mov    (%rax),%edi
mov    -0x28(%rbp),%rdx
add    $0x2,%rdx
lea    0xd38(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
jmp    132f <book_any+0x17f>
mov    0x2cc1(%rip),%rax
mov    (%rax),%edi
lea    0xd06(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    0x2ca2(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x14(%rbp),%edi
call   1090 <read_line@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
call   1040 <strlen@plt>
cmp    $0x0,%eax
je     1389 <book_any+0x1d9>
mov    0x2c75(%rip),%rax
mov    (%rax),%edi
lea    0xcba(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    0x2c56(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
cmp    $0xffffffff,%eax
jne    13bd <book_any+0x20d>
lea    0xcae(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2c22(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<book_any>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rsi,%rbx
mov    %rdi,%r14
lea    0xe3b(%rip),%rdi
call   1040 <strlen@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1040 <strlen@plt>
lea    (%rax,%rbp,1),%edi
add    $0x1,%edi
call   1080 <malloc@plt>
test   %rax,%rax
je     127b <book_any+0xcb>
mov    %rax,%r15
lea    0xe0e(%rip),%r12
mov    %r12,%rdi
call   1040 <strlen@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1040 <strlen@plt>
lea    (%rax,%rbp,1),%esi
add    $0x1,%esi
mov    %r15,%rdi
mov    %r12,%rdx
mov    %rbx,%rcx
call   1050 <snprintf@plt>
mov    %r14,%rdi
call   10b0 <connect_to_gateway@plt>
mov    %eax,%ebp
mov    %eax,%edi
mov    %r15,%rsi
call   10d0 <write_line@plt>
mov    %ebp,%edi
call   1090 <read_line@plt>
test   %rax,%rax
je     12b4 <book_any+0x104>
mov    %rax,%rbx
cmpb   $0x30,(%rax)
jne    1289 <book_any+0xd9>
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    12b4 <book_any+0x104>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %ebp,%edi
call   1060 <close@plt>
cmp    $0xffffffff,%eax
je     12cd <book_any+0x11d>
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd88(%rip),%rdi
call   10a0 <perror@plt>
jmp    12e9 <book_any+0x139>
mov    %rbx,%rdi
call   1040 <strlen@plt>
cmp    $0x3,%eax
jl     12b4 <book_any+0x104>
mov    %rbx,%rdx
add    $0x2,%rdx
cmpb   $0x31,(%rbx)
mov    0x2d39(%rip),%rax
mov    (%rax),%edi
jne    12db <book_any+0x12b>
lea    0xda2(%rip),%rsi
jmp    12e2 <book_any+0x132>
mov    0x2d25(%rip),%rax
mov    (%rax),%edi
lea    0xd6a(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
jmp    12e9 <book_any+0x139>
lea    0xd84(%rip),%rdi
call   10a0 <perror@plt>
jmp    12e9 <book_any+0x139>
lea    0xd62(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    0x2ce8(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>

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

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