#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 stat {
  scalar_t__ st_size;
};

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ SEEK_SET;
int applysignature_buf(char *, scalar_t__, char const *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ fstat(int, struct stat *);
int /*<<< orphan*/ lseek64(int, int /*<<< orphan*/, int /*<<< orphan*/);
char *malloc(scalar_t__);
int open(char const *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, char const *, char *);
scalar_t__ read(int, char *, scalar_t__);
char *strerror(int /*<<< orphan*/);
int applysignature(const char *filename, const char *partition_name) {
  char *rbuf;
  int result = 0;
  int in_fd;
  struct stat f_buf;
  in_fd = open(filename, O_RDONLY);
  fstat(in_fd, &f_buf);
  rbuf = malloc(f_buf.st_size);
  if (in_fd != -1) {
    while (read(in_fd, rbuf, f_buf.st_size) != f_buf.st_size) {
      lseek64(in_fd, 0, SEEK_SET);
    }
    result = applysignature_buf(rbuf, f_buf.st_size, partition_name);
  } else {
    printf("Fail to open %s, %s", filename, strerror(errno));
    result = -1;
  }
  close(in_fd);
  free(rbuf);
  return result;
}


x86 assembly (clang, -O0):
<applysignature>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2e26(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%edi
lea    -0x28(%rbp),%rsi
call   10c0 <fstat@plt>
mov    -0x28(%rbp),%rdi
call   1070 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmpl   $0xffffffff,-0x20(%rbp)
je     123f <applysignature+0x9f>
jmp    11f4 <applysignature+0x54>
mov    -0x20(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1060 <read@plt>
cmp    -0x28(%rbp),%rax
je     1226 <applysignature+0x86>
mov    -0x20(%rbp),%edi
mov    0x2db8(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   10a0 <lseek64@plt>
jmp    11f4 <applysignature+0x54>
mov    -0x18(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x10(%rbp),%rdx
call   1090 <applysignature_buf@plt>
mov    %eax,-0x1c(%rbp)
jmp    126f <applysignature+0xcf>
mov    -0x8(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    0x2d8a(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    -0x30(%rbp),%rsi
mov    %rax,%rdx
lea    0xd9d(%rip),%rdi
call   1040 <printf@plt>
movl   $0xffffffff,-0x1c(%rbp)
mov    -0x20(%rbp),%edi
call   1050 <close@plt>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    -0x1c(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<applysignature>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r15
mov    %rdi,%r12
mov    0x2e2f(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,%ebp
lea    0x8(%rsp),%rsi
mov    %eax,%edi
call   10c0 <fstat@plt>
mov    0x8(%rsp),%rbx
mov    %rbx,%rdi
call   1070 <malloc@plt>
mov    %rax,%r14
cmp    $0xffffffff,%ebp
je     1231 <applysignature+0x91>
mov    %ebp,%edi
mov    %r14,%rsi
mov    %rbx,%rdx
call   1060 <read@plt>
cmp    %rbx,%rax
je     121f <applysignature+0x7f>
mov    0x2dd4(%rip),%r12
nopl   0x0(%rax)
mov    (%r12),%edx
mov    %ebp,%edi
xor    %esi,%esi
call   10a0 <lseek64@plt>
mov    %ebp,%edi
mov    %r14,%rsi
mov    %rbx,%rdx
call   1060 <read@plt>
cmp    %rbx,%rax
jne    1200 <applysignature+0x60>
mov    %r14,%rdi
mov    %rbx,%rsi
mov    %r15,%rdx
call   1090 <applysignature_buf@plt>
mov    %eax,%ebx
jmp    1256 <applysignature+0xb6>
mov    0x2da0(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
lea    0xdba(%rip),%rdi
mov    %r12,%rsi
mov    %rax,%rdx
call   1040 <printf@plt>
mov    $0xffffffff,%ebx
mov    %ebp,%edi
call   1050 <close@plt>
mov    %r14,%rdi
call   1030 <free@plt>
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i64 }
@O_RDONLY = global i32 0
@SEEK_SET = global i32 0
@.str = [20 x i8] c"Fail to open %s, %s\00"
@errno = global i32 0
define i32 @applysignature(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca %struct.stat
store i8* %0, i8** %3
store i8* %1, i8** %4
store i32 0, i32* %6
%9 = load i8*, i8** %3
%10 = load i32, i32* @O_RDONLY
%11 = call i32 @open(i8* %9, i32 %10)
store i32 %11, i32* %7
%12 = load i32, i32* %7
%13 = call i32 @fstat(i32 %12, %struct.stat* %8)
%14 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 0
%15 = load i64, i64* %14
%16 = call i8* @malloc(i64 %15)
store i8* %16, i8** %5
%17 = load i32, i32* %7
%18 = icmp ne i32 %17, -1
br i1 %18, label %19, label %39
19: ; preds = %2
br label %20
20: ; preds = %29, %19
%21 = load i32, i32* %7
%22 = load i8*, i8** %5
%23 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 0
%24 = load i64, i64* %23
%25 = call i64 @read(i32 %21, i8* %22, i64 %24)
%26 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 0
%27 = load i64, i64* %26
%28 = icmp ne i64 %25, %27
br i1 %28, label %29, label %33
29: ; preds = %20
%30 = load i32, i32* %7
%31 = load i32, i32* @SEEK_SET
%32 = call i32 @lseek64(i32 %30, i32 0, i32 %31)
br label %20
33: ; preds = %20
%34 = load i8*, i8** %5
%35 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 0
%36 = load i64, i64* %35
%37 = load i8*, i8** %4
%38 = call i32 @applysignature_buf(i8* %34, i64 %36, i8* %37)
store i32 %38, i32* %6
br label %44
39: ; preds = %2
%40 = load i8*, i8** %3
%41 = load i32, i32* @errno
%42 = call i8* @strerror(i32 %41)
%43 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %40, i8* %42)
store i32 -1, i32* %6
br label %44
44: ; preds = %39, %33
%45 = load i32, i32* %7
%46 = call i32 @close(i32 %45)
%47 = load i8*, i8** %5
%48 = call i32 @free(i8* %47)
%49 = load i32, i32* %6
ret i32 %49
}

LLVM IR (-O2):
%struct.stat = type { i64 }
@O_RDONLY = global i32 0
@SEEK_SET = global i32 0
@.str = [20 x i8] c"Fail to open %s, %s\00"
@errno = global i32 0
define i32 @applysignature(i8* %0, i8* %1) {
%3 = alloca %struct.stat
%4 = bitcast %struct.stat* %3 to i8*
%5 = load i32, i32* @O_RDONLY
%6 = tail call i32 @open(i8* %0, i32 %5)
%7 = call i32 @fstat(i32 %6, %struct.stat* %3)
%8 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%9 = load i64, i64* %8
%10 = tail call i8* @malloc(i64 %9)
%11 = icmp eq i32 %6, -1
br i1 %11, label %22, label %12
12: ; preds = %2
%13 = tail call i64 @read(i32 %6, i8* %10, i64 %9)
%14 = icmp eq i64 %13, %9
br i1 %14, label %20, label %15
15: ; preds = %12, %15
%16 = load i32, i32* @SEEK_SET
%17 = tail call i32 @lseek64(i32 %6, i32 0, i32 %16)
%18 = tail call i64 @read(i32 %6, i8* %10, i64 %9)
%19 = icmp eq i64 %18, %9
br i1 %19, label %20, label %15
20: ; preds = %15, %12
%21 = tail call i32 @applysignature_buf(i8* %10, i64 %9, i8* %1)
br label %26
22: ; preds = %2
%23 = load i32, i32* @errno
%24 = tail call i8* @strerror(i32 %23)
%25 = tail call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %24)
br label %26
26: ; preds = %22, %20
%27 = phi i32 [ %21, %20 ], [ -1, %22 ]
%28 = tail call i32 @close(i32 %6)
%29 = tail call i32 @free(i8* %10)
ret i32 %27
}