#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*/ buffer;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
#define EINTR 128
int errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
char *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ interpret_command(char *,
                                     struct snis_device_io_connection *);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int snis_device_io_setup(struct snis_device_io_connection **);
int /*<<< orphan*/ stderr;
char *strerror(int);
int /*<<< orphan*/ usage();
int /*<<< orphan*/ usleep(int);
int main(int argc, char *argv[]) {
  int rc;
  char *device, *s;
  FILE *d;
  char buffer[1024];
  struct snis_device_io_connection *snis_client;
  fprintf(stderr, "snis_arduino begins\n");
  rc = snis_device_io_setup(&snis_client);
  if (rc != 0) {
    fprintf(stderr, "snis_arduino: snis_device_io_setup() failed\n");
    exit(1);
  }
  if (argc < 2)
    usage();
  device = argv[1];
  d = fopen(device, "rw");
  if (!d) {
    fprintf(stderr, "snis_arduino: Failed to open %s for read/write\n", device);
    return -1;
  }
  fprintf(stderr, "snis_arduino: opened %s\n", device);
  do {
    memset(buffer, 0, sizeof(buffer));
    errno = 0;
    s = fgets(buffer, sizeof(buffer), d);
    if (!s) {
      switch (errno) {
      case 0:
        usleep(100000);
        continue;
      case EINTR:
        continue;
      default:
        break;
      }
      fprintf(stderr, "snis_arduino: Error on device %s: %s\n", device,
              strerror(errno));
      goto bailout;
    }
    interpret_command(buffer, snis_client);
  } while (1);
bailout:
  fclose(d);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x450,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e10(%rip),%rax
mov    (%rax),%edi
lea    0xe27(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
lea    -0x438(%rbp),%rdi
call   10a0 <snis_device_io_setup@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     121a <main+0x6a>
mov    0x2de0(%rip),%rax
mov    (%rax),%edi
lea    0xe0c(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
cmpl   $0x2,-0x8(%rbp)
jge    122b <main+0x7b>
mov    $0x0,%al
call   1080 <usage@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
lea    0xe00(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    127d <main+0xcd>
mov    0x2d83(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%rdx
lea    0xddb(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13a9 <main+0x1f9>
mov    0x2d5c(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%rdx
lea    0xde4(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
lea    -0x430(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1050 <memset@plt>
mov    0x2d26(%rip),%rax
movl   $0x0,(%rax)
lea    -0x430(%rbp),%rdi
mov    -0x30(%rbp),%rdx
mov    $0x400,%esi
call   1060 <fgets@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1372 <main+0x1c2>
mov    0x2cf5(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x43c(%rbp)
test   %eax,%eax
je     130e <main+0x15e>
jmp    12f8 <main+0x148>
mov    -0x43c(%rbp),%eax
sub    $0x80,%eax
je     131d <main+0x16d>
jmp    1322 <main+0x172>
mov    $0x186a0,%edi
call   10d0 <usleep@plt>
jmp    1385 <main+0x1d5>
jmp    1385 <main+0x1d5>
jmp    1327 <main+0x177>
mov    0x2cb2(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x44c(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x448(%rbp)
mov    0x2c90(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
mov    -0x44c(%rbp),%edi
mov    -0x448(%rbp),%rdx
mov    %rax,%rcx
lea    0xd28(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
jmp    1399 <main+0x1e9>
lea    -0x430(%rbp),%rdi
mov    -0x438(%rbp),%rsi
call   1040 <interpret_command@plt>
mov    $0x1,%al
test   $0x1,%al
jne    1298 <main+0xe8>
jmp    1394 <main+0x1e4>
jmp    1399 <main+0x1e9>
mov    -0x30(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x450,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x410,%rsp
mov    %rsi,%rbx
mov    %edi,%ebp
mov    0x2e15(%rip),%r12
mov    (%r12),%edi
lea    0xe2a(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
lea    0x8(%rsp),%rdi
call   10a0 <snis_device_io_setup@plt>
test   %eax,%eax
jne    12e6 <main+0x136>
cmp    $0x1,%ebp
jg     11fb <main+0x4b>
xor    %eax,%eax
call   1080 <usage@plt>
mov    0x8(%rbx),%r14
lea    0xe3c(%rip),%rsi
mov    %r14,%rdi
call   1090 <fopen@plt>
mov    (%r12),%edi
test   %rax,%rax
je     12be <main+0x10e>
mov    %rax,%r15
lea    0xe50(%rip),%rsi
mov    %r14,%rdx
xor    %eax,%eax
call   1070 <fprintf@plt>
lea    0x10(%rsp),%rbx
mov    0x2d9d(%rip),%rbp
jmp    124d <main+0x9d>
nopl   (%rax)
mov    0x8(%rsp),%rsi
mov    %rbx,%rdi
call   1040 <interpret_command@plt>
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1050 <memset@plt>
movl   $0x0,0x0(%rbp)
mov    %rbx,%rdi
mov    $0x400,%esi
mov    %r15,%rdx
call   1060 <fgets@plt>
test   %rax,%rax
jne    1240 <main+0x90>
mov    0x0(%rbp),%edi
cmp    $0x80,%edi
je     124d <main+0x9d>
test   %edi,%edi
jne    1293 <main+0xe3>
mov    $0x186a0,%edi
call   10d0 <usleep@plt>
jmp    124d <main+0x9d>
mov    (%r12),%ebp
call   10c0 <strerror@plt>
lea    0xdeb(%rip),%rsi
xor    %ebx,%ebx
mov    %ebp,%edi
mov    %r14,%rdx
mov    %rax,%rcx
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    %r15,%rdi
call   1030 <fclose@plt>
jmp    12d4 <main+0x124>
lea    0xd80(%rip),%rsi
mov    %r14,%rdx
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x410,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    (%r12),%edi
lea    0xd24(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
%struct.snis_device_io_connection = type { i32 }
@stderr = global i32 0
@.str = [21 x i8] c"snis_arduino begins\0A\00"
@.str.1 = [45 x i8] c"snis_arduino: snis_device_io_setup() failed\0A\00"
@.str.2 = [3 x i8] c"rw\00"
@.str.3 = [48 x i8] c"snis_arduino: Failed to open %s for read/write\0A\00"
@.str.4 = [25 x i8] c"snis_arduino: opened %s\0A\00"
@errno = global i32 0
@.str.5 = [38 x i8] c"snis_arduino: Error on device %s: %s\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i32*
%10 = alloca [1024 x i8]
%11 = alloca %struct.snis_device_io_connection*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = load i32, i32* @stderr
%13 = call i32 (i32, i8*, ...) @fprintf(i32 %12, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
%14 = call i32 @snis_device_io_setup(%struct.snis_device_io_connection** %11)
store i32 %14, i32* %6
%15 = load i32, i32* %6
%16 = icmp ne i32 %15, 0
br i1 %16, label %17, label %21
17: ; preds = %2
%18 = load i32, i32* @stderr
%19 = call i32 (i32, i8*, ...) @fprintf(i32 %18, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.1, i64 0, i64 0))
%20 = call i32 @exit(i32 1)
unreachable
21: ; preds = %2
%22 = load i32, i32* %4
%23 = icmp slt i32 %22, 2
br i1 %23, label %24, label %26
24: ; preds = %21
%25 = call i32 (...) @usage()
br label %26
26: ; preds = %24, %21
%27 = load i8**, i8*** %5
%28 = getelementptr inbounds i8*, i8** %27, i64 1
%29 = load i8*, i8** %28
store i8* %29, i8** %7
%30 = load i8*, i8** %7
%31 = call i32* @fopen(i8* %30, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
store i32* %31, i32** %9
%32 = load i32*, i32** %9
%33 = icmp ne i32* %32, null
br i1 %33, label %38, label %34
34: ; preds = %26
%35 = load i32, i32* @stderr
%36 = load i8*, i8** %7
%37 = call i32 (i32, i8*, ...) @fprintf(i32 %35, i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.3, i64 0, i64 0), i8* %36)
store i32 -1, i32* %3
br label %71
38: ; preds = %26
%39 = load i32, i32* @stderr
%40 = load i8*, i8** %7
%41 = call i32 (i32, i8*, ...) @fprintf(i32 %39, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0), i8* %40)
br label %42
42: ; preds = %66, %38
%43 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 0
%44 = call i32 @memset(i8* %43, i32 0, i32 1024)
store i32 0, i32* @errno
%45 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 0
%46 = load i32*, i32** %9
%47 = call i8* @fgets(i8* %45, i32 1024, i32* %46)
store i8* %47, i8** %8
%48 = load i8*, i8** %8
%49 = icmp ne i8* %48, null
br i1 %49, label %62, label %50
50: ; preds = %42
%51 = load i32, i32* @errno
switch i32 %51, label %55 [
i32 0, label %52
i32 128, label %54
]
52: ; preds = %50
%53 = call i32 @usleep(i32 100000)
br label %66
54: ; preds = %50
br label %66
55: ; preds = %50
br label %56
56: ; preds = %55
%57 = load i32, i32* @stderr
%58 = load i8*, i8** %7
%59 = load i32, i32* @errno
%60 = call i8* @strerror(i32 %59)
%61 = call i32 (i32, i8*, ...) @fprintf(i32 %57, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.5, i64 0, i64 0), i8* %58, i8* %60)
br label %68
62: ; preds = %42
%63 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 0
%64 = load %struct.snis_device_io_connection*, %struct.snis_device_io_connection** %11
%65 = call i32 @interpret_command(i8* %63, %struct.snis_device_io_connection* %64)
br label %66
66: ; preds = %62, %54, %52
br i1 true, label %42, label %67
67: ; preds = %66
br label %68
68: ; preds = %67, %56
%69 = load i32*, i32** %9
%70 = call i32 @fclose(i32* %69)
store i32 0, i32* %3
br label %71
71: ; preds = %68, %34
%72 = load i32, i32* %3
ret i32 %72
}

LLVM IR (-O2):
%struct.snis_device_io_connection = type { i32 }
@stderr = global i32 0
@.str = [21 x i8] c"snis_arduino begins\0A\00"
@.str.1 = [45 x i8] c"snis_arduino: snis_device_io_setup() failed\0A\00"
@.str.2 = [3 x i8] c"rw\00"
@.str.3 = [48 x i8] c"snis_arduino: Failed to open %s for read/write\0A\00"
@.str.4 = [25 x i8] c"snis_arduino: opened %s\0A\00"
@errno = global i32 0
@.str.5 = [38 x i8] c"snis_arduino: Error on device %s: %s\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [1024 x i8]
%4 = alloca %struct.snis_device_io_connection*
%5 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%6 = bitcast %struct.snis_device_io_connection** %4 to i8*
%7 = load i32, i32* @stderr
%8 = tail call i32 (i32, i8*, ...) @fprintf(i32 %7, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
%9 = call i32 @snis_device_io_setup(%struct.snis_device_io_connection** %4)
%10 = icmp eq i32 %9, 0
br i1 %10, label %15, label %11
11: ; preds = %2
%12 = load i32, i32* @stderr
%13 = call i32 (i32, i8*, ...) @fprintf(i32 %12, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.1, i64 0, i64 0))
%14 = call i32 @exit(i32 1)
unreachable
15: ; preds = %2
%16 = icmp slt i32 %0, 2
br i1 %16, label %17, label %19
17: ; preds = %15
%18 = call i32 (...) @usage()
br label %19
19: ; preds = %17, %15
%20 = getelementptr inbounds i8*, i8** %1, i64 1
%21 = load i8*, i8** %20
%22 = call i32* @fopen(i8* %21, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%23 = icmp eq i32* %22, null
%24 = load i32, i32* @stderr
br i1 %23, label %25, label %27
25: ; preds = %19
%26 = call i32 (i32, i8*, ...) @fprintf(i32 %24, i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.3, i64 0, i64 0), i8* %21)
br label %46
27: ; preds = %19
%28 = call i32 (i32, i8*, ...) @fprintf(i32 %24, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0), i8* %21)
br label %29
29: ; preds = %35, %27
%30 = call i32 @memset(i8* %5, i32 0, i32 1024)
store i32 0, i32* @errno
%31 = call i8* @fgets(i8* %5, i32 1024, i32* %22)
%32 = icmp eq i8* %31, null
br i1 %32, label %33, label %43
33: ; preds = %29
%34 = load i32, i32* @errno
switch i32 %34, label %38 [
i32 0, label %36
i32 128, label %35
]
35: ; preds = %33, %43, %36
br label %29
36: ; preds = %33
%37 = call i32 @usleep(i32 100000)
br label %35
38: ; preds = %33
%39 = load i32, i32* @stderr
%40 = call i8* @strerror(i32 %34)
%41 = call i32 (i32, i8*, ...) @fprintf(i32 %39, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.5, i64 0, i64 0), i8* %21, i8* %40)
%42 = call i32 @fclose(i32* %22)
br label %46
43: ; preds = %29
%44 = load %struct.snis_device_io_connection*, %struct.snis_device_io_connection** %4
%45 = call i32 @interpret_command(i8* %5, %struct.snis_device_io_connection* %44)
br label %35
46: ; preds = %38, %25
%47 = phi i32 [ 0, %38 ], [ -1, %25 ]
ret i32 %47
}