#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 /*<<< orphan*/ member_0;
};

/* Variables and functions */
int /*<<< orphan*/ LOCK_EX;
int O_CREAT;
int O_RDWR;
int WEXITSTATUS(int);
scalar_t__ WIFEXITED(int);
int /*<<< orphan*/ errno;
int flock(int, int /*<<< orphan*/);
int fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, ...);
int fstat64(int, struct stat *);
int open(char *, int, int /*<<< orphan*/);
int run_child(char *);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int /*<<< orphan*/ system(char *);
int /*<<< orphan*/ wait(int *);
int main(int argc, char *argv[]) {
  int fd = -1, ret = -1, status = 0;
  char *filename = NULL, *cmd = NULL;
  struct stat stbuf = {
      0,
  };
  if (argc != 3) {
    fprintf(stderr,
            "Usage: %s <filename> "
            "<gluster-cmd-to-trigger-graph-switch>\n",
            argv[0]);
    goto out;
  }
  filename = argv[1];
  cmd = argv[2];
  fd = open(filename, O_RDWR | O_CREAT, 0);
  if (fd < 0) {
    fprintf(stderr, "open (%s) failed (%s)\n", filename, strerror(errno));
    goto out;
  }
  ret = flock(fd, LOCK_EX);
  if (ret < 0) {
    fprintf(stderr, "flock failed (%s)\n", strerror(errno));
    goto out;
  }
  system(cmd);
  ret = fstat64(fd, &stbuf);
  if (ret < 0) {
    fprintf(stderr, "fstat64 failure (%s)\n", strerror(errno));
    goto out;
  }
  sleep(10);
  ret = fork();
  if (ret == 0) {
    ret = run_child(filename);
  } else {
    wait(&status);
    if (WIFEXITED(status)) {
      ret = WEXITSTATUS(status);
    } else {
      ret = 0;
    }
  }
out:
  return ret;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0xffffffff,-0x14(%rbp)
movl   $0xffffffff,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
movq   $0x0,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
lea    -0x38(%rbp),%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1040 <memset@plt>
cmpl   $0x3,-0x8(%rbp)
je     1248 <main+0x78>
mov    0x2d9c(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xdc4(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
jmp    13da <main+0x20a>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rax
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%rdi
mov    0x2d75(%rip),%rax
mov    (%rax),%esi
mov    0x2d84(%rip),%rax
or     (%rax),%esi
xor    %edx,%edx
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    12c9 <main+0xf9>
mov    0x2d37(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x44(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    0x2d1b(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
mov    -0x44(%rbp),%edi
mov    -0x40(%rbp),%rdx
mov    %rax,%rcx
lea    0xd7f(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
jmp    13da <main+0x20a>
mov    -0x14(%rbp),%edi
mov    0x2d05(%rip),%rax
mov    (%rax),%esi
call   1080 <flock@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    131a <main+0x14a>
mov    0x2cda(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x48(%rbp)
mov    0x2cc6(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
mov    -0x48(%rbp),%edi
mov    %rax,%rdx
lea    0xd45(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
jmp    13da <main+0x20a>
mov    -0x30(%rbp),%rdi
call   1030 <system@plt>
mov    -0x14(%rbp),%edi
lea    -0x38(%rbp),%rsi
call   10b0 <fstat64@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    136f <main+0x19f>
mov    0x2c85(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4c(%rbp)
mov    0x2c71(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
mov    -0x4c(%rbp),%edi
mov    %rax,%rdx
lea    0xd03(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
jmp    13da <main+0x20a>
mov    $0xa,%edi
call   10d0 <sleep@plt>
mov    $0x0,%al
call   10f0 <fork@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jne    139e <main+0x1ce>
mov    -0x28(%rbp),%rdi
call   1090 <run_child@plt>
mov    %eax,-0x18(%rbp)
jmp    13d5 <main+0x205>
lea    -0x1c(%rbp),%rdi
call   10e0 <wait@plt>
mov    -0x1c(%rbp),%edi
call   10a0 <WIFEXITED@plt>
cmp    $0x0,%rax
je     13c9 <main+0x1f9>
mov    -0x1c(%rbp),%edi
call   1060 <WEXITSTATUS@plt>
mov    %eax,-0x18(%rbp)
jmp    13d0 <main+0x200>
movl   $0x0,-0x18(%rbp)
jmp    13d5 <main+0x205>
jmp    13da <main+0x20a>
mov    -0x18(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
movl   $0x0,0xc(%rsp)
movl   $0x0,0x10(%rsp)
cmp    $0x3,%edi
jne    128c <main+0xcc>
mov    0x8(%rsi),%r14
mov    0x10(%rsi),%r15
mov    0x2dee(%rip),%rax
mov    0x2dff(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %r14,%rdi
xor    %edx,%edx
call   1060 <open@plt>
test   %eax,%eax
js     12b0 <main+0xf0>
mov    %eax,%ebp
mov    0x2dc0(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
call   1070 <flock@plt>
test   %eax,%eax
js     12e4 <main+0x124>
mov    %r15,%rdi
call   1030 <system@plt>
lea    0x10(%rsp),%rsi
mov    %ebp,%edi
call   10a0 <fstat64@plt>
test   %eax,%eax
js     1306 <main+0x146>
mov    $0xa,%edi
call   10c0 <sleep@plt>
xor    %ebx,%ebx
xor    %eax,%eax
call   10e0 <fork@plt>
test   %eax,%eax
je     133f <main+0x17f>
lea    0xc(%rsp),%rdi
call   10d0 <wait@plt>
mov    0xc(%rsp),%edi
call   1090 <WIFEXITED@plt>
test   %rax,%rax
je     1332 <main+0x172>
mov    0xc(%rsp),%edi
call   1050 <WEXITSTATUS@plt>
mov    %eax,%ebx
jmp    1332 <main+0x172>
mov    0x2d35(%rip),%rax
mov    (%rax),%edi
mov    (%rsi),%rdx
lea    0xd61(%rip),%rsi
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    $0xffffffff,%ebx
jmp    1332 <main+0x172>
mov    0x2d11(%rip),%rax
mov    (%rax),%ebx
mov    0x2d00(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
lea    0xd6e(%rip),%rsi
mov    %ebx,%edi
mov    %r14,%rdx
mov    %rax,%rcx
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    $0xffffffff,%ebx
jmp    1332 <main+0x172>
mov    %eax,%ebx
mov    0x2cdb(%rip),%rax
mov    (%rax),%ebp
mov    0x2cca(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
lea    0xd4f(%rip),%rsi
jmp    1326 <main+0x166>
mov    %eax,%ebx
mov    0x2cb9(%rip),%rax
mov    (%rax),%ebp
mov    0x2ca8(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
lea    0xd40(%rip),%rsi
mov    %ebp,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %r14,%rdi
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1080 <run_child@plt>

LLVM IR (-O0):
%struct.stat = type { i32 }
@stderr = global i32 0
@.str = [60 x i8] c"Usage: %s <filename> <gluster-cmd-to-trigger-graph-switch>\0A\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@.str.1 = [23 x i8] c"open (%s) failed (%s)\0A\00"
@errno = global i32 0
@LOCK_EX = global i32 0
@.str.2 = [19 x i8] c"flock failed (%s)\0A\00"
@.str.3 = [22 x i8] c"fstat64 failure (%s)\0A\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 i8*
%10 = alloca i8*
%11 = alloca %struct.stat
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 -1, i32* %6
store i32 -1, i32* %7
store i32 0, i32* %8
store i8* null, i8** %9
store i8* null, i8** %10
%12 = bitcast %struct.stat* %11 to i8*
%13 = load i32, i32* %4
%14 = icmp ne i32 %13, 3
br i1 %14, label %15, label %21
15: ; preds = %2
%16 = load i32, i32* @stderr
%17 = load i8**, i8*** %5
%18 = getelementptr inbounds i8*, i8** %17, i64 0
%19 = load i8*, i8** %18
%20 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %16, i8* getelementptr inbounds ([60 x i8], [60 x i8]* @.str, i64 0, i64 0), i8* %19)
br label %83
21: ; preds = %2
%22 = load i8**, i8*** %5
%23 = getelementptr inbounds i8*, i8** %22, i64 1
%24 = load i8*, i8** %23
store i8* %24, i8** %9
%25 = load i8**, i8*** %5
%26 = getelementptr inbounds i8*, i8** %25, i64 2
%27 = load i8*, i8** %26
store i8* %27, i8** %10
%28 = load i8*, i8** %9
%29 = load i32, i32* @O_RDWR
%30 = load i32, i32* @O_CREAT
%31 = or i32 %29, %30
%32 = call i32 @open(i8* %28, i32 %31, i32 0)
store i32 %32, i32* %6
%33 = load i32, i32* %6
%34 = icmp slt i32 %33, 0
br i1 %34, label %35, label %41
35: ; preds = %21
%36 = load i32, i32* @stderr
%37 = load i8*, i8** %9
%38 = load i32, i32* @errno
%39 = call i8* @strerror(i32 %38)
%40 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %36, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i8* %37, i8* %39)
br label %83
41: ; preds = %21
%42 = load i32, i32* %6
%43 = load i32, i32* @LOCK_EX
%44 = call i32 @flock(i32 %42, i32 %43)
store i32 %44, i32* %7
%45 = load i32, i32* %7
%46 = icmp slt i32 %45, 0
br i1 %46, label %47, label %52
47: ; preds = %41
%48 = load i32, i32* @stderr
%49 = load i32, i32* @errno
%50 = call i8* @strerror(i32 %49)
%51 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %48, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i8* %50)
br label %83
52: ; preds = %41
%53 = load i8*, i8** %10
%54 = call i32 @system(i8* %53)
%55 = load i32, i32* %6
%56 = call i32 @fstat64(i32 %55, %struct.stat* %11)
store i32 %56, i32* %7
%57 = load i32, i32* %7
%58 = icmp slt i32 %57, 0
br i1 %58, label %59, label %64
59: ; preds = %52
%60 = load i32, i32* @stderr
%61 = load i32, i32* @errno
%62 = call i8* @strerror(i32 %61)
%63 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %60, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0), i8* %62)
br label %83
64: ; preds = %52
%65 = call i32 @sleep(i32 10)
%66 = call i32 (...) @fork()
store i32 %66, i32* %7
%67 = load i32, i32* %7
%68 = icmp eq i32 %67, 0
br i1 %68, label %69, label %72
69: ; preds = %64
%70 = load i8*, i8** %9
%71 = call i32 @run_child(i8* %70)
store i32 %71, i32* %7
br label %82
72: ; preds = %64
%73 = call i32 @wait(i32* %8)
%74 = load i32, i32* %8
%75 = call i64 @WIFEXITED(i32 %74)
%76 = icmp ne i64 %75, 0
br i1 %76, label %77, label %80
77: ; preds = %72
%78 = load i32, i32* %8
%79 = call i32 @WEXITSTATUS(i32 %78)
store i32 %79, i32* %7
br label %81
80: ; preds = %72
store i32 0, i32* %7
br label %81
81: ; preds = %80, %77
br label %82
82: ; preds = %81, %69
br label %83
83: ; preds = %82, %59, %47, %35, %15
%84 = load i32, i32* %7
ret i32 %84
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@stderr = global i32 0
@.str = [60 x i8] c"Usage: %s <filename> <gluster-cmd-to-trigger-graph-switch>\0A\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@.str.1 = [23 x i8] c"open (%s) failed (%s)\0A\00"
@errno = global i32 0
@LOCK_EX = global i32 0
@.str.2 = [19 x i8] c"flock failed (%s)\0A\00"
@.str.3 = [22 x i8] c"fstat64 failure (%s)\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i32
%4 = alloca %struct.stat
%5 = bitcast i32* %3 to i8*
store i32 0, i32* %3
%6 = bitcast %struct.stat* %4 to i8*
%7 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 0
store i32 0, i32* %7
%8 = icmp eq i32 %0, 3
br i1 %8, label %13, label %9
9: ; preds = %2
%10 = load i32, i32* @stderr
%11 = load i8*, i8** %1
%12 = tail call i32 (i32, i8*, i8*, ...) @fprintf(i32 %10, i8* getelementptr inbounds ([60 x i8], [60 x i8]* @.str, i64 0, i64 0), i8* %11)
br label %60
13: ; preds = %2
%14 = getelementptr inbounds i8*, i8** %1, i64 1
%15 = load i8*, i8** %14
%16 = getelementptr inbounds i8*, i8** %1, i64 2
%17 = load i8*, i8** %16
%18 = load i32, i32* @O_RDWR
%19 = load i32, i32* @O_CREAT
%20 = or i32 %19, %18
%21 = tail call i32 @open(i8* %15, i32 %20, i32 0)
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %28
23: ; preds = %13
%24 = load i32, i32* @stderr
%25 = load i32, i32* @errno
%26 = tail call i8* @strerror(i32 %25)
%27 = tail call i32 (i32, i8*, i8*, ...) @fprintf(i32 %24, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i8* %15, i8* %26)
br label %60
28: ; preds = %13
%29 = load i32, i32* @LOCK_EX
%30 = tail call i32 @flock(i32 %21, i32 %29)
%31 = icmp slt i32 %30, 0
br i1 %31, label %32, label %37
32: ; preds = %28
%33 = load i32, i32* @stderr
%34 = load i32, i32* @errno
%35 = tail call i8* @strerror(i32 %34)
%36 = tail call i32 (i32, i8*, i8*, ...) @fprintf(i32 %33, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i8* %35)
br label %60
37: ; preds = %28
%38 = tail call i32 @system(i8* %17)
%39 = call i32 @fstat64(i32 %21, %struct.stat* %4)
%40 = icmp slt i32 %39, 0
br i1 %40, label %41, label %46
41: ; preds = %37
%42 = load i32, i32* @stderr
%43 = load i32, i32* @errno
%44 = tail call i8* @strerror(i32 %43)
%45 = tail call i32 (i32, i8*, i8*, ...) @fprintf(i32 %42, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0), i8* %44)
br label %60
46: ; preds = %37
%47 = tail call i32 @sleep(i32 10)
%48 = tail call i32 (...) @fork()
%49 = icmp eq i32 %48, 0
br i1 %49, label %50, label %52
50: ; preds = %46
%51 = tail call i32 @run_child(i8* %15)
br label %60
52: ; preds = %46
%53 = call i32 @wait(i32* %3)
%54 = load i32, i32* %3
%55 = call i64 @WIFEXITED(i32 %54)
%56 = icmp eq i64 %55, 0
br i1 %56, label %60, label %57
57: ; preds = %52
%58 = load i32, i32* %3
%59 = call i32 @WEXITSTATUS(i32 %58)
br label %60
60: ; preds = %52, %50, %57, %41, %32, %23, %9
%61 = phi i32 [ -1, %9 ], [ -1, %23 ], [ %30, %32 ], [ %39, %41 ], [ %51, %50 ], [ %59, %57 ], [ 0, %52 ]
ret i32 %61
}