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

/* Variables and functions */
int /*<<< orphan*/ O_WRONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int major(int /*<<< orphan*/);
int minor(int /*<<< orphan*/);
int /*<<< orphan*/ name_to_dev_t(char const *);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, char const *);
int snprintf(char *, int, char *, int, int);
int /*<<< orphan*/ stderr;
int write(int, char *, int);
int resume(const char *resume_file) {
  dev_t resume_device;
  int powerfd = -1;
  char device_string[64];
  int len;
  resume_device = name_to_dev_t(resume_file);
  if (major(resume_device) == 0) {
    fprintf(stderr, "Invalid resume device: %s\n", resume_file);
    goto failure;
  }
  if ((powerfd = open("/sys/power/resume", O_WRONLY)) < 0)
    goto fail_r;
  len = snprintf(device_string, sizeof device_string, "%u:%u",
                 major(resume_device), minor(resume_device));
  if (len >= sizeof device_string)
    goto fail_r;
  printf("kinit: trying to resume from %s\n", resume_file);
  if (write(powerfd, device_string, len) != len)
    goto fail_r;
failure:
  if (powerfd >= 0)
    close(powerfd);
  fprintf(stderr, "kinit: No resume image, doing normal boot...\n");
  return -1;
fail_r:
  fprintf(stderr, "Cannot write /sys/power/resume "
                  "(no software suspend kernel support?)\n");
  goto failure;
}


x86 assembly (clang, -O0):
<resume>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0xffffffff,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
call   1090 <name_to_dev_t@plt>
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%edi
call   1070 <major@plt>
cmp    $0x0,%eax
jne    11e0 <resume+0x50>
mov    0x2e19(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xe2c(%rip),%rsi
mov    $0x0,%al
call   10a0 <fprintf@plt>
jmp    1284 <resume+0xf4>
mov    0x2de9(%rip),%rax
mov    (%rax),%esi
lea    0xe2b(%rip),%rdi
call   10b0 <open@plt>
mov    %eax,-0x10(%rbp)
cmp    $0x0,%eax
jge    1206 <resume+0x76>
jmp    12b8 <resume+0x128>
lea    -0x50(%rbp),%rax
mov    %rax,-0x60(%rbp)
mov    -0xc(%rbp),%edi
call   1070 <major@plt>
mov    %eax,-0x58(%rbp)
mov    -0xc(%rbp),%edi
call   1040 <minor@plt>
mov    -0x60(%rbp),%rdi
mov    -0x58(%rbp),%ecx
mov    %eax,%r8d
mov    $0x40,%esi
lea    0xdf6(%rip),%rdx
call   1060 <snprintf@plt>
mov    %eax,-0x54(%rbp)
movslq -0x54(%rbp),%rax
cmp    $0x40,%rax
jb     1252 <resume+0xc2>
jmp    12b8 <resume+0x128>
mov    -0x8(%rbp),%rsi
lea    0xdd6(%rip),%rdi
call   1050 <printf@plt>
mov    -0x10(%rbp),%edi
lea    -0x50(%rbp),%rsi
mov    -0x54(%rbp),%edx
call   1030 <write@plt>
cmp    -0x54(%rbp),%eax
je     127f <resume+0xef>
jmp    12b8 <resume+0x128>
jmp    1284 <resume+0xf4>
cmpl   $0x0,-0x10(%rbp)
jl     1296 <resume+0x106>
mov    -0x10(%rbp),%edi
call   1080 <close@plt>
mov    0x2d43(%rip),%rax
mov    (%rax),%edi
lea    0xdae(%rip),%rsi
mov    $0x0,%al
call   10a0 <fprintf@plt>
mov    $0xffffffff,%eax
add    $0x60,%rsp
pop    %rbp
ret
mov    0x2d21(%rip),%rax
mov    (%rax),%edi
lea    0xdba(%rip),%rsi
mov    $0x0,%al
call   10a0 <fprintf@plt>
jmp    1284 <resume+0xf4>

x86 assembly (clang, -O2):
<resume>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x48,%rsp
mov    %rdi,%rbx
call   1090 <name_to_dev_t@plt>
mov    %eax,%ebp
mov    %eax,%edi
call   1070 <major@plt>
test   %eax,%eax
je     1246 <resume+0xb6>
mov    0x2e16(%rip),%rax
mov    (%rax),%esi
lea    0xe58(%rip),%rdi
call   10b0 <open@plt>
test   %eax,%eax
js     1262 <resume+0xd2>
mov    %eax,%r14d
mov    %ebp,%edi
call   1070 <major@plt>
mov    %eax,%r15d
mov    %ebp,%edi
call   1040 <minor@plt>
lea    0xe42(%rip),%rdx
mov    %rsp,%rdi
mov    $0x40,%esi
mov    %r15d,%ecx
mov    %eax,%r8d
call   1060 <snprintf@plt>
cmp    $0x3f,%eax
ja     1225 <resume+0x95>
mov    %eax,%ebp
lea    0xe27(%rip),%rdi
mov    %rbx,%rsi
call   1050 <printf@plt>
mov    %rsp,%rsi
mov    %r14d,%edi
mov    %ebp,%edx
call   1030 <write@plt>
cmp    %ebp,%eax
je     123c <resume+0xac>
mov    0x2db4(%rip),%rax
mov    (%rax),%edi
lea    0xe4d(%rip),%rsi
xor    %eax,%eax
call   10a0 <fprintf@plt>
mov    %r14d,%edi
call   1080 <close@plt>
jmp    1279 <resume+0xe9>
mov    0x2d93(%rip),%rax
mov    (%rax),%edi
lea    0xdaa(%rip),%rsi
mov    %rbx,%rdx
xor    %eax,%eax
call   10a0 <fprintf@plt>
jmp    1279 <resume+0xe9>
mov    0x2d77(%rip),%rax
mov    (%rax),%edi
lea    0xe10(%rip),%rsi
xor    %eax,%eax
call   10a0 <fprintf@plt>
mov    0x2d60(%rip),%rax
mov    (%rax),%edi
lea    0xdcb(%rip),%rsi
xor    %eax,%eax
call   10a0 <fprintf@plt>
mov    $0xffffffff,%eax
add    $0x48,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [27 x i8] c"Invalid resume device: %s\0A\00"
@.str.1 = [18 x i8] c"/sys/power/resume\00"
@O_WRONLY = global i32 0
@.str.2 = [6 x i8] c"%u:%u\00"
@.str.3 = [33 x i8] c"kinit: trying to resume from %s\0A\00"
@.str.4 = [46 x i8] c"kinit: No resume image, doing normal boot...\0A\00"
@.str.5 = [70 x i8] c"Cannot write /sys/power/resume (no software suspend kernel support?)\0A\00"
define i32 @resume(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
%5 = alloca [64 x i8]
%6 = alloca i32
store i8* %0, i8** %2
store i32 -1, i32* %4
%7 = load i8*, i8** %2
%8 = call i32 @name_to_dev_t(i8* %7)
store i32 %8, i32* %3
%9 = load i32, i32* %3
%10 = call i32 @major(i32 %9)
%11 = icmp eq i32 %10, 0
br i1 %11, label %12, label %16
12: ; preds = %1
%13 = load i32, i32* @stderr
%14 = load i8*, i8** %2
%15 = call i32 (i32, i8*, ...) @fprintf(i32 %13, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i8* %14)
br label %43
16: ; preds = %1
%17 = load i32, i32* @O_WRONLY
%18 = call i32 @open(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i32 %17)
store i32 %18, i32* %4
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %21
20: ; preds = %16
br label %52
21: ; preds = %16
%22 = getelementptr inbounds [64 x i8], [64 x i8]* %5, i64 0, i64 0
%23 = load i32, i32* %3
%24 = call i32 @major(i32 %23)
%25 = load i32, i32* %3
%26 = call i32 @minor(i32 %25)
%27 = call i32 @snprintf(i8* %22, i32 64, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i32 %24, i32 %26)
store i32 %27, i32* %6
%28 = load i32, i32* %6
%29 = sext i32 %28 to i64
%30 = icmp uge i64 %29, 64
br i1 %30, label %31, label %32
31: ; preds = %21
br label %52
32: ; preds = %21
%33 = load i8*, i8** %2
%34 = call i32 @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.3, i64 0, i64 0), i8* %33)
%35 = load i32, i32* %4
%36 = getelementptr inbounds [64 x i8], [64 x i8]* %5, i64 0, i64 0
%37 = load i32, i32* %6
%38 = call i32 @write(i32 %35, i8* %36, i32 %37)
%39 = load i32, i32* %6
%40 = icmp ne i32 %38, %39
br i1 %40, label %41, label %42
41: ; preds = %32
br label %52
42: ; preds = %32
br label %43
43: ; preds = %52, %42, %12
%44 = load i32, i32* %4
%45 = icmp sge i32 %44, 0
br i1 %45, label %46, label %49
46: ; preds = %43
%47 = load i32, i32* %4
%48 = call i32 @close(i32 %47)
br label %49
49: ; preds = %46, %43
%50 = load i32, i32* @stderr
%51 = call i32 (i32, i8*, ...) @fprintf(i32 %50, i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.4, i64 0, i64 0))
ret i32 -1
52: ; preds = %41, %31, %20
%53 = load i32, i32* @stderr
%54 = call i32 (i32, i8*, ...) @fprintf(i32 %53, i8* getelementptr inbounds ([70 x i8], [70 x i8]* @.str.5, i64 0, i64 0))
br label %43
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [27 x i8] c"Invalid resume device: %s\0A\00"
@.str.1 = [18 x i8] c"/sys/power/resume\00"
@O_WRONLY = global i32 0
@.str.2 = [6 x i8] c"%u:%u\00"
@.str.3 = [33 x i8] c"kinit: trying to resume from %s\0A\00"
@.str.4 = [46 x i8] c"kinit: No resume image, doing normal boot...\0A\00"
@.str.5 = [70 x i8] c"Cannot write /sys/power/resume (no software suspend kernel support?)\0A\00"
define i32 @resume(i8* %0) {
%2 = alloca [64 x i8]
%3 = getelementptr inbounds [64 x i8], [64 x i8]* %2, i64 0, i64 0
%4 = tail call i32 @name_to_dev_t(i8* %0)
%5 = tail call i32 @major(i32 %4)
%6 = icmp eq i32 %5, 0
br i1 %6, label %7, label %10
7: ; preds = %1
%8 = load i32, i32* @stderr
%9 = tail call i32 (i32, i8*, ...) @fprintf(i32 %8, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %31
10: ; preds = %1
%11 = load i32, i32* @O_WRONLY
%12 = tail call i32 @open(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i32 %11)
%13 = icmp slt i32 %12, 0
br i1 %13, label %26, label %14
14: ; preds = %10
%15 = tail call i32 @major(i32 %4)
%16 = tail call i32 @minor(i32 %4)
%17 = call i32 @snprintf(i8* %3, i32 64, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i32 %15, i32 %16)
%18 = icmp ugt i32 %17, 63
br i1 %18, label %23, label %19
19: ; preds = %14
%20 = call i32 @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.3, i64 0, i64 0), i8* %0)
%21 = call i32 @write(i32 %12, i8* %3, i32 %17)
%22 = icmp eq i32 %21, %17
br i1 %22, label %29, label %23
23: ; preds = %14, %19
%24 = load i32, i32* @stderr
%25 = call i32 (i32, i8*, ...) @fprintf(i32 %24, i8* getelementptr inbounds ([70 x i8], [70 x i8]* @.str.5, i64 0, i64 0))
br label %29
26: ; preds = %10
%27 = load i32, i32* @stderr
%28 = tail call i32 (i32, i8*, ...) @fprintf(i32 %27, i8* getelementptr inbounds ([70 x i8], [70 x i8]* @.str.5, i64 0, i64 0))
br label %31
29: ; preds = %19, %23
%30 = call i32 @close(i32 %12)
br label %31
31: ; preds = %26, %7, %29
%32 = load i32, i32* @stderr
%33 = call i32 (i32, i8*, ...) @fprintf(i32 %32, i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.4, i64 0, i64 0))
ret i32 -1
}