#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 *, ...);
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 = snprintf(out, sz, "./%s", cmd);
  assert(n > 0);
  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
mov    $0x0,%al
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     11b5 <cmdpath+0x65>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1222 <cmdpath+0xd2>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    -0x20(%rbp),%rcx
lea    0xe45(%rip),%rdx
mov    $0x0,%al
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     1203 <cmdpath+0xb3>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1222 <cmdpath+0xd2>
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   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %esi,%r15d
mov    %rdi,%rbx
lea    0xe99(%rip),%rdx
mov    %r14,%rcx
mov    %r14,%r8
xor    %eax,%eax
call   1050 <snprintf@plt>
mov    %eax,%ebp
xor    %edi,%edi
test   %eax,%eax
setg   %dil
call   1060 <assert@plt>
mov    %rsp,%rsi
mov    %rbx,%rdi
call   1070 <stat@plt>
cmp    $0xffffffff,%eax
jne    11de <cmdpath+0x8e>
lea    0xe72(%rip),%rdx
mov    %rbx,%rdi
mov    %r15d,%esi
mov    %r14,%rcx
xor    %eax,%eax
call   1050 <snprintf@plt>
mov    %eax,%ebp
xor    %edi,%edi
test   %eax,%eax
setg   %dil
call   1060 <assert@plt>
mov    %rsp,%rsi
mov    %rbx,%rdi
call   1070 <stat@plt>
cmp    $0xffffffff,%eax
jne    11de <cmdpath+0x8e>
mov    %r14,%rdi
call   1040 <strlen@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
mov    %r14,%rsi
call   1030 <strcpy@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [12 x i8] c"./cmd/%s/%s\00"
@.str.1 = [5 x i8] c"./%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 (i8*, i32, i8*, i8*, ...) @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 %45
24: ; preds = %3
%25 = load i8*, i8** %5
%26 = load i32, i32* %6
%27 = load i8*, i8** %7
%28 = call i32 (i8*, i32, i8*, i8*, ...) @snprintf(i8* %25, i32 %26, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* %27)
store i32 %28, i32* %8
%29 = load i32, i32* %8
%30 = icmp sgt i32 %29, 0
%31 = zext i1 %30 to i32
%32 = call i32 @assert(i32 %31)
%33 = load i8*, i8** %5
%34 = call i32 @stat(i8* %33, %struct.stat* %9)
%35 = icmp ne i32 %34, -1
br i1 %35, label %36, label %38
36: ; preds = %24
%37 = load i32, i32* %8
store i32 %37, i32* %4
br label %45
38: ; preds = %24
%39 = load i8*, i8** %7
%40 = call i32 @strlen(i8* %39)
store i32 %40, i32* %8
%41 = load i8*, i8** %5
%42 = load i8*, i8** %7
%43 = call i32 @strcpy(i8* %41, i8* %42)
%44 = load i32, i32* %8
store i32 %44, i32* %4
br label %45
45: ; preds = %38, %36, %22
%46 = load i32, i32* %4
ret i32 %46
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [12 x i8] c"./cmd/%s/%s\00"
@.str.1 = [5 x i8] c"./%s\00"
define i32 @cmdpath(i8* %0, i32 %1, i8* %2) {
%4 = alloca %struct.stat
%5 = tail call i32 (i8*, i32, i8*, i8*, ...) @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 %22
12: ; preds = %3
%13 = tail call i32 (i8*, i32, i8*, i8*, ...) @snprintf(i8* %0, i32 %1, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* %2)
%14 = icmp sgt i32 %13, 0
%15 = zext i1 %14 to i32
%16 = tail call i32 @assert(i32 %15)
%17 = call i32 @stat(i8* %0, %struct.stat* %4)
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %22
19: ; preds = %12
%20 = tail call i32 @strlen(i8* %2)
%21 = tail call i32 @strcpy(i8* %0, i8* %2)
br label %22
22: ; preds = %12, %3, %19
%23 = phi i32 [ %20, %19 ], [ %5, %3 ], [ %13, %12 ]
ret i32 %23
}