#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 atoi(char const *);
int /*<<< orphan*/ check_errors();
int /*<<< orphan*/ clear_input_stream();
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, ...);
char *recv_msg(char *, int);
scalar_t__ send_msg(char *, int);
int /*<<< orphan*/ sprintf(char *, char *, char *);
int strlen(char *);
scalar_t__ strncmp(char const *, char *, int);
int one_sensor_read(char *sensorname, int *value) {
  char sendbuf[80];
  char recvbuf[80];
  const char *arg;
  const char *reply_msg;
  sprintf(sendbuf, "S %s\n", sensorname);
  if (send_msg(sendbuf, strlen(sendbuf))) {
    memset(recvbuf, 0, 80);
    while (1) {
      reply_msg = recv_msg(recvbuf, 80);
      if (strncmp(reply_msg, sendbuf, strlen(sendbuf) - 1) == 0) {
        arg = reply_msg + strlen(sendbuf);
        *value = atoi(arg);
        check_errors();
        return 0;
      } else if (reply_msg[0] == 'W') {
        printf("Warning: %s", reply_msg + 2);
      } else {
        printf("got an error >>%s<<\n", reply_msg);
        clear_input_stream();
        return -1;
      }
    }
  } else {
    printf("send failed!!!\n");
    return one_sensor_read(sensorname, value);
  }
}


x86 assembly (clang, -O0):
<one_sensor_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0xf0,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
lea    -0x70(%rbp),%rdi
mov    -0x10(%rbp),%rdx
lea    0xe2e(%rip),%rsi
call   10c0 <sprintf@plt>
lea    -0x70(%rbp),%rax
mov    %rax,-0xd8(%rbp)
lea    -0x70(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0xd8(%rbp),%rdi
mov    %eax,%esi
call   1080 <send_msg@plt>
cmp    $0x0,%rax
je     1329 <one_sensor_read+0x179>
lea    -0xc0(%rbp),%rdi
xor    %esi,%esi
mov    $0x50,%edx
call   1070 <memset@plt>
lea    -0xc0(%rbp),%rdi
mov    $0x50,%esi
call   1040 <recv_msg@plt>
mov    %rax,-0xd0(%rbp)
mov    -0xd0(%rbp),%rax
mov    %rax,-0xe8(%rbp)
lea    -0x70(%rbp),%rax
mov    %rax,-0xe0(%rbp)
lea    -0x70(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0xe8(%rbp),%rdi
mov    -0xe0(%rbp),%rsi
mov    %eax,%edx
sub    $0x1,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%rax
jne    12c6 <one_sensor_read+0x116>
mov    -0xd0(%rbp),%rax
mov    %rax,-0xf0(%rbp)
lea    -0x70(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,%ecx
mov    -0xf0(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0xc8(%rbp)
mov    -0xc8(%rbp),%rdi
call   10a0 <atoi@plt>
mov    %eax,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    $0x0,%al
call   10b0 <check_errors@plt>
movl   $0x0,-0x4(%rbp)
jmp    1347 <one_sensor_read+0x197>
mov    -0xd0(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x57,%eax
jne    12f7 <one_sensor_read+0x147>
mov    -0xd0(%rbp),%rsi
add    $0x2,%rsi
lea    0xd1b(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
jmp    131f <one_sensor_read+0x16f>
mov    -0xd0(%rbp),%rsi
lea    0xd0d(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0x0,%al
call   10d0 <clear_input_stream@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1347 <one_sensor_read+0x197>
jmp    1324 <one_sensor_read+0x174>
jmp    1216 <one_sensor_read+0x66>
lea    0xcf7(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1090 <one_sensor_read@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0xf0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<one_sensor_read>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0xa8,%rsp
mov    %rsi,%r14
mov    %rdi,%rbx
lea    0xe22(%rip),%rsi
mov    %rsp,%rbp
mov    %rbp,%rdi
mov    %rbx,%rdx
call   10d0 <sprintf@plt>
mov    %rbp,%rdi
call   1060 <strlen@plt>
mov    %rbp,%rdi
mov    %eax,%esi
call   1090 <send_msg@plt>
test   %rax,%rax
je     12c0 <one_sensor_read+0x100>
lea    0x50(%rsp),%rbx
xor    %r15d,%r15d
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x50,%edx
call   1080 <memset@plt>
mov    %rbx,%rdi
mov    $0x50,%esi
call   1050 <recv_msg@plt>
mov    %rax,%rbx
mov    %rsp,%rbp
mov    %rbp,%rdi
call   1060 <strlen@plt>
lea    -0x1(%rax),%edx
mov    %rbx,%rdi
mov    %rbp,%rsi
call   1030 <strncmp@plt>
test   %rax,%rax
je     12a1 <one_sensor_read+0xe1>
lea    0xdb3(%rip),%r12
lea    0x50(%rsp),%r13
mov    %rsp,%rbp
nopl   0x0(%rax,%rax,1)
cmpb   $0x57,(%rbx)
jne    12dc <one_sensor_read+0x11c>
add    $0x2,%rbx
mov    %r12,%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1070 <printf@plt>
mov    %r13,%rdi
mov    $0x50,%esi
call   1050 <recv_msg@plt>
mov    %rax,%rbx
mov    %rbp,%rdi
call   1060 <strlen@plt>
lea    -0x1(%rax),%edx
mov    %rbx,%rdi
mov    %rbp,%rsi
call   1030 <strncmp@plt>
test   %rax,%rax
jne    1260 <one_sensor_read+0xa0>
mov    %rsp,%rdi
call   1060 <strlen@plt>
movslq %eax,%rdi
add    %rbx,%rdi
call   10b0 <atoi@plt>
mov    %eax,(%r14)
xor    %eax,%eax
call   10c0 <check_errors@plt>
jmp    12fa <one_sensor_read+0x13a>
lea    0xd60(%rip),%rdi
call   1040 <puts@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
call   10a0 <one_sensor_read@plt>
mov    %eax,%r15d
jmp    12fa <one_sensor_read+0x13a>
lea    0xd2f(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1070 <printf@plt>
xor    %eax,%eax
call   10e0 <clear_input_stream@plt>
mov    $0xffffffff,%r15d
mov    %r15d,%eax
add    $0xa8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [6 x i8] c"S %s\0A\00"
@.str.1 = [12 x i8] c"Warning: %s\00"
@.str.2 = [21 x i8] c"got an error >>%s<<\0A\00"
@.str.3 = [16 x i8] c"send failed!!!\0A\00"
define i32 @one_sensor_read(i8* %0, i32* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca [80 x i8]
%7 = alloca [80 x i8]
%8 = alloca i8*
%9 = alloca i8*
store i8* %0, i8** %4
store i32* %1, i32** %5
%10 = getelementptr inbounds [80 x i8], [80 x i8]* %6, i64 0, i64 0
%11 = load i8*, i8** %4
%12 = call i32 @sprintf(i8* %10, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %11)
%13 = getelementptr inbounds [80 x i8], [80 x i8]* %6, i64 0, i64 0
%14 = getelementptr inbounds [80 x i8], [80 x i8]* %6, i64 0, i64 0
%15 = call i32 @strlen(i8* %14)
%16 = call i64 @send_msg(i8* %13, i32 %15)
%17 = icmp ne i64 %16, 0
br i1 %17, label %18, label %57
18: ; preds = %2
%19 = getelementptr inbounds [80 x i8], [80 x i8]* %7, i64 0, i64 0
%20 = call i32 @memset(i8* %19, i32 0, i32 80)
br label %21
21: ; preds = %18, %56
%22 = getelementptr inbounds [80 x i8], [80 x i8]* %7, i64 0, i64 0
%23 = call i8* @recv_msg(i8* %22, i32 80)
store i8* %23, i8** %9
%24 = load i8*, i8** %9
%25 = getelementptr inbounds [80 x i8], [80 x i8]* %6, i64 0, i64 0
%26 = getelementptr inbounds [80 x i8], [80 x i8]* %6, i64 0, i64 0
%27 = call i32 @strlen(i8* %26)
%28 = sub nsw i32 %27, 1
%29 = call i64 @strncmp(i8* %24, i8* %25, i32 %28)
%30 = icmp eq i64 %29, 0
br i1 %30, label %31, label %41
31: ; preds = %21
%32 = load i8*, i8** %9
%33 = getelementptr inbounds [80 x i8], [80 x i8]* %6, i64 0, i64 0
%34 = call i32 @strlen(i8* %33)
%35 = sext i32 %34 to i64
%36 = getelementptr inbounds i8, i8* %32, i64 %35
store i8* %36, i8** %8
%37 = load i8*, i8** %8
%38 = call i32 @atoi(i8* %37)
%39 = load i32*, i32** %5
store i32 %38, i32* %39
%40 = call i32 (...) @check_errors()
store i32 0, i32* %3
br label %62
41: ; preds = %21
%42 = load i8*, i8** %9
%43 = getelementptr inbounds i8, i8* %42, i64 0
%44 = load i8, i8* %43
%45 = sext i8 %44 to i32
%46 = icmp eq i32 %45, 87
br i1 %46, label %47, label %51
47: ; preds = %41
%48 = load i8*, i8** %9
%49 = getelementptr inbounds i8, i8* %48, i64 2
%50 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i8* %49)
br label %55
51: ; preds = %41
%52 = load i8*, i8** %9
%53 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i8* %52)
%54 = call i32 (...) @clear_input_stream()
store i32 -1, i32* %3
br label %62
55: ; preds = %47
br label %56
56: ; preds = %55
br label %21
57: ; preds = %2
%58 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0))
%59 = load i8*, i8** %4
%60 = load i32*, i32** %5
%61 = call i32 @one_sensor_read(i8* %59, i32* %60)
store i32 %61, i32* %3
br label %62
62: ; preds = %57, %51, %31
%63 = load i32, i32* %3
ret i32 %63
}

LLVM IR (-O2):
@.str = [6 x i8] c"S %s\0A\00"
@.str.1 = [12 x i8] c"Warning: %s\00"
@.str.2 = [21 x i8] c"got an error >>%s<<\0A\00"
@str = [15 x i8] c"send failed!!!\00"
define i32 @one_sensor_read(i8* %0, i32* nocapture writeonly %1) {
%3 = alloca [80 x i8]
%4 = alloca [80 x i8]
%5 = getelementptr inbounds [80 x i8], [80 x i8]* %3, i64 0, i64 0
%6 = getelementptr inbounds [80 x i8], [80 x i8]* %4, i64 0, i64 0
%7 = call i32 @sprintf(i8* %5, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %0)
%8 = call i32 @strlen(i8* %5)
%9 = call i64 @send_msg(i8* %5, i32 %8)
%10 = icmp eq i64 %9, 0
br i1 %10, label %40, label %11
11: ; preds = %2
%12 = call i32 @memset(i8* %6, i32 0, i32 80)
%13 = call i8* @recv_msg(i8* %6, i32 80)
%14 = call i32 @strlen(i8* %5)
%15 = add nsw i32 %14, -1
%16 = call i64 @strncmp(i8* %13, i8* %5, i32 %15)
%17 = icmp eq i64 %16, 0
br i1 %17, label %18, label %25
18: ; preds = %29, %11
%19 = phi i8* [ %13, %11 ], [ %32, %29 ]
%20 = call i32 @strlen(i8* %5)
%21 = sext i32 %20 to i64
%22 = getelementptr inbounds i8, i8* %19, i64 %21
%23 = call i32 @atoi(i8* %22)
store i32 %23, i32* %1
%24 = call i32 (...) @check_errors()
br label %43
25: ; preds = %11, %29
%26 = phi i8* [ %32, %29 ], [ %13, %11 ]
%27 = load i8, i8* %26
%28 = icmp eq i8 %27, 87
br i1 %28, label %29, label %37
29: ; preds = %25
%30 = getelementptr inbounds i8, i8* %26, i64 2
%31 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i8* %30)
%32 = call i8* @recv_msg(i8* %6, i32 80)
%33 = call i32 @strlen(i8* %5)
%34 = add nsw i32 %33, -1
%35 = call i64 @strncmp(i8* %32, i8* %5, i32 %34)
%36 = icmp eq i64 %35, 0
br i1 %36, label %18, label %25
37: ; preds = %25
%38 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i8* %26)
%39 = call i32 (...) @clear_input_stream()
br label %43
40: ; preds = %2
%41 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str, i64 0, i64 0))
%42 = call i32 @one_sensor_read(i8* %0, i32* %1)
br label %43
43: ; preds = %40, %37, %18
%44 = phi i32 [ 0, %18 ], [ -1, %37 ], [ %42, %40 ]
ret i32 %44
}