#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*/ resp;

/* Variables and functions */
int ipmi_cmd_mc(int, unsigned char *, unsigned char, unsigned char *, int *,
                unsigned char *, char);
int /*<<< orphan*/ memcpy(unsigned char *, unsigned char *, int);
int /*<<< orphan*/ printf(char *, ...);
int write_fru_data(unsigned char id, unsigned short offset, unsigned char *data,
                   int dlen, char fdebug) {
  int ret = -1;
  int chunk;
  unsigned short fruoff;
  unsigned char req[25];
  unsigned char resp[16];
  int sresp;
  unsigned char cc;
  int i, j;
  req[0] = id;
  fruoff = offset;
  chunk = 16;
  for (i = 0; i < dlen; i += chunk) {
    req[1] = fruoff & 0x00ff;
    req[2] = (fruoff & 0xff00) >> 8;
    if ((i + chunk) > dlen)
      chunk = dlen - i;
    memcpy(&req[3], &data[i], chunk);
    if (fdebug) {
      printf("write_fru_data[%d] (len=%d): ", i, chunk + 3);
      for (j = 0; j < chunk + 3; j++)
        printf("%02x ", req[j]);
      printf("\n");
    }
    sresp = sizeof(resp);
    ret = ipmi_cmd_mc((0x12 | (0x0a << 8)), req, (unsigned char)(chunk + 3),
                      resp, &sresp, &cc, fdebug);
    if ((ret == 0) && (cc != 0))
      ret = cc & 0x00ff;
    if (fdebug && ret == 0)
      printf("write_fru_data[%d]: %d bytes written\n", i, resp[0]);
    if (ret != 0)
      break;
    fruoff += chunk;
  }
  return (ret);
}


x86 assembly (clang, -O0):
<write_fru_data>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %r8b,%al
mov    %dil,-0x1(%rbp)
mov    %si,-0x4(%rbp)
mov    %rdx,-0x10(%rbp)
mov    %ecx,-0x14(%rbp)
mov    %al,-0x15(%rbp)
movl   $0xffffffff,-0x1c(%rbp)
mov    -0x1(%rbp),%al
mov    %al,-0x40(%rbp)
mov    -0x4(%rbp),%ax
mov    %ax,-0x22(%rbp)
movl   $0x10,-0x20(%rbp)
movl   $0x0,-0x5c(%rbp)
mov    -0x5c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    12e3 <write_fru_data+0x1b3>
movzwl -0x22(%rbp),%eax
and    $0xff,%eax
mov    %al,-0x3f(%rbp)
movzwl -0x22(%rbp),%eax
and    $0xff00,%eax
sar    $0x8,%eax
mov    %al,-0x3e(%rbp)
mov    -0x5c(%rbp),%eax
add    -0x20(%rbp),%eax
cmp    -0x14(%rbp),%eax
jle    11af <write_fru_data+0x7f>
mov    -0x14(%rbp),%eax
sub    -0x5c(%rbp),%eax
mov    %eax,-0x20(%rbp)
lea    -0x40(%rbp),%rdi
add    $0x3,%rdi
mov    -0x10(%rbp),%rsi
movslq -0x5c(%rbp),%rax
add    %rax,%rsi
mov    -0x20(%rbp),%edx
call   1050 <memcpy@plt>
cmpb   $0x0,-0x15(%rbp)
je     1236 <write_fru_data+0x106>
mov    -0x5c(%rbp),%esi
mov    -0x20(%rbp),%edx
add    $0x3,%edx
lea    0xe1c(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x60(%rbp)
mov    -0x60(%rbp),%eax
mov    -0x20(%rbp),%ecx
add    $0x3,%ecx
cmp    %ecx,%eax
jge    1228 <write_fru_data+0xf8>
movslq -0x60(%rbp),%rax
movzbl -0x40(%rbp,%rax,1),%esi
lea    0xe0b(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x60(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x60(%rbp)
jmp    11f2 <write_fru_data+0xc2>
lea    0xe19(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x10,-0x54(%rbp)
lea    -0x40(%rbp),%rsi
mov    -0x20(%rbp),%eax
add    $0x3,%eax
lea    -0x50(%rbp),%rcx
mov    $0xa12,%edi
lea    -0x54(%rbp),%r8
lea    -0x55(%rbp),%r9
movzbl %al,%edx
movsbl -0x15(%rbp),%eax
mov    %eax,(%rsp)
call   1030 <ipmi_cmd_mc@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jne    128d <write_fru_data+0x15d>
movzbl -0x55(%rbp),%eax
cmp    $0x0,%eax
je     128d <write_fru_data+0x15d>
movzbl -0x55(%rbp),%eax
and    $0xff,%eax
mov    %eax,-0x1c(%rbp)
movsbl -0x15(%rbp),%eax
cmp    $0x0,%eax
je     12b9 <write_fru_data+0x189>
cmpl   $0x0,-0x1c(%rbp)
jne    12b9 <write_fru_data+0x189>
mov    -0x5c(%rbp),%esi
movzbl -0x50(%rbp),%edx
lea    0xd72(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
cmpl   $0x0,-0x1c(%rbp)
je     12c8 <write_fru_data+0x198>
jmp    12e3 <write_fru_data+0x1b3>
mov    -0x20(%rbp),%ecx
movzwl -0x22(%rbp),%eax
add    %ecx,%eax
mov    %ax,-0x22(%rbp)
mov    -0x20(%rbp),%eax
add    -0x5c(%rbp),%eax
mov    %eax,-0x5c(%rbp)
jmp    1170 <write_fru_data+0x40>
mov    -0x1c(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_fru_data>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x68,%rsp
mov    %rdx,0x28(%rsp)
mov    %dil,0x30(%rsp)
test   %ecx,%ecx
jle    129a <write_fru_data+0x15a>
mov    %r8d,%ebx
mov    %ecx,%ebp
mov    %esi,%r13d
mov    $0x10,%r12d
movsbl %bl,%eax
mov    %eax,0x20(%rsp)
xor    %r14d,%r14d
mov    %ecx,0x1c(%rsp)
mov    %r8d,0x18(%rsp)
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %r12d,%eax
mov    %r13w,0x31(%rsp)
lea    (%r14,%rax,1),%ecx
mov    %ebp,%r12d
sub    %r14d,%r12d
cmp    %ebp,%ecx
cmovle %eax,%r12d
movslq %r14d,%rsi
add    0x28(%rsp),%rsi
lea    0x33(%rsp),%rdi
mov    %r12d,%edx
call   1060 <memcpy@plt>
test   %bl,%bl
je     121a <write_fru_data+0xda>
lea    0x3(%r12),%ebp
lea    0xe32(%rip),%rdi
mov    %r14d,%esi
mov    %ebp,%edx
xor    %eax,%eax
call   1050 <printf@plt>
cmp    $0xfffffffe,%r12d
lea    0xe39(%rip),%r15
jl     1208 <write_fru_data+0xc8>
mov    %ebp,%ebp
xor    %ebx,%ebx
nopl   0x0(%rax,%rax,1)
movzbl 0x30(%rsp,%rbx,1),%esi
mov    %r15,%rdi
xor    %eax,%eax
call   1050 <printf@plt>
add    $0x1,%rbx
cmp    %rbx,%rbp
jne    11f0 <write_fru_data+0xb0>
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    0x18(%rsp),%ebx
mov    0x1c(%rsp),%ebp
movl   $0x10,0x24(%rsp)
lea    0x3(%r12),%eax
mov    0x20(%rsp),%ecx
mov    %ecx,(%rsp)
movzbl %al,%edx
mov    $0xa12,%edi
lea    0x30(%rsp),%rsi
lea    0x50(%rsp),%rcx
lea    0x24(%rsp),%r8
lea    0x17(%rsp),%r9
call   1040 <ipmi_cmd_mc@plt>
movzbl 0x17(%rsp),%r15d
test   %r15d,%r15d
cmove  %eax,%r15d
test   %eax,%eax
cmovne %eax,%r15d
test   %bl,%bl
je     1281 <write_fru_data+0x141>
test   %r15d,%r15d
jne    1281 <write_fru_data+0x141>
movzbl 0x50(%rsp),%edx
lea    0xdad(%rip),%rdi
mov    %r14d,%esi
xor    %eax,%eax
call   1050 <printf@plt>
test   %r15d,%r15d
jne    12a0 <write_fru_data+0x160>
add    %r12d,%r13d
add    %r12d,%r14d
cmp    %ebp,%r14d
jl     1190 <write_fru_data+0x50>
xor    %r15d,%r15d
jmp    12a0 <write_fru_data+0x160>
mov    $0xffffffff,%r15d
mov    %r15d,%eax
add    $0x68,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [30 x i8] c"write_fru_data[%d] (len=%d): \00"
@.str.1 = [6 x i8] c"%02x \00"
@.str.2 = [2 x i8] c"\0A\00"
@.str.3 = [38 x i8] c"write_fru_data[%d]: %d bytes written\0A\00"
define i32 @write_fru_data(i8 zeroext %0, i16 zeroext %1, i8* %2, i32 %3, i8 signext %4) {
%6 = alloca i8
%7 = alloca i16
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i8
%11 = alloca i32
%12 = alloca i32
%13 = alloca i16
%14 = alloca [25 x i8]
%15 = alloca [16 x i8]
%16 = alloca i32
%17 = alloca i8
%18 = alloca i32
%19 = alloca i32
store i8 %0, i8* %6
store i16 %1, i16* %7
store i8* %2, i8** %8
store i32 %3, i32* %9
store i8 %4, i8* %10
store i32 -1, i32* %11
%20 = load i8, i8* %6
%21 = getelementptr inbounds [25 x i8], [25 x i8]* %14, i64 0, i64 0
store i8 %20, i8* %21
%22 = load i16, i16* %7
store i16 %22, i16* %13
store i32 16, i32* %12
store i32 0, i32* %18
br label %23
23: ; preds = %121, %5
%24 = load i32, i32* %18
%25 = load i32, i32* %9
%26 = icmp slt i32 %24, %25
br i1 %26, label %27, label %125
27: ; preds = %23
%28 = load i16, i16* %13
%29 = zext i16 %28 to i32
%30 = and i32 %29, 255
%31 = trunc i32 %30 to i8
%32 = getelementptr inbounds [25 x i8], [25 x i8]* %14, i64 0, i64 1
store i8 %31, i8* %32
%33 = load i16, i16* %13
%34 = zext i16 %33 to i32
%35 = and i32 %34, 65280
%36 = ashr i32 %35, 8
%37 = trunc i32 %36 to i8
%38 = getelementptr inbounds [25 x i8], [25 x i8]* %14, i64 0, i64 2
store i8 %37, i8* %38
%39 = load i32, i32* %18
%40 = load i32, i32* %12
%41 = add nsw i32 %39, %40
%42 = load i32, i32* %9
%43 = icmp sgt i32 %41, %42
br i1 %43, label %44, label %48
44: ; preds = %27
%45 = load i32, i32* %9
%46 = load i32, i32* %18
%47 = sub nsw i32 %45, %46
store i32 %47, i32* %12
br label %48
48: ; preds = %44, %27
%49 = getelementptr inbounds [25 x i8], [25 x i8]* %14, i64 0, i64 3
%50 = load i8*, i8** %8
%51 = load i32, i32* %18
%52 = sext i32 %51 to i64
%53 = getelementptr inbounds i8, i8* %50, i64 %52
%54 = load i32, i32* %12
%55 = call i32 @memcpy(i8* %49, i8* %53, i32 %54)
%56 = load i8, i8* %10
%57 = icmp ne i8 %56, 0
br i1 %57, label %58, label %80
58: ; preds = %48
%59 = load i32, i32* %18
%60 = load i32, i32* %12
%61 = add nsw i32 %60, 3
%62 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i32 %59, i32 %61)
store i32 0, i32* %19
br label %63
63: ; preds = %75, %58
%64 = load i32, i32* %19
%65 = load i32, i32* %12
%66 = add nsw i32 %65, 3
%67 = icmp slt i32 %64, %66
br i1 %67, label %68, label %78
68: ; preds = %63
%69 = load i32, i32* %19
%70 = sext i32 %69 to i64
%71 = getelementptr inbounds [25 x i8], [25 x i8]* %14, i64 0, i64 %70
%72 = load i8, i8* %71
%73 = zext i8 %72 to i32
%74 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i32 %73)
br label %75
75: ; preds = %68
%76 = load i32, i32* %19
%77 = add nsw i32 %76, 1
store i32 %77, i32* %19
br label %63
78: ; preds = %63
%79 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
br label %80
80: ; preds = %78, %48
store i32 16, i32* %16
%81 = getelementptr inbounds [25 x i8], [25 x i8]* %14, i64 0, i64 0
%82 = load i32, i32* %12
%83 = add nsw i32 %82, 3
%84 = trunc i32 %83 to i8
%85 = getelementptr inbounds [16 x i8], [16 x i8]* %15, i64 0, i64 0
%86 = load i8, i8* %10
%87 = call i32 @ipmi_cmd_mc(i32 2578, i8* %81, i8 zeroext %84, i8* %85, i32* %16, i8* %17, i8 signext %86)
store i32 %87, i32* %11
%88 = load i32, i32* %11
%89 = icmp eq i32 %88, 0
br i1 %89, label %90, label %98
90: ; preds = %80
%91 = load i8, i8* %17
%92 = zext i8 %91 to i32
%93 = icmp ne i32 %92, 0
br i1 %93, label %94, label %98
94: ; preds = %90
%95 = load i8, i8* %17
%96 = zext i8 %95 to i32
%97 = and i32 %96, 255
store i32 %97, i32* %11
br label %98
98: ; preds = %94, %90, %80
%99 = load i8, i8* %10
%100 = sext i8 %99 to i32
%101 = icmp ne i32 %100, 0
br i1 %101, label %102, label %111
102: ; preds = %98
%103 = load i32, i32* %11
%104 = icmp eq i32 %103, 0
br i1 %104, label %105, label %111
105: ; preds = %102
%106 = load i32, i32* %18
%107 = getelementptr inbounds [16 x i8], [16 x i8]* %15, i64 0, i64 0
%108 = load i8, i8* %107
%109 = zext i8 %108 to i32
%110 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.3, i64 0, i64 0), i32 %106, i32 %109)
br label %111
111: ; preds = %105, %102, %98
%112 = load i32, i32* %11
%113 = icmp ne i32 %112, 0
br i1 %113, label %114, label %115
114: ; preds = %111
br label %125
115: ; preds = %111
%116 = load i32, i32* %12
%117 = load i16, i16* %13
%118 = zext i16 %117 to i32
%119 = add nsw i32 %118, %116
%120 = trunc i32 %119 to i16
store i16 %120, i16* %13
br label %121
121: ; preds = %115
%122 = load i32, i32* %12
%123 = load i32, i32* %18
%124 = add nsw i32 %123, %122
store i32 %124, i32* %18
br label %23
125: ; preds = %114, %23
%126 = load i32, i32* %11
ret i32 %126
}

LLVM IR (-O2):
@.str = [30 x i8] c"write_fru_data[%d] (len=%d): \00"
@.str.1 = [6 x i8] c"%02x \00"
@.str.3 = [38 x i8] c"write_fru_data[%d]: %d bytes written\0A\00"
define i32 @write_fru_data(i8 zeroext %0, i16 zeroext %1, i8* %2, i32 %3, i8 signext %4) {
%6 = alloca [25 x i8]
%7 = alloca [16 x i8]
%8 = alloca i32
%9 = alloca i8
%10 = getelementptr inbounds [25 x i8], [25 x i8]* %6, i64 0, i64 0
%11 = getelementptr inbounds [16 x i8], [16 x i8]* %7, i64 0, i64 0
%12 = bitcast i32* %8 to i8*
store i8 %0, i8* %10
%13 = icmp sgt i32 %3, 0
br i1 %13, label %14, label %72
14: ; preds = %5
%15 = getelementptr inbounds [25 x i8], [25 x i8]* %6, i64 0, i64 1
%16 = getelementptr inbounds [25 x i8], [25 x i8]* %6, i64 0, i64 2
%17 = getelementptr inbounds [25 x i8], [25 x i8]* %6, i64 0, i64 3
%18 = icmp eq i8 %4, 0
%19 = icmp ne i8 %4, 0
br label %20
20: ; preds = %14, %67
%21 = phi i32 [ 0, %14 ], [ %70, %67 ]
%22 = phi i16 [ %1, %14 ], [ %69, %67 ]
%23 = phi i32 [ 16, %14 ], [ %30, %67 ]
%24 = trunc i16 %22 to i8
store i8 %24, i8* %15
%25 = lshr i16 %22, 8
%26 = trunc i16 %25 to i8
store i8 %26, i8* %16
%27 = add nsw i32 %21, %23
%28 = icmp sgt i32 %27, %3
%29 = sub nsw i32 %3, %21
%30 = select i1 %28, i32 %29, i32 %23
%31 = sext i32 %21 to i64
%32 = getelementptr inbounds i8, i8* %2, i64 %31
%33 = call i32 @memcpy(i8* %17, i8* %32, i32 %30)
br i1 %18, label %50, label %34
34: ; preds = %20
%35 = add nsw i32 %30, 3
%36 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i32 %21, i32 %35)
%37 = icmp sgt i32 %30, -3
br i1 %37, label %38, label %48
38: ; preds = %34
%39 = zext i32 %35 to i64
br label %40
40: ; preds = %38, %40
%41 = phi i64 [ 0, %38 ], [ %46, %40 ]
%42 = getelementptr inbounds [25 x i8], [25 x i8]* %6, i64 0, i64 %41
%43 = load i8, i8* %42
%44 = zext i8 %43 to i32
%45 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i32 %44)
%46 = add nuw nsw i64 %41, 1
%47 = icmp eq i64 %46, %39
br i1 %47, label %48, label %40
48: ; preds = %40, %34
%49 = call i32 @putchar(i32 10)
br label %50
50: ; preds = %48, %20
store i32 16, i32* %8
%51 = trunc i32 %30 to i8
%52 = add i8 %51, 3
%53 = call i32 @ipmi_cmd_mc(i32 2578, i8* %10, i8 zeroext %52, i8* %11, i32* %8, i8* %9, i8 signext %4)
%54 = icmp eq i32 %53, 0
%55 = load i8, i8* %9
%56 = icmp ne i8 %55, 0
%57 = select i1 %54, i1 %56, i1 false
%58 = zext i8 %55 to i32
%59 = select i1 %57, i32 %58, i32 %53
%60 = icmp eq i32 %59, 0
%61 = select i1 %19, i1 %60, i1 false
br i1 %61, label %62, label %66
62: ; preds = %50
%63 = load i8, i8* %11
%64 = zext i8 %63 to i32
%65 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([38 x i8], [38 x i8]* @.str.3, i64 0, i64 0), i32 %21, i32 %64)
br label %66
66: ; preds = %62, %50
br i1 %60, label %67, label %72
67: ; preds = %66
%68 = trunc i32 %30 to i16
%69 = add i16 %22, %68
%70 = add nsw i32 %30, %21
%71 = icmp slt i32 %70, %3
br i1 %71, label %20, label %72
72: ; preds = %67, %66, %5
%73 = phi i32 [ -1, %5 ], [ %59, %66 ], [ 0, %67 ]
ret i32 %73
}