#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 /*<<< orphan*/ assert(int);
int snprintf(char *, int, char *, char *, char *);
int stat(char *, struct stat *);
int /*<<< orphan*/ strcpy(char *, char *);
int strlen(char *);
int cmdpath(char *out, int sz, char *cmd) {
  int n = snprintf(out, sz, "./cmd/%s/%s", cmd, cmd);
  assert(n > 0);
  struct stat s;
  if (stat(out, &s) != -1)
    return n;
  n = strlen(cmd);
  strcpy(out, cmd);
  return n;
}


x86 assembly (clang, -O0):
<cmdpath>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    -0x20(%rbp),%rcx
mov    -0x20(%rbp),%r8
lea    0xe87(%rip),%rdx
call   1050 <snprintf@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
setg   %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
mov    -0x10(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   1070 <stat@plt>
cmp    $0xffffffff,%eax
je     11b3 <cmdpath+0x63>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11d2 <cmdpath+0x82>
mov    -0x20(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cmdpath>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdx,%rbx
mov    %rdi,%r14
lea    0xe9b(%rip),%rdx
mov    %rbx,%rcx
mov    %rbx,%r8
call   1050 <snprintf@plt>
mov    %eax,%ebp
xor    %edi,%edi
test   %eax,%eax
setg   %dil
call   1060 <assert@plt>
lea    0x8(%rsp),%rsi
mov    %r14,%rdi
call   1070 <stat@plt>
cmp    $0xffffffff,%eax
jne    11a6 <cmdpath+0x56>
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %eax,%ebp
mov    %r14,%rdi
mov    %rbx,%rsi
call   1030 <strcpy@plt>
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [12 x i8] c"./cmd/%s/%s\00"
define i32 @cmdpath(i8* %0, i32 %1, i8* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca %struct.stat
store i8* %0, i8** %5
store i32 %1, i32* %6
store i8* %2, i8** %7
%10 = load i8*, i8** %5
%11 = load i32, i32* %6
%12 = load i8*, i8** %7
%13 = load i8*, i8** %7
%14 = call i32 @snprintf(i8* %10, i32 %11, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8* %12, i8* %13)
store i32 %14, i32* %8
%15 = load i32, i32* %8
%16 = icmp sgt i32 %15, 0
%17 = zext i1 %16 to i32
%18 = call i32 @assert(i32 %17)
%19 = load i8*, i8** %5
%20 = call i32 @stat(i8* %19, %struct.stat* %9)
%21 = icmp ne i32 %20, -1
br i1 %21, label %22, label %24
22: ; preds = %3
%23 = load i32, i32* %8
store i32 %23, i32* %4
br label %31
24: ; preds = %3
%25 = load i8*, i8** %7
%26 = call i32 @strlen(i8* %25)
store i32 %26, i32* %8
%27 = load i8*, i8** %5
%28 = load i8*, i8** %7
%29 = call i32 @strcpy(i8* %27, i8* %28)
%30 = load i32, i32* %8
store i32 %30, i32* %4
br label %31
31: ; preds = %24, %22
%32 = load i32, i32* %4
ret i32 %32
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [12 x i8] c"./cmd/%s/%s\00"
define i32 @cmdpath(i8* %0, i32 %1, i8* %2) {
%4 = alloca %struct.stat
%5 = tail call i32 @snprintf(i8* %0, i32 %1, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8* %2, i8* %2)
%6 = icmp sgt i32 %5, 0
%7 = zext i1 %6 to i32
%8 = tail call i32 @assert(i32 %7)
%9 = bitcast %struct.stat* %4 to i8*
%10 = call i32 @stat(i8* %0, %struct.stat* %4)
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %15
12: ; preds = %3
%13 = tail call i32 @strlen(i8* %2)
%14 = tail call i32 @strcpy(i8* %0, i8* %2)
br label %15
15: ; preds = %3, %12
%16 = phi i32 [ %13, %12 ], [ %5, %3 ]
ret i32 %16
}