#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 scalar_t__ pid_t;

/* Variables and functions */
scalar_t__ ENOENT;
int INT_MAX;
scalar_t__ errno;
int /*<<< orphan*/ fatalx(char *, long);
char *format(char const *, char *);
int /*<<< orphan*/ snprintf(char *, int, char *, long, int);
int stat(char const *, struct stat *);
const char *generate_tag(pid_t pid) {
  static char tag[sizeof("LONG_MAX"
                         "."
                         "INT_MAX")];
  int i;
  const char *path;
  struct stat st;
  const char *pathfmts[] = {"/var/crash_reports"
                            "/"
                            "crash_report"
                            "__"
                            "%s",
                            "/var/crash_reports"
                            "/"
                            "core"
                            "__"
                            "%s",
                            NULL},
             **fmt;
  for (i = 0; i < INT_MAX; ++i) {
    snprintf(tag, sizeof tag, "%ld.%d", (long)pid, i);
    for (fmt = pathfmts; *fmt != NULL; ++fmt) {
      path = format(*fmt, tag);
      if (stat(path, &st) == -1 && errno == ENOENT)
        continue;
      break;
    }
    if (*fmt == NULL)
      return tag;
  }
  fatalx("failed to generate a tag for pid %ld", (long)pid);
  return NULL;
}


x86 assembly (clang, -O0):
<generate_tag>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2c9d(%rip),%rax
mov    %rax,-0x40(%rbp)
mov    0x2c9a(%rip),%rax
mov    %rax,-0x38(%rbp)
mov    0x2c97(%rip),%rax
mov    %rax,-0x30(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    0x2e6a(%rip),%rcx
cmp    (%rcx),%eax
jge    124f <generate_tag+0x10f>
mov    -0x10(%rbp),%rcx
mov    -0x14(%rbp),%r8d
lea    0x2ebb(%rip),%rdi
mov    $0x11,%esi
lea    0xe9f(%rip),%rdx
call   1040 <snprintf@plt>
lea    -0x40(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x48(%rbp),%rax
cmpq   $0x0,(%rax)
je     121e <generate_tag+0xde>
mov    -0x48(%rbp),%rax
mov    (%rax),%rdi
lea    0x2e86(%rip),%rsi
call   1060 <format@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   1050 <stat@plt>
cmp    $0xffffffff,%eax
jne    1208 <generate_tag+0xc8>
mov    0x2de8(%rip),%rax
mov    (%rax),%rax
mov    0x2dce(%rip),%rcx
cmp    (%rcx),%rax
jne    1208 <generate_tag+0xc8>
jmp    120d <generate_tag+0xcd>
jmp    121e <generate_tag+0xde>
mov    -0x48(%rbp),%rax
add    $0x8,%rax
mov    %rax,-0x48(%rbp)
jmp    11ae <generate_tag+0x6e>
mov    -0x48(%rbp),%rax
cmpq   $0x0,(%rax)
jne    123c <generate_tag+0xfc>
lea    0x2e1d(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    1267 <generate_tag+0x127>
jmp    1241 <generate_tag+0x101>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1174 <generate_tag+0x34>
mov    -0x10(%rbp),%rsi
lea    0xded(%rip),%rdi
call   1030 <fatalx@plt>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<generate_tag>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    %rdi,%rcx
mov    0x2ca8(%rip),%rax
mov    %rax,0x30(%rsp)
movaps 0x2c8c(%rip),%xmm0
movaps %xmm0,0x20(%rsp)
mov    0x2e78(%rip),%rax
cmpl   $0x0,(%rax)
jle    1238 <generate_tag+0xf8>
xor    %r8d,%r8d
lea    0x28(%rsp),%r14
lea    0x2ec8(%rip),%rbx
lea    0xeb1(%rip),%rdx
lea    0xe6a(%rip),%r15
lea    0x18(%rsp),%r12
mov    0x2e26(%rip),%r13
mov    %rcx,0x10(%rsp)
mov    %rbx,%rdi
mov    $0x11,%esi
mov    %r8d,0xc(%rsp)
call   1040 <snprintf@plt>
mov    %r15,%rdi
mov    0x2e15(%rip),%rbp
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %rbx,%rsi
call   1060 <format@plt>
mov    %rax,%rdi
mov    %r12,%rsi
call   1050 <stat@plt>
cmp    $0xffffffff,%eax
jne    1200 <generate_tag+0xc0>
mov    0x0(%rbp),%rax
cmp    0x0(%r13),%rax
jne    1200 <generate_tag+0xc0>
mov    (%r14),%rdi
add    $0x8,%r14
test   %rdi,%rdi
jne    11d0 <generate_tag+0x90>
jmp    1249 <generate_tag+0x109>
cmpq   $0x0,-0x8(%r14)
je     1249 <generate_tag+0x109>
mov    0xc(%rsp),%r8d
add    $0x1,%r8d
mov    0x2dd1(%rip),%rax
cmp    (%rax),%r8d
mov    0x10(%rsp),%rcx
lea    0x28(%rsp),%r14
lea    0xe15(%rip),%rdx
lea    0xdce(%rip),%r15
jl     11a7 <generate_tag+0x67>
lea    0xe08(%rip),%rdi
mov    %rcx,%rsi
call   1030 <fatalx@plt>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@generate_tag.tag = internal global [17 x i8] zeroinitializer
@.str = [36 x i8] c"/var/crash_reports/crash_report__%s\00"
@.str.1 = [28 x i8] c"/var/crash_reports/core__%s\00"
@__const.generate_tag.pathfmts = [3 x i8*] [i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i32 0, i32 0), i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i32 0, i32 0), i8* null]
@INT_MAX = global i32 0
@.str.2 = [7 x i8] c"%ld.%d\00"
@errno = global i64 0
@ENOENT = global i64 0
@.str.3 = [37 x i8] c"failed to generate a tag for pid %ld\00"
define i8* @generate_tag(i64 %0) {
%2 = alloca i8*
%3 = alloca i64
%4 = alloca i32
%5 = alloca i8*
%6 = alloca %struct.stat
%7 = alloca [3 x i8*]
%8 = alloca i8**
store i64 %0, i64* %3
%9 = bitcast [3 x i8*]* %7 to i8*
store i32 0, i32* %4
br label %10
10: ; preds = %45, %1
%11 = load i32, i32* %4
%12 = load i32, i32* @INT_MAX
%13 = icmp slt i32 %11, %12
br i1 %13, label %14, label %48
14: ; preds = %10
%15 = load i64, i64* %3
%16 = load i32, i32* %4
%17 = call i32 @snprintf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @generate_tag.tag, i64 0, i64 0), i32 17, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i64 %15, i32 %16)
%18 = getelementptr inbounds [3 x i8*], [3 x i8*]* %7, i64 0, i64 0
store i8** %18, i8*** %8
br label %19
19: ; preds = %36, %14
%20 = load i8**, i8*** %8
%21 = load i8*, i8** %20
%22 = icmp ne i8* %21, null
br i1 %22, label %23, label %39
23: ; preds = %19
%24 = load i8**, i8*** %8
%25 = load i8*, i8** %24
%26 = call i8* @format(i8* %25, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @generate_tag.tag, i64 0, i64 0))
store i8* %26, i8** %5
%27 = load i8*, i8** %5
%28 = call i32 @stat(i8* %27, %struct.stat* %6)
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %35
30: ; preds = %23
%31 = load i64, i64* @errno
%32 = load i64, i64* @ENOENT
%33 = icmp eq i64 %31, %32
br i1 %33, label %34, label %35
34: ; preds = %30
br label %36
35: ; preds = %30, %23
br label %39
36: ; preds = %34
%37 = load i8**, i8*** %8
%38 = getelementptr inbounds i8*, i8** %37, i32 1
store i8** %38, i8*** %8
br label %19
39: ; preds = %35, %19
%40 = load i8**, i8*** %8
%41 = load i8*, i8** %40
%42 = icmp eq i8* %41, null
br i1 %42, label %43, label %44
43: ; preds = %39
store i8* getelementptr inbounds ([17 x i8], [17 x i8]* @generate_tag.tag, i64 0, i64 0), i8** %2
br label %51
44: ; preds = %39
br label %45
45: ; preds = %44
%46 = load i32, i32* %4
%47 = add nsw i32 %46, 1
store i32 %47, i32* %4
br label %10
48: ; preds = %10
%49 = load i64, i64* %3
%50 = call i32 @fatalx(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.3, i64 0, i64 0), i64 %49)
store i8* null, i8** %2
br label %51
51: ; preds = %48, %43
%52 = load i8*, i8** %2
ret i8* %52
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@generate_tag.tag = internal global [17 x i8] zeroinitializer
@.str = [36 x i8] c"/var/crash_reports/crash_report__%s\00"
@.str.1 = [28 x i8] c"/var/crash_reports/core__%s\00"
@__const.generate_tag.pathfmts = [3 x i8*] [i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i32 0, i32 0), i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i32 0, i32 0), i8* null]
@INT_MAX = global i32 0
@.str.2 = [7 x i8] c"%ld.%d\00"
@errno = global i64 0
@ENOENT = global i64 0
@.str.3 = [37 x i8] c"failed to generate a tag for pid %ld\00"
define i8* @generate_tag(i64 %0) {
%2 = alloca %struct.stat
%3 = alloca [3 x i8*]
%4 = bitcast %struct.stat* %2 to i8*
%5 = bitcast [3 x i8*]* %3 to i8*
%6 = load i32, i32* @INT_MAX
%7 = icmp sgt i32 %6, 0
br i1 %7, label %8, label %34
8: ; preds = %1
%9 = getelementptr inbounds [3 x i8*], [3 x i8*]* %3, i64 0, i64 0
br label %13
10: ; preds = %30
%11 = load i32, i32* @INT_MAX
%12 = icmp slt i32 %33, %11
br i1 %12, label %13, label %34
13: ; preds = %10, %8
%14 = phi i32 [ 0, %8 ], [ %33, %10 ]
%15 = tail call i32 @snprintf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @generate_tag.tag, i64 0, i64 0), i32 17, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i64 %0, i32 %14)
br label %16
16: ; preds = %13, %26
%17 = phi i8* [ %28, %26 ], [ getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), %13 ]
%18 = phi i8** [ %27, %26 ], [ %9, %13 ]
%19 = tail call i8* @format(i8* %17, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @generate_tag.tag, i64 0, i64 0))
%20 = call i32 @stat(i8* %19, %struct.stat* %2)
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %30
22: ; preds = %16
%23 = load i64, i64* @errno
%24 = load i64, i64* @ENOENT
%25 = icmp eq i64 %23, %24
br i1 %25, label %26, label %30
26: ; preds = %22
%27 = getelementptr inbounds i8*, i8** %18, i64 1
%28 = load i8*, i8** %27
%29 = icmp eq i8* %28, null
br i1 %29, label %36, label %16
30: ; preds = %16, %22
%31 = load i8*, i8** %18
%32 = icmp eq i8* %31, null
%33 = add nuw nsw i32 %14, 1
br i1 %32, label %36, label %10
34: ; preds = %10, %1
%35 = tail call i32 @fatalx(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.3, i64 0, i64 0), i64 %0)
br label %36
36: ; preds = %30, %26, %34
%37 = phi i8* [ null, %34 ], [ getelementptr inbounds ([17 x i8], [17 x i8]* @generate_tag.tag, i64 0, i64 0), %26 ], [ getelementptr inbounds ([17 x i8], [17 x i8]* @generate_tag.tag, i64 0, i64 0), %30 ]
ret i8* %37
}