#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;
};

/* Variables and functions */
int O_CREAT;
int O_WRONLY;
int /*<<< orphan*/ PATH_MAX;
int /*<<< orphan*/ close(int);
int open(char *, int, int);
int pcapwritehdr(int, int);
int /*<<< orphan*/ snprintf(char *, int /*<<< orphan*/, char *, char *, int);
scalar_t__ stat(char *, struct stat *);
int /*<<< orphan*/ strcpy(char *, char *);
int /*<<< orphan*/ umask(int /*<<< orphan*/);
int hcxopencapdump(char *capdumpname) {
  int fd;
  int c;
  struct stat statinfo;
  char newcapdumpname[PATH_MAX + 2];
  c = 0;
  strcpy(newcapdumpname, capdumpname);
  while (stat(newcapdumpname, &statinfo) == 0) {
    snprintf(newcapdumpname, PATH_MAX, "%s_%d", capdumpname, c);
    c++;
  }
  umask(0);
  fd = open(newcapdumpname, O_WRONLY | O_CREAT, 0644);
  if (fd == -1)
    return -1;
  if (pcapwritehdr(fd, 105) == false) {
    close(fd);
    return 0;
  }
  return fd;
}


x86 assembly (clang, -O0):
<hcxopencapdump>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e65(%rip),%rax
mov    (%rax),%eax
add    $0x2,%eax
mov    %rsp,%rcx
mov    %rcx,-0x28(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,-0x40(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x30(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x10(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x40(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1070 <stat@plt>
cmp    $0x0,%rax
jne    1201 <hcxopencapdump+0x91>
mov    -0x40(%rbp),%rdi
mov    0x2e0b(%rip),%rax
mov    (%rax),%esi
mov    -0x10(%rbp),%rcx
mov    -0x18(%rbp),%r8d
lea    0xe12(%rip),%rdx
call   1040 <snprintf@plt>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    11bb <hcxopencapdump+0x4b>
xor    %edi,%edi
call   1060 <umask@plt>
mov    -0x40(%rbp),%rdi
mov    0x2db5(%rip),%rax
mov    (%rax),%esi
mov    0x2ddc(%rip),%rax
or     (%rax),%esi
mov    $0x1a4,%edx
call   1090 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    1248 <hcxopencapdump+0xd8>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x34(%rbp)
jmp    1286 <hcxopencapdump+0x116>
mov    -0x14(%rbp),%edi
mov    $0x69,%esi
call   1080 <pcapwritehdr@plt>
cmp    $0x0,%eax
jne    1279 <hcxopencapdump+0x109>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x34(%rbp)
jmp    1286 <hcxopencapdump+0x116>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x34(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<hcxopencapdump>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdi,%r14
mov    %rsp,-0x30(%rbp)
mov    0x2e59(%rip),%rax
mov    (%rax),%eax
add    $0x2,%eax
mov    %rsp,%r15
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r15
mov    %r15,%rsp
mov    %r15,%rdi
mov    %r14,%rsi
call   1030 <strcpy@plt>
lea    -0x38(%rbp),%rsi
mov    %r15,%rdi
call   1070 <stat@plt>
test   %rax,%rax
jne    11fd <hcxopencapdump+0x8d>
xor    %ebx,%ebx
lea    0xe36(%rip),%r12
lea    -0x38(%rbp),%r13
xchg   %ax,%ax
mov    0x2e11(%rip),%rax
mov    (%rax),%esi
mov    %r15,%rdi
mov    %r12,%rdx
mov    %r14,%rcx
mov    %ebx,%r8d
call   1040 <snprintf@plt>
add    $0x1,%ebx
mov    %r15,%rdi
mov    %r13,%rsi
call   1070 <stat@plt>
test   %rax,%rax
je     11d0 <hcxopencapdump+0x60>
xor    %edi,%edi
call   1060 <umask@plt>
mov    0x2dbd(%rip),%rax
mov    0x2de6(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %r15,%rdi
mov    $0x1a4,%edx
call   1090 <open@plt>
cmp    $0xffffffff,%eax
je     1245 <hcxopencapdump+0xd5>
mov    %eax,%ebx
mov    %eax,%edi
mov    $0x69,%esi
call   1080 <pcapwritehdr@plt>
test   %eax,%eax
jne    124a <hcxopencapdump+0xda>
mov    %ebx,%edi
call   1050 <close@plt>
xor    %ebx,%ebx
jmp    124a <hcxopencapdump+0xda>
mov    $0xffffffff,%ebx
mov    -0x30(%rbp),%rax
mov    %rax,%rsp
mov    %ebx,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@PATH_MAX = global i32 0
@.str = [6 x i8] c"%s_%d\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
define i32 @hcxopencapdump(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.stat
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i32
store i8* %0, i8** %3
%10 = load i32, i32* @PATH_MAX
%11 = add nsw i32 %10, 2
%12 = zext i32 %11 to i64
%13 = call i8* @llvm.stacksave()
store i8* %13, i8** %7
%14 = alloca i8, i64 %12
store i64 %12, i64* %8
store i32 0, i32* %5
%15 = load i8*, i8** %3
%16 = call i32 @strcpy(i8* %14, i8* %15)
br label %17
17: ; preds = %20, %1
%18 = call i64 @stat(i8* %14, %struct.stat* %6)
%19 = icmp eq i64 %18, 0
br i1 %19, label %20, label %27
20: ; preds = %17
%21 = load i32, i32* @PATH_MAX
%22 = load i8*, i8** %3
%23 = load i32, i32* %5
%24 = call i32 @snprintf(i8* %14, i32 %21, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %22, i32 %23)
%25 = load i32, i32* %5
%26 = add nsw i32 %25, 1
store i32 %26, i32* %5
br label %17
27: ; preds = %17
%28 = call i32 @umask(i32 0)
%29 = load i32, i32* @O_WRONLY
%30 = load i32, i32* @O_CREAT
%31 = or i32 %29, %30
%32 = call i32 @open(i8* %14, i32 %31, i32 420)
store i32 %32, i32* %4
%33 = load i32, i32* %4
%34 = icmp eq i32 %33, -1
br i1 %34, label %35, label %36
35: ; preds = %27
store i32 -1, i32* %2
store i32 1, i32* %9
br label %45
36: ; preds = %27
%37 = load i32, i32* %4
%38 = call i32 @pcapwritehdr(i32 %37, i32 105)
%39 = icmp eq i32 %38, 0
br i1 %39, label %40, label %43
40: ; preds = %36
%41 = load i32, i32* %4
%42 = call i32 @close(i32 %41)
store i32 0, i32* %2
store i32 1, i32* %9
br label %45
43: ; preds = %36
%44 = load i32, i32* %4
store i32 %44, i32* %2
store i32 1, i32* %9
br label %45
45: ; preds = %43, %40, %35
%46 = load i8*, i8** %7
%47 = load i32, i32* %2
ret i32 %47
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@PATH_MAX = global i32 0
@.str = [6 x i8] c"%s_%d\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
define i32 @hcxopencapdump(i8* %0) {
%2 = alloca %struct.stat
%3 = bitcast %struct.stat* %2 to i8*
%4 = load i32, i32* @PATH_MAX
%5 = add nsw i32 %4, 2
%6 = zext i32 %5 to i64
%7 = tail call i8* @llvm.stacksave()
%8 = alloca i8, i64 %6
%9 = call i32 @strcpy(i8* %8, i8* %0)
%10 = call i64 @stat(i8* %8, %struct.stat* %2)
%11 = icmp eq i64 %10, 0
br i1 %11, label %12, label %19
12: ; preds = %1, %12
%13 = phi i32 [ %16, %12 ], [ 0, %1 ]
%14 = load i32, i32* @PATH_MAX
%15 = call i32 @snprintf(i8* %8, i32 %14, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %0, i32 %13)
%16 = add nuw nsw i32 %13, 1
%17 = call i64 @stat(i8* %8, %struct.stat* %2)
%18 = icmp eq i64 %17, 0
br i1 %18, label %12, label %19
19: ; preds = %12, %1
%20 = call i32 @umask(i32 0)
%21 = load i32, i32* @O_WRONLY
%22 = load i32, i32* @O_CREAT
%23 = or i32 %22, %21
%24 = call i32 @open(i8* %8, i32 %23, i32 420)
%25 = icmp eq i32 %24, -1
br i1 %25, label %31, label %26
26: ; preds = %19
%27 = call i32 @pcapwritehdr(i32 %24, i32 105)
%28 = icmp eq i32 %27, 0
br i1 %28, label %29, label %31
29: ; preds = %26
%30 = call i32 @close(i32 %24)
br label %31
31: ; preds = %26, %19, %29
%32 = phi i32 [ 0, %29 ], [ -1, %19 ], [ %24, %26 ]
ret i32 %32
}