#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] = 0x00;
  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 += (unsigned short)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)
movb   $0x0,-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    12e4 <write_fru_data+0x1b4>
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    11ad <write_fru_data+0x7d>
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     1234 <write_fru_data+0x104>
mov    -0x5c(%rbp),%esi
mov    -0x20(%rbp),%edx
add    $0x3,%edx
lea    0xe1e(%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    1226 <write_fru_data+0xf6>
movslq -0x60(%rbp),%rax
movzbl -0x40(%rbp,%rax,1),%esi
lea    0xe0d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x60(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x60(%rbp)
jmp    11f0 <write_fru_data+0xc0>
lea    0xe1b(%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    128b <write_fru_data+0x15b>
movzbl -0x55(%rbp),%eax
cmp    $0x0,%eax
je     128b <write_fru_data+0x15b>
movzbl -0x55(%rbp),%eax
and    $0xff,%eax
mov    %eax,-0x1c(%rbp)
movsbl -0x15(%rbp),%eax
cmp    $0x0,%eax
je     12b7 <write_fru_data+0x187>
cmpl   $0x0,-0x1c(%rbp)
jne    12b7 <write_fru_data+0x187>
mov    -0x5c(%rbp),%esi
movzbl -0x50(%rbp),%edx
lea    0xd74(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
cmpl   $0x0,-0x1c(%rbp)
je     12c6 <write_fru_data+0x196>
jmp    12e4 <write_fru_data+0x1b4>
mov    -0x20(%rbp),%eax
movzwl %ax,%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    116e <write_fru_data+0x3e>
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)
movb   $0x0,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 = getelementptr inbounds [25 x i8], [25 x i8]* %14, i64 0, i64 0
store i8 0, i8* %20
%21 = load i16, i16* %7
store i16 %21, i16* %13
store i32 16, i32* %12
store i32 0, i32* %18
br label %22
22: ; preds = %122, %5
%23 = load i32, i32* %18
%24 = load i32, i32* %9
%25 = icmp slt i32 %23, %24
br i1 %25, label %26, label %126
26: ; preds = %22
%27 = load i16, i16* %13
%28 = zext i16 %27 to i32
%29 = and i32 %28, 255
%30 = trunc i32 %29 to i8
%31 = getelementptr inbounds [25 x i8], [25 x i8]* %14, i64 0, i64 1
store i8 %30, i8* %31
%32 = load i16, i16* %13
%33 = zext i16 %32 to i32
%34 = and i32 %33, 65280
%35 = ashr i32 %34, 8
%36 = trunc i32 %35 to i8
%37 = getelementptr inbounds [25 x i8], [25 x i8]* %14, i64 0, i64 2
store i8 %36, i8* %37
%38 = load i32, i32* %18
%39 = load i32, i32* %12
%40 = add nsw i32 %38, %39
%41 = load i32, i32* %9
%42 = icmp sgt i32 %40, %41
br i1 %42, label %43, label %47
43: ; preds = %26
%44 = load i32, i32* %9
%45 = load i32, i32* %18
%46 = sub nsw i32 %44, %45
store i32 %46, i32* %12
br label %47
47: ; preds = %43, %26
%48 = getelementptr inbounds [25 x i8], [25 x i8]* %14, i64 0, i64 3
%49 = load i8*, i8** %8
%50 = load i32, i32* %18
%51 = sext i32 %50 to i64
%52 = getelementptr inbounds i8, i8* %49, i64 %51
%53 = load i32, i32* %12
%54 = call i32 @memcpy(i8* %48, i8* %52, i32 %53)
%55 = load i8, i8* %10
%56 = icmp ne i8 %55, 0
br i1 %56, label %57, label %79
57: ; preds = %47
%58 = load i32, i32* %18
%59 = load i32, i32* %12
%60 = add nsw i32 %59, 3
%61 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i32 %58, i32 %60)
store i32 0, i32* %19
br label %62
62: ; preds = %74, %57
%63 = load i32, i32* %19
%64 = load i32, i32* %12
%65 = add nsw i32 %64, 3
%66 = icmp slt i32 %63, %65
br i1 %66, label %67, label %77
67: ; preds = %62
%68 = load i32, i32* %19
%69 = sext i32 %68 to i64
%70 = getelementptr inbounds [25 x i8], [25 x i8]* %14, i64 0, i64 %69
%71 = load i8, i8* %70
%72 = zext i8 %71 to i32
%73 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i32 %72)
br label %74
74: ; preds = %67
%75 = load i32, i32* %19
%76 = add nsw i32 %75, 1
store i32 %76, i32* %19
br label %62
77: ; preds = %62
%78 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
br label %79
79: ; preds = %77, %47
store i32 16, i32* %16
%80 = getelementptr inbounds [25 x i8], [25 x i8]* %14, i64 0, i64 0
%81 = load i32, i32* %12
%82 = add nsw i32 %81, 3
%83 = trunc i32 %82 to i8
%84 = getelementptr inbounds [16 x i8], [16 x i8]* %15, i64 0, i64 0
%85 = load i8, i8* %10
%86 = call i32 @ipmi_cmd_mc(i32 2578, i8* %80, i8 zeroext %83, i8* %84, i32* %16, i8* %17, i8 signext %85)
store i32 %86, i32* %11
%87 = load i32, i32* %11
%88 = icmp eq i32 %87, 0
br i1 %88, label %89, label %97
89: ; preds = %79
%90 = load i8, i8* %17
%91 = zext i8 %90 to i32
%92 = icmp ne i32 %91, 0
br i1 %92, label %93, label %97
93: ; preds = %89
%94 = load i8, i8* %17
%95 = zext i8 %94 to i32
%96 = and i32 %95, 255
store i32 %96, i32* %11
br label %97
97: ; preds = %93, %89, %79
%98 = load i8, i8* %10
%99 = sext i8 %98 to i32
%100 = icmp ne i32 %99, 0
br i1 %100, label %101, label %110
101: ; preds = %97
%102 = load i32, i32* %11
%103 = icmp eq i32 %102, 0
br i1 %103, label %104, label %110
104: ; preds = %101
%105 = load i32, i32* %18
%106 = getelementptr inbounds [16 x i8], [16 x i8]* %15, i64 0, i64 0
%107 = load i8, i8* %106
%108 = zext i8 %107 to i32
%109 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.3, i64 0, i64 0), i32 %105, i32 %108)
br label %110
110: ; preds = %104, %101, %97
%111 = load i32, i32* %11
%112 = icmp ne i32 %111, 0
br i1 %112, label %113, label %114
113: ; preds = %110
br label %126
114: ; preds = %110
%115 = load i32, i32* %12
%116 = trunc i32 %115 to i16
%117 = zext i16 %116 to i32
%118 = load i16, i16* %13
%119 = zext i16 %118 to i32
%120 = add nsw i32 %119, %117
%121 = trunc i32 %120 to i16
store i16 %121, i16* %13
br label %122
122: ; preds = %114
%123 = load i32, i32* %12
%124 = load i32, i32* %18
%125 = add nsw i32 %124, %123
store i32 %125, i32* %18
br label %22
126: ; preds = %113, %22
%127 = load i32, i32* %11
ret i32 %127
}

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
}