#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__ uint8_t;
typedef int /*<<< orphan*/ pid_t;

/* Variables and functions */
int /*<<< orphan*/ ECHILD;
int WEXITSTATUS(int);
int /*<<< orphan*/ WIFEXITED(int);
int WTERMSIG(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
int /*<<< orphan*/ fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int);
char *mntpoint(char const *);
int /*<<< orphan*/ perform_fsck_exec(char const *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdout;
int waitpid(int /*<<< orphan*/, int *, int /*<<< orphan*/);
int perform_fsck(const char *path, uint8_t search, uint8_t only) {
  pid_t pid;
  int st;
  const char *p;
  if (search) {
    p = mntpoint(path);
    if (p)
      path = p;
  }
  if (only)
    perform_fsck_exec(path);
  fflush(stdout);
  switch (pid = fork()) {
  case -1:
    perror("fork");
    exit(127);
  case 0:
    perform_fsck_exec(path);
  default:
    while (waitpid(pid, &st, 0) == -1) {
      if (errno == ECHILD) {
        perror("waitpid");
        exit(1);
      }
    }
    if (WIFEXITED(st))
      return WEXITSTATUS(st);
    fprintf(stderr, "child process failed %d.\n", WTERMSIG(st));
    exit(1);
  }
}


x86 assembly (clang, -O0):
<perform_fsck>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     11f4 <perform_fsck+0x44>
mov    -0x8(%rbp),%rdi
call   1030 <mntpoint@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     11ef <perform_fsck+0x3f>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11f4 <perform_fsck+0x44>
cmpq   $0x0,-0x18(%rbp)
je     1208 <perform_fsck+0x58>
mov    -0x8(%rbp),%rdi
call   1050 <perform_fsck_exec@plt>
mov    0x2dc1(%rip),%rax
mov    (%rax),%edi
call   1060 <fflush@plt>
xor    %eax,%eax
call   10d0 <fork@plt>
mov    %eax,-0x1c(%rbp)
mov    %eax,%ecx
mov    %ecx,-0x2c(%rbp)
sub    $0xffffffff,%eax
je     1243 <perform_fsck+0x93>
jmp    1233 <perform_fsck+0x83>
mov    -0x2c(%rbp),%eax
test   %eax,%eax
je     1259 <perform_fsck+0xa9>
jmp    1262 <perform_fsck+0xb2>
lea    0xdb6(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x7f,%edi
call   10c0 <exit@plt>
mov    -0x8(%rbp),%rdi
call   1050 <perform_fsck_exec@plt>
jmp    1267 <perform_fsck+0xb7>
mov    -0x1c(%rbp),%edi
lea    -0x20(%rbp),%rsi
xor    %edx,%edx
call   1090 <waitpid@plt>
cmp    $0xffffffff,%eax
jne    12b1 <perform_fsck+0x101>
mov    0x2d53(%rip),%rax
mov    (%rax),%eax
mov    0x2d3a(%rip),%rcx
cmp    (%rcx),%eax
jne    12ac <perform_fsck+0xfc>
lea    0xd68(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
jmp    1267 <perform_fsck+0xb7>
mov    -0x20(%rbp),%edi
call   10b0 <WIFEXITED@plt>
cmp    $0x0,%eax
je     12d0 <perform_fsck+0x120>
mov    -0x20(%rbp),%edi
call   1080 <WEXITSTATUS@plt>
add    $0x30,%rsp
pop    %rbp
ret
mov    0x2d09(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    -0x20(%rbp),%edi
call   1070 <WTERMSIG@plt>
mov    -0x30(%rbp),%edi
mov    %eax,%edx
lea    0xd1d(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>

x86 assembly (clang, -O2):
<perform_fsck>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdx,%rbp
mov    %rdi,%rbx
test   %rsi,%rsi
je     11d1 <perform_fsck+0x21>
mov    %rbx,%rdi
call   1030 <mntpoint@plt>
test   %rax,%rax
cmovne %rax,%rbx
test   %rbp,%rbp
je     11de <perform_fsck+0x2e>
mov    %rbx,%rdi
call   1050 <perform_fsck_exec@plt>
mov    0x2deb(%rip),%rax
mov    (%rax),%edi
call   1060 <fflush@plt>
xor    %eax,%eax
call   10d0 <fork@plt>
mov    %eax,%ebp
test   %eax,%eax
je     1214 <perform_fsck+0x64>
cmp    $0xffffffff,%ebp
jne    121c <perform_fsck+0x6c>
lea    0xdfb(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x7f,%edi
call   10c0 <exit@plt>
mov    %rbx,%rdi
call   1050 <perform_fsck_exec@plt>
lea    0x4(%rsp),%r14
mov    0x2db0(%rip),%r15
mov    0x2d99(%rip),%rbx
nop
mov    %ebp,%edi
mov    %r14,%rsi
xor    %edx,%edx
call   1090 <waitpid@plt>
cmp    $0xffffffff,%eax
jne    125e <perform_fsck+0xae>
mov    (%r15),%eax
cmp    (%rbx),%eax
jne    1230 <perform_fsck+0x80>
lea    0xdb6(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    0x4(%rsp),%edi
call   10b0 <WIFEXITED@plt>
test   %eax,%eax
je     127f <perform_fsck+0xcf>
mov    0x4(%rsp),%edi
call   1080 <WEXITSTATUS@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2d5a(%rip),%rax
mov    (%rax),%ebx
mov    0x4(%rsp),%edi
call   1070 <WTERMSIG@plt>
lea    0xd75(%rip),%rsi
mov    %ebx,%edi
mov    %eax,%edx
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
@stdout = global i32 0
@.str = [5 x i8] c"fork\00"
@errno = global i32 0
@ECHILD = global i32 0
@.str.1 = [8 x i8] c"waitpid\00"
@stderr = global i32 0
@.str.2 = [26 x i8] c"child process failed %d.\0A\00"
define i32 @perform_fsck(i8* %0, i64 %1, i64 %2) {
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i64
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
store i8* %0, i8** %4
store i64 %1, i64* %5
store i64 %2, i64* %6
%10 = load i64, i64* %5
%11 = icmp ne i64 %10, 0
br i1 %11, label %12, label %20
12: ; preds = %3
%13 = load i8*, i8** %4
%14 = call i8* @mntpoint(i8* %13)
store i8* %14, i8** %9
%15 = load i8*, i8** %9
%16 = icmp ne i8* %15, null
br i1 %16, label %17, label %19
17: ; preds = %12
%18 = load i8*, i8** %9
store i8* %18, i8** %4
br label %19
19: ; preds = %17, %12
br label %20
20: ; preds = %19, %3
%21 = load i64, i64* %6
%22 = icmp ne i64 %21, 0
br i1 %22, label %23, label %26
23: ; preds = %20
%24 = load i8*, i8** %4
%25 = call i32 @perform_fsck_exec(i8* %24)
br label %26
26: ; preds = %23, %20
%27 = load i32, i32* @stdout
%28 = call i32 @fflush(i32 %27)
%29 = call i32 (...) @fork()
store i32 %29, i32* %7
switch i32 %29, label %36 [
i32 -1, label %30
i32 0, label %33
]
30: ; preds = %26
%31 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%32 = call i32 @exit(i32 127)
unreachable
33: ; preds = %26
%34 = load i8*, i8** %4
%35 = call i32 @perform_fsck_exec(i8* %34)
br label %36
36: ; preds = %26, %33
br label %37
37: ; preds = %48, %36
%38 = load i32, i32* %7
%39 = call i32 @waitpid(i32 %38, i32* %8, i32 0)
%40 = icmp eq i32 %39, -1
br i1 %40, label %41, label %49
41: ; preds = %37
%42 = load i32, i32* @errno
%43 = load i32, i32* @ECHILD
%44 = icmp eq i32 %42, %43
br i1 %44, label %45, label %48
45: ; preds = %41
%46 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
%47 = call i32 @exit(i32 1)
unreachable
48: ; preds = %41
br label %37
49: ; preds = %37
%50 = load i32, i32* %8
%51 = call i32 @WIFEXITED(i32 %50)
%52 = icmp ne i32 %51, 0
br i1 %52, label %53, label %56
53: ; preds = %49
%54 = load i32, i32* %8
%55 = call i32 @WEXITSTATUS(i32 %54)
ret i32 %55
56: ; preds = %49
%57 = load i32, i32* @stderr
%58 = load i32, i32* %8
%59 = call i32 @WTERMSIG(i32 %58)
%60 = call i32 @fprintf(i32 %57, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i32 %59)
%61 = call i32 @exit(i32 1)
unreachable
}

LLVM IR (-O2):
@stdout = global i32 0
@.str = [5 x i8] c"fork\00"
@errno = global i32 0
@ECHILD = global i32 0
@.str.1 = [8 x i8] c"waitpid\00"
@stderr = global i32 0
@.str.2 = [26 x i8] c"child process failed %d.\0A\00"
define i32 @perform_fsck(i8* %0, i64 %1, i64 %2) {
%4 = alloca i32
%5 = bitcast i32* %4 to i8*
%6 = icmp eq i64 %1, 0
br i1 %6, label %11, label %7
7: ; preds = %3
%8 = tail call i8* @mntpoint(i8* %0)
%9 = icmp eq i8* %8, null
%10 = select i1 %9, i8* %0, i8* %8
br label %11
11: ; preds = %7, %3
%12 = phi i8* [ %0, %3 ], [ %10, %7 ]
%13 = icmp eq i64 %2, 0
br i1 %13, label %16, label %14
14: ; preds = %11
%15 = tail call i32 @perform_fsck_exec(i8* %12)
br label %16
16: ; preds = %14, %11
%17 = load i32, i32* @stdout
%18 = tail call i32 @fflush(i32 %17)
%19 = tail call i32 (...) @fork()
switch i32 %19, label %25 [
i32 -1, label %20
i32 0, label %23
]
20: ; preds = %16
%21 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%22 = tail call i32 @exit(i32 127)
unreachable
23: ; preds = %16
%24 = tail call i32 @perform_fsck_exec(i8* %12)
br label %25
25: ; preds = %16, %23
br label %26
26: ; preds = %25, %29
%27 = call i32 @waitpid(i32 %19, i32* %4, i32 0)
%28 = icmp eq i32 %27, -1
br i1 %28, label %29, label %36
29: ; preds = %26
%30 = load i32, i32* @errno
%31 = load i32, i32* @ECHILD
%32 = icmp eq i32 %30, %31
br i1 %32, label %33, label %26
33: ; preds = %29
%34 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
%35 = call i32 @exit(i32 1)
unreachable
36: ; preds = %26
%37 = load i32, i32* %4
%38 = call i32 @WIFEXITED(i32 %37)
%39 = icmp eq i32 %38, 0
br i1 %39, label %43, label %40
40: ; preds = %36
%41 = load i32, i32* %4
%42 = call i32 @WEXITSTATUS(i32 %41)
ret i32 %42
43: ; preds = %36
%44 = load i32, i32* @stderr
%45 = load i32, i32* %4
%46 = call i32 @WTERMSIG(i32 %45)
%47 = call i32 @fprintf(i32 %44, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i32 %46)
%48 = call i32 @exit(i32 1)
unreachable
}