#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 /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int, int);
int /*<<< orphan*/ snprintf(char *, int, char *);
scalar_t__ strncmp(char const *, char *, int);
int strtol(char const *, int /*<<< orphan*/ *, int);
int handle_i2c_reply(const char *line) {
  if (strncmp(line, "I2C DATA ", 9) == 0) {
    int reg = strtol(line + 9, NULL, 16) << 8 | strtol(line + 9 + 3, NULL, 16);
    int voltage = reg * 4;
    printf("ina219 reg 2: 0x%x, bus voltage: %imV\n", reg, voltage);
    FILE *f;
    char filename[128];
    snprintf(filename, sizeof(filename), "/tmp/telemetry.ina219.voltage");
    f = fopen(filename, "w");
    if (!f) {
      perror(filename);
      return -EACCES;
    }
    fprintf(f, "%i\n", voltage);
    fclose(f);
  }
  return 0;
}


x86 assembly (clang, -O0):
<handle_i2c_reply>:
push   %rbp
mov    %rsp,%rbp
sub    $0xb0,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe66(%rip),%rsi
mov    $0x9,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%rax
jne    1284 <handle_i2c_reply+0x104>
mov    -0x10(%rbp),%rdi
add    $0x9,%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0x10,%edx
call   1080 <strtol@plt>
shl    $0x8,%eax
mov    %eax,-0xa4(%rbp)
mov    -0x10(%rbp),%rdi
add    $0x9,%rdi
add    $0x3,%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0x10,%edx
call   1080 <strtol@plt>
mov    %eax,%ecx
mov    -0xa4(%rbp),%eax
or     %ecx,%eax
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
shl    $0x2,%eax
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%esi
mov    -0x18(%rbp),%edx
lea    0xe00(%rip),%rdi
call   1050 <printf@plt>
lea    -0xa0(%rbp),%rdi
mov    $0x80,%esi
lea    0xe0f(%rip),%rdx
call   1060 <snprintf@plt>
lea    -0xa0(%rbp),%rdi
lea    0xe1a(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1268 <handle_i2c_reply+0xe8>
lea    -0xa0(%rbp),%rdi
call   10a0 <perror@plt>
xor    %eax,%eax
mov    0x2d8a(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    128b <handle_i2c_reply+0x10b>
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%edx
lea    0xddb(%rip),%rsi
call   1070 <fprintf@plt>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0xb0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<handle_i2c_reply>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x88,%rsp
mov    %rdi,%rbp
lea    0xe69(%rip),%rsi
mov    $0x9,%edx
call   1030 <strncmp@plt>
xor    %r15d,%r15d
test   %rax,%rax
jne    123b <handle_i2c_reply+0xbb>
lea    0x9(%rbp),%rdi
xor    %r15d,%r15d
xor    %esi,%esi
mov    $0x10,%edx
call   1080 <strtol@plt>
mov    %eax,%ebx
shl    $0x8,%ebx
add    $0xc,%rbp
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1080 <strtol@plt>
or     %eax,%ebx
lea    0x0(,%rbx,4),%r14d
lea    0xe21(%rip),%rdi
mov    %ebx,%esi
mov    %r14d,%edx
call   1050 <printf@plt>
lea    0xe37(%rip),%rdx
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x80,%esi
call   1060 <snprintf@plt>
lea    0xe3e(%rip),%rsi
mov    %rbx,%rdi
call   1090 <fopen@plt>
test   %rax,%rax
je     124c <handle_i2c_reply+0xcc>
mov    %rax,%rbp
lea    0xe29(%rip),%rsi
mov    %rax,%rdi
mov    %r14d,%edx
call   1070 <fprintf@plt>
mov    %rbp,%rdi
call   1040 <fclose@plt>
mov    %r15d,%eax
add    $0x88,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %rsp,%rdi
call   10a0 <perror@plt>
mov    0x2d8d(%rip),%rax
xor    %r15d,%r15d
sub    (%rax),%r15d
jmp    123b <handle_i2c_reply+0xbb>

LLVM IR (-O0):
@.str = [10 x i8] c"I2C DATA \00"
@.str.1 = [39 x i8] c"ina219 reg 2: 0x%x, bus voltage: %imV\0A\00"
@.str.2 = [30 x i8] c"/tmp/telemetry.ina219.voltage\00"
@.str.3 = [2 x i8] c"w\00"
@EACCES = global i32 0
@.str.4 = [4 x i8] c"%i\0A\00"
define i32 @handle_i2c_reply(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32*
%7 = alloca [128 x i8]
store i8* %0, i8** %3
%8 = load i8*, i8** %3
%9 = call i64 @strncmp(i8* %8, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 9)
%10 = icmp eq i64 %9, 0
br i1 %10, label %11, label %43
11: ; preds = %1
%12 = load i8*, i8** %3
%13 = getelementptr inbounds i8, i8* %12, i64 9
%14 = call i32 @strtol(i8* %13, i32* null, i32 16)
%15 = shl i32 %14, 8
%16 = load i8*, i8** %3
%17 = getelementptr inbounds i8, i8* %16, i64 9
%18 = getelementptr inbounds i8, i8* %17, i64 3
%19 = call i32 @strtol(i8* %18, i32* null, i32 16)
%20 = or i32 %15, %19
store i32 %20, i32* %4
%21 = load i32, i32* %4
%22 = mul nsw i32 %21, 4
store i32 %22, i32* %5
%23 = load i32, i32* %4
%24 = load i32, i32* %5
%25 = call i32 @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0), i32 %23, i32 %24)
%26 = getelementptr inbounds [128 x i8], [128 x i8]* %7, i64 0, i64 0
%27 = call i32 @snprintf(i8* %26, i32 128, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0))
%28 = getelementptr inbounds [128 x i8], [128 x i8]* %7, i64 0, i64 0
%29 = call i32* @fopen(i8* %28, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i32* %29, i32** %6
%30 = load i32*, i32** %6
%31 = icmp ne i32* %30, null
br i1 %31, label %37, label %32
32: ; preds = %11
%33 = getelementptr inbounds [128 x i8], [128 x i8]* %7, i64 0, i64 0
%34 = call i32 @perror(i8* %33)
%35 = load i32, i32* @EACCES
%36 = sub nsw i32 0, %35
store i32 %36, i32* %2
br label %44
37: ; preds = %11
%38 = load i32*, i32** %6
%39 = load i32, i32* %5
%40 = call i32 @fprintf(i32* %38, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i32 %39)
%41 = load i32*, i32** %6
%42 = call i32 @fclose(i32* %41)
br label %43
43: ; preds = %37, %1
store i32 0, i32* %2
br label %44
44: ; preds = %43, %32
%45 = load i32, i32* %2
ret i32 %45
}

LLVM IR (-O2):
@.str = [10 x i8] c"I2C DATA \00"
@.str.1 = [39 x i8] c"ina219 reg 2: 0x%x, bus voltage: %imV\0A\00"
@.str.2 = [30 x i8] c"/tmp/telemetry.ina219.voltage\00"
@.str.3 = [2 x i8] c"w\00"
@EACCES = global i32 0
@.str.4 = [4 x i8] c"%i\0A\00"
define i32 @handle_i2c_reply(i8* %0) {
%2 = alloca [128 x i8]
%3 = tail call i64 @strncmp(i8* %0, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 9)
%4 = icmp eq i64 %3, 0
br i1 %4, label %5, label %25
5: ; preds = %1
%6 = getelementptr inbounds i8, i8* %0, i64 9
%7 = tail call i32 @strtol(i8* nocapture %6, i32* null, i32 16)
%8 = shl i32 %7, 8
%9 = getelementptr inbounds i8, i8* %0, i64 12
%10 = tail call i32 @strtol(i8* nocapture %9, i32* null, i32 16)
%11 = or i32 %8, %10
%12 = shl nsw i32 %11, 2
%13 = tail call i32 @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0), i32 %11, i32 %12)
%14 = getelementptr inbounds [128 x i8], [128 x i8]* %2, i64 0, i64 0
%15 = call i32 @snprintf(i8* %14, i32 128, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0))
%16 = call i32* @fopen(i8* %14, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%17 = icmp eq i32* %16, null
br i1 %17, label %21, label %18
18: ; preds = %5
%19 = call i32 @fprintf(i32* %16, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i32 %12)
%20 = call i32 @fclose(i32* %16)
br label %25
21: ; preds = %5
%22 = call i32 @perror(i8* %14)
%23 = load i32, i32* @EACCES
%24 = sub nsw i32 0, %23
br label %25
25: ; preds = %1, %18, %21
%26 = phi i32 [ %24, %21 ], [ 0, %18 ], [ 0, %1 ]
ret i32 %26
}