#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 snis_device_io_connection {
  int dummy;
};
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
char *fgets(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *);
int snis_device_io_send(struct snis_device_io_connection *, unsigned short,
                        unsigned short);
int snis_device_io_setup(struct snis_device_io_connection **);
int /*<<< orphan*/ snis_device_io_shutdown(struct snis_device_io_connection *);
int sscanf(char *, char *, unsigned short *, unsigned short *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ stdout;
char *strerror(int /*<<< orphan*/);
scalar_t__ strncmp(char *, char *, int);
int main(__attribute__((unused)) int argc,
         __attribute__((unused)) char *argv[]) {
  struct snis_device_io_connection *con = NULL;
  char *c;
  char buf[100];
  unsigned short opcode, value;
  int rc;
  rc = snis_device_io_setup(&con);
  if (rc < 0) {
    fprintf(stderr, "Failed to setup SNIS device io.\n");
    return -1;
  }
  for (;;) {
    printf("Enter device i/o opcode and value, 'q' to exit: ");
    fflush(stdout);
    memset(buf, 0, sizeof(buf));
    c = fgets(buf, sizeof(buf) - 1, stdin);
    if (!c)
      goto done;
    if (strncmp(c, "q", 1) == 0)
      goto done;
    rc = sscanf(buf, "%hu %hu", &opcode, &value);
    if (rc != 2) {
      fprintf(stderr, "Syntax error, try again\n");
      continue;
    }
    rc = snis_device_io_send(con, opcode, value);
    if (rc) {
      fprintf(stderr, "Error sending device io: %s\n", strerror(errno));
    }
  }
done:
  if (con)
    snis_device_io_shutdown(con);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0xa0,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
lea    -0x18(%rbp),%rdi
call   10b0 <snis_device_io_setup@plt>
mov    %eax,-0x98(%rbp)
cmpl   $0x0,-0x98(%rbp)
jge    1210 <main+0x60>
mov    0x2de4(%rip),%rax
mov    (%rax),%edi
lea    0xe03(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1365 <main+0x1b5>
jmp    1215 <main+0x65>
lea    0xe05(%rip),%rdi
call   1040 <printf@plt>
mov    0x2da0(%rip),%rax
mov    (%rax),%edi
call   1080 <fflush@plt>
lea    -0x90(%rbp),%rdi
xor    %esi,%esi
mov    $0x64,%edx
call   1050 <memset@plt>
lea    -0x90(%rbp),%rdi
mov    0x2d98(%rip),%rax
mov    (%rax),%edx
mov    $0x63,%esi
call   1060 <fgets@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1270 <main+0xc0>
jmp    134a <main+0x19a>
mov    -0x20(%rbp),%rdi
lea    0xdd7(%rip),%rsi
mov    $0x1,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%rax
jne    1294 <main+0xe4>
jmp    134a <main+0x19a>
lea    -0x90(%rbp),%rdi
lea    0xdb2(%rip),%rsi
lea    -0x92(%rbp),%rdx
lea    -0x94(%rbp),%rcx
call   1090 <sscanf@plt>
mov    %eax,-0x98(%rbp)
cmpl   $0x2,-0x98(%rbp)
je     12e4 <main+0x134>
mov    0x2d09(%rip),%rax
mov    (%rax),%edi
lea    0xd84(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
jmp    1215 <main+0x65>
mov    -0x18(%rbp),%rdi
mov    -0x92(%rbp),%ax
movzwl %ax,%esi
movzwl -0x94(%rbp),%edx
call   10c0 <snis_device_io_send@plt>
mov    %eax,-0x98(%rbp)
cmpl   $0x0,-0x98(%rbp)
je     1345 <main+0x195>
mov    0x2cc0(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x9c(%rbp)
mov    0x2ca9(%rip),%rax
mov    (%rax),%edi
call   10d0 <strerror@plt>
mov    -0x9c(%rbp),%edi
mov    %rax,%rdx
lea    0xd37(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
jmp    1215 <main+0x65>
cmpq   $0x0,-0x18(%rbp)
je     135e <main+0x1ae>
mov    -0x18(%rbp),%rdi
call   10a0 <snis_device_io_shutdown@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0xa0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x78,%rsp
movq   $0x0,0x8(%rsp)
lea    0x8(%rsp),%rdi
call   10b0 <snis_device_io_setup@plt>
test   %eax,%eax
js     1342 <main+0x192>
lea    0xe41(%rip),%rdi
call   1040 <printf@plt>
mov    0x2ddc(%rip),%rbx
mov    (%rbx),%edi
call   1080 <fflush@plt>
lea    0x10(%rsp),%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x64,%edx
call   1050 <memset@plt>
mov    0x2dda(%rip),%rbp
mov    0x0(%rbp),%edx
mov    %r14,%rdi
mov    $0x63,%esi
call   1060 <fgets@plt>
test   %rax,%rax
je     132f <main+0x17f>
lea    0xe24(%rip),%r15
lea    0xe1f(%rip),%r12
lea    0x10(%rsp),%r13
lea    0x6(%rsp),%r14
jmp    12a2 <main+0xf2>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    0x2d81(%rip),%rax
mov    (%rax),%edi
lea    0xdfc(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
lea    0xdb3(%rip),%rdi
call   1040 <printf@plt>
mov    (%rbx),%edi
call   1080 <fflush@plt>
mov    %r13,%rdi
xor    %esi,%esi
mov    $0x64,%edx
call   1050 <memset@plt>
mov    0x0(%rbp),%edx
mov    %r13,%rdi
mov    $0x63,%esi
call   1060 <fgets@plt>
test   %rax,%rax
je     132f <main+0x17f>
mov    %rax,%rdi
mov    %r15,%rsi
mov    $0x1,%edx
call   1030 <strncmp@plt>
test   %rax,%rax
je     132f <main+0x17f>
mov    %r13,%rdi
mov    %r12,%rsi
mov    %r14,%rdx
lea    0x4(%rsp),%rcx
call   1090 <sscanf@plt>
cmp    $0x2,%eax
jne    1250 <main+0xa0>
mov    0x8(%rsp),%rdi
movzwl 0x6(%rsp),%esi
movzwl 0x4(%rsp),%edx
call   10c0 <snis_device_io_send@plt>
test   %eax,%eax
je     1267 <main+0xb7>
mov    %r14,%r13
mov    %r12,%r14
mov    %r15,%r12
mov    0x2ce1(%rip),%rax
mov    (%rax),%r15d
mov    0x2ccf(%rip),%rax
mov    (%rax),%edi
call   10d0 <strerror@plt>
mov    %r15d,%edi
mov    %r12,%r15
mov    %r14,%r12
mov    %r13,%r14
lea    0x10(%rsp),%r13
lea    0xd55(%rip),%rsi
mov    %rax,%rdx
xor    %eax,%eax
call   1070 <fprintf@plt>
jmp    1267 <main+0xb7>
mov    0x8(%rsp),%rdi
test   %rdi,%rdi
je     133e <main+0x18e>
call   10a0 <snis_device_io_shutdown@plt>
xor    %eax,%eax
jmp    135e <main+0x1ae>
mov    0x2c8f(%rip),%rax
mov    (%rax),%edi
lea    0xcae(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    $0xffffffff,%eax
add    $0x78,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.snis_device_io_connection = type { i32 }
@stderr = global i32 0
@.str = [33 x i8] c"Failed to setup SNIS device io.\0A\00"
@.str.1 = [49 x i8] c"Enter device i/o opcode and value, 'q' to exit: \00"
@stdout = global i32 0
@stdin = global i32 0
@.str.2 = [2 x i8] c"q\00"
@.str.3 = [8 x i8] c"%hu %hu\00"
@.str.4 = [25 x i8] c"Syntax error, try again\0A\00"
@.str.5 = [29 x i8] c"Error sending device io: %s\0A\00"
@errno = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca %struct.snis_device_io_connection*
%7 = alloca i8*
%8 = alloca [100 x i8]
%9 = alloca i16
%10 = alloca i16
%11 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store %struct.snis_device_io_connection* null, %struct.snis_device_io_connection** %6
%12 = call i32 @snis_device_io_setup(%struct.snis_device_io_connection** %6)
store i32 %12, i32* %11
%13 = load i32, i32* %11
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %18
15: ; preds = %2
%16 = load i32, i32* @stderr
%17 = call i32 (i32, i8*, ...) @fprintf(i32 %16, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %64
18: ; preds = %2
br label %19
19: ; preds = %56, %41, %18
%20 = call i32 @printf(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.1, i64 0, i64 0))
%21 = load i32, i32* @stdout
%22 = call i32 @fflush(i32 %21)
%23 = getelementptr inbounds [100 x i8], [100 x i8]* %8, i64 0, i64 0
%24 = call i32 @memset(i8* %23, i32 0, i32 100)
%25 = getelementptr inbounds [100 x i8], [100 x i8]* %8, i64 0, i64 0
%26 = load i32, i32* @stdin
%27 = call i8* @fgets(i8* %25, i32 99, i32 %26)
store i8* %27, i8** %7
%28 = load i8*, i8** %7
%29 = icmp ne i8* %28, null
br i1 %29, label %31, label %30
30: ; preds = %19
br label %57
31: ; preds = %19
%32 = load i8*, i8** %7
%33 = call i64 @strncmp(i8* %32, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i32 1)
%34 = icmp eq i64 %33, 0
br i1 %34, label %35, label %36
35: ; preds = %31
br label %57
36: ; preds = %31
%37 = getelementptr inbounds [100 x i8], [100 x i8]* %8, i64 0, i64 0
%38 = call i32 @sscanf(i8* %37, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i16* %9, i16* %10)
store i32 %38, i32* %11
%39 = load i32, i32* %11
%40 = icmp ne i32 %39, 2
br i1 %40, label %41, label %44
41: ; preds = %36
%42 = load i32, i32* @stderr
%43 = call i32 (i32, i8*, ...) @fprintf(i32 %42, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0))
br label %19
44: ; preds = %36
%45 = load %struct.snis_device_io_connection*, %struct.snis_device_io_connection** %6
%46 = load i16, i16* %9
%47 = load i16, i16* %10
%48 = call i32 @snis_device_io_send(%struct.snis_device_io_connection* %45, i16 zeroext %46, i16 zeroext %47)
store i32 %48, i32* %11
%49 = load i32, i32* %11
%50 = icmp ne i32 %49, 0
br i1 %50, label %51, label %56
51: ; preds = %44
%52 = load i32, i32* @stderr
%53 = load i32, i32* @errno
%54 = call i8* @strerror(i32 %53)
%55 = call i32 (i32, i8*, ...) @fprintf(i32 %52, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.5, i64 0, i64 0), i8* %54)
br label %56
56: ; preds = %51, %44
br label %19
57: ; preds = %35, %30
%58 = load %struct.snis_device_io_connection*, %struct.snis_device_io_connection** %6
%59 = icmp ne %struct.snis_device_io_connection* %58, null
br i1 %59, label %60, label %63
60: ; preds = %57
%61 = load %struct.snis_device_io_connection*, %struct.snis_device_io_connection** %6
%62 = call i32 @snis_device_io_shutdown(%struct.snis_device_io_connection* %61)
br label %63
63: ; preds = %60, %57
store i32 0, i32* %3
br label %64
64: ; preds = %63, %15
%65 = load i32, i32* %3
ret i32 %65
}

LLVM IR (-O2):
%struct.snis_device_io_connection = type { i32 }
@stderr = global i32 0
@.str = [33 x i8] c"Failed to setup SNIS device io.\0A\00"
@.str.1 = [49 x i8] c"Enter device i/o opcode and value, 'q' to exit: \00"
@stdout = global i32 0
@stdin = global i32 0
@.str.2 = [2 x i8] c"q\00"
@.str.3 = [8 x i8] c"%hu %hu\00"
@.str.4 = [25 x i8] c"Syntax error, try again\0A\00"
@.str.5 = [29 x i8] c"Error sending device io: %s\0A\00"
@errno = global i32 0
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca %struct.snis_device_io_connection*
%4 = alloca [100 x i8]
%5 = alloca i16
%6 = alloca i16
%7 = bitcast %struct.snis_device_io_connection** %3 to i8*
store %struct.snis_device_io_connection* null, %struct.snis_device_io_connection** %3
%8 = getelementptr inbounds [100 x i8], [100 x i8]* %4, i64 0, i64 0
%9 = bitcast i16* %5 to i8*
%10 = bitcast i16* %6 to i8*
%11 = call i32 @snis_device_io_setup(%struct.snis_device_io_connection** %3)
%12 = icmp slt i32 %11, 0
br i1 %12, label %21, label %13
13: ; preds = %2
%14 = call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([49 x i8], [49 x i8]* @.str.1, i64 0, i64 0))
%15 = load i32, i32* @stdout
%16 = call i32 @fflush(i32 %15)
%17 = call i32 @memset(i8* %8, i32 0, i32 100)
%18 = load i32, i32* @stdin
%19 = call i8* @fgets(i8* %8, i32 99, i32 %18)
%20 = icmp eq i8* %19, null
br i1 %20, label %53, label %24
21: ; preds = %2
%22 = load i32, i32* @stderr
%23 = call i32 (i32, i8*, ...) @fprintf(i32 %22, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0))
br label %58
24: ; preds = %13, %34
%25 = phi i8* [ %40, %34 ], [ %19, %13 ]
%26 = call i64 @strncmp(i8* %25, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i32 1)
%27 = icmp eq i64 %26, 0
br i1 %27, label %53, label %28
28: ; preds = %24
%29 = call i32 @sscanf(i8* %8, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i16* %5, i16* %6)
%30 = icmp eq i32 %29, 2
br i1 %30, label %42, label %31
31: ; preds = %28
%32 = load i32, i32* @stderr
%33 = call i32 (i32, i8*, ...) @fprintf(i32 %32, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0))
br label %34
34: ; preds = %42, %48, %31
%35 = call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([49 x i8], [49 x i8]* @.str.1, i64 0, i64 0))
%36 = load i32, i32* @stdout
%37 = call i32 @fflush(i32 %36)
%38 = call i32 @memset(i8* %8, i32 0, i32 100)
%39 = load i32, i32* @stdin
%40 = call i8* @fgets(i8* %8, i32 99, i32 %39)
%41 = icmp eq i8* %40, null
br i1 %41, label %53, label %24
42: ; preds = %28
%43 = load %struct.snis_device_io_connection*, %struct.snis_device_io_connection** %3
%44 = load i16, i16* %5
%45 = load i16, i16* %6
%46 = call i32 @snis_device_io_send(%struct.snis_device_io_connection* %43, i16 zeroext %44, i16 zeroext %45)
%47 = icmp eq i32 %46, 0
br i1 %47, label %34, label %48
48: ; preds = %42
%49 = load i32, i32* @stderr
%50 = load i32, i32* @errno
%51 = call i8* @strerror(i32 %50)
%52 = call i32 (i32, i8*, ...) @fprintf(i32 %49, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.5, i64 0, i64 0), i8* %51)
br label %34
53: ; preds = %34, %24, %13
%54 = load %struct.snis_device_io_connection*, %struct.snis_device_io_connection** %3
%55 = icmp eq %struct.snis_device_io_connection* %54, null
br i1 %55, label %58, label %56
56: ; preds = %53
%57 = call i32 @snis_device_io_shutdown(%struct.snis_device_io_connection* %54)
br label %58
58: ; preds = %53, %56, %21
%59 = phi i32 [ -1, %21 ], [ 0, %56 ], [ 0, %53 ]
ret i32 %59
}