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

/* Variables and functions */
int /*<<< orphan*/ free(char *);
scalar_t__ malloc(int);
int sprintf(char *, char *, ...);
int strlen(char *);
char *make_nextKey_str(char *key, int nextId) {
  int idlen;
  char id_str[10];
  char *nextKey;
  int r;
  idlen = sprintf(id_str, "%d", nextId);
  if (nextId < 0 || idlen < 0)
    return NULL;
  r = strlen(key) + idlen + 1 + 1;
  nextKey = (char *)malloc(r * sizeof(char) + 2);
  if (nextKey == NULL)
    return NULL;
  r = sprintf(nextKey, "%s-%s", key, id_str);
  if (r < 0) {
    free(nextKey);
    return NULL;
  }
  return nextKey;
}


x86 assembly (clang, -O0):
<make_nextKey_str>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
lea    -0x22(%rbp),%rdi
mov    -0x14(%rbp),%edx
lea    0xea3(%rip),%rsi
mov    $0x0,%al
call   1060 <sprintf@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x14(%rbp)
jl     117b <make_nextKey_str+0x3b>
cmpl   $0x0,-0x18(%rbp)
jge    1188 <make_nextKey_str+0x48>
movq   $0x0,-0x8(%rbp)
jmp    1211 <make_nextKey_str+0xd1>
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
add    -0x18(%rbp),%eax
add    $0x1,%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
movslq -0x34(%rbp),%rax
shl    $0x0,%rax
add    $0x2,%rax
mov    %eax,%edi
call   1050 <malloc@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    11cc <make_nextKey_str+0x8c>
movq   $0x0,-0x8(%rbp)
jmp    1211 <make_nextKey_str+0xd1>
mov    -0x30(%rbp),%rdi
mov    -0x10(%rbp),%rdx
lea    -0x22(%rbp),%rcx
lea    0xe24(%rip),%rsi
mov    $0x0,%al
call   1060 <sprintf@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
jge    1209 <make_nextKey_str+0xc9>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    1211 <make_nextKey_str+0xd1>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<make_nextKey_str>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %esi,%ebp
mov    %rdi,%r14
lea    0xeaa(%rip),%rsi
xor    %r15d,%r15d
lea    0xe(%rsp),%rdi
mov    %ebp,%edx
xor    %eax,%eax
call   1060 <sprintf@plt>
or     %eax,%ebp
js     11b0 <make_nextKey_str+0x70>
mov    %eax,%ebx
mov    %r14,%rdi
call   1040 <strlen@plt>
lea    (%rbx,%rax,1),%edi
add    $0x4,%edi
call   1050 <malloc@plt>
test   %rax,%rax
je     11ad <make_nextKey_str+0x6d>
mov    %rax,%r15
lea    0xe74(%rip),%rsi
lea    0xe(%rsp),%rcx
mov    %rax,%rdi
mov    %r14,%rdx
xor    %eax,%eax
call   1060 <sprintf@plt>
test   %eax,%eax
jns    11b0 <make_nextKey_str+0x70>
mov    %r15,%rdi
call   1030 <free@plt>
xor    %r15d,%r15d
mov    %r15,%rax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"%d\00"
@.str.1 = [6 x i8] c"%s-%s\00"
define i8* @make_nextKey_str(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca [10 x i8]
%8 = alloca i8*
%9 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
%10 = getelementptr inbounds [10 x i8], [10 x i8]* %7, i64 0, i64 0
%11 = load i32, i32* %5
%12 = call i32 (i8*, i8*, ...) @sprintf(i8* %10, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %11)
store i32 %12, i32* %6
%13 = load i32, i32* %5
%14 = icmp slt i32 %13, 0
br i1 %14, label %18, label %15
15: ; preds = %2
%16 = load i32, i32* %6
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %19
18: ; preds = %15, %2
store i8* null, i8** %3
br label %48
19: ; preds = %15
%20 = load i8*, i8** %4
%21 = call i32 @strlen(i8* %20)
%22 = load i32, i32* %6
%23 = add nsw i32 %21, %22
%24 = add nsw i32 %23, 1
%25 = add nsw i32 %24, 1
store i32 %25, i32* %9
%26 = load i32, i32* %9
%27 = sext i32 %26 to i64
%28 = mul i64 %27, 1
%29 = add i64 %28, 2
%30 = trunc i64 %29 to i32
%31 = call i64 @malloc(i32 %30)
%32 = inttoptr i64 %31 to i8*
store i8* %32, i8** %8
%33 = load i8*, i8** %8
%34 = icmp eq i8* %33, null
br i1 %34, label %35, label %36
35: ; preds = %19
store i8* null, i8** %3
br label %48
36: ; preds = %19
%37 = load i8*, i8** %8
%38 = load i8*, i8** %4
%39 = getelementptr inbounds [10 x i8], [10 x i8]* %7, i64 0, i64 0
%40 = call i32 (i8*, i8*, ...) @sprintf(i8* %37, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* %38, i8* %39)
store i32 %40, i32* %9
%41 = load i32, i32* %9
%42 = icmp slt i32 %41, 0
br i1 %42, label %43, label %46
43: ; preds = %36
%44 = load i8*, i8** %8
%45 = call i32 @free(i8* %44)
store i8* null, i8** %3
br label %48
46: ; preds = %36
%47 = load i8*, i8** %8
store i8* %47, i8** %3
br label %48
48: ; preds = %46, %43, %35, %18
%49 = load i8*, i8** %3
ret i8* %49
}

LLVM IR (-O2):
@.str = [3 x i8] c"%d\00"
@.str.1 = [6 x i8] c"%s-%s\00"
define i8* @make_nextKey_str(i8* %0, i32 %1) {
%3 = alloca [10 x i8]
%4 = getelementptr inbounds [10 x i8], [10 x i8]* %3, i64 0, i64 0
%5 = call i32 (i8*, i8*, ...) @sprintf(i8* %4, i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %1)
%6 = or i32 %5, %1
%7 = icmp sgt i32 %6, -1
br i1 %7, label %8, label %20
8: ; preds = %2
%9 = tail call i32 @strlen(i8* %0)
%10 = add i32 %5, 4
%11 = add i32 %10, %9
%12 = tail call i64 @malloc(i32 %11)
%13 = inttoptr i64 %12 to i8*
%14 = icmp eq i64 %12, 0
br i1 %14, label %20, label %15
15: ; preds = %8
%16 = call i32 (i8*, i8*, ...) @sprintf(i8* %13, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* %0, i8* %4)
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %15
%19 = call i32 @free(i8* %13)
br label %20
20: ; preds = %15, %8, %2, %18
%21 = phi i8* [ null, %18 ], [ null, %2 ], [ null, %8 ], [ %13, %15 ]
ret i8* %21
}