#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 scalar_t__ time_t;
struct stat {
  int dummy;
};
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
char *ctime(scalar_t__ *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, char *);
int /*<<< orphan*/ fputc(char, int /*<<< orphan*/ *);
int /*<<< orphan*/ fputs(char const *, int /*<<< orphan*/ *);
scalar_t__ stat(char *, struct stat *);
scalar_t__ time(int /*<<< orphan*/ *);
int create_nologin(const char *message) {
  struct stat st;
  if (stat("/etc/nologin", &st) < 0) {
    FILE *f = fopen("/etc/nologin", "w");
    if (f) {
      time_t t = time(NULL) + 300;
      fprintf(f, "The system is going down on %s\n", ctime(&t));
      if (message) {
        fputs(message, f);
        fputc('\n', f);
      }
      fclose(f);
      return 1;
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<create_nologin>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
lea    0xe6d(%rip),%rdi
lea    -0x18(%rbp),%rsi
call   1080 <stat@plt>
cmp    $0x0,%rax
jge    123f <create_nologin+0xbf>
lea    0xe53(%rip),%rdi
lea    0xe59(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     123a <create_nologin+0xba>
xor    %eax,%eax
mov    %eax,%edi
call   1090 <time@plt>
add    $0x12c,%rax
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x30(%rbp)
lea    -0x28(%rbp),%rdi
call   1040 <ctime@plt>
mov    -0x30(%rbp),%rdi
mov    %rax,%rdx
lea    0xe15(%rip),%rsi
call   1070 <fprintf@plt>
cmpq   $0x0,-0x10(%rbp)
je     1225 <create_nologin+0xa5>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1050 <fputs@plt>
mov    -0x20(%rbp),%rsi
mov    $0xa,%edi
call   1060 <fputc@plt>
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x1,-0x4(%rbp)
jmp    1246 <create_nologin+0xc6>
jmp    123f <create_nologin+0xbf>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<create_nologin>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%r14
lea    0xe6e(%rip),%rdi
lea    0x8(%rsp),%rsi
call   1080 <stat@plt>
xor    %ebp,%ebp
test   %rax,%rax
jns    1213 <create_nologin+0x93>
lea    0xe56(%rip),%rdi
lea    0xe5c(%rip),%rsi
call   10a0 <fopen@plt>
test   %rax,%rax
je     1213 <create_nologin+0x93>
mov    %rax,%rbx
xor    %edi,%edi
call   1090 <time@plt>
add    $0x12c,%rax
mov    %rax,(%rsp)
mov    %rsp,%rdi
call   1040 <ctime@plt>
lea    0xe31(%rip),%rsi
mov    %rbx,%rdi
mov    %rax,%rdx
call   1070 <fprintf@plt>
test   %r14,%r14
je     1206 <create_nologin+0x86>
mov    %r14,%rdi
mov    %rbx,%rsi
call   1050 <fputs@plt>
mov    $0xa,%edi
mov    %rbx,%rsi
call   1060 <fputc@plt>
mov    %rbx,%rdi
call   1030 <fclose@plt>
mov    $0x1,%ebp
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [13 x i8] c"/etc/nologin\00"
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [32 x i8] c"The system is going down on %s\0A\00"
define i32 @create_nologin(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca %struct.stat
%5 = alloca i32*
%6 = alloca i64
store i8* %0, i8** %3
%7 = call i64 @stat(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), %struct.stat* %4)
%8 = icmp slt i64 %7, 0
br i1 %8, label %9, label %31
9: ; preds = %1
%10 = call i32* @fopen(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %10, i32** %5
%11 = load i32*, i32** %5
%12 = icmp ne i32* %11, null
br i1 %12, label %13, label %30
13: ; preds = %9
%14 = call i64 @time(i32* null)
%15 = add nsw i64 %14, 300
store i64 %15, i64* %6
%16 = load i32*, i32** %5
%17 = call i8* @ctime(i64* %6)
%18 = call i32 @fprintf(i32* %16, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0), i8* %17)
%19 = load i8*, i8** %3
%20 = icmp ne i8* %19, null
br i1 %20, label %21, label %27
21: ; preds = %13
%22 = load i8*, i8** %3
%23 = load i32*, i32** %5
%24 = call i32 @fputs(i8* %22, i32* %23)
%25 = load i32*, i32** %5
%26 = call i32 @fputc(i8 signext 10, i32* %25)
br label %27
27: ; preds = %21, %13
%28 = load i32*, i32** %5
%29 = call i32 @fclose(i32* %28)
store i32 1, i32* %2
br label %32
30: ; preds = %9
br label %31
31: ; preds = %30, %1
store i32 0, i32* %2
br label %32
32: ; preds = %31, %27
%33 = load i32, i32* %2
ret i32 %33
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [13 x i8] c"/etc/nologin\00"
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [32 x i8] c"The system is going down on %s\0A\00"
define i32 @create_nologin(i8* %0) {
%2 = alloca %struct.stat
%3 = alloca i64
%4 = bitcast %struct.stat* %2 to i8*
%5 = call i64 @stat(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), %struct.stat* %2)
%6 = icmp slt i64 %5, 0
br i1 %6, label %7, label %22
7: ; preds = %1
%8 = call i32* @fopen(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%9 = icmp eq i32* %8, null
br i1 %9, label %22, label %10
10: ; preds = %7
%11 = bitcast i64* %3 to i8*
%12 = call i64 @time(i32* null)
%13 = add nsw i64 %12, 300
store i64 %13, i64* %3
%14 = call i8* @ctime(i64* %3)
%15 = call i32 @fprintf(i32* %8, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0), i8* %14)
%16 = icmp eq i8* %0, null
br i1 %16, label %20, label %17
17: ; preds = %10
%18 = call i32 @fputs(i8* %0, i32* %8)
%19 = call i32 @fputc(i8 signext 10, i32* %8)
br label %20
20: ; preds = %17, %10
%21 = call i32 @fclose(i32* %8)
br label %22
22: ; preds = %1, %7, %20
%23 = phi i32 [ 1, %20 ], [ 0, %7 ], [ 0, %1 ]
ret i32 %23
}