int inv_serial_write(void *sl_handle, unsigned char slave_addr,
                     unsigned short length, unsigned char const *data) {
  int result;
  const unsigned short data_length = length - 1;
  const unsigned char start_reg_addr = data[0];
  unsigned char i2c_write[128 + 1];
  unsigned short bytes_written = 0;
  while (bytes_written < data_length) {
    unsigned short this_len = min(128, data_length - bytes_written);
    if (bytes_written == 0) {
      result = inv_i2c_write((struct i2c_adapter *)sl_handle, slave_addr,
                             1 + this_len, data);
    } else {
      i2c_write[0] = start_reg_addr + bytes_written;
      memcpy(&i2c_write[1], &data[1 + bytes_written], this_len);
      result = inv_i2c_write((struct i2c_adapter *)sl_handle, slave_addr,
                             1 + this_len, i2c_write);
    }
    if (result) {
      do {
        __print_result_location(
            (int)(result),
            "/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/"
            "2022-01-22/02-19-57/repos/Ahmed-Hady/"
            "android_kernel_nokia_normandy/refs/heads/Stock1.1.2.2/drivers/"
            "misc/inv_mpu/mlsl-kernel.c",
            __func__, 247);
      } while (0);
      return result;
    }
    bytes_written += this_len;
  }
  return 0;
}


x86 assembly (clang, -O0):
<inv_serial_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0xc0,%rsp
mov    %dx,%ax
mov    %sil,%dl
mov    %rdi,-0x10(%rbp)
mov    %dl,-0x11(%rbp)
mov    %ax,-0x14(%rbp)
mov    %rcx,-0x20(%rbp)
movzwl -0x14(%rbp),%eax
sub    $0x1,%eax
mov    %ax,-0x26(%rbp)
mov    -0x20(%rbp),%rax
mov    (%rax),%al
mov    %al,-0x27(%rbp)
movw   $0x0,-0xb2(%rbp)
movzwl -0xb2(%rbp),%eax
movzwl -0x26(%rbp),%ecx
cmp    %ecx,%eax
jge    129a <inv_serial_write+0x15a>
movzwl -0x26(%rbp),%esi
movzwl -0xb2(%rbp),%eax
sub    %eax,%esi
mov    $0x80,%edi
mov    $0x0,%al
call   1040 <min@plt>
mov    %ax,-0xb4(%rbp)
movzwl -0xb2(%rbp),%eax
cmp    $0x0,%eax
jne    11e5 <inv_serial_write+0xa5>
mov    -0x10(%rbp),%rdi
movzbl -0x11(%rbp),%esi
movzwl -0xb4(%rbp),%edx
add    $0x1,%edx
mov    -0x20(%rbp),%rcx
mov    $0x0,%al
call   1030 <inv_i2c_write@plt>
mov    %eax,-0x24(%rbp)
jmp    1247 <inv_serial_write+0x107>
movzbl -0x27(%rbp),%eax
movzwl -0xb2(%rbp),%ecx
add    %ecx,%eax
mov    %al,-0xb0(%rbp)
lea    -0xb0(%rbp),%rdi
add    $0x1,%rdi
mov    -0x20(%rbp),%rsi
movzwl -0xb2(%rbp),%eax
add    $0x1,%eax
cltq
add    %rax,%rsi
movzwl -0xb4(%rbp),%eax
mov    %eax,%edx
call   1050 <memcpy@plt>
mov    -0x10(%rbp),%rdi
movzbl -0x11(%rbp),%esi
movzwl -0xb4(%rbp),%edx
add    $0x1,%edx
lea    -0xb0(%rbp),%rcx
mov    $0x0,%al
call   1030 <inv_i2c_write@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
je     127e <inv_serial_write+0x13e>
jmp    1256 <inv_serial_write+0x116>
mov    -0x24(%rbp),%edi
lea    0xda0(%rip),%rsi
lea    0xe57(%rip),%rdx
mov    $0xf7,%ecx
mov    $0x0,%al
call   1060 <__print_result_location@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12a1 <inv_serial_write+0x161>
movzwl -0xb4(%rbp),%ecx
movzwl -0xb2(%rbp),%eax
add    %ecx,%eax
mov    %ax,-0xb2(%rbp)
jmp    117d <inv_serial_write+0x3d>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0xc0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<inv_serial_write>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0xa8,%rsp
mov    %edx,%r14d
mov    %rdi,0x8(%rsp)
add    $0xffff,%r14w
je     1226 <inv_serial_write+0xe6>
mov    %rcx,%rbp
mov    (%rcx),%al
mov    %rax,0x10(%rsp)
movzwl %r14w,%eax
mov    %eax,0x4(%rsp)
movzbl %sil,%eax
mov    %eax,(%rsp)
xor    %ebx,%ebx
xor    %r12d,%r12d
mov    %rcx,0x18(%rsp)
nopw   0x0(%rax,%rax,1)
mov    0x4(%rsp),%esi
sub    %ebx,%esi
mov    $0x80,%edi
xor    %eax,%eax
call   1040 <min@plt>
mov    %eax,%r15d
test   %r12w,%r12w
je     11f0 <inv_serial_write+0xb0>
mov    0x10(%rsp),%rax
add    %r12d,%eax
mov    %al,0x20(%rsp)
mov    %ebx,%eax
lea    0x1(%rax,%rbp,1),%rsi
movzwl %r15w,%ebx
lea    0x21(%rsp),%rdi
mov    %rbx,%rdx
call   1050 <memcpy@plt>
add    $0x1,%ebx
mov    0x8(%rsp),%rdi
mov    (%rsp),%esi
mov    %ebx,%edx
lea    0x20(%rsp),%rcx
jmp    1202 <inv_serial_write+0xc2>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
movzwl %r15w,%edx
add    $0x1,%edx
mov    0x8(%rsp),%rdi
mov    (%rsp),%esi
mov    %rbp,%rcx
xor    %eax,%eax
call   1030 <inv_i2c_write@plt>
mov    %eax,%r13d
test   %eax,%eax
jne    122b <inv_serial_write+0xeb>
add    %r15d,%r12d
movzwl %r12w,%ebx
cmp    %bx,%r14w
mov    0x18(%rsp),%rbp
ja     1190 <inv_serial_write+0x50>
xor    %r13d,%r13d
jmp    1248 <inv_serial_write+0x108>
lea    0xdce(%rip),%rsi
lea    0xe85(%rip),%rdx
mov    %r13d,%edi
mov    $0xf7,%ecx
xor    %eax,%eax
call   1060 <__print_result_location@plt>
mov    %r13d,%eax
add    $0xa8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.i2c_adapter = type opaque
%struct.i2c_adapter.0 = type opaque
@.str = [190 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/Ahmed-Hady/android_kernel_nokia_normandy/refs/heads/Stock1.1.2.2/drivers/misc/inv_mpu/mlsl-kernel.c\00"
@__func__.inv_serial_write = [17 x i8] c"inv_serial_write\00"
define i32 @inv_serial_write(i8* %0, i8 zeroext %1, i16 zeroext %2, i8* %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8
%8 = alloca i16
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i16
%12 = alloca i8
%13 = alloca [129 x i8]
%14 = alloca i16
%15 = alloca i16
store i8* %0, i8** %6
store i8 %1, i8* %7
store i16 %2, i16* %8
store i8* %3, i8** %9
%16 = load i16, i16* %8
%17 = zext i16 %16 to i32
%18 = sub nsw i32 %17, 1
%19 = trunc i32 %18 to i16
store i16 %19, i16* %11
%20 = load i8*, i8** %9
%21 = getelementptr inbounds i8, i8* %20, i64 0
%22 = load i8, i8* %21
store i8 %22, i8* %12
store i16 0, i16* %14
br label %23
23: ; preds = %85, %4
%24 = load i16, i16* %14
%25 = zext i16 %24 to i32
%26 = load i16, i16* %11
%27 = zext i16 %26 to i32
%28 = icmp slt i32 %25, %27
br i1 %28, label %29, label %92
29: ; preds = %23
%30 = load i16, i16* %11
%31 = zext i16 %30 to i32
%32 = load i16, i16* %14
%33 = zext i16 %32 to i32
%34 = sub nsw i32 %31, %33
%35 = call i32 (i32, i32, ...) bitcast (i32 (...)* @min to i32 (i32, i32, ...)*)(i32 128, i32 %34)
%36 = trunc i32 %35 to i16
store i16 %36, i16* %15
%37 = load i16, i16* %14
%38 = zext i16 %37 to i32
%39 = icmp eq i32 %38, 0
br i1 %39, label %40, label %50
40: ; preds = %29
%41 = load i8*, i8** %6
%42 = bitcast i8* %41 to %struct.i2c_adapter*
%43 = load i8, i8* %7
%44 = zext i8 %43 to i32
%45 = load i16, i16* %15
%46 = zext i16 %45 to i32
%47 = add nsw i32 1, %46
%48 = load i8*, i8** %9
%49 = call i32 (%struct.i2c_adapter*, i32, i32, i8*, ...) bitcast (i32 (...)* @inv_i2c_write to i32 (%struct.i2c_adapter*, i32, i32, i8*, ...)*)(%struct.i2c_adapter* %42, i32 %44, i32 %47, i8* %48)
store i32 %49, i32* %10
br label %76
50: ; preds = %29
%51 = load i8, i8* %12
%52 = zext i8 %51 to i32
%53 = load i16, i16* %14
%54 = zext i16 %53 to i32
%55 = add nsw i32 %52, %54
%56 = trunc i32 %55 to i8
%57 = getelementptr inbounds [129 x i8], [129 x i8]* %13, i64 0, i64 0
store i8 %56, i8* %57
%58 = getelementptr inbounds [129 x i8], [129 x i8]* %13, i64 0, i64 1
%59 = load i8*, i8** %9
%60 = load i16, i16* %14
%61 = zext i16 %60 to i32
%62 = add nsw i32 1, %61
%63 = sext i32 %62 to i64
%64 = getelementptr inbounds i8, i8* %59, i64 %63
%65 = load i16, i16* %15
%66 = zext i16 %65 to i64
%67 = load i8*, i8** %6
%68 = bitcast i8* %67 to %struct.i2c_adapter.0*
%69 = load i8, i8* %7
%70 = zext i8 %69 to i32
%71 = load i16, i16* %15
%72 = zext i16 %71 to i32
%73 = add nsw i32 1, %72
%74 = getelementptr inbounds [129 x i8], [129 x i8]* %13, i64 0, i64 0
%75 = call i32 (%struct.i2c_adapter.0*, i32, i32, i8*, ...) bitcast (i32 (...)* @inv_i2c_write to i32 (%struct.i2c_adapter.0*, i32, i32, i8*, ...)*)(%struct.i2c_adapter.0* %68, i32 %70, i32 %73, i8* %74)
store i32 %75, i32* %10
br label %76
76: ; preds = %50, %40
%77 = load i32, i32* %10
%78 = icmp ne i32 %77, 0
br i1 %78, label %79, label %85
79: ; preds = %76
br label %80
80: ; preds = %79
%81 = load i32, i32* %10
%82 = call i32 (i32, i8*, i8*, i32, ...) bitcast (i32 (...)* @__print_result_location to i32 (i32, i8*, i8*, i32, ...)*)(i32 %81, i8* getelementptr inbounds ([190 x i8], [190 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([17 x i8], [17 x i8]* @__func__.inv_serial_write, i64 0, i64 0), i32 247)
br label %83
83: ; preds = %80
%84 = load i32, i32* %10
store i32 %84, i32* %5
br label %93
85: ; preds = %76
%86 = load i16, i16* %15
%87 = zext i16 %86 to i32
%88 = load i16, i16* %14
%89 = zext i16 %88 to i32
%90 = add nsw i32 %89, %87
%91 = trunc i32 %90 to i16
store i16 %91, i16* %14
br label %23
92: ; preds = %23
store i32 0, i32* %5
br label %93
93: ; preds = %92, %83
%94 = load i32, i32* %5
ret i32 %94
}

LLVM IR (-O2):
%struct.i2c_adapter.0 = type opaque
%struct.i2c_adapter = type opaque
@.str = [190 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/Ahmed-Hady/android_kernel_nokia_normandy/refs/heads/Stock1.1.2.2/drivers/misc/inv_mpu/mlsl-kernel.c\00"
@__func__.inv_serial_write = [17 x i8] c"inv_serial_write\00"
define i32 @inv_serial_write(i8* %0, i8 zeroext %1, i16 zeroext %2, i8* %3) {
%5 = alloca [129 x i8]
%6 = add i16 %2, -1
%7 = load i8, i8* %3
%8 = getelementptr inbounds [129 x i8], [129 x i8]* %5, i64 0, i64 0
%9 = icmp eq i16 %6, 0
br i1 %9, label %46, label %10
10: ; preds = %4
%11 = zext i16 %6 to i32
%12 = getelementptr inbounds [129 x i8], [129 x i8]* %5, i64 0, i64 1
%13 = bitcast i8* %0 to %struct.i2c_adapter.0*
%14 = zext i8 %1 to i32
%15 = bitcast i8* %0 to %struct.i2c_adapter*
br label %19
16: ; preds = %39
%17 = zext i16 %43 to i32
%18 = icmp ugt i16 %6, %43
br i1 %18, label %19, label %46
19: ; preds = %10, %16
%20 = phi i32 [ 0, %10 ], [ %17, %16 ]
%21 = phi i16 [ 0, %10 ], [ %43, %16 ]
%22 = sub nsw i32 %11, %20
%23 = call i32 (i32, i32, ...) bitcast (i32 (...)* @min to i32 (i32, i32, ...)*)(i32 128, i32 %22)
%24 = icmp eq i16 %21, 0
br i1 %24, label %25, label %29
25: ; preds = %19
%26 = and i32 %23, 65535
%27 = add nuw nsw i32 %26, 1
%28 = call i32 (%struct.i2c_adapter*, i32, i32, i8*, ...) bitcast (i32 (...)* @inv_i2c_write to i32 (%struct.i2c_adapter*, i32, i32, i8*, ...)*)(%struct.i2c_adapter* %15, i32 %14, i32 %27, i8* %3)
br label %39
29: ; preds = %19
%30 = trunc i16 %21 to i8
%31 = add i8 %7, %30
store i8 %31, i8* %8
%32 = add nuw nsw i32 %20, 1
%33 = zext i32 %32 to i64
%34 = getelementptr inbounds i8, i8* %3, i64 %33
%35 = and i32 %23, 65535
%36 = zext i32 %35 to i64
%37 = add nuw nsw i32 %35, 1
%38 = call i32 (%struct.i2c_adapter.0*, i32, i32, i8*, ...) bitcast (i32 (...)* @inv_i2c_write to i32 (%struct.i2c_adapter.0*, i32, i32, i8*, ...)*)(%struct.i2c_adapter.0* %13, i32 %14, i32 %37, i8* %8)
br label %39
39: ; preds = %29, %25
%40 = phi i32 [ %28, %25 ], [ %38, %29 ]
%41 = icmp eq i32 %40, 0
%42 = trunc i32 %23 to i16
%43 = add i16 %21, %42
br i1 %41, label %16, label %44
44: ; preds = %39
%45 = call i32 (i32, i8*, i8*, i32, ...) bitcast (i32 (...)* @__print_result_location to i32 (i32, i8*, i8*, i32, ...)*)(i32 %40, i8* getelementptr inbounds ([190 x i8], [190 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([17 x i8], [17 x i8]* @__func__.inv_serial_write, i64 0, i64 0), i32 247)
br label %46
46: ; preds = %16, %4, %44
%47 = phi i32 [ %40, %44 ], [ 0, %4 ], [ 0, %16 ]
ret i32 %47
}