#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 */
typedef struct TYPE_2__ TYPE_1__;

/* Type definitions */
struct TYPE_2__ {
  int *input;
  int /*<<< orphan*/ cmd_select;
  int /*<<< orphan*/ cmd_class;
};
struct dell_wmi_smbios_buffer {
  TYPE_1__ std;
  scalar_t__ length;
};
typedef scalar_t__ __u64;

/* Variables and functions */
int /*<<< orphan*/ CAPSULE_DIS_TOKEN;
int /*<<< orphan*/ CAPSULE_EN_TOKEN;
int /*<<< orphan*/ CLASS_FLASH_INTERFACE;
int ENOMEM;
int EXIT_FAILURE;
int EXIT_SUCCESS;
int /*<<< orphan*/ SELECT_FLASH_INTERFACE;
scalar_t__ activate_token(struct dell_wmi_smbios_buffer *, int /*<<< orphan*/);
int /*<<< orphan*/ free(struct dell_wmi_smbios_buffer *);
struct dell_wmi_smbios_buffer *malloc(scalar_t__);
int /*<<< orphan*/ printf(char *, ...);
int query_buffer_size(scalar_t__ *);
int query_token(int /*<<< orphan*/, struct dell_wmi_smbios_buffer *);
int run_wmi_smbios_cmd(struct dell_wmi_smbios_buffer *);
int /*<<< orphan*/ show_buffer(struct dell_wmi_smbios_buffer *);
int main(void) {
  struct dell_wmi_smbios_buffer *buffer;
  int ret;
  __u64 value = 0;
  ret = query_buffer_size(&value);
  if (ret == EXIT_FAILURE || !value) {
    printf("Unable to read buffer size\n");
    goto out;
  }
  printf("Detected required buffer size %lld\n", value);
  buffer = malloc(value);
  if (buffer == NULL) {
    printf("failed to alloc memory for ioctl\n");
    ret = -ENOMEM;
    goto out;
  }
  buffer->length = value;
  buffer->std.cmd_class = CLASS_FLASH_INTERFACE;
  buffer->std.cmd_select = SELECT_FLASH_INTERFACE;
  buffer->std.input[0] = 2;
  ret = run_wmi_smbios_cmd(buffer);
  if (ret) {
    printf("smbios ioctl failed: %d\n", ret);
    ret = EXIT_FAILURE;
    goto out;
  }
  show_buffer(buffer);
  ret = query_token(CAPSULE_EN_TOKEN, buffer);
  printf("UEFI Capsule enabled token is: %d\n", ret);
  ret = query_token(CAPSULE_DIS_TOKEN, buffer);
  printf("UEFI Capsule disabled token is: %d\n", ret);
  if (ret) {
    printf("Enabling UEFI capsule token");
    if (activate_token(buffer, CAPSULE_EN_TOKEN)) {
      printf("activate failed\n");
      ret = -1;
      goto out;
    }
  }
  ret = EXIT_SUCCESS;
out:
  free(buffer);
  return ret;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
movq   $0x0,-0x20(%rbp)
lea    -0x20(%rbp),%rdi
call   10a0 <query_buffer_size@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    0x2e0b(%rip),%rcx
cmp    (%rcx),%eax
je     11c0 <main+0x40>
cmpq   $0x0,-0x20(%rbp)
jne    11d3 <main+0x53>
lea    0xe39(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1343 <main+0x1c3>
mov    -0x20(%rbp),%rsi
lea    0xe3e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x20(%rbp),%rdi
call   1060 <malloc@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    121e <main+0x9e>
lea    0xe3c(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %eax,%eax
mov    0x2db4(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x14(%rbp)
jmp    1343 <main+0x1c3>
mov    -0x20(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,0x10(%rax)
mov    0x2da7(%rip),%rax
mov    (%rax),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,0xc(%rax)
mov    0x2d9f(%rip),%rax
mov    (%rax),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,0x8(%rax)
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
movl   $0x2,(%rax)
mov    -0x10(%rbp),%rdi
call   1080 <run_wmi_smbios_cmd@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     128f <main+0x10f>
mov    -0x14(%rbp),%esi
lea    0xdeb(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2d33(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
jmp    1343 <main+0x1c3>
mov    -0x10(%rbp),%rdi
call   1090 <show_buffer@plt>
mov    0x2d49(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rsi
call   1050 <query_token@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%esi
lea    0xdc4(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2cfb(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rsi
call   1050 <query_token@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%esi
lea    0xdc1(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
cmpl   $0x0,-0x14(%rbp)
je     1337 <main+0x1b7>
lea    0xdcd(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x10(%rbp),%rdi
mov    0x2ce1(%rip),%rax
mov    (%rax),%esi
call   1070 <activate_token@plt>
cmp    $0x0,%rax
je     1332 <main+0x1b2>
lea    0xdbf(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x14(%rbp)
jmp    1343 <main+0x1c3>
jmp    1337 <main+0x1b7>
mov    0x2c72(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
mov    -0x14(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
movq   $0x0,(%rsp)
mov    %rsp,%rdi
call   10b0 <query_buffer_size@plt>
mov    0x2e0a(%rip),%r15
cmp    (%r15),%eax
je     12ed <main+0x15d>
mov    (%rsp),%rsi
test   %rsi,%rsi
je     12ed <main+0x15d>
lea    0xe35(%rip),%rdi
xor    %ebp,%ebp
xor    %eax,%eax
call   1050 <printf@plt>
mov    (%rsp),%rbx
mov    %rbx,%rdi
call   1070 <malloc@plt>
mov    %rax,%r14
test   %rax,%rax
je     1233 <main+0xa3>
mov    %rbx,0x10(%r14)
mov    0x2de5(%rip),%rax
mov    (%rax),%eax
mov    %eax,0xc(%r14)
mov    0x2de0(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%r14)
mov    (%r14),%rax
movl   $0x2,(%rax)
mov    %r14,%rdi
call   1090 <run_wmi_smbios_cmd@plt>
test   %eax,%eax
je     124d <main+0xbd>
lea    0xe02(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    (%r15),%ebp
jmp    12d8 <main+0x148>
lea    0xe91(%rip),%rdi
call   1040 <puts@plt>
mov    0x2d82(%rip),%rax
sub    (%rax),%ebp
jmp    12d8 <main+0x148>
mov    %r14,%rdi
call   10a0 <show_buffer@plt>
mov    0x2d8c(%rip),%rbp
mov    0x0(%rbp),%edi
mov    %r14,%rsi
call   1060 <query_token@plt>
lea    0xdcf(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x2d42(%rip),%rax
mov    (%rax),%edi
mov    %r14,%rsi
call   1060 <query_token@plt>
mov    %eax,%ebx
lea    0xdcf(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
test   %ebx,%ebx
je     12cf <main+0x13f>
lea    0xddf(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x0(%rbp),%esi
mov    %r14,%rdi
call   1080 <activate_token@plt>
test   %rax,%rax
je     12cf <main+0x13f>
lea    0xdf8(%rip),%rdi
call   1040 <puts@plt>
mov    $0xffffffff,%ebp
jmp    12d8 <main+0x148>
mov    0x2cda(%rip),%rax
mov    (%rax),%ebp
mov    %r14,%rdi
call   1030 <free@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdac(%rip),%rdi
call   1040 <puts@plt>

LLVM IR (-O0):
%struct.dell_wmi_smbios_buffer = type { %struct.TYPE_2__, i64 }
%struct.TYPE_2__ = type { i32*, i32, i32 }
@EXIT_FAILURE = global i32 0
@.str = [28 x i8] c"Unable to read buffer size\0A\00"
@.str.1 = [36 x i8] c"Detected required buffer size %lld\0A\00"
@.str.2 = [34 x i8] c"failed to alloc memory for ioctl\0A\00"
@ENOMEM = global i32 0
@CLASS_FLASH_INTERFACE = global i32 0
@SELECT_FLASH_INTERFACE = global i32 0
@.str.3 = [25 x i8] c"smbios ioctl failed: %d\0A\00"
@CAPSULE_EN_TOKEN = global i32 0
@.str.4 = [35 x i8] c"UEFI Capsule enabled token is: %d\0A\00"
@CAPSULE_DIS_TOKEN = global i32 0
@.str.5 = [36 x i8] c"UEFI Capsule disabled token is: %d\0A\00"
@.str.6 = [28 x i8] c"Enabling UEFI capsule token\00"
@.str.7 = [17 x i8] c"activate failed\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca %struct.dell_wmi_smbios_buffer*
%3 = alloca i32
%4 = alloca i64
store i32 0, i32* %1
store i64 0, i64* %4
%5 = call i32 @query_buffer_size(i64* %4)
store i32 %5, i32* %3
%6 = load i32, i32* %3
%7 = load i32, i32* @EXIT_FAILURE
%8 = icmp eq i32 %6, %7
br i1 %8, label %12, label %9
9: ; preds = %0
%10 = load i64, i64* %4
%11 = icmp ne i64 %10, 0
br i1 %11, label %14, label %12
12: ; preds = %9, %0
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
br label %76
14: ; preds = %9
%15 = load i64, i64* %4
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0), i64 %15)
%17 = load i64, i64* %4
%18 = call %struct.dell_wmi_smbios_buffer* @malloc(i64 %17)
store %struct.dell_wmi_smbios_buffer* %18, %struct.dell_wmi_smbios_buffer** %2
%19 = load %struct.dell_wmi_smbios_buffer*, %struct.dell_wmi_smbios_buffer** %2
%20 = icmp eq %struct.dell_wmi_smbios_buffer* %19, null
br i1 %20, label %21, label %25
21: ; preds = %14
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0))
%23 = load i32, i32* @ENOMEM
%24 = sub nsw i32 0, %23
store i32 %24, i32* %3
br label %76
25: ; preds = %14
%26 = load i64, i64* %4
%27 = load %struct.dell_wmi_smbios_buffer*, %struct.dell_wmi_smbios_buffer** %2
%28 = getelementptr inbounds %struct.dell_wmi_smbios_buffer, %struct.dell_wmi_smbios_buffer* %27, i32 0, i32 1
store i64 %26, i64* %28
%29 = load i32, i32* @CLASS_FLASH_INTERFACE
%30 = load %struct.dell_wmi_smbios_buffer*, %struct.dell_wmi_smbios_buffer** %2
%31 = getelementptr inbounds %struct.dell_wmi_smbios_buffer, %struct.dell_wmi_smbios_buffer* %30, i32 0, i32 0
%32 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %31, i32 0, i32 2
store i32 %29, i32* %32
%33 = load i32, i32* @SELECT_FLASH_INTERFACE
%34 = load %struct.dell_wmi_smbios_buffer*, %struct.dell_wmi_smbios_buffer** %2
%35 = getelementptr inbounds %struct.dell_wmi_smbios_buffer, %struct.dell_wmi_smbios_buffer* %34, i32 0, i32 0
%36 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %35, i32 0, i32 1
store i32 %33, i32* %36
%37 = load %struct.dell_wmi_smbios_buffer*, %struct.dell_wmi_smbios_buffer** %2
%38 = getelementptr inbounds %struct.dell_wmi_smbios_buffer, %struct.dell_wmi_smbios_buffer* %37, i32 0, i32 0
%39 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %38, i32 0, i32 0
%40 = load i32*, i32** %39
%41 = getelementptr inbounds i32, i32* %40, i64 0
store i32 2, i32* %41
%42 = load %struct.dell_wmi_smbios_buffer*, %struct.dell_wmi_smbios_buffer** %2
%43 = call i32 @run_wmi_smbios_cmd(%struct.dell_wmi_smbios_buffer* %42)
store i32 %43, i32* %3
%44 = load i32, i32* %3
%45 = icmp ne i32 %44, 0
br i1 %45, label %46, label %50
46: ; preds = %25
%47 = load i32, i32* %3
%48 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0), i32 %47)
%49 = load i32, i32* @EXIT_FAILURE
store i32 %49, i32* %3
br label %76
50: ; preds = %25
%51 = load %struct.dell_wmi_smbios_buffer*, %struct.dell_wmi_smbios_buffer** %2
%52 = call i32 @show_buffer(%struct.dell_wmi_smbios_buffer* %51)
%53 = load i32, i32* @CAPSULE_EN_TOKEN
%54 = load %struct.dell_wmi_smbios_buffer*, %struct.dell_wmi_smbios_buffer** %2
%55 = call i32 @query_token(i32 %53, %struct.dell_wmi_smbios_buffer* %54)
store i32 %55, i32* %3
%56 = load i32, i32* %3
%57 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.4, i64 0, i64 0), i32 %56)
%58 = load i32, i32* @CAPSULE_DIS_TOKEN
%59 = load %struct.dell_wmi_smbios_buffer*, %struct.dell_wmi_smbios_buffer** %2
%60 = call i32 @query_token(i32 %58, %struct.dell_wmi_smbios_buffer* %59)
store i32 %60, i32* %3
%61 = load i32, i32* %3
%62 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.5, i64 0, i64 0), i32 %61)
%63 = load i32, i32* %3
%64 = icmp ne i32 %63, 0
br i1 %64, label %65, label %74
65: ; preds = %50
%66 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.6, i64 0, i64 0))
%67 = load %struct.dell_wmi_smbios_buffer*, %struct.dell_wmi_smbios_buffer** %2
%68 = load i32, i32* @CAPSULE_EN_TOKEN
%69 = call i64 @activate_token(%struct.dell_wmi_smbios_buffer* %67, i32 %68)
%70 = icmp ne i64 %69, 0
br i1 %70, label %71, label %73
71: ; preds = %65
%72 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.7, i64 0, i64 0))
store i32 -1, i32* %3
br label %76
73: ; preds = %65
br label %74
74: ; preds = %73, %50
%75 = load i32, i32* @EXIT_SUCCESS
store i32 %75, i32* %3
br label %76
76: ; preds = %74, %71, %46, %21, %12
%77 = load %struct.dell_wmi_smbios_buffer*, %struct.dell_wmi_smbios_buffer** %2
%78 = call i32 @free(%struct.dell_wmi_smbios_buffer* %77)
%79 = load i32, i32* %3
ret i32 %79
}

LLVM IR (-O2):
%struct.dell_wmi_smbios_buffer = type { %struct.TYPE_2__, i64 }
%struct.TYPE_2__ = type { i32*, i32, i32 }
@EXIT_FAILURE = global i32 0
@.str.1 = [36 x i8] c"Detected required buffer size %lld\0A\00"
@ENOMEM = global i32 0
@CLASS_FLASH_INTERFACE = global i32 0
@SELECT_FLASH_INTERFACE = global i32 0
@.str.3 = [25 x i8] c"smbios ioctl failed: %d\0A\00"
@CAPSULE_EN_TOKEN = global i32 0
@.str.4 = [35 x i8] c"UEFI Capsule enabled token is: %d\0A\00"
@CAPSULE_DIS_TOKEN = global i32 0
@.str.5 = [36 x i8] c"UEFI Capsule disabled token is: %d\0A\00"
@.str.6 = [28 x i8] c"Enabling UEFI capsule token\00"
@EXIT_SUCCESS = global i32 0
@str = [27 x i8] c"Unable to read buffer size\00"
@str.8 = [16 x i8] c"activate failed\00"
@str.9 = [33 x i8] c"failed to alloc memory for ioctl\00"
define i32 @main() {
%1 = alloca i64
%2 = bitcast i64* %1 to i8*
store i64 0, i64* %1
%3 = call i32 @query_buffer_size(i64* %1)
%4 = load i32, i32* @EXIT_FAILURE
%5 = icmp ne i32 %3, %4
%6 = load i64, i64* %1
%7 = icmp ne i64 %6, 0
%8 = select i1 %5, i1 %7, i1 false
br i1 %8, label %11, label %9
9: ; preds = %0
%10 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @str, i64 0, i64 0))
unreachable
11: ; preds = %0
%12 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0), i64 %6)
%13 = load i64, i64* %1
%14 = call %struct.dell_wmi_smbios_buffer* @malloc(i64 %13)
%15 = icmp eq %struct.dell_wmi_smbios_buffer* %14, null
br i1 %15, label %16, label %20
16: ; preds = %11
%17 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @str.9, i64 0, i64 0))
%18 = load i32, i32* @ENOMEM
%19 = sub nsw i32 0, %18
br label %51
20: ; preds = %11
%21 = getelementptr inbounds %struct.dell_wmi_smbios_buffer, %struct.dell_wmi_smbios_buffer* %14, i64 0, i32 1
store i64 %13, i64* %21
%22 = load i32, i32* @CLASS_FLASH_INTERFACE
%23 = getelementptr inbounds %struct.dell_wmi_smbios_buffer, %struct.dell_wmi_smbios_buffer* %14, i64 0, i32 0, i32 2
store i32 %22, i32* %23
%24 = load i32, i32* @SELECT_FLASH_INTERFACE
%25 = getelementptr inbounds %struct.dell_wmi_smbios_buffer, %struct.dell_wmi_smbios_buffer* %14, i64 0, i32 0, i32 1
store i32 %24, i32* %25
%26 = getelementptr inbounds %struct.dell_wmi_smbios_buffer, %struct.dell_wmi_smbios_buffer* %14, i64 0, i32 0, i32 0
%27 = load i32*, i32** %26
store i32 2, i32* %27
%28 = call i32 @run_wmi_smbios_cmd(%struct.dell_wmi_smbios_buffer* %14)
%29 = icmp eq i32 %28, 0
br i1 %29, label %33, label %30
30: ; preds = %20
%31 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0), i32 %28)
%32 = load i32, i32* @EXIT_FAILURE
br label %51
33: ; preds = %20
%34 = call i32 @show_buffer(%struct.dell_wmi_smbios_buffer* %14)
%35 = load i32, i32* @CAPSULE_EN_TOKEN
%36 = call i32 @query_token(i32 %35, %struct.dell_wmi_smbios_buffer* %14)
%37 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @.str.4, i64 0, i64 0), i32 %36)
%38 = load i32, i32* @CAPSULE_DIS_TOKEN
%39 = call i32 @query_token(i32 %38, %struct.dell_wmi_smbios_buffer* %14)
%40 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @.str.5, i64 0, i64 0), i32 %39)
%41 = icmp eq i32 %39, 0
br i1 %41, label %49, label %42
42: ; preds = %33
%43 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @.str.6, i64 0, i64 0))
%44 = load i32, i32* @CAPSULE_EN_TOKEN
%45 = call i64 @activate_token(%struct.dell_wmi_smbios_buffer* %14, i32 %44)
%46 = icmp eq i64 %45, 0
br i1 %46, label %49, label %47
47: ; preds = %42
%48 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str.8, i64 0, i64 0))
br label %51
49: ; preds = %42, %33
%50 = load i32, i32* @EXIT_SUCCESS
br label %51
51: ; preds = %49, %47, %30, %16
%52 = phi i32 [ %19, %16 ], [ %32, %30 ], [ -1, %47 ], [ %50, %49 ]
%53 = call i32 @free(%struct.dell_wmi_smbios_buffer* %14)
ret i32 %52
}