#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 {
  int dummy;
};
typedef int pid_t;

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int atoi(char *);
int check_changes(int, struct stat *, int);
int check_end_child_status(int);
int /*<<< orphan*/ close_and_exit(int, int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int fork();
int open_and_stat(char *, struct stat *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
int testfchmod(int, int);
int main(int argc, char *argv[]) {
  int ret;
  int fd, mode;
  pid_t pid;
  struct stat prev_stat;
  if (argc != 3) {
    printf("usage: %s path <mode>\n", argv[0]);
    exit(EXIT_FAILURE);
  }
  fd = open_and_stat(argv[1], &prev_stat);
  if (fd == -1)
    exit(EXIT_FAILURE);
  mode = atoi(argv[2]);
  pid = fork();
  if (pid == -1) {
    perror("fork");
    ret = -1;
    goto exit;
  }
  if (pid == 0) {
    ret = testfchmod(fd, mode);
  } else {
    ret = check_end_child_status(pid);
    if (ret)
      goto exit;
    ret = check_changes(fd, &prev_stat, mode);
    if (ret)
      goto exit;
  }
exit:
  close_and_exit(fd, ret);
  return 0;
}


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     11e1 <main+0x41>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe32(%rip),%rdi
call   1050 <printf@plt>
mov    0x2e06(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    -0x28(%rbp),%rsi
call   1040 <open_and_stat@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    120d <main+0x6d>
mov    0x2dda(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   1090 <atoi@plt>
mov    %eax,-0x1c(%rbp)
mov    $0x0,%al
call   10c0 <fork@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0xffffffff,-0x20(%rbp)
jne    1249 <main+0xa9>
lea    0xddf(%rip),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x14(%rbp)
jmp    12ab <main+0x10b>
cmpl   $0x0,-0x20(%rbp)
jne    1266 <main+0xc6>
mov    -0x18(%rbp),%edi
mov    -0x1c(%rbp),%esi
call   1060 <testfchmod@plt>
mov    %eax,-0x14(%rbp)
jmp    12a6 <main+0x106>
mov    -0x20(%rbp),%edi
call   1070 <check_end_child_status@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     1280 <main+0xe0>
jmp    12ab <main+0x10b>
mov    -0x18(%rbp),%edi
mov    -0x1c(%rbp),%edx
lea    -0x28(%rbp),%rsi
call   10b0 <check_changes@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     12a1 <main+0x101>
jmp    12ab <main+0x10b>
jmp    12a6 <main+0x106>
jmp    12ab <main+0x10b>
mov    -0x18(%rbp),%edi
mov    -0x14(%rbp),%esi
call   1030 <close_and_exit@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
mov    %rsi,%rbp
cmp    $0x3,%edi
jne    1220 <main+0x80>
mov    0x8(%rbp),%rdi
mov    %rsp,%rsi
call   1040 <open_and_stat@plt>
cmp    $0xffffffff,%eax
je     1230 <main+0x90>
mov    %eax,%ebx
mov    0x10(%rbp),%rdi
call   1090 <atoi@plt>
mov    %eax,%ebp
xor    %eax,%eax
call   10c0 <fork@plt>
test   %eax,%eax
je     11f2 <main+0x52>
cmp    $0xffffffff,%eax
je     120d <main+0x6d>
mov    %eax,%edi
call   1070 <check_end_child_status@plt>
test   %eax,%eax
jne    11fb <main+0x5b>
mov    %rsp,%rsi
mov    %ebx,%edi
mov    %ebp,%edx
call   10b0 <check_changes@plt>
jmp    11fb <main+0x5b>
mov    %ebx,%edi
mov    %ebp,%esi
call   1060 <testfchmod@plt>
mov    %ebx,%edi
mov    %eax,%esi
call   1030 <close_and_exit@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xe03(%rip),%rdi
call   1080 <perror@plt>
mov    $0xffffffff,%eax
jmp    11fb <main+0x5b>
mov    0x0(%rbp),%rsi
lea    0xdd5(%rip),%rdi
call   1050 <printf@plt>
mov    0x2da9(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [23 x i8] c"usage: %s path <mode>\0A\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [5 x i8] c"fork\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca %struct.stat
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = load i32, i32* %4
%12 = icmp ne i32 %11, 3
br i1 %12, label %13, label %20
13: ; preds = %2
%14 = load i8**, i8*** %5
%15 = getelementptr inbounds i8*, i8** %14, i64 0
%16 = load i8*, i8** %15
%17 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i8* %16)
%18 = load i32, i32* @EXIT_FAILURE
%19 = call i32 @exit(i32 %18)
unreachable
20: ; preds = %2
%21 = load i8**, i8*** %5
%22 = getelementptr inbounds i8*, i8** %21, i64 1
%23 = load i8*, i8** %22
%24 = call i32 @open_and_stat(i8* %23, %struct.stat* %10)
store i32 %24, i32* %7
%25 = load i32, i32* %7
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %30
27: ; preds = %20
%28 = load i32, i32* @EXIT_FAILURE
%29 = call i32 @exit(i32 %28)
unreachable
30: ; preds = %20
%31 = load i8**, i8*** %5
%32 = getelementptr inbounds i8*, i8** %31, i64 2
%33 = load i8*, i8** %32
%34 = call i32 @atoi(i8* %33)
store i32 %34, i32* %8
%35 = call i32 (...) @fork()
store i32 %35, i32* %9
%36 = load i32, i32* %9
%37 = icmp eq i32 %36, -1
br i1 %37, label %38, label %40
38: ; preds = %30
%39 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %6
br label %62
40: ; preds = %30
%41 = load i32, i32* %9
%42 = icmp eq i32 %41, 0
br i1 %42, label %43, label %47
43: ; preds = %40
%44 = load i32, i32* %7
%45 = load i32, i32* %8
%46 = call i32 @testfchmod(i32 %44, i32 %45)
store i32 %46, i32* %6
br label %61
47: ; preds = %40
%48 = load i32, i32* %9
%49 = call i32 @check_end_child_status(i32 %48)
store i32 %49, i32* %6
%50 = load i32, i32* %6
%51 = icmp ne i32 %50, 0
br i1 %51, label %52, label %53
52: ; preds = %47
br label %62
53: ; preds = %47
%54 = load i32, i32* %7
%55 = load i32, i32* %8
%56 = call i32 @check_changes(i32 %54, %struct.stat* %10, i32 %55)
store i32 %56, i32* %6
%57 = load i32, i32* %6
%58 = icmp ne i32 %57, 0
br i1 %58, label %59, label %60
59: ; preds = %53
br label %62
60: ; preds = %53
br label %61
61: ; preds = %60, %43
br label %62
62: ; preds = %61, %59, %52, %38
%63 = load i32, i32* %7
%64 = load i32, i32* %6
%65 = call i32 @close_and_exit(i32 %63, i32 %64)
ret i32 0
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [23 x i8] c"usage: %s path <mode>\0A\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [5 x i8] c"fork\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.stat
%4 = bitcast %struct.stat* %3 to i8*
%5 = icmp eq i32 %0, 3
br i1 %5, label %11, label %6
6: ; preds = %2
%7 = load i8*, i8** %1
%8 = tail call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i8* %7)
%9 = load i32, i32* @EXIT_FAILURE
%10 = tail call i32 @exit(i32 %9)
unreachable
11: ; preds = %2
%12 = getelementptr inbounds i8*, i8** %1, i64 1
%13 = load i8*, i8** %12
%14 = call i32 @open_and_stat(i8* %13, %struct.stat* %3)
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %19
16: ; preds = %11
%17 = load i32, i32* @EXIT_FAILURE
%18 = call i32 @exit(i32 %17)
unreachable
19: ; preds = %11
%20 = getelementptr inbounds i8*, i8** %1, i64 2
%21 = load i8*, i8** %20
%22 = call i32 @atoi(i8* %21)
%23 = call i32 (...) @fork()
switch i32 %23, label %28 [
i32 -1, label %24
i32 0, label %26
]
24: ; preds = %19
%25 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %33
26: ; preds = %19
%27 = call i32 @testfchmod(i32 %14, i32 %22)
br label %33
28: ; preds = %19
%29 = call i32 @check_end_child_status(i32 %23)
%30 = icmp eq i32 %29, 0
br i1 %30, label %31, label %33
31: ; preds = %28
%32 = call i32 @check_changes(i32 %14, %struct.stat* %3, i32 %22)
br label %33
33: ; preds = %31, %26, %28, %24
%34 = phi i32 [ -1, %24 ], [ %27, %26 ], [ %29, %28 ], [ %32, %31 ]
%35 = call i32 @close_and_exit(i32 %14, i32 %34)
ret i32 0
}