#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 = 0;
      while (reply_msg == 0) {
        reply_msg = recv_msg(recvbuf, 80);
        if (reply_msg == 0) {
          printf("one sensors read failed - retrying\n");
        }
      }
      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     1363 <one_sensor_read+0x1b3>
lea    -0xc0(%rbp),%rdi
xor    %esi,%esi
mov    $0x50,%edx
call   1070 <memset@plt>
movq   $0x0,-0xd0(%rbp)
cmpq   $0x0,-0xd0(%rbp)
jne    1268 <one_sensor_read+0xb8>
lea    -0xc0(%rbp),%rdi
mov    $0x50,%esi
call   1040 <recv_msg@plt>
mov    %rax,-0xd0(%rbp)
cmpq   $0x0,-0xd0(%rbp)
jne    1263 <one_sensor_read+0xb3>
lea    0xdaa(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
jmp    1221 <one_sensor_read+0x71>
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    1300 <one_sensor_read+0x150>
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    1381 <one_sensor_read+0x1d1>
mov    -0xd0(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x57,%eax
jne    1331 <one_sensor_read+0x181>
mov    -0xd0(%rbp),%rsi
add    $0x2,%rsi
lea    0xd05(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
jmp    1359 <one_sensor_read+0x1a9>
mov    -0xd0(%rbp),%rsi
lea    0xcf7(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0x0,%al
call   10d0 <clear_input_stream@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1381 <one_sensor_read+0x1d1>
jmp    135e <one_sensor_read+0x1ae>
jmp    1216 <one_sensor_read+0x66>
lea    0xce1(%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     12a1 <one_sensor_read+0xe1>
lea    0x50(%rsp),%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x50,%edx
call   1080 <memset@plt>
lea    0xe14(%rip),%rbx
mov    %rsp,%r12
lea    0xdda(%rip),%r13
nopl   0x0(%rax)
mov    %r15,%rdi
mov    $0x50,%esi
call   1050 <recv_msg@plt>
mov    %rax,%rbp
test   %rax,%rax
jne    126d <one_sensor_read+0xad>
cs nopw 0x0(%rax,%rax,1)
nop
mov    %rbx,%rdi
call   1040 <puts@plt>
mov    %r15,%rdi
mov    $0x50,%esi
call   1050 <recv_msg@plt>
test   %rax,%rax
je     1250 <one_sensor_read+0x90>
mov    %rax,%rbp
mov    %r12,%rdi
call   1060 <strlen@plt>
lea    -0x1(%rax),%edx
mov    %rbp,%rdi
mov    %r12,%rsi
call   1030 <strncmp@plt>
test   %rax,%rax
je     12bc <one_sensor_read+0xfc>
cmpb   $0x57,0x0(%rbp)
jne    12df <one_sensor_read+0x11f>
add    $0x2,%rbp
mov    %r13,%rdi
mov    %rbp,%rsi
xor    %eax,%eax
call   1070 <printf@plt>
jmp    1230 <one_sensor_read+0x70>
lea    0xd7f(%rip),%rdi
call   1040 <puts@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
call   10a0 <one_sensor_read@plt>
mov    %eax,%ebx
jmp    12fc <one_sensor_read+0x13c>
mov    %rsp,%rdi
call   1060 <strlen@plt>
cltq
add    %rax,%rbp
mov    %rbp,%rdi
call   10b0 <atoi@plt>
mov    %eax,(%r14)
xor    %ebx,%ebx
xor    %eax,%eax
call   10c0 <check_errors@plt>
jmp    12fc <one_sensor_read+0x13c>
lea    0xd2c(%rip),%rdi
mov    %rbp,%rsi
xor    %eax,%eax
call   1070 <printf@plt>
xor    %eax,%eax
call   10e0 <clear_input_stream@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%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 = [36 x i8] c"one sensors read failed - retrying\0A\00"
@.str.2 = [12 x i8] c"Warning: %s\00"
@.str.3 = [21 x i8] c"got an error >>%s<<\0A\00"
@.str.4 = [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 %67
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, %66
store i8* null, i8** %9
br label %22
22: ; preds = %32, %21
%23 = load i8*, i8** %9
%24 = icmp eq i8* %23, null
br i1 %24, label %25, label %33
25: ; preds = %22
%26 = getelementptr inbounds [80 x i8], [80 x i8]* %7, i64 0, i64 0
%27 = call i8* @recv_msg(i8* %26, i32 80)
store i8* %27, i8** %9
%28 = load i8*, i8** %9
%29 = icmp eq i8* %28, null
br i1 %29, label %30, label %32
30: ; preds = %25
%31 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0))
br label %32
32: ; preds = %30, %25
br label %22
33: ; preds = %22
%34 = load i8*, i8** %9
%35 = getelementptr inbounds [80 x i8], [80 x i8]* %6, i64 0, i64 0
%36 = getelementptr inbounds [80 x i8], [80 x i8]* %6, i64 0, i64 0
%37 = call i32 @strlen(i8* %36)
%38 = sub nsw i32 %37, 1
%39 = call i64 @strncmp(i8* %34, i8* %35, i32 %38)
%40 = icmp eq i64 %39, 0
br i1 %40, label %41, label %51
41: ; preds = %33
%42 = load i8*, i8** %9
%43 = getelementptr inbounds [80 x i8], [80 x i8]* %6, i64 0, i64 0
%44 = call i32 @strlen(i8* %43)
%45 = sext i32 %44 to i64
%46 = getelementptr inbounds i8, i8* %42, i64 %45
store i8* %46, i8** %8
%47 = load i8*, i8** %8
%48 = call i32 @atoi(i8* %47)
%49 = load i32*, i32** %5
store i32 %48, i32* %49
%50 = call i32 (...) @check_errors()
store i32 0, i32* %3
br label %72
51: ; preds = %33
%52 = load i8*, i8** %9
%53 = getelementptr inbounds i8, i8* %52, i64 0
%54 = load i8, i8* %53
%55 = sext i8 %54 to i32
%56 = icmp eq i32 %55, 87
br i1 %56, label %57, label %61
57: ; preds = %51
%58 = load i8*, i8** %9
%59 = getelementptr inbounds i8, i8* %58, i64 2
%60 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i8* %59)
br label %65
61: ; preds = %51
%62 = load i8*, i8** %9
%63 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0), i8* %62)
%64 = call i32 (...) @clear_input_stream()
store i32 -1, i32* %3
br label %72
65: ; preds = %57
br label %66
66: ; preds = %65
br label %21
67: ; preds = %2
%68 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0))
%69 = load i8*, i8** %4
%70 = load i32*, i32** %5
%71 = call i32 @one_sensor_read(i8* %69, i32* %70)
store i32 %71, i32* %3
br label %72
72: ; preds = %67, %61, %41
%73 = load i32, i32* %3
ret i32 %73
}

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