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

/* Variables and functions */
int /*<<< orphan*/ ENOMEM;
int /*<<< orphan*/ PATH_MAX;
int /*<<< orphan*/ errn_exit(int /*<<< orphan*/, char *);
char **malloc(int);
int /*<<< orphan*/ snprintf(char *, int, char *, char *, int);
char *strdup(char *);
char **searchpath_create(char *root, int length) {
  char path[PATH_MAX + 1];
  char **searchpath;
  int i;
  if (!(searchpath = malloc(sizeof(char *) * length)))
    errn_exit(ENOMEM, "malloc searchpath");
  for (i = 0; i < length; i++) {
    snprintf(path, sizeof(path), "%s/%d", root, i);
    if (!(searchpath[i] = strdup(path)))
      errn_exit(ENOMEM, "strdup searchpath");
  }
  return searchpath;
}


x86 assembly (clang, -O0):
<searchpath_create>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    0x2e9a(%rip),%rax
mov    (%rax),%eax
add    $0x1,%eax
mov    %rax,-0x40(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x18(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x38(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x20(%rbp)
movslq -0xc(%rbp),%rax
shl    $0x3,%rax
mov    %eax,%edi
call   1050 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    11b4 <searchpath_create+0x74>
mov    0x2e32(%rip),%rax
mov    (%rax),%edi
lea    0xe51(%rip),%rsi
call   1030 <errn_exit@plt>
movl   $0x0,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    122c <searchpath_create+0xec>
mov    -0x38(%rbp),%rdi
mov    -0x40(%rbp),%rax
mov    %eax,%esi
mov    -0x8(%rbp),%rcx
mov    -0x2c(%rbp),%r8d
lea    0xe32(%rip),%rdx
call   1040 <snprintf@plt>
mov    -0x38(%rbp),%rdi
call   1060 <strdup@plt>
mov    -0x28(%rbp),%rcx
movslq -0x2c(%rbp),%rdx
mov    %rax,(%rcx,%rdx,8)
cmp    $0x0,%rax
jne    1219 <searchpath_create+0xd9>
mov    0x2dcd(%rip),%rax
mov    (%rax),%edi
lea    0xe04(%rip),%rsi
call   1030 <errn_exit@plt>
jmp    121e <searchpath_create+0xde>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    11bb <searchpath_create+0x7b>
mov    -0x28(%rbp),%rax
mov    -0x18(%rbp),%rcx
mov    %rcx,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<searchpath_create>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %esi,%ebx
mov    %rdi,-0x30(%rbp)
mov    0x2e95(%rip),%rax
mov    (%rax),%r15d
add    $0x1,%r15d
mov    %rsp,%r13
lea    0xf(%r15),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r13
mov    %r13,%rsp
lea    0x0(,%rbx,8),%edi
call   1050 <malloc@plt>
mov    %rax,%r12
test   %rax,%rax
je     118d <searchpath_create+0x4d>
test   %ebx,%ebx
jg     11a6 <searchpath_create+0x66>
jmp    11fa <searchpath_create+0xba>
mov    0x2e44(%rip),%rax
mov    (%rax),%edi
lea    0xe63(%rip),%rsi
call   1030 <errn_exit@plt>
test   %ebx,%ebx
jle    11fa <searchpath_create+0xba>
mov    %ebx,%r14d
xor    %ebx,%ebx
jmp    11b9 <searchpath_create+0x79>
nopl   (%rax)
add    $0x1,%rbx
cmp    %rbx,%r14
je     11fa <searchpath_create+0xba>
mov    %r13,%rdi
mov    %r15d,%esi
lea    0xe4c(%rip),%rdx
mov    -0x30(%rbp),%rcx
mov    %ebx,%r8d
call   1040 <snprintf@plt>
mov    %r13,%rdi
call   1060 <strdup@plt>
mov    %rax,(%r12,%rbx,8)
test   %rax,%rax
jne    11b0 <searchpath_create+0x70>
mov    0x2dee(%rip),%rax
mov    (%rax),%edi
lea    0xe25(%rip),%rsi
call   1030 <errn_exit@plt>
jmp    11b0 <searchpath_create+0x70>
mov    %r12,%rax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@PATH_MAX = global i32 0
@ENOMEM = global i32 0
@.str = [18 x i8] c"malloc searchpath\00"
@.str.1 = [6 x i8] c"%s/%d\00"
@.str.2 = [18 x i8] c"strdup searchpath\00"
define noalias i8** @searchpath_create(i8* %0, i32 %1) {
%3 = load i32, i32* @PATH_MAX
%4 = add nsw i32 %3, 1
%5 = zext i32 %4 to i64
%6 = alloca i8, i64 %5
%7 = shl i32 %1, 3
%8 = tail call i8** @malloc(i32 %7)
%9 = icmp eq i8** %8, null
br i1 %9, label %10, label %13
10: ; preds = %2
%11 = load i32, i32* @ENOMEM
%12 = tail call i32 @errn_exit(i32 %11, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
br label %13
13: ; preds = %10, %2
%14 = icmp sgt i32 %1, 0
br i1 %14, label %15, label %30
15: ; preds = %13
%16 = zext i32 %1 to i64
br label %17
17: ; preds = %15, %27
%18 = phi i64 [ 0, %15 ], [ %28, %27 ]
%19 = trunc i64 %18 to i32
%20 = call i32 @snprintf(i8* %6, i32 %4, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* %0, i32 %19)
%21 = call i8* @strdup(i8* %6)
%22 = getelementptr inbounds i8*, i8** %8, i64 %18
store i8* %21, i8** %22
%23 = icmp eq i8* %21, null
br i1 %23, label %24, label %27
24: ; preds = %17
%25 = load i32, i32* @ENOMEM
%26 = call i32 @errn_exit(i32 %25, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0))
br label %27
27: ; preds = %17, %24
%28 = add nuw nsw i64 %18, 1
%29 = icmp eq i64 %28, %16
br i1 %29, label %30, label %17
30: ; preds = %27, %13
ret i8** %8
}