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

/* Variables and functions */
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
scalar_t__ geteuid();
scalar_t__ getpid();
scalar_t__ malloc(int);
int /*<<< orphan*/ sprintf(char *, char *, char *, long, long, char *);
int /*<<< orphan*/ stderr;
int strlen(char *);
char *get_unique_name(char *path, char *tail) {
  char *name;
  int length;
  pid_t pid;
  uid_t uid;
  length = (path ? strlen(path) : strlen(".")) + 1 + 3 + 10 + 10 +
           (tail ? strlen(tail) : strlen("pooh")) + 1;
  name = (char *)malloc(length * sizeof(char));
  pid = getpid();
  uid = geteuid();
  sprintf(name, "%s/zc-%ld-%ld-%s", (path ? path : "."), (long)pid, (long)uid,
          (tail ? tail : "pooh"));
  if (length < strlen(name) + 1) {
    fprintf(stderr, "Buffer overflow (unrecoverable)!\n");
    exit(1);
  }
  return (name);
}


x86 assembly (clang, -O0):
<get_unique_name>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpq   $0x0,-0x8(%rbp)
je     119f <get_unique_name+0x2f>
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x34(%rbp)
jmp    11ae <get_unique_name+0x3e>
lea    0xe5a(%rip),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x34(%rbp)
mov    -0x34(%rbp),%eax
add    $0x1,%eax
add    $0x3,%eax
add    $0xa,%eax
add    $0xa,%eax
mov    %eax,-0x38(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     11dc <get_unique_name+0x6c>
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x3c(%rbp)
jmp    11eb <get_unique_name+0x7b>
lea    0xe1f(%rip),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x3c(%rbp)
mov    -0x38(%rbp),%eax
mov    -0x3c(%rbp),%ecx
add    %ecx,%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
movslq -0x1c(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%edi
call   1070 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %rax,-0x28(%rbp)
mov    $0x0,%al
call   1050 <geteuid@plt>
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x8(%rbp)
je     1242 <get_unique_name+0xd2>
mov    -0x8(%rbp),%rax
mov    %rax,-0x50(%rbp)
jmp    1252 <get_unique_name+0xe2>
lea    0xdb7(%rip),%rax
mov    %rax,-0x50(%rbp)
jmp    1252 <get_unique_name+0xe2>
mov    -0x50(%rbp),%rax
mov    %rax,-0x68(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x60(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,-0x58(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1282 <get_unique_name+0x112>
mov    -0x10(%rbp),%rax
mov    %rax,-0x70(%rbp)
jmp    1292 <get_unique_name+0x122>
lea    0xd79(%rip),%rax
mov    %rax,-0x70(%rbp)
jmp    1292 <get_unique_name+0x122>
mov    -0x58(%rbp),%r8
mov    -0x60(%rbp),%rcx
mov    -0x68(%rbp),%rdx
mov    -0x48(%rbp),%rdi
mov    -0x70(%rbp),%r9
lea    0xd5a(%rip),%rsi
call   1080 <sprintf@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x74(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x74(%rbp),%eax
add    $0x1,%ecx
cmp    %ecx,%eax
jge    12f0 <get_unique_name+0x180>
mov    0x2d08(%rip),%rax
mov    (%rax),%edi
lea    0xd37(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x18(%rbp),%rax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_unique_name>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rsi,%r14
mov    %rdi,%r15
test   %rdi,%rdi
je     1188 <get_unique_name+0x18>
mov    %r15,%rdi
jmp    118f <get_unique_name+0x1f>
lea    0xe71(%rip),%rdi
call   1040 <strlen@plt>
mov    %eax,%ebx
add    $0x18,%ebx
test   %r14,%r14
jne    11a5 <get_unique_name+0x35>
lea    0xe5d(%rip),%r14
mov    %r14,%rdi
call   1040 <strlen@plt>
lea    (%rbx,%rax,1),%ebp
lea    (%rbx,%rax,1),%edi
add    $0x1,%edi
call   1070 <malloc@plt>
mov    %rax,%rbx
xor    %eax,%eax
call   1030 <getpid@plt>
mov    %rax,%r12
xor    %eax,%eax
call   1050 <geteuid@plt>
test   %r15,%r15
lea    0xe27(%rip),%rdx
cmovne %r15,%rdx
lea    0xe23(%rip),%rsi
mov    %rbx,%rdi
mov    %r12,%rcx
mov    %rax,%r8
mov    %r14,%r9
call   1080 <sprintf@plt>
mov    %rbx,%rdi
call   1040 <strlen@plt>
cmp    %eax,%ebp
jl     120d <get_unique_name+0x9d>
mov    %rbx,%rax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2dcc(%rip),%rax
mov    (%rax),%edi
lea    0xdfb(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [2 x i8] c".\00"
@.str.1 = [5 x i8] c"pooh\00"
@.str.2 = [17 x i8] c"%s/zc-%ld-%ld-%s\00"
@stderr = global i32 0
@.str.3 = [34 x i8] c"Buffer overflow (unrecoverable)!\0A\00"
define i8* @get_unique_name(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i64
%8 = alloca i64
store i8* %0, i8** %3
store i8* %1, i8** %4
%9 = load i8*, i8** %3
%10 = icmp ne i8* %9, null
br i1 %10, label %11, label %14
11: ; preds = %2
%12 = load i8*, i8** %3
%13 = call i32 @strlen(i8* %12)
br label %16
14: ; preds = %2
%15 = call i32 @strlen(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
br label %16
16: ; preds = %14, %11
%17 = phi i32 [ %13, %11 ], [ %15, %14 ]
%18 = add nsw i32 %17, 1
%19 = add nsw i32 %18, 3
%20 = add nsw i32 %19, 10
%21 = add nsw i32 %20, 10
%22 = load i8*, i8** %4
%23 = icmp ne i8* %22, null
br i1 %23, label %24, label %27
24: ; preds = %16
%25 = load i8*, i8** %4
%26 = call i32 @strlen(i8* %25)
br label %29
27: ; preds = %16
%28 = call i32 @strlen(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %29
29: ; preds = %27, %24
%30 = phi i32 [ %26, %24 ], [ %28, %27 ]
%31 = add nsw i32 %21, %30
%32 = add nsw i32 %31, 1
store i32 %32, i32* %6
%33 = load i32, i32* %6
%34 = sext i32 %33 to i64
%35 = mul i64 %34, 1
%36 = trunc i64 %35 to i32
%37 = call i64 @malloc(i32 %36)
%38 = inttoptr i64 %37 to i8*
store i8* %38, i8** %5
%39 = call i64 (...) @getpid()
store i64 %39, i64* %7
%40 = call i64 (...) @geteuid()
store i64 %40, i64* %8
%41 = load i8*, i8** %5
%42 = load i8*, i8** %3
%43 = icmp ne i8* %42, null
br i1 %43, label %44, label %46
44: ; preds = %29
%45 = load i8*, i8** %3
br label %47
46: ; preds = %29
br label %47
47: ; preds = %46, %44
%48 = phi i8* [ %45, %44 ], [ getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), %46 ]
%49 = load i64, i64* %7
%50 = load i64, i64* %8
%51 = load i8*, i8** %4
%52 = icmp ne i8* %51, null
br i1 %52, label %53, label %55
53: ; preds = %47
%54 = load i8*, i8** %4
br label %56
55: ; preds = %47
br label %56
56: ; preds = %55, %53
%57 = phi i8* [ %54, %53 ], [ getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), %55 ]
%58 = call i32 @sprintf(i8* %41, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i8* %48, i64 %49, i64 %50, i8* %57)
%59 = load i32, i32* %6
%60 = load i8*, i8** %5
%61 = call i32 @strlen(i8* %60)
%62 = add nsw i32 %61, 1
%63 = icmp slt i32 %59, %62
br i1 %63, label %64, label %68
64: ; preds = %56
%65 = load i32, i32* @stderr
%66 = call i32 @fprintf(i32 %65, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.3, i64 0, i64 0))
%67 = call i32 @exit(i32 1)
unreachable
68: ; preds = %56
%69 = load i8*, i8** %5
ret i8* %69
}

LLVM IR (-O2):
@.str = [2 x i8] c".\00"
@.str.1 = [5 x i8] c"pooh\00"
@.str.2 = [17 x i8] c"%s/zc-%ld-%ld-%s\00"
@stderr = global i32 0
@.str.3 = [34 x i8] c"Buffer overflow (unrecoverable)!\0A\00"
define i8* @get_unique_name(i8* %0, i8* %1) {
%3 = icmp eq i8* %0, null
br i1 %3, label %6, label %4
4: ; preds = %2
%5 = tail call i32 @strlen(i8* %0)
br label %8
6: ; preds = %2
%7 = tail call i32 @strlen(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
br label %8
8: ; preds = %6, %4
%9 = phi i32 [ %5, %4 ], [ %7, %6 ]
%10 = add nsw i32 %9, 24
%11 = icmp eq i8* %1, null
br i1 %11, label %14, label %12
12: ; preds = %8
%13 = tail call i32 @strlen(i8* %1)
br label %16
14: ; preds = %8
%15 = tail call i32 @strlen(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %16
16: ; preds = %14, %12
%17 = phi i8* [ getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), %14 ], [ %1, %12 ]
%18 = phi i32 [ %15, %14 ], [ %13, %12 ]
%19 = add nsw i32 %10, %18
%20 = add nsw i32 %19, 1
%21 = tail call i64 @malloc(i32 %20)
%22 = inttoptr i64 %21 to i8*
%23 = tail call i64 (...) @getpid()
%24 = tail call i64 (...) @geteuid()
%25 = select i1 %3, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i8* %0
%26 = tail call i32 @sprintf(i8* dereferenceable(1) %22, i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i8* %25, i64 %23, i64 %24, i8* %17)
%27 = tail call i32 @strlen(i8* %22)
%28 = icmp slt i32 %19, %27
br i1 %28, label %29, label %33
29: ; preds = %16
%30 = load i32, i32* @stderr
%31 = tail call i32 @fprintf(i32 %30, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.3, i64 0, i64 0))
%32 = tail call i32 @exit(i32 1)
unreachable
33: ; preds = %16
ret i8* %22
}