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

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ fgets(char *, size_t, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ kmsg(char *, char *);
size_t strcspn(char *, char *);
int sc_read_reboot_arg(char *arg, size_t max_size) {
  FILE *f;
  f = fopen("/run/systemd/reboot-param", "r");
  if (!f) {
    return -1;
  }
  if (!fgets(arg, max_size, f)) {
    fclose(f);
    return -1;
  }
  arg[strcspn(arg, "\n")] = '\0';
  kmsg("reboot arg is %s", arg);
  fclose(f);
  return 0;
}


x86 assembly (clang, -O0):
<sc_read_reboot_arg>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
lea    0xe99(%rip),%rdi
lea    0xeac(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    118e <sc_read_reboot_arg+0x3e>
movl   $0xffffffff,-0x4(%rbp)
jmp    1200 <sc_read_reboot_arg+0xb0>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1050 <fgets@plt>
cmp    $0x0,%eax
jne    11bd <sc_read_reboot_arg+0x6d>
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1200 <sc_read_reboot_arg+0xb0>
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe4c(%rip),%rsi
call   1040 <strcspn@plt>
mov    %rax,%rcx
mov    -0x28(%rbp),%rax
movb   $0x0,(%rax,%rcx,1)
mov    -0x10(%rbp),%rsi
lea    0xe33(%rip),%rdi
call   1070 <kmsg@plt>
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sc_read_reboot_arg>:
push   %rbp
push   %r14
push   %rbx
mov    %rsi,%rbp
mov    %rdi,%r14
lea    0xe9f(%rip),%rdi
lea    0xeb2(%rip),%rsi
call   1060 <fopen@plt>
test   %rax,%rax
je     11ae <sc_read_reboot_arg+0x5e>
mov    %rax,%rbx
mov    %r14,%rdi
mov    %rbp,%rsi
mov    %rax,%rdx
call   1050 <fgets@plt>
test   %eax,%eax
je     11b5 <sc_read_reboot_arg+0x65>
lea    0xe8e(%rip),%rsi
mov    %r14,%rdi
call   1040 <strcspn@plt>
movb   $0x0,(%r14,%rax,1)
lea    0xe7c(%rip),%rdi
mov    %r14,%rsi
call   1070 <kmsg@plt>
xor    %ebp,%ebp
jmp    11ba <sc_read_reboot_arg+0x6a>
mov    $0xffffffff,%ebp
jmp    11c2 <sc_read_reboot_arg+0x72>
mov    $0xffffffff,%ebp
mov    %rbx,%rdi
call   1030 <fclose@plt>
mov    %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [26 x i8] c"/run/systemd/reboot-param\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [2 x i8] c"\0A\00"
@.str.3 = [17 x i8] c"reboot arg is %s\00"
define i32 @sc_read_reboot_arg(i8* %0, i64 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i32*
store i8* %0, i8** %4
store i64 %1, i64* %5
%7 = call i32* @fopen(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %7, i32** %6
%8 = load i32*, i32** %6
%9 = icmp ne i32* %8, null
br i1 %9, label %11, label %10
10: ; preds = %2
store i32 -1, i32* %3
br label %29
11: ; preds = %2
%12 = load i8*, i8** %4
%13 = load i64, i64* %5
%14 = load i32*, i32** %6
%15 = call i32 @fgets(i8* %12, i64 %13, i32* %14)
%16 = icmp ne i32 %15, 0
br i1 %16, label %20, label %17
17: ; preds = %11
%18 = load i32*, i32** %6
%19 = call i32 @fclose(i32* %18)
store i32 -1, i32* %3
br label %29
20: ; preds = %11
%21 = load i8*, i8** %4
%22 = load i8*, i8** %4
%23 = call i64 @strcspn(i8* %22, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%24 = getelementptr inbounds i8, i8* %21, i64 %23
store i8 0, i8* %24
%25 = load i8*, i8** %4
%26 = call i32 @kmsg(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0), i8* %25)
%27 = load i32*, i32** %6
%28 = call i32 @fclose(i32* %27)
store i32 0, i32* %3
br label %29
29: ; preds = %20, %17, %10
%30 = load i32, i32* %3
ret i32 %30
}

LLVM IR (-O2):
@.str = [26 x i8] c"/run/systemd/reboot-param\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [2 x i8] c"\0A\00"
@.str.3 = [17 x i8] c"reboot arg is %s\00"
define i32 @sc_read_reboot_arg(i8* %0, i64 %1) {
%3 = tail call i32* @fopen(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %15, label %5
5: ; preds = %2
%6 = tail call i32 @fgets(i8* %0, i64 %1, i32* %3)
%7 = icmp eq i32 %6, 0
br i1 %7, label %12, label %8
8: ; preds = %5
%9 = tail call i64 @strcspn(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%10 = getelementptr inbounds i8, i8* %0, i64 %9
store i8 0, i8* %10
%11 = tail call i32 @kmsg(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0), i8* %0)
br label %12
12: ; preds = %5, %8
%13 = phi i32 [ 0, %8 ], [ -1, %5 ]
%14 = tail call i32 @fclose(i32* %3)
br label %15
15: ; preds = %12, %2
%16 = phi i32 [ -1, %2 ], [ %13, %12 ]
ret i32 %16
}