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

/* Variables and functions */
int EACCES;
int EINVAL;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, char const *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ snprintf(char *, int, char *, char *);
int /*<<< orphan*/ *stderr;
int /*<<< orphan*/ strcpy(char *, char const *);
scalar_t__ strncmp(char const *, char *, int);
int strspn(char *, char *);
int handle_1w_reply(const char *line) {
  if (strncmp(line, "1WIRE DS18B20 ", 14) == 0) {
    FILE *f;
    char filename[128];
    char id[17];
    strcpy(id, &line[14]);
    id[16] = '\0';
    if (strspn(id, "0123456789abcdef") != 16) {
      fprintf(stderr, "device id (%s) invalid\n", id);
      return -EINVAL;
    }
    snprintf(filename, sizeof(filename), "/tmp/telemetry.ds18b20.%s", id);
    f = fopen(filename, "w");
    if (!f) {
      perror(filename);
      return -EACCES;
    }
    fprintf(f, "%s\n", line + 14 + 17);
    fclose(f);
  }
  return 0;
}


x86 assembly (clang, -O0):
<handle_1w_reply>:
push   %rbp
mov    %rsp,%rbp
sub    $0xc0,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe66(%rip),%rsi
mov    $0xe,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%rax
jne    129a <handle_1w_reply+0x11a>
lea    -0xc0(%rbp),%rdi
mov    -0x10(%rbp),%rsi
add    $0xe,%rsi
call   1040 <strcpy@plt>
movb   $0x0,-0xb0(%rbp)
lea    -0xc0(%rbp),%rdi
lea    0xe38(%rip),%rsi
call   1070 <strspn@plt>
cmp    $0x10,%eax
je     1215 <handle_1w_reply+0x95>
mov    0x2dec(%rip),%rax
mov    (%rax),%rdi
lea    -0xc0(%rbp),%rdx
lea    0xe23(%rip),%rsi
call   1080 <fprintf@plt>
xor    %eax,%eax
mov    0x2dc5(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    12a1 <handle_1w_reply+0x121>
lea    -0xa0(%rbp),%rdi
lea    -0xc0(%rbp),%rcx
mov    $0x80,%esi
lea    0xe09(%rip),%rdx
call   1060 <snprintf@plt>
lea    -0xa0(%rbp),%rdi
lea    0xe10(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1275 <handle_1w_reply+0xf5>
lea    -0xa0(%rbp),%rdi
call   10a0 <perror@plt>
xor    %eax,%eax
mov    0x2d7d(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    12a1 <handle_1w_reply+0x121>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rdx
add    $0xe,%rdx
add    $0x11,%rdx
lea    0xdc8(%rip),%rsi
call   1080 <fprintf@plt>
mov    -0x18(%rbp),%rdi
call   1050 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0xc0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<handle_1w_reply>:
push   %rbp
push   %r14
push   %rbx
sub    $0xa0,%rsp
mov    %rdi,%rbx
lea    0xe6b(%rip),%rsi
mov    $0xe,%edx
call   1030 <strncmp@plt>
xor    %ebp,%ebp
test   %rax,%rax
jne    1249 <handle_1w_reply+0xc9>
lea    0xe(%rbx),%rsi
mov    %rsp,%r14
mov    %r14,%rdi
call   1040 <strcpy@plt>
movb   $0x0,0x10(%rsp)
lea    0xe4a(%rip),%rsi
mov    %r14,%rdi
call   1070 <strspn@plt>
cmp    $0x10,%eax
jne    1225 <handle_1w_reply+0xa5>
lea    0xe5f(%rip),%rdx
lea    0x20(%rsp),%r14
mov    %rsp,%rcx
mov    %r14,%rdi
mov    $0x80,%esi
call   1060 <snprintf@plt>
lea    0xe5d(%rip),%rsi
mov    %r14,%rdi
call   1090 <fopen@plt>
test   %rax,%rax
je     1257 <handle_1w_reply+0xd7>
mov    %rax,%r14
add    $0x1f,%rbx
lea    0xe44(%rip),%rsi
mov    %rax,%rdi
mov    %rbx,%rdx
call   1080 <fprintf@plt>
mov    %r14,%rdi
call   1050 <fclose@plt>
jmp    1249 <handle_1w_reply+0xc9>
mov    0x2dac(%rip),%rax
mov    (%rax),%rdi
lea    0xdea(%rip),%rsi
mov    %rsp,%rdx
call   1080 <fprintf@plt>
mov    0x2d8b(%rip),%rax
xor    %ebp,%ebp
sub    (%rax),%ebp
mov    %ebp,%eax
add    $0xa0,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0x20(%rsp),%rdi
call   10a0 <perror@plt>
mov    0x2d80(%rip),%rax
jmp    1245 <handle_1w_reply+0xc5>

LLVM IR (-O0):
@.str = [15 x i8] c"1WIRE DS18B20 \00"
@.str.1 = [17 x i8] c"0123456789abcdef\00"
@stderr = global i32* null
@.str.2 = [24 x i8] c"device id (%s) invalid\0A\00"
@EINVAL = global i32 0
@.str.3 = [26 x i8] c"/tmp/telemetry.ds18b20.%s\00"
@.str.4 = [2 x i8] c"w\00"
@EACCES = global i32 0
@.str.5 = [4 x i8] c"%s\0A\00"
define i32 @handle_1w_reply(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca [128 x i8]
%6 = alloca [17 x i8]
store i8* %0, i8** %3
%7 = load i8*, i8** %3
%8 = call i64 @strncmp(i8* %7, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 14)
%9 = icmp eq i64 %8, 0
br i1 %9, label %10, label %46
10: ; preds = %1
%11 = getelementptr inbounds [17 x i8], [17 x i8]* %6, i64 0, i64 0
%12 = load i8*, i8** %3
%13 = getelementptr inbounds i8, i8* %12, i64 14
%14 = call i32 @strcpy(i8* %11, i8* %13)
%15 = getelementptr inbounds [17 x i8], [17 x i8]* %6, i64 0, i64 16
store i8 0, i8* %15
%16 = getelementptr inbounds [17 x i8], [17 x i8]* %6, i64 0, i64 0
%17 = call i32 @strspn(i8* %16, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
%18 = icmp ne i32 %17, 16
br i1 %18, label %19, label %25
19: ; preds = %10
%20 = load i32*, i32** @stderr
%21 = getelementptr inbounds [17 x i8], [17 x i8]* %6, i64 0, i64 0
%22 = call i32 @fprintf(i32* %20, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i8* %21)
%23 = load i32, i32* @EINVAL
%24 = sub nsw i32 0, %23
store i32 %24, i32* %2
br label %47
25: ; preds = %10
%26 = getelementptr inbounds [128 x i8], [128 x i8]* %5, i64 0, i64 0
%27 = getelementptr inbounds [17 x i8], [17 x i8]* %6, i64 0, i64 0
%28 = call i32 @snprintf(i8* %26, i32 128, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0), i8* %27)
%29 = getelementptr inbounds [128 x i8], [128 x i8]* %5, i64 0, i64 0
%30 = call i32* @fopen(i8* %29, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
store i32* %30, i32** %4
%31 = load i32*, i32** %4
%32 = icmp ne i32* %31, null
br i1 %32, label %38, label %33
33: ; preds = %25
%34 = getelementptr inbounds [128 x i8], [128 x i8]* %5, i64 0, i64 0
%35 = call i32 @perror(i8* %34)
%36 = load i32, i32* @EACCES
%37 = sub nsw i32 0, %36
store i32 %37, i32* %2
br label %47
38: ; preds = %25
%39 = load i32*, i32** %4
%40 = load i8*, i8** %3
%41 = getelementptr inbounds i8, i8* %40, i64 14
%42 = getelementptr inbounds i8, i8* %41, i64 17
%43 = call i32 @fprintf(i32* %39, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0), i8* %42)
%44 = load i32*, i32** %4
%45 = call i32 @fclose(i32* %44)
br label %46
46: ; preds = %38, %1
store i32 0, i32* %2
br label %47
47: ; preds = %46, %33, %19
%48 = load i32, i32* %2
ret i32 %48
}

LLVM IR (-O2):
@.str = [15 x i8] c"1WIRE DS18B20 \00"
@.str.1 = [17 x i8] c"0123456789abcdef\00"
@stderr = global i32* null
@.str.2 = [24 x i8] c"device id (%s) invalid\0A\00"
@EINVAL = global i32 0
@.str.3 = [26 x i8] c"/tmp/telemetry.ds18b20.%s\00"
@.str.4 = [2 x i8] c"w\00"
@EACCES = global i32 0
@.str.5 = [4 x i8] c"%s\0A\00"
define i32 @handle_1w_reply(i8* %0) {
%2 = alloca [128 x i8]
%3 = alloca [17 x i8]
%4 = tail call i64 @strncmp(i8* %0, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 14)
%5 = icmp eq i64 %4, 0
br i1 %5, label %6, label %31
6: ; preds = %1
%7 = getelementptr inbounds [128 x i8], [128 x i8]* %2, i64 0, i64 0
%8 = getelementptr inbounds [17 x i8], [17 x i8]* %3, i64 0, i64 0
%9 = getelementptr inbounds i8, i8* %0, i64 14
%10 = call i32 @strcpy(i8* %8, i8* %9)
%11 = getelementptr inbounds [17 x i8], [17 x i8]* %3, i64 0, i64 16
store i8 0, i8* %11
%12 = call i32 @strspn(i8* %8, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
%13 = icmp eq i32 %12, 16
br i1 %13, label %17, label %14
14: ; preds = %6
%15 = load i32*, i32** @stderr
%16 = call i32 @fprintf(i32* %15, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i8* %8)
br label %23
17: ; preds = %6
%18 = call i32 @snprintf(i8* %7, i32 128, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0), i8* %8)
%19 = call i32* @fopen(i8* %7, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%20 = icmp eq i32* %19, null
br i1 %20, label %21, label %27
21: ; preds = %17
%22 = call i32 @perror(i8* %7)
br label %23
23: ; preds = %14, %21
%24 = phi i32* [ @EACCES, %21 ], [ @EINVAL, %14 ]
%25 = load i32, i32* %24
%26 = sub nsw i32 0, %25
br label %31
27: ; preds = %17
%28 = getelementptr inbounds i8, i8* %0, i64 31
%29 = call i32 @fprintf(i32* %19, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0), i8* %28)
%30 = call i32 @fclose(i32* %19)
br label %31
31: ; preds = %1, %27, %23
%32 = phi i32 [ %26, %23 ], [ 0, %27 ], [ 0, %1 ]
ret i32 %32
}