#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 */
struct FRU_DATA {
  int dummy;
};
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ STDOUT_FILENO;
unsigned char *build_FRU_blob(struct FRU_DATA *, size_t *, int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ free(unsigned char *);
int /*<<< orphan*/ fwrite(unsigned char *, int, size_t, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf_err(char *);
int /*<<< orphan*/ printf_info(char *, size_t, char *);
int /*<<< orphan*/ strcmp(char *, char *);
size_t write(int /*<<< orphan*/, unsigned char *, size_t);
void write_FRU(struct FRU_DATA *fru, char *file_name, bool packed) {
  size_t tmp, len;
  unsigned char *buf = NULL;
  FILE *fp;
  buf = build_FRU_blob(fru, &len, packed);
  if (len >= 255 && !packed) {
    free(buf);
    buf = build_FRU_blob(fru, &len, 1);
    if (len >= 255) {
      free(buf);
      printf_err("Not able to pack things into 255 char, no output\n");
      return;
    }
  }
  if (!strcmp("-", file_name)) {
    tmp = write(STDOUT_FILENO, buf, len);
    if (tmp != len)
      printf_err("Didn't write entire file\n");
  } else {
    if ((fp = fopen(file_name, "wb")) == NULL)
      printf_err("Cannot open file.\n");
    fwrite(buf, 1, len, fp);
    fclose(fp);
  }
  printf_info("wrote %i bytes to %s\n", len, file_name);
  free(buf);
}


x86 assembly (clang, -O0):
<write_FRU>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movq   $0x0,-0x30(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x14(%rbp),%edx
lea    -0x28(%rbp),%rsi
call   1070 <build_FRU_blob@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0xff,-0x28(%rbp)
jb     1223 <write_FRU+0x93>
cmpl   $0x0,-0x14(%rbp)
jne    1223 <write_FRU+0x93>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    -0x8(%rbp),%rdi
lea    -0x28(%rbp),%rsi
mov    $0x1,%edx
call   1070 <build_FRU_blob@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0xff,-0x28(%rbp)
jb     121e <write_FRU+0x8e>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
lea    0xdec(%rip),%rdi
call   1080 <printf_err@plt>
jmp    12db <write_FRU+0x14b>
jmp    1223 <write_FRU+0x93>
mov    -0x10(%rbp),%rsi
lea    0xe04(%rip),%rdi
call   1090 <strcmp@plt>
cmp    $0x0,%eax
jne    1275 <write_FRU+0xe5>
mov    0x2dad(%rip),%rax
mov    (%rax),%edi
mov    -0x30(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1050 <write@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
cmp    -0x28(%rbp),%rax
je     1270 <write_FRU+0xe0>
lea    0xdc9(%rip),%rdi
call   1080 <printf_err@plt>
jmp    12be <write_FRU+0x12e>
mov    -0x10(%rbp),%rdi
lea    0xdce(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x38(%rbp)
cmp    $0x0,%rax
jne    129f <write_FRU+0x10f>
lea    0xdb7(%rip),%rdi
call   1080 <printf_err@plt>
mov    -0x30(%rbp),%rdi
mov    -0x28(%rbp),%rdx
mov    -0x38(%rbp),%rcx
mov    $0x1,%esi
call   10b0 <fwrite@plt>
mov    -0x38(%rbp),%rdi
call   1060 <fclose@plt>
mov    -0x28(%rbp),%rsi
mov    -0x10(%rbp),%rdx
lea    0xd97(%rip),%rdi
call   1040 <printf_info@plt>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_FRU>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %edx,%ebp
mov    %rsi,%r14
mov    %rdi,%rbx
lea    0x8(%rsp),%rsi
call   1070 <build_FRU_blob@plt>
mov    %rax,%r15
mov    0x8(%rsp),%r12
test   %ebp,%ebp
jne    1207 <write_FRU+0x77>
cmp    $0xff,%r12
jb     1207 <write_FRU+0x77>
mov    %r15,%rdi
call   1030 <free@plt>
lea    0x8(%rsp),%rsi
mov    %rbx,%rdi
mov    $0x1,%edx
call   1070 <build_FRU_blob@plt>
mov    %rax,%r15
mov    0x8(%rsp),%r12
cmp    $0xff,%r12
jb     1207 <write_FRU+0x77>
mov    %r15,%rdi
call   1030 <free@plt>
lea    0xe03(%rip),%rdi
call   1080 <printf_err@plt>
jmp    129f <write_FRU+0x10f>
lea    0xe24(%rip),%rdi
mov    %r14,%rsi
call   1090 <strcmp@plt>
test   %eax,%eax
je     125c <write_FRU+0xcc>
lea    0xe2d(%rip),%rsi
mov    %r14,%rdi
call   10a0 <fopen@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    123d <write_FRU+0xad>
lea    0xe19(%rip),%rdi
call   1080 <printf_err@plt>
mov    0x8(%rsp),%rdx
mov    %r15,%rdi
mov    $0x1,%esi
mov    %rbx,%rcx
call   10b0 <fwrite@plt>
mov    %rbx,%rdi
call   1060 <fclose@plt>
jmp    1283 <write_FRU+0xf3>
mov    0x2d8d(%rip),%rax
mov    (%rax),%edi
mov    %r15,%rsi
mov    %r12,%rdx
call   1050 <write@plt>
cmp    0x8(%rsp),%rax
je     1283 <write_FRU+0xf3>
lea    0xdb6(%rip),%rdi
call   1080 <printf_err@plt>
mov    0x8(%rsp),%rsi
lea    0xdd5(%rip),%rdi
mov    %r14,%rdx
call   1040 <printf_info@plt>
mov    %r15,%rdi
call   1030 <free@plt>
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.FRU_DATA = type { i32 }
@.str = [50 x i8] c"Not able to pack things into 255 char, no output\0A\00"
@.str.1 = [2 x i8] c"-\00"
@STDOUT_FILENO = global i32 0
@.str.2 = [26 x i8] c"Didn't write entire file\0A\00"
@.str.3 = [3 x i8] c"wb\00"
@.str.4 = [19 x i8] c"Cannot open file.\0A\00"
@.str.5 = [22 x i8] c"wrote %i bytes to %s\0A\00"
define void @write_FRU(%struct.FRU_DATA* %0, i8* %1, i32 %2) {
%4 = alloca %struct.FRU_DATA*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i64
%8 = alloca i64
%9 = alloca i8*
%10 = alloca i32*
store %struct.FRU_DATA* %0, %struct.FRU_DATA** %4
store i8* %1, i8** %5
store i32 %2, i32* %6
store i8* null, i8** %9
%11 = load %struct.FRU_DATA*, %struct.FRU_DATA** %4
%12 = load i32, i32* %6
%13 = call i8* @build_FRU_blob(%struct.FRU_DATA* %11, i64* %8, i32 %12)
store i8* %13, i8** %9
%14 = load i64, i64* %8
%15 = icmp uge i64 %14, 255
br i1 %15, label %16, label %31
16: ; preds = %3
%17 = load i32, i32* %6
%18 = icmp ne i32 %17, 0
br i1 %18, label %31, label %19
19: ; preds = %16
%20 = load i8*, i8** %9
%21 = call i32 @free(i8* %20)
%22 = load %struct.FRU_DATA*, %struct.FRU_DATA** %4
%23 = call i8* @build_FRU_blob(%struct.FRU_DATA* %22, i64* %8, i32 1)
store i8* %23, i8** %9
%24 = load i64, i64* %8
%25 = icmp uge i64 %24, 255
br i1 %25, label %26, label %30
26: ; preds = %19
%27 = load i8*, i8** %9
%28 = call i32 @free(i8* %27)
%29 = call i32 @printf_err(i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str, i64 0, i64 0))
br label %65
30: ; preds = %19
br label %31
31: ; preds = %30, %16, %3
%32 = load i8*, i8** %5
%33 = call i32 @strcmp(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i8* %32)
%34 = icmp ne i32 %33, 0
br i1 %34, label %46, label %35
35: ; preds = %31
%36 = load i32, i32* @STDOUT_FILENO
%37 = load i8*, i8** %9
%38 = load i64, i64* %8
%39 = call i64 @write(i32 %36, i8* %37, i64 %38)
store i64 %39, i64* %7
%40 = load i64, i64* %7
%41 = load i64, i64* %8
%42 = icmp ne i64 %40, %41
br i1 %42, label %43, label %45
43: ; preds = %35
%44 = call i32 @printf_err(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0))
br label %45
45: ; preds = %43, %35
br label %59
46: ; preds = %31
%47 = load i8*, i8** %5
%48 = call i32* @fopen(i8* %47, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
store i32* %48, i32** %10
%49 = icmp eq i32* %48, null
br i1 %49, label %50, label %52
50: ; preds = %46
%51 = call i32 @printf_err(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0))
br label %52
52: ; preds = %50, %46
%53 = load i8*, i8** %9
%54 = load i64, i64* %8
%55 = load i32*, i32** %10
%56 = call i32 @fwrite(i8* %53, i32 1, i64 %54, i32* %55)
%57 = load i32*, i32** %10
%58 = call i32 @fclose(i32* %57)
br label %59
59: ; preds = %52, %45
%60 = load i64, i64* %8
%61 = load i8*, i8** %5
%62 = call i32 @printf_info(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.5, i64 0, i64 0), i64 %60, i8* %61)
%63 = load i8*, i8** %9
%64 = call i32 @free(i8* %63)
br label %65
65: ; preds = %59, %26
ret void
}

LLVM IR (-O2):
%struct.FRU_DATA = type { i32 }
@.str = [50 x i8] c"Not able to pack things into 255 char, no output\0A\00"
@.str.1 = [2 x i8] c"-\00"
@STDOUT_FILENO = global i32 0
@.str.2 = [26 x i8] c"Didn't write entire file\0A\00"
@.str.3 = [3 x i8] c"wb\00"
@.str.4 = [19 x i8] c"Cannot open file.\0A\00"
@.str.5 = [22 x i8] c"wrote %i bytes to %s\0A\00"
define void @write_FRU(%struct.FRU_DATA* %0, i8* %1, i32 %2) {
%4 = alloca i64
%5 = bitcast i64* %4 to i8*
%6 = call i8* @build_FRU_blob(%struct.FRU_DATA* %0, i64* %4, i32 %2)
%7 = load i64, i64* %4
%8 = icmp ult i64 %7, 255
%9 = icmp ne i32 %2, 0
%10 = or i1 %9, %8
br i1 %10, label %19, label %11
11: ; preds = %3
%12 = call i32 @free(i8* %6)
%13 = call i8* @build_FRU_blob(%struct.FRU_DATA* %0, i64* %4, i32 1)
%14 = load i64, i64* %4
%15 = icmp ugt i64 %14, 254
br i1 %15, label %16, label %19
16: ; preds = %11
%17 = call i32 @free(i8* %13)
%18 = call i32 @printf_err(i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str, i64 0, i64 0))
br label %44
19: ; preds = %11, %3
%20 = phi i64 [ %7, %3 ], [ %14, %11 ]
%21 = phi i8* [ %6, %3 ], [ %13, %11 ]
%22 = call i32 @strcmp(i8* dereferenceable(2) getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i8* dereferenceable(1) %1)
%23 = icmp eq i32 %22, 0
br i1 %23, label %24, label %31
24: ; preds = %19
%25 = load i32, i32* @STDOUT_FILENO
%26 = call i64 @write(i32 %25, i8* %21, i64 %20)
%27 = load i64, i64* %4
%28 = icmp eq i64 %26, %27
br i1 %28, label %40, label %29
29: ; preds = %24
%30 = call i32 @printf_err(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0))
br label %40
31: ; preds = %19
%32 = call i32* @fopen(i8* %1, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
%33 = icmp eq i32* %32, null
br i1 %33, label %34, label %36
34: ; preds = %31
%35 = call i32 @printf_err(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0))
br label %36
36: ; preds = %34, %31
%37 = load i64, i64* %4
%38 = call i32 @fwrite(i8* %21, i32 1, i64 %37, i32* %32)
%39 = call i32 @fclose(i32* %32)
br label %40
40: ; preds = %24, %29, %36
%41 = load i64, i64* %4
%42 = call i32 @printf_info(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.5, i64 0, i64 0), i64 %41, i8* %1)
%43 = call i32 @free(i8* %21)
br label %44
44: ; preds = %40, %16
ret void
}