#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 *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ print_availability(char *);
char *read_line(int);
int /*<<< orphan*/ stderr;
scalar_t__ strlen(char *);
int /*<<< orphan*/ write_line(int, char *);
void list(char *hostname) {
  int server_socket;
  char *request = "list\n\n";
  char *line;
  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);
  }
  do {
    free(line);
    line = read_line(server_socket);
    if (!line) {
      fprintf(stderr, "Cannot read response\n");
      exit(EXIT_FAILURE);
    }
    if (strlen(line) > 0) {
      print_availability(line);
    }
  } while (strlen(line) > 0);
  free(line);
  if (close(server_socket) == -1) {
    perror("Error closing connection");
    exit(EXIT_FAILURE);
  }
}


x86 assembly (clang, -O0):
<list>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
lea    0xe4d(%rip),%rax
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
call   10a0 <connect_to_gateway@plt>
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%edi
mov    -0x18(%rbp),%rsi
call   10c0 <write_line@plt>
mov    -0xc(%rbp),%edi
call   1080 <read_line@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    120b <list+0x6b>
mov    0x2df3(%rip),%rax
mov    (%rax),%edi
lea    0xe11(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    0x2dd4(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x30,%eax
je     12ab <list+0x10b>
mov    -0x20(%rbp),%rdi
call   1040 <strlen@plt>
cmp    $0x2,%rax
jle    1286 <list+0xe6>
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x31,%eax
jne    1262 <list+0xc2>
mov    0x2d9b(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%rdx
add    $0x2,%rdx
lea    0xdd7(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
jmp    1281 <list+0xe1>
mov    0x2d77(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%rdx
add    $0x2,%rdx
lea    0xda3(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
jmp    129d <list+0xfd>
mov    0x2d53(%rip),%rax
mov    (%rax),%edi
lea    0xd71(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    0x2d34(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
jmp    12b0 <list+0x110>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0xc(%rbp),%edi
call   1080 <read_line@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    12f5 <list+0x155>
mov    0x2d09(%rip),%rax
mov    (%rax),%edi
lea    0xd27(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    0x2cea(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
mov    -0x20(%rbp),%rdi
call   1040 <strlen@plt>
cmp    $0x0,%rax
jle    1311 <list+0x171>
mov    -0x20(%rbp),%rdi
call   1050 <print_availability@plt>
jmp    1316 <list+0x176>
mov    -0x20(%rbp),%rdi
call   1040 <strlen@plt>
cmp    $0x0,%rax
jg     12b0 <list+0x110>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0xc(%rbp),%edi
call   1060 <close@plt>
cmp    $0xffffffff,%eax
jne    135d <list+0x1bd>
lea    0xce7(%rip),%rdi
call   1090 <perror@plt>
mov    0x2c82(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<list>:
push   %rbp
push   %r14
push   %rbx
call   10a0 <connect_to_gateway@plt>
mov    %eax,%ebp
lea    0xe4e(%rip),%rsi
mov    %eax,%edi
call   10c0 <write_line@plt>
mov    %ebp,%edi
call   1080 <read_line@plt>
test   %rax,%rax
je     1264 <list+0xc4>
mov    %rax,%rbx
mov    (%rax),%r14b
cmp    $0x30,%r14b
je     121d <list+0x7d>
mov    %rbx,%rdi
call   1040 <strlen@plt>
cmp    $0x3,%rax
jl     1264 <list+0xc4>
add    $0x2,%rbx
mov    0x2dee(%rip),%rax
mov    (%rax),%edi
cmp    $0x31,%r14b
jne    1297 <list+0xf7>
lea    0xe28(%rip),%rsi
jmp    129e <list+0xfe>
nopw   0x0(%rax,%rax,1)
mov    %rbx,%rdi
call   1040 <strlen@plt>
test   %rax,%rax
jle    124b <list+0xab>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %ebp,%edi
call   1080 <read_line@plt>
test   %rax,%rax
je     1264 <list+0xc4>
mov    %rax,%rbx
mov    %rax,%rdi
call   1040 <strlen@plt>
test   %rax,%rax
jle    1210 <list+0x70>
mov    %rbx,%rdi
call   1050 <print_availability@plt>
jmp    1210 <list+0x70>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %ebp,%edi
call   1060 <close@plt>
cmp    $0xffffffff,%eax
je     1289 <list+0xe9>
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    0x2d75(%rip),%rax
mov    (%rax),%edi
lea    0xd93(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    0x2d56(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
lea    0xda1(%rip),%rdi
call   1090 <perror@plt>
jmp    127b <list+0xdb>
lea    0xd7f(%rip),%rsi
mov    %rbx,%rdx
xor    %eax,%eax
call   1070 <fprintf@plt>
jmp    127b <list+0xdb>

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

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