#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 netio {
  int dummy;
};
struct atrf_dsc {
  int dummy;
};
typedef size_t ssize_t;
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int /*<<< orphan*/ abort();
int /*<<< orphan*/ atrf_close(struct atrf_dsc *);
struct atrf_dsc *atrf_open(char const *);
int /*<<< orphan*/ cmd_more(struct atrf_dsc *, struct netio *, char *);
int /*<<< orphan*/ cmd_zero(struct atrf_dsc *, struct netio *, char *);
scalar_t__ netio_printf(struct netio *, char *, ...);
size_t netio_read_until(struct netio *, char *, char *, int, char *);
void session(const char *driver, struct netio *netio) {
  struct atrf_dsc *dsc;
  dsc = atrf_open(driver);
  if (!dsc) {
    netio_printf(netio, "-unable to open driver\n");
    return;
  }
  if (netio_printf(netio, "+connected to %s\n", driver ? driver : "default") <
      0)
    goto done;
  while (1) {
    char buf[100];
    ssize_t got;
    char last;
    got = netio_read_until(netio, " \n", buf, sizeof(buf), &last);
    if (got < 0)
      break;
    if (!got && last) {
      netio_printf(netio, "-empty input\n");
      continue;
    }
    buf[got] = 0;
    switch (last) {
    case ' ':
      if (cmd_more(dsc, netio, buf) < 0)
        goto done;
      break;
    case '\n':
      if (cmd_zero(dsc, netio, buf) < 0)
        goto done;
      break;
    case 0:
      goto done;
    default:
      abort();
    }
  }
done:
  atrf_close(dsc);
}


x86 assembly (clang, -O0):
<session>:
push   %rbp
mov    %rsp,%rbp
sub    $0xb0,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
call   1050 <atrf_open@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11b2 <session+0x42>
mov    -0x10(%rbp),%rdi
lea    0xe5a(%rip),%rsi
mov    $0x0,%al
call   1090 <netio_printf@plt>
jmp    133f <session+0x1cf>
mov    -0x10(%rbp),%rax
mov    %rax,-0x98(%rbp)
cmpq   $0x0,-0x8(%rbp)
je     11d8 <session+0x68>
mov    -0x8(%rbp),%rax
mov    %rax,-0xa0(%rbp)
jmp    11eb <session+0x7b>
lea    0xe4b(%rip),%rax
mov    %rax,-0xa0(%rbp)
jmp    11eb <session+0x7b>
mov    -0x98(%rbp),%rdi
mov    -0xa0(%rbp),%rdx
lea    0xe18(%rip),%rsi
mov    $0x0,%al
call   1090 <netio_printf@plt>
cmp    $0x0,%rax
jge    1216 <session+0xa6>
jmp    1336 <session+0x1c6>
jmp    121b <session+0xab>
mov    -0x10(%rbp),%rdi
lea    -0x80(%rbp),%rdx
lea    0xe08(%rip),%rsi
mov    $0x64,%ecx
lea    -0x89(%rbp),%r8
call   1080 <netio_read_until@plt>
mov    %rax,-0x88(%rbp)
cmpq   $0x0,-0x88(%rbp)
jae    1255 <session+0xe5>
jmp    1331 <session+0x1c1>
cmpq   $0x0,-0x88(%rbp)
jne    128a <session+0x11a>
movsbl -0x89(%rbp),%eax
cmp    $0x0,%eax
je     128a <session+0x11a>
mov    -0x10(%rbp),%rdi
lea    0xdb7(%rip),%rsi
mov    $0x0,%al
call   1090 <netio_printf@plt>
jmp    121b <session+0xab>
mov    -0x88(%rbp),%rax
movb   $0x0,-0x80(%rbp,%rax,1)
movsbl -0x89(%rbp),%eax
mov    %eax,-0xa4(%rbp)
test   %eax,%eax
je     1320 <session+0x1b0>
jmp    12b0 <session+0x140>
mov    -0xa4(%rbp),%eax
sub    $0xa,%eax
je     12fc <session+0x18c>
jmp    12c4 <session+0x154>
mov    -0xa4(%rbp),%eax
sub    $0x20,%eax
jne    1325 <session+0x1b5>
jmp    12d8 <session+0x168>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
lea    -0x80(%rbp),%rdx
call   1060 <cmd_more@plt>
cmp    $0x0,%eax
jge    12f7 <session+0x187>
jmp    1336 <session+0x1c6>
jmp    132c <session+0x1bc>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
lea    -0x80(%rbp),%rdx
call   1070 <cmd_zero@plt>
cmp    $0x0,%eax
jge    131b <session+0x1ab>
jmp    1336 <session+0x1c6>
jmp    132c <session+0x1bc>
jmp    1336 <session+0x1c6>
mov    $0x0,%al
call   1030 <abort@plt>
jmp    121b <session+0xab>
jmp    1336 <session+0x1c6>
mov    -0x18(%rbp),%rdi
call   1040 <atrf_close@plt>
add    $0xb0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<session>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x78,%rsp
mov    %rsi,%rbx
mov    %rdi,%rbp
call   1050 <atrf_open@plt>
test   %rax,%rax
je     1254 <session+0xe4>
mov    %rax,%r14
test   %rbp,%rbp
lea    0xe8b(%rip),%rdx
cmovne %rbp,%rdx
lea    0xe6e(%rip),%rsi
mov    %rbx,%rdi
xor    %eax,%eax
call   1090 <netio_printf@plt>
test   %rax,%rax
js     1277 <session+0x107>
lea    0xe6e(%rip),%r15
lea    0x10(%rsp),%r12
lea    0xf(%rsp),%r13
lea    0xe60(%rip),%rbp
jmp    11ed <session+0x7d>
nopw   0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    %rbp,%rsi
xor    %eax,%eax
call   1090 <netio_printf@plt>
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %r12,%rdx
mov    $0x64,%ecx
mov    %r13,%r8
call   1080 <netio_read_until@plt>
movzbl 0xf(%rsp),%ecx
test   %rax,%rax
jne    1211 <session+0xa1>
test   %cl,%cl
jne    11e0 <session+0x70>
movsbl %cl,%ecx
movb   $0x0,0x10(%rsp,%rax,1)
cmp    $0xa,%ecx
je     1240 <session+0xd0>
cmp    $0x20,%ecx
jne    1273 <session+0x103>
mov    %r14,%rdi
mov    %rbx,%rsi
mov    %r12,%rdx
call   1060 <cmd_more@plt>
test   %eax,%eax
jns    11ed <session+0x7d>
jmp    1277 <session+0x107>
nopw   0x0(%rax,%rax,1)
mov    %r14,%rdi
mov    %rbx,%rsi
mov    %r12,%rdx
call   1070 <cmd_zero@plt>
test   %eax,%eax
jns    11ed <session+0x7d>
jmp    1277 <session+0x107>
lea    0xda5(%rip),%rsi
mov    %rbx,%rdi
xor    %eax,%eax
add    $0x78,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1090 <netio_printf@plt>
test   %ecx,%ecx
jne    128e <session+0x11e>
mov    %r14,%rdi
call   1040 <atrf_close@plt>
add    $0x78,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
xor    %eax,%eax
call   1030 <abort@plt>

LLVM IR (-O0):
%struct.netio = type { i32 }
%struct.atrf_dsc = type { i32 }
@.str = [24 x i8] c"-unable to open driver\0A\00"
@.str.1 = [18 x i8] c"+connected to %s\0A\00"
@.str.2 = [8 x i8] c"default\00"
@.str.3 = [3 x i8] c" \0A\00"
@.str.4 = [14 x i8] c"-empty input\0A\00"
define void @session(i8* %0, %struct.netio* %1) {
%3 = alloca i8*
%4 = alloca %struct.netio*
%5 = alloca %struct.atrf_dsc*
%6 = alloca [100 x i8]
%7 = alloca i64
%8 = alloca i8
store i8* %0, i8** %3
store %struct.netio* %1, %struct.netio** %4
%9 = load i8*, i8** %3
%10 = call %struct.atrf_dsc* @atrf_open(i8* %9)
store %struct.atrf_dsc* %10, %struct.atrf_dsc** %5
%11 = load %struct.atrf_dsc*, %struct.atrf_dsc** %5
%12 = icmp ne %struct.atrf_dsc* %11, null
br i1 %12, label %16, label %13
13: ; preds = %2
%14 = load %struct.netio*, %struct.netio** %4
%15 = call i64 (%struct.netio*, i8*, ...) @netio_printf(%struct.netio* %14, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
br label %75
16: ; preds = %2
%17 = load %struct.netio*, %struct.netio** %4
%18 = load i8*, i8** %3
%19 = icmp ne i8* %18, null
br i1 %19, label %20, label %22
20: ; preds = %16
%21 = load i8*, i8** %3
br label %23
22: ; preds = %16
br label %23
23: ; preds = %22, %20
%24 = phi i8* [ %21, %20 ], [ getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), %22 ]
%25 = call i64 (%struct.netio*, i8*, ...) @netio_printf(%struct.netio* %17, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i8* %24)
%26 = icmp slt i64 %25, 0
br i1 %26, label %27, label %28
27: ; preds = %23
br label %72
28: ; preds = %23
br label %29
29: ; preds = %28, %43, %70
%30 = load %struct.netio*, %struct.netio** %4
%31 = getelementptr inbounds [100 x i8], [100 x i8]* %6, i64 0, i64 0
%32 = call i64 @netio_read_until(%struct.netio* %30, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* %31, i32 100, i8* %8)
store i64 %32, i64* %7
%33 = load i64, i64* %7
%34 = icmp ult i64 %33, 0
br i1 %34, label %35, label %36
35: ; preds = %29
br label %71
36: ; preds = %29
%37 = load i64, i64* %7
%38 = icmp ne i64 %37, 0
br i1 %38, label %46, label %39
39: ; preds = %36
%40 = load i8, i8* %8
%41 = sext i8 %40 to i32
%42 = icmp ne i32 %41, 0
br i1 %42, label %43, label %46
43: ; preds = %39
%44 = load %struct.netio*, %struct.netio** %4
%45 = call i64 (%struct.netio*, i8*, ...) @netio_printf(%struct.netio* %44, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0))
br label %29
46: ; preds = %39, %36
%47 = load i64, i64* %7
%48 = getelementptr inbounds [100 x i8], [100 x i8]* %6, i64 0, i64 %47
store i8 0, i8* %48
%49 = load i8, i8* %8
%50 = sext i8 %49 to i32
switch i32 %50, label %68 [
i32 32, label %51
i32 10, label %59
i32 0, label %67
]
51: ; preds = %46
%52 = load %struct.atrf_dsc*, %struct.atrf_dsc** %5
%53 = load %struct.netio*, %struct.netio** %4
%54 = getelementptr inbounds [100 x i8], [100 x i8]* %6, i64 0, i64 0
%55 = call i32 @cmd_more(%struct.atrf_dsc* %52, %struct.netio* %53, i8* %54)
%56 = icmp slt i32 %55, 0
br i1 %56, label %57, label %58
57: ; preds = %51
br label %72
58: ; preds = %51
br label %70
59: ; preds = %46
%60 = load %struct.atrf_dsc*, %struct.atrf_dsc** %5
%61 = load %struct.netio*, %struct.netio** %4
%62 = getelementptr inbounds [100 x i8], [100 x i8]* %6, i64 0, i64 0
%63 = call i32 @cmd_zero(%struct.atrf_dsc* %60, %struct.netio* %61, i8* %62)
%64 = icmp slt i32 %63, 0
br i1 %64, label %65, label %66
65: ; preds = %59
br label %72
66: ; preds = %59
br label %70
67: ; preds = %46
br label %72
68: ; preds = %46
%69 = call i32 (...) @abort()
unreachable
70: ; preds = %66, %58
br label %29
71: ; preds = %35
br label %72
72: ; preds = %71, %67, %65, %57, %27
%73 = load %struct.atrf_dsc*, %struct.atrf_dsc** %5
%74 = call i32 @atrf_close(%struct.atrf_dsc* %73)
br label %75
75: ; preds = %72, %13
ret void
}

LLVM IR (-O2):
%struct.netio = type { i32 }
%struct.atrf_dsc = type { i32 }
@.str = [24 x i8] c"-unable to open driver\0A\00"
@.str.1 = [18 x i8] c"+connected to %s\0A\00"
@.str.2 = [8 x i8] c"default\00"
@.str.3 = [3 x i8] c" \0A\00"
@.str.4 = [14 x i8] c"-empty input\0A\00"
define void @session(i8* %0, %struct.netio* %1) {
%3 = alloca [100 x i8]
%4 = alloca i8
%5 = tail call %struct.atrf_dsc* @atrf_open(i8* %0)
%6 = icmp eq %struct.atrf_dsc* %5, null
br i1 %6, label %7, label %9
7: ; preds = %2
%8 = tail call i64 (%struct.netio*, i8*, ...) @netio_printf(%struct.netio* %1, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
br label %39
9: ; preds = %2
%10 = icmp eq i8* %0, null
%11 = select i1 %10, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i8* %0
%12 = tail call i64 (%struct.netio*, i8*, ...) @netio_printf(%struct.netio* %1, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i8* %11)
%13 = icmp slt i64 %12, 0
br i1 %13, label %37, label %14
14: ; preds = %9
%15 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 0
br label %16
16: ; preds = %14, %36
%17 = call i64 @netio_read_until(%struct.netio* %1, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* %15, i32 100, i8* %4)
%18 = icmp eq i64 %17, 0
%19 = load i8, i8* %4
%20 = icmp ne i8 %19, 0
%21 = select i1 %18, i1 %20, i1 false
br i1 %21, label %22, label %24
22: ; preds = %16
%23 = call i64 (%struct.netio*, i8*, ...) @netio_printf(%struct.netio* %1, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0))
br label %36
24: ; preds = %16
%25 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 %17
store i8 0, i8* %25
%26 = sext i8 %19 to i32
switch i32 %26, label %33 [
i32 32, label %27
i32 10, label %30
i32 0, label %35
]
27: ; preds = %24
%28 = call i32 @cmd_more(%struct.atrf_dsc* %5, %struct.netio* %1, i8* %15)
%29 = icmp slt i32 %28, 0
br i1 %29, label %35, label %36
30: ; preds = %24
%31 = call i32 @cmd_zero(%struct.atrf_dsc* %5, %struct.netio* %1, i8* %15)
%32 = icmp slt i32 %31, 0
br i1 %32, label %35, label %36
33: ; preds = %24
%34 = call i32 (...) @abort()
unreachable
35: ; preds = %27, %30, %24
br label %37
36: ; preds = %27, %30, %22
br label %16
37: ; preds = %35, %9
%38 = call i32 @atrf_close(%struct.atrf_dsc* %5)
br label %39
39: ; preds = %37, %7
ret void
}