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

/* Variables and functions */
int EXIT_FAILURE;
int EXIT_SUCCESS;
scalar_t__ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int);
int sscanf(char *, char *, int *);
int stat(char *, struct stat *);
int /*<<< orphan*/ stderr;
int main(int argc, char *argv[]) {
  struct stat sb;
  uint64_t vmlinux_size, vmlinux_load_addr, vmlinuz_load_addr;
  if (argc != 3) {
    fprintf(stderr, "Usage: %s <pathname> <vmlinux_load_addr>\n", argv[0]);
    return EXIT_FAILURE;
  }
  if (stat(argv[1], &sb) == -1) {
    perror("stat");
    return EXIT_FAILURE;
  }
  errno = 0;
  if (sscanf(argv[2], "%llx", &vmlinux_load_addr) != 1) {
    if (errno != 0)
      perror("sscanf");
    else
      fprintf(stderr, "No matching characters\n");
    return EXIT_FAILURE;
  }
  vmlinux_size = (uint64_t)sb.st_size;
  vmlinuz_load_addr = vmlinux_load_addr + vmlinux_size;
  vmlinuz_load_addr += (16 - vmlinux_size % 16);
  printf("0x%llx\n", vmlinuz_load_addr);
  return EXIT_SUCCESS;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x3,-0x8(%rbp)
je     119f <main+0x4f>
mov    0x2e69(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe79(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    0x2e3b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1292 <main+0x142>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    -0x18(%rbp),%rsi
call   1050 <stat@plt>
cmp    $0xffffffff,%eax
jne    11d6 <main+0x86>
lea    0xe6a(%rip),%rdi
call   1070 <perror@plt>
mov    0x2e04(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1292 <main+0x142>
mov    0x2dfb(%rip),%rax
movq   $0x0,(%rax)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
lea    0xe3c(%rip),%rsi
lea    -0x20(%rbp),%rdx
call   1060 <sscanf@plt>
cmp    $0x1,%eax
je     124f <main+0xff>
mov    0x2dcc(%rip),%rax
cmpq   $0x0,(%rax)
je     1227 <main+0xd7>
lea    0xe17(%rip),%rdi
call   1070 <perror@plt>
jmp    123e <main+0xee>
mov    0x2db2(%rip),%rax
mov    (%rax),%edi
lea    0xe04(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    0x2d8b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1292 <main+0x142>
mov    -0x18(%rbp),%rax
mov    %eax,-0x1c(%rbp)
mov    -0x20(%rbp),%eax
add    -0x1c(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x1c(%rbp),%eax
mov    $0x10,%ecx
cltd
idiv   %ecx
mov    $0x10,%eax
sub    %edx,%eax
add    -0x24(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%esi
lea    0xdd2(%rip),%rdi
call   1030 <printf@plt>
mov    0x2d3b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%rbx
cmp    $0x3,%edi
jne    11ce <main+0x7e>
mov    0x8(%rbx),%rdi
lea    0x10(%rsp),%rsi
call   1050 <stat@plt>
cmp    $0xffffffff,%eax
je     1218 <main+0xc8>
mov    0x2e5b(%rip),%r14
movq   $0x0,(%r14)
mov    0x10(%rbx),%rdi
lea    0xea0(%rip),%rsi
lea    0xc(%rsp),%rdx
call   1060 <sscanf@plt>
cmp    $0x1,%eax
jne    11ea <main+0x9a>
mov    0x10(%rsp),%eax
lea    0xf(%rax),%ecx
test   %eax,%eax
cmovns %eax,%ecx
and    $0xfffffff0,%ecx
sub    %eax,%ecx
add    0xc(%rsp),%eax
lea    (%rcx,%rax,1),%esi
add    $0x10,%esi
lea    0xe93(%rip),%rdi
call   1030 <printf@plt>
mov    0x2dfc(%rip),%rax
jmp    120e <main+0xbe>
mov    0x2e0b(%rip),%rax
mov    (%rax),%edi
mov    (%rbx),%rdx
lea    0xe1f(%rip),%rsi
xor    %eax,%eax
call   1040 <fprintf@plt>
jmp    1207 <main+0xb7>
cmpq   $0x0,(%r14)
jne    1221 <main+0xd1>
mov    0x2de9(%rip),%rax
mov    (%rax),%edi
lea    0xe3b(%rip),%rsi
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    0x2dc2(%rip),%rax
mov    (%rax),%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret
lea    0xe0b(%rip),%rdi
jmp    1228 <main+0xd8>
lea    0xe0c(%rip),%rdi
call   1070 <perror@plt>
jmp    1207 <main+0xb7>

LLVM IR (-O0):
%struct.stat = type { i64 }
@stderr = global i32 0
@.str = [42 x i8] c"Usage: %s <pathname> <vmlinux_load_addr>\0A\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [5 x i8] c"stat\00"
@errno = global i64 0
@.str.2 = [5 x i8] c"%llx\00"
@.str.3 = [7 x i8] c"sscanf\00"
@.str.4 = [24 x i8] c"No matching characters\0A\00"
@.str.5 = [8 x i8] c"0x%llx\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca %struct.stat
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* %4
%11 = icmp ne i32 %10, 3
br i1 %11, label %12, label %19
12: ; preds = %2
%13 = load i32, i32* @stderr
%14 = load i8**, i8*** %5
%15 = getelementptr inbounds i8*, i8** %14, i64 0
%16 = load i8*, i8** %15
%17 = call i32 (i32, i8*, ...) @fprintf(i32 %13, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str, i64 0, i64 0), i8* %16)
%18 = load i32, i32* @EXIT_FAILURE
store i32 %18, i32* %3
br label %59
19: ; preds = %2
%20 = load i8**, i8*** %5
%21 = getelementptr inbounds i8*, i8** %20, i64 1
%22 = load i8*, i8** %21
%23 = call i32 @stat(i8* %22, %struct.stat* %6)
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %28
25: ; preds = %19
%26 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%27 = load i32, i32* @EXIT_FAILURE
store i32 %27, i32* %3
br label %59
28: ; preds = %19
store i64 0, i64* @errno
%29 = load i8**, i8*** %5
%30 = getelementptr inbounds i8*, i8** %29, i64 2
%31 = load i8*, i8** %30
%32 = call i32 @sscanf(i8* %31, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i32* %8)
%33 = icmp ne i32 %32, 1
br i1 %33, label %34, label %44
34: ; preds = %28
%35 = load i64, i64* @errno
%36 = icmp ne i64 %35, 0
br i1 %36, label %37, label %39
37: ; preds = %34
%38 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
br label %42
39: ; preds = %34
%40 = load i32, i32* @stderr
%41 = call i32 (i32, i8*, ...) @fprintf(i32 %40, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.4, i64 0, i64 0))
br label %42
42: ; preds = %39, %37
%43 = load i32, i32* @EXIT_FAILURE
store i32 %43, i32* %3
br label %59
44: ; preds = %28
%45 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%46 = load i64, i64* %45
%47 = trunc i64 %46 to i32
store i32 %47, i32* %7
%48 = load i32, i32* %8
%49 = load i32, i32* %7
%50 = add nsw i32 %48, %49
store i32 %50, i32* %9
%51 = load i32, i32* %7
%52 = srem i32 %51, 16
%53 = sub nsw i32 16, %52
%54 = load i32, i32* %9
%55 = add nsw i32 %54, %53
store i32 %55, i32* %9
%56 = load i32, i32* %9
%57 = call i32 @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.5, i64 0, i64 0), i32 %56)
%58 = load i32, i32* @EXIT_SUCCESS
store i32 %58, i32* %3
br label %59
59: ; preds = %44, %42, %25, %12
%60 = load i32, i32* %3
ret i32 %60
}

LLVM IR (-O2):
%struct.stat = type { i64 }
@stderr = global i32 0
@.str = [42 x i8] c"Usage: %s <pathname> <vmlinux_load_addr>\0A\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [5 x i8] c"stat\00"
@errno = global i64 0
@.str.2 = [5 x i8] c"%llx\00"
@.str.3 = [7 x i8] c"sscanf\00"
@.str.4 = [24 x i8] c"No matching characters\0A\00"
@.str.5 = [8 x i8] c"0x%llx\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.stat
%4 = alloca i32
%5 = bitcast %struct.stat* %3 to i8*
%6 = bitcast i32* %4 to i8*
%7 = icmp eq i32 %0, 3
br i1 %7, label %12, label %8
8: ; preds = %2
%9 = load i32, i32* @stderr
%10 = load i8*, i8** %1
%11 = tail call i32 (i32, i8*, ...) @fprintf(i32 %9, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str, i64 0, i64 0), i8* %10)
br label %42
12: ; preds = %2
%13 = getelementptr inbounds i8*, i8** %1, i64 1
%14 = load i8*, i8** %13
%15 = call i32 @stat(i8* %14, %struct.stat* %3)
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %19
17: ; preds = %12
%18 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %42
19: ; preds = %12
store i64 0, i64* @errno
%20 = getelementptr inbounds i8*, i8** %1, i64 2
%21 = load i8*, i8** %20
%22 = call i32 @sscanf(i8* %21, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i32* %4)
%23 = icmp eq i32 %22, 1
br i1 %23, label %32, label %24
24: ; preds = %19
%25 = load i64, i64* @errno
%26 = icmp eq i64 %25, 0
br i1 %26, label %29, label %27
27: ; preds = %24
%28 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
br label %42
29: ; preds = %24
%30 = load i32, i32* @stderr
%31 = call i32 (i32, i8*, ...) @fprintf(i32 %30, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.4, i64 0, i64 0))
br label %42
32: ; preds = %19
%33 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%34 = load i64, i64* %33
%35 = trunc i64 %34 to i32
%36 = load i32, i32* %4
%37 = srem i32 %35, 16
%38 = add i32 %35, 16
%39 = add i32 %38, %36
%40 = sub i32 %39, %37
%41 = call i32 @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.5, i64 0, i64 0), i32 %40)
br label %42
42: ; preds = %27, %29, %32, %17, %8
%43 = phi i32* [ @EXIT_FAILURE, %8 ], [ @EXIT_FAILURE, %17 ], [ @EXIT_SUCCESS, %32 ], [ @EXIT_FAILURE, %29 ], [ @EXIT_FAILURE, %27 ]
%44 = load i32, i32* %43
ret i32 %44
}