#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*/ xml_filename;
typedef int /*<<< orphan*/ time_stamp;
struct tm {
  int tm_year;
  int tm_mon;
  int tm_mday;
  int tm_hour;
  int tm_min;
  int tm_sec;
};

/* Variables and functions */
int PATH_MAX;
int /*<<< orphan*/ close_output_xml_file();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int get_current_time(struct tm *);
int get_system_vpd(char *, char *, char **);
int open_output_xml_file(char *);
int /*<<< orphan*/ populate_xml_footer();
int /*<<< orphan*/ populate_xml_header(char *, char *, char *, char *, char *);
int /*<<< orphan*/ remove_old_log_file();
int /*<<< orphan*/ snprintf(char *, int, char *, ...);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ sysfs_sg_disk_scan(char *, char *);
int diag_disk(char *disk_name) {
  char serial_num[8 + 1];
  char mach_type_model[8 + 1];
  char *mach_model = NULL;
  char time_stamp[256];
  char xml_filename[PATH_MAX];
  struct tm tm;
  int ret;
  ret = get_system_vpd(serial_num, mach_type_model, &mach_model);
  if (ret) {
    fprintf(stderr, "Unable to read system vpd.\n");
    return -1;
  }
  ret = get_current_time(&tm);
  if (ret) {
    fprintf(stderr, "Unable to read current time.\n");
    return -1;
  }
  snprintf(time_stamp, sizeof(time_stamp) - 1, "%d-%d-%dT%d:%d:%dZ\n",
           tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min,
           tm.tm_sec);
  snprintf(xml_filename, sizeof(xml_filename) - 1, "%s~%s~%s~diskAnalytics.xml",
           mach_type_model, mach_model, serial_num);
  remove_old_log_file();
  ret = open_output_xml_file(xml_filename);
  if (ret) {
    fprintf(stderr, "Unable to open output xml file.\n");
    return -1;
  }
  populate_xml_header(xml_filename, time_stamp, mach_type_model, mach_model,
                      serial_num);
  sysfs_sg_disk_scan("/sys/class/scsi_generic", disk_name);
  populate_xml_footer();
  close_output_xml_file();
  return 0;
}


x86 assembly (clang, -O0):
<diag_disk>:
push   %rbp
mov    %rsp,%rbp
sub    $0x170,%rsp
mov    %rdi,-0x10(%rbp)
movq   $0x0,-0x30(%rbp)
mov    0x2e32(%rip),%rax
mov    (%rax),%eax
mov    %rax,-0x170(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x138(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x168(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x140(%rbp)
lea    -0x19(%rbp),%rdi
lea    -0x22(%rbp),%rsi
lea    -0x30(%rbp),%rdx
call   1090 <get_system_vpd@plt>
mov    %eax,-0x15c(%rbp)
cmpl   $0x0,-0x15c(%rbp)
je     1242 <diag_disk+0xa2>
mov    0x2dba(%rip),%rax
mov    (%rax),%edi
lea    0xdd9(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x160(%rbp)
jmp    13ba <diag_disk+0x21a>
lea    -0x158(%rbp),%rdi
call   10a0 <get_current_time@plt>
mov    %eax,-0x15c(%rbp)
cmpl   $0x0,-0x15c(%rbp)
je     128c <diag_disk+0xec>
mov    0x2d70(%rip),%rax
mov    (%rax),%edi
lea    0xdab(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x160(%rbp)
jmp    13ba <diag_disk+0x21a>
lea    -0x130(%rbp),%rdi
mov    -0x158(%rbp),%ecx
add    $0x76c,%ecx
mov    -0x154(%rbp),%r8d
add    $0x1,%r8d
mov    -0x150(%rbp),%r9d
mov    -0x14c(%rbp),%r11d
mov    -0x148(%rbp),%r10d
mov    -0x144(%rbp),%eax
mov    $0xff,%esi
lea    0xd69(%rip),%rdx
sub    $0x20,%rsp
mov    %r11d,(%rsp)
mov    %r10d,0x8(%rsp)
mov    %eax,0x10(%rsp)
mov    $0x0,%al
call   1040 <snprintf@plt>
mov    -0x168(%rbp),%rdi
add    $0x20,%rsp
mov    -0x170(%rbp),%rax
sub    $0x1,%rax
mov    %eax,%esi
lea    -0x22(%rbp),%rcx
mov    -0x30(%rbp),%r8
lea    -0x19(%rbp),%r9
lea    0xd3a(%rip),%rdx
mov    $0x0,%al
call   1040 <snprintf@plt>
mov    $0x0,%al
call   1070 <remove_old_log_file@plt>
mov    -0x168(%rbp),%rdi
call   10b0 <open_output_xml_file@plt>
mov    %eax,-0x15c(%rbp)
cmpl   $0x0,-0x15c(%rbp)
je     136c <diag_disk+0x1cc>
mov    0x2c90(%rip),%rax
mov    (%rax),%edi
lea    0xd18(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x160(%rbp)
jmp    13ba <diag_disk+0x21a>
mov    -0x168(%rbp),%rdi
lea    -0x130(%rbp),%rsi
lea    -0x22(%rbp),%rdx
mov    -0x30(%rbp),%rcx
lea    -0x19(%rbp),%r8
call   1080 <populate_xml_header@plt>
mov    -0x10(%rbp),%rsi
lea    0xcf4(%rip),%rdi
call   10c0 <sysfs_sg_disk_scan@plt>
mov    $0x0,%al
call   1030 <populate_xml_footer@plt>
mov    $0x0,%al
call   1050 <close_output_xml_file@plt>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x160(%rbp)
mov    -0x138(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<diag_disk>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x148,%rsp
mov    %rdi,%r14
mov    %rsp,%r13
movq   $0x0,-0x30(%rbp)
mov    0x2e27(%rip),%rax
mov    (%rax),%r12d
mov    %rsp,%rbx
lea    0xf(%r12),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
lea    -0x42(%rbp),%rdi
lea    -0x39(%rbp),%rsi
lea    -0x30(%rbp),%rdx
call   1090 <get_system_vpd@plt>
test   %eax,%eax
je     1208 <diag_disk+0x68>
mov    0x2dde(%rip),%rax
mov    (%rax),%edi
lea    0xdfd(%rip),%rsi
jmp    12c0 <diag_disk+0x120>
lea    -0x60(%rbp),%rdi
call   10a0 <get_current_time@plt>
test   %eax,%eax
je     122a <diag_disk+0x8a>
mov    0x2dbc(%rip),%rax
mov    (%rax),%edi
lea    0xdf7(%rip),%rsi
jmp    12c0 <diag_disk+0x120>
mov    $0x76c,%ecx
add    -0x60(%rbp),%ecx
mov    -0x5c(%rbp),%r8d
mov    -0x58(%rbp),%r9d
add    $0x1,%r8d
mov    -0x54(%rbp),%eax
mov    %rax,-0x68(%rbp)
mov    -0x50(%rbp),%r11d
mov    -0x4c(%rbp),%r10d
sub    $0x8,%rsp
lea    0xde2(%rip),%rdx
xor    %r15d,%r15d
lea    -0x170(%rbp),%rdi
mov    $0xff,%esi
xor    %eax,%eax
push   %r10
push   %r11
push   -0x68(%rbp)
call   1040 <snprintf@plt>
add    $0x20,%rsp
add    $0xffffffff,%r12d
mov    -0x30(%rbp),%r8
lea    0xdc6(%rip),%rdx
lea    -0x39(%rbp),%rcx
lea    -0x42(%rbp),%r9
mov    %rbx,%rdi
mov    %r12d,%esi
xor    %eax,%eax
call   1040 <snprintf@plt>
xor    %eax,%eax
call   1070 <remove_old_log_file@plt>
mov    %rbx,%rdi
call   10b0 <open_output_xml_file@plt>
test   %eax,%eax
je     12e0 <diag_disk+0x140>
mov    0x2d21(%rip),%rax
mov    (%rax),%edi
lea    0xda9(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0xffffffff,%r15d
mov    %r13,%rsp
mov    %r15d,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    -0x30(%rbp),%rcx
lea    -0x170(%rbp),%rsi
lea    -0x39(%rbp),%rdx
lea    -0x42(%rbp),%r8
mov    %rbx,%rdi
call   1080 <populate_xml_header@plt>
lea    0xd88(%rip),%rdi
mov    %r14,%rsi
call   10c0 <sysfs_sg_disk_scan@plt>
xor    %eax,%eax
call   1030 <populate_xml_footer@plt>
xor    %eax,%eax
call   1050 <close_output_xml_file@plt>
jmp    12cb <diag_disk+0x12b>

LLVM IR (-O0):
%struct.tm = type { i32, i32, i32, i32, i32, i32 }
@PATH_MAX = global i32 0
@stderr = global i32 0
@.str = [28 x i8] c"Unable to read system vpd.\0A\00"
@.str.1 = [30 x i8] c"Unable to read current time.\0A\00"
@.str.2 = [20 x i8] c"%d-%d-%dT%d:%d:%dZ\0A\00"
@.str.3 = [27 x i8] c"%s~%s~%s~diskAnalytics.xml\00"
@.str.4 = [33 x i8] c"Unable to open output xml file.\0A\00"
@.str.5 = [24 x i8] c"/sys/class/scsi_generic\00"
define i32 @diag_disk(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca [9 x i8]
%5 = alloca [9 x i8]
%6 = alloca i8*
%7 = alloca [256 x i8]
%8 = alloca i8*
%9 = alloca i64
%10 = alloca %struct.tm
%11 = alloca i32
%12 = alloca i32
store i8* %0, i8** %3
store i8* null, i8** %6
%13 = load i32, i32* @PATH_MAX
%14 = zext i32 %13 to i64
%15 = call i8* @llvm.stacksave()
store i8* %15, i8** %8
%16 = alloca i8, i64 %14
store i64 %14, i64* %9
%17 = getelementptr inbounds [9 x i8], [9 x i8]* %4, i64 0, i64 0
%18 = getelementptr inbounds [9 x i8], [9 x i8]* %5, i64 0, i64 0
%19 = call i32 @get_system_vpd(i8* %17, i8* %18, i8** %6)
store i32 %19, i32* %11
%20 = load i32, i32* %11
%21 = icmp ne i32 %20, 0
br i1 %21, label %22, label %25
22: ; preds = %1
%23 = load i32, i32* @stderr
%24 = call i32 @fprintf(i32 %23, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %2
store i32 1, i32* %12
br label %72
25: ; preds = %1
%26 = call i32 @get_current_time(%struct.tm* %10)
store i32 %26, i32* %11
%27 = load i32, i32* %11
%28 = icmp ne i32 %27, 0
br i1 %28, label %29, label %32
29: ; preds = %25
%30 = load i32, i32* @stderr
%31 = call i32 @fprintf(i32 %30, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %2
store i32 1, i32* %12
br label %72
32: ; preds = %25
%33 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 0
%34 = getelementptr inbounds %struct.tm, %struct.tm* %10, i32 0, i32 0
%35 = load i32, i32* %34
%36 = add nsw i32 %35, 1900
%37 = getelementptr inbounds %struct.tm, %struct.tm* %10, i32 0, i32 1
%38 = load i32, i32* %37
%39 = add nsw i32 %38, 1
%40 = getelementptr inbounds %struct.tm, %struct.tm* %10, i32 0, i32 2
%41 = load i32, i32* %40
%42 = getelementptr inbounds %struct.tm, %struct.tm* %10, i32 0, i32 3
%43 = load i32, i32* %42
%44 = getelementptr inbounds %struct.tm, %struct.tm* %10, i32 0, i32 4
%45 = load i32, i32* %44
%46 = getelementptr inbounds %struct.tm, %struct.tm* %10, i32 0, i32 5
%47 = load i32, i32* %46
%48 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %33, i32 255, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i32 %36, i32 %39, i32 %41, i32 %43, i32 %45, i32 %47)
%49 = sub i64 %14, 1
%50 = trunc i64 %49 to i32
%51 = getelementptr inbounds [9 x i8], [9 x i8]* %5, i64 0, i64 0
%52 = load i8*, i8** %6
%53 = getelementptr inbounds [9 x i8], [9 x i8]* %4, i64 0, i64 0
%54 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %16, i32 %50, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.3, i64 0, i64 0), i8* %51, i8* %52, i8* %53)
%55 = call i32 (...) @remove_old_log_file()
%56 = call i32 @open_output_xml_file(i8* %16)
store i32 %56, i32* %11
%57 = load i32, i32* %11
%58 = icmp ne i32 %57, 0
br i1 %58, label %59, label %62
59: ; preds = %32
%60 = load i32, i32* @stderr
%61 = call i32 @fprintf(i32 %60, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.4, i64 0, i64 0))
store i32 -1, i32* %2
store i32 1, i32* %12
br label %72
62: ; preds = %32
%63 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 0
%64 = getelementptr inbounds [9 x i8], [9 x i8]* %5, i64 0, i64 0
%65 = load i8*, i8** %6
%66 = getelementptr inbounds [9 x i8], [9 x i8]* %4, i64 0, i64 0
%67 = call i32 @populate_xml_header(i8* %16, i8* %63, i8* %64, i8* %65, i8* %66)
%68 = load i8*, i8** %3
%69 = call i32 @sysfs_sg_disk_scan(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.5, i64 0, i64 0), i8* %68)
%70 = call i32 (...) @populate_xml_footer()
%71 = call i32 (...) @close_output_xml_file()
store i32 0, i32* %2
store i32 1, i32* %12
br label %72
72: ; preds = %62, %59, %29, %22
%73 = load i8*, i8** %8
%74 = load i32, i32* %2
ret i32 %74
}

LLVM IR (-O2):
%struct.tm = type { i32, i32, i32, i32, i32, i32 }
@PATH_MAX = global i32 0
@stderr = global i32 0
@.str = [28 x i8] c"Unable to read system vpd.\0A\00"
@.str.1 = [30 x i8] c"Unable to read current time.\0A\00"
@.str.2 = [20 x i8] c"%d-%d-%dT%d:%d:%dZ\0A\00"
@.str.3 = [27 x i8] c"%s~%s~%s~diskAnalytics.xml\00"
@.str.4 = [33 x i8] c"Unable to open output xml file.\0A\00"
@.str.5 = [24 x i8] c"/sys/class/scsi_generic\00"
define i32 @diag_disk(i8* %0) {
%2 = alloca [9 x i8]
%3 = alloca [9 x i8]
%4 = alloca i8*
%5 = alloca [256 x i8]
%6 = alloca %struct.tm
%7 = getelementptr inbounds [9 x i8], [9 x i8]* %2, i64 0, i64 0
%8 = getelementptr inbounds [9 x i8], [9 x i8]* %3, i64 0, i64 0
%9 = bitcast i8** %4 to i8*
store i8* null, i8** %4
%10 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%11 = load i32, i32* @PATH_MAX
%12 = zext i32 %11 to i64
%13 = tail call i8* @llvm.stacksave()
%14 = alloca i8, i64 %12
%15 = bitcast %struct.tm* %6 to i8*
%16 = call i32 @get_system_vpd(i8* %7, i8* %8, i8** %4)
%17 = icmp eq i32 %16, 0
br i1 %17, label %21, label %18
18: ; preds = %1
%19 = load i32, i32* @stderr
%20 = call i32 @fprintf(i32 %19, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
br label %58
21: ; preds = %1
%22 = call i32 @get_current_time(%struct.tm* %6)
%23 = icmp eq i32 %22, 0
br i1 %23, label %27, label %24
24: ; preds = %21
%25 = load i32, i32* @stderr
%26 = call i32 @fprintf(i32 %25, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0))
br label %58
27: ; preds = %21
%28 = getelementptr inbounds %struct.tm, %struct.tm* %6, i64 0, i32 0
%29 = load i32, i32* %28
%30 = add nsw i32 %29, 1900
%31 = getelementptr inbounds %struct.tm, %struct.tm* %6, i64 0, i32 1
%32 = load i32, i32* %31
%33 = add nsw i32 %32, 1
%34 = getelementptr inbounds %struct.tm, %struct.tm* %6, i64 0, i32 2
%35 = load i32, i32* %34
%36 = getelementptr inbounds %struct.tm, %struct.tm* %6, i64 0, i32 3
%37 = load i32, i32* %36
%38 = getelementptr inbounds %struct.tm, %struct.tm* %6, i64 0, i32 4
%39 = load i32, i32* %38
%40 = getelementptr inbounds %struct.tm, %struct.tm* %6, i64 0, i32 5
%41 = load i32, i32* %40
%42 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %10, i32 255, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i32 %30, i32 %33, i32 %35, i32 %37, i32 %39, i32 %41)
%43 = add i32 %11, -1
%44 = load i8*, i8** %4
%45 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %14, i32 %43, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.3, i64 0, i64 0), i8* %8, i8* %44, i8* %7)
%46 = call i32 (...) @remove_old_log_file()
%47 = call i32 @open_output_xml_file(i8* %14)
%48 = icmp eq i32 %47, 0
br i1 %48, label %52, label %49
49: ; preds = %27
%50 = load i32, i32* @stderr
%51 = call i32 @fprintf(i32 %50, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.4, i64 0, i64 0))
br label %58
52: ; preds = %27
%53 = load i8*, i8** %4
%54 = call i32 @populate_xml_header(i8* %14, i8* %10, i8* %8, i8* %53, i8* %7)
%55 = call i32 @sysfs_sg_disk_scan(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.5, i64 0, i64 0), i8* %0)
%56 = call i32 (...) @populate_xml_footer()
%57 = call i32 (...) @close_output_xml_file()
br label %58
58: ; preds = %52, %49, %24, %18
%59 = phi i32 [ -1, %18 ], [ -1, %24 ], [ -1, %49 ], [ 0, %52 ]
ret i32 %59
}