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

/* Variables and functions */
scalar_t__ EEXIST;
int O_CREAT;
int O_EXCL;
int O_NOFOLLOW;
int O_TRUNC;
int O_WRONLY;
int /*<<< orphan*/ close(int);
scalar_t__ errno;
int /*<<< orphan*/ exit(int);
int getppid();
int open(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
scalar_t__ strcmp(char *, char *);
int /*<<< orphan*/ unlink(char *);
int /*<<< orphan*/ usage();
int main(int argc, char **argv) {
  int action = 1;
  char netreport_name[64];
  int netreport_file;
  if (argc > 2) {
    usage();
  }
  if (argc > 1) {
    if (argc == 2 && strcmp(argv[1], "-r") == 0) {
      action = 0;
    } else {
      usage();
    }
  }
  snprintf(netreport_name, sizeof(netreport_name), "/var/run/netreport/%d",
           getppid());
  if (action == 1) {
    netreport_file = open(
        netreport_name, O_EXCL | O_CREAT | O_WRONLY | O_TRUNC | O_NOFOLLOW, 0);
    if (netreport_file == -1) {
      if (errno != EEXIST) {
        perror("Could not create netreport file");
        exit(1);
      }
    } else {
      close(netreport_file);
    }
  } else {
    unlink(netreport_name);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x1,-0x14(%rbp)
cmpl   $0x2,-0x8(%rbp)
jle    11be <main+0x2e>
mov    $0x0,%al
call   1070 <usage@plt>
cmpl   $0x1,-0x8(%rbp)
jle    1208 <main+0x78>
cmpl   $0x2,-0x8(%rbp)
jne    11fc <main+0x6c>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe1f(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
jne    11fc <main+0x6c>
movl   $0x0,-0x14(%rbp)
jmp    1203 <main+0x73>
mov    $0x0,%al
call   1070 <usage@plt>
jmp    1208 <main+0x78>
lea    -0x60(%rbp),%rax
mov    %rax,-0x70(%rbp)
mov    $0x0,%al
call   10a0 <getppid@plt>
mov    -0x70(%rbp),%rdi
mov    %eax,%ecx
mov    $0x40,%esi
lea    0xdda(%rip),%rdx
call   1040 <snprintf@plt>
cmpl   $0x1,-0x14(%rbp)
jne    12bf <main+0x12f>
lea    -0x60(%rbp),%rdi
mov    0x2d7d(%rip),%rax
mov    (%rax),%esi
mov    0x2dac(%rip),%rax
or     (%rax),%esi
mov    0x2d53(%rip),%rax
or     (%rax),%esi
mov    0x2d8a(%rip),%rax
or     (%rax),%esi
mov    0x2d89(%rip),%rax
or     (%rax),%esi
xor    %edx,%edx
call   1080 <open@plt>
mov    %eax,-0x64(%rbp)
cmpl   $0xffffffff,-0x64(%rbp)
jne    12b2 <main+0x122>
mov    0x2d34(%rip),%rax
mov    (%rax),%rax
mov    0x2d3a(%rip),%rcx
cmp    (%rcx),%rax
je     12ad <main+0x11d>
lea    0xd7b(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
jmp    12ba <main+0x12a>
mov    -0x64(%rbp),%edi
call   1050 <close@plt>
jmp    12c8 <main+0x138>
lea    -0x60(%rbp),%rdi
call   1030 <unlink@plt>
xor    %eax,%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x40,%rsp
cmp    $0x2,%edi
jle    11a3 <main+0x13>
xor    %eax,%eax
call   1070 <usage@plt>
jmp    11be <main+0x2e>
jne    11c5 <main+0x35>
mov    0x8(%rsi),%rdi
lea    0xe50(%rip),%rsi
call   1060 <strcmp@plt>
test   %rax,%rax
je     1256 <main+0xc6>
xor    %eax,%eax
call   1070 <usage@plt>
xor    %eax,%eax
call   10a0 <getppid@plt>
lea    0xe30(%rip),%rdx
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x40,%esi
mov    %eax,%ecx
call   1040 <snprintf@plt>
mov    0x2dd4(%rip),%rax
mov    0x2e05(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2daa(%rip),%rax
or     (%rax),%esi
mov    0x2de1(%rip),%rax
or     (%rax),%esi
mov    0x2de0(%rip),%rax
or     (%rax),%esi
mov    %rbx,%rdi
xor    %edx,%edx
call   1080 <open@plt>
cmp    $0xffffffff,%eax
je     122a <main+0x9a>
mov    %eax,%edi
call   1050 <close@plt>
jmp    127e <main+0xee>
mov    0x2d87(%rip),%rax
mov    (%rax),%rax
mov    0x2d8d(%rip),%rcx
cmp    (%rcx),%rax
je     127e <main+0xee>
lea    0xdd2(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
xor    %eax,%eax
call   10a0 <getppid@plt>
lea    0xd9f(%rip),%rdx
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x40,%esi
mov    %eax,%ecx
call   1040 <snprintf@plt>
mov    %rbx,%rdi
call   1030 <unlink@plt>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@.str = [3 x i8] c"-r\00"
@.str.1 = [22 x i8] c"/var/run/netreport/%d\00"
@O_EXCL = global i32 0
@O_CREAT = global i32 0
@O_WRONLY = global i32 0
@O_TRUNC = global i32 0
@O_NOFOLLOW = global i32 0
@errno = global i64 0
@EEXIST = global i64 0
@.str.2 = [32 x i8] c"Could not create netreport file\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca [64 x i8]
%8 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 1, i32* %6
%9 = load i32, i32* %4
%10 = icmp sgt i32 %9, 2
br i1 %10, label %11, label %13
11: ; preds = %2
%12 = call i32 (...) @usage()
br label %13
13: ; preds = %11, %2
%14 = load i32, i32* %4
%15 = icmp sgt i32 %14, 1
br i1 %15, label %16, label %29
16: ; preds = %13
%17 = load i32, i32* %4
%18 = icmp eq i32 %17, 2
br i1 %18, label %19, label %26
19: ; preds = %16
%20 = load i8**, i8*** %5
%21 = getelementptr inbounds i8*, i8** %20, i64 1
%22 = load i8*, i8** %21
%23 = call i64 @strcmp(i8* %22, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%24 = icmp eq i64 %23, 0
br i1 %24, label %25, label %26
25: ; preds = %19
store i32 0, i32* %6
br label %28
26: ; preds = %19, %16
%27 = call i32 (...) @usage()
br label %28
28: ; preds = %26, %25
br label %29
29: ; preds = %28, %13
%30 = getelementptr inbounds [64 x i8], [64 x i8]* %7, i64 0, i64 0
%31 = call i32 (...) @getppid()
%32 = call i32 @snprintf(i8* %30, i32 64, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i32 %31)
%33 = load i32, i32* %6
%34 = icmp eq i32 %33, 1
br i1 %34, label %35, label %61
35: ; preds = %29
%36 = getelementptr inbounds [64 x i8], [64 x i8]* %7, i64 0, i64 0
%37 = load i32, i32* @O_EXCL
%38 = load i32, i32* @O_CREAT
%39 = or i32 %37, %38
%40 = load i32, i32* @O_WRONLY
%41 = or i32 %39, %40
%42 = load i32, i32* @O_TRUNC
%43 = or i32 %41, %42
%44 = load i32, i32* @O_NOFOLLOW
%45 = or i32 %43, %44
%46 = call i32 @open(i8* %36, i32 %45, i32 0)
store i32 %46, i32* %8
%47 = load i32, i32* %8
%48 = icmp eq i32 %47, -1
br i1 %48, label %49, label %57
49: ; preds = %35
%50 = load i64, i64* @errno
%51 = load i64, i64* @EEXIST
%52 = icmp ne i64 %50, %51
br i1 %52, label %53, label %56
53: ; preds = %49
%54 = call i32 @perror(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0))
%55 = call i32 @exit(i32 1)
unreachable
56: ; preds = %49
br label %60
57: ; preds = %35
%58 = load i32, i32* %8
%59 = call i32 @close(i32 %58)
br label %60
60: ; preds = %57, %56
br label %64
61: ; preds = %29
%62 = getelementptr inbounds [64 x i8], [64 x i8]* %7, i64 0, i64 0
%63 = call i32 @unlink(i8* %62)
br label %64
64: ; preds = %61, %60
ret i32 0
}

LLVM IR (-O2):
@.str = [3 x i8] c"-r\00"
@.str.1 = [22 x i8] c"/var/run/netreport/%d\00"
@O_EXCL = global i32 0
@O_CREAT = global i32 0
@O_WRONLY = global i32 0
@O_TRUNC = global i32 0
@O_NOFOLLOW = global i32 0
@errno = global i64 0
@EEXIST = global i64 0
@.str.2 = [32 x i8] c"Could not create netreport file\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [64 x i8]
%4 = getelementptr inbounds [64 x i8], [64 x i8]* %3, i64 0, i64 0
%5 = icmp sgt i32 %0, 2
br i1 %5, label %8, label %6
6: ; preds = %2
%7 = icmp eq i32 %0, 2
br i1 %7, label %10, label %21
8: ; preds = %2
%9 = tail call i32 (...) @usage()
br label %15
10: ; preds = %6
%11 = getelementptr inbounds i8*, i8** %1, i64 1
%12 = load i8*, i8** %11
%13 = tail call i64 @strcmp(i8* %12, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%14 = icmp eq i64 %13, 0
br i1 %14, label %17, label %15
15: ; preds = %8, %10
%16 = tail call i32 (...) @usage()
br label %21
17: ; preds = %10
%18 = tail call i32 (...) @getppid()
%19 = call i32 @snprintf(i8* %4, i32 64, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i32 %18)
%20 = call i32 @unlink(i8* %4)
br label %44
21: ; preds = %6, %15
%22 = tail call i32 (...) @getppid()
%23 = call i32 @snprintf(i8* %4, i32 64, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i32 %22)
%24 = load i32, i32* @O_EXCL
%25 = load i32, i32* @O_CREAT
%26 = or i32 %25, %24
%27 = load i32, i32* @O_WRONLY
%28 = or i32 %26, %27
%29 = load i32, i32* @O_TRUNC
%30 = or i32 %28, %29
%31 = load i32, i32* @O_NOFOLLOW
%32 = or i32 %30, %31
%33 = call i32 @open(i8* %4, i32 %32, i32 0)
%34 = icmp eq i32 %33, -1
br i1 %34, label %35, label %42
35: ; preds = %21
%36 = load i64, i64* @errno
%37 = load i64, i64* @EEXIST
%38 = icmp eq i64 %36, %37
br i1 %38, label %44, label %39
39: ; preds = %35
%40 = call i32 @perror(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0))
%41 = call i32 @exit(i32 1)
unreachable
42: ; preds = %21
%43 = call i32 @close(i32 %33)
br label %44
44: ; preds = %42, %35, %17
ret i32 0
}