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

/* Variables and functions */
int PATH_MAX;
int /*<<< orphan*/ basename(char *);
int /*<<< orphan*/ puts(int /*<<< orphan*/);
int /*<<< orphan*/ shell_error(char *, char *);
int /*<<< orphan*/ strncpy(char *, char *, int);
int builtin_basename(int argc, char **argv) {
  char buf[PATH_MAX];
  int i;
  if (argc == 1) {
    shell_error(argv[0], "missing operand");
    return 1;
  } else
    for (i = 1; i < argc; i++) {
      strncpy(buf, argv[i], sizeof(buf));
      buf[sizeof(buf) - 1] = '\0';
      puts(basename(buf));
    }
  return 0;
}


x86 assembly (clang, -O0):
<builtin_basename>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e9a(%rip),%rax
mov    (%rax),%eax
mov    %rax,-0x38(%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,-0x30(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x20(%rbp)
cmpl   $0x1,-0x8(%rbp)
jne    11af <builtin_basename+0x6f>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
lea    0xe69(%rip),%rsi
call   1050 <shell_error@plt>
movl   $0x1,-0x4(%rbp)
movl   $0x1,-0x28(%rbp)
jmp    121a <builtin_basename+0xda>
movl   $0x1,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    1207 <builtin_basename+0xc7>
mov    -0x30(%rbp),%rdi
mov    -0x38(%rbp),%rax
mov    -0x10(%rbp),%rcx
movslq -0x24(%rbp),%rdx
mov    (%rcx,%rdx,8),%rsi
mov    %eax,%edx
call   1030 <strncpy@plt>
mov    -0x30(%rbp),%rdi
mov    -0x38(%rbp),%rax
sub    $0x1,%rax
movb   $0x0,(%rdi,%rax,1)
call   1060 <basename@plt>
mov    %eax,%edi
call   1040 <puts@plt>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    11b6 <builtin_basename+0x76>
jmp    120c <builtin_basename+0xcc>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x28(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<builtin_basename>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    0x2e98(%rip),%rax
mov    (%rax),%r12d
mov    %rsp,%rbx
lea    0xf(%r12),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
cmp    $0x1,%edi
jne    1188 <builtin_basename+0x48>
mov    (%r14),%rdi
lea    0xe84(%rip),%rsi
call   1050 <shell_error@plt>
mov    $0x1,%eax
jmp    11cf <builtin_basename+0x8f>
xor    %eax,%eax
cmp    $0x2,%edi
jl     11cf <builtin_basename+0x8f>
mov    %edi,%r13d
mov    $0x1,%r15d
nopl   0x0(%rax,%rax,1)
mov    (%r14,%r15,8),%rsi
mov    %rbx,%rdi
mov    %r12d,%edx
call   1030 <strncpy@plt>
movb   $0x0,-0x1(%rbx,%r12,1)
mov    %rbx,%rdi
call   1060 <basename@plt>
mov    %eax,%edi
call   1040 <puts@plt>
add    $0x1,%r15
cmp    %r15,%r13
jne    11a0 <builtin_basename+0x60>
xor    %eax,%eax
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
@.str = [16 x i8] c"missing operand\00"
define i32 @builtin_basename(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32
%9 = alloca i32
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* @PATH_MAX
%11 = zext i32 %10 to i64
%12 = call i8* @llvm.stacksave()
store i8* %12, i8** %6
%13 = alloca i8, i64 %11
store i64 %11, i64* %7
%14 = load i32, i32* %4
%15 = icmp eq i32 %14, 1
br i1 %15, label %16, label %21
16: ; preds = %2
%17 = load i8**, i8*** %5
%18 = getelementptr inbounds i8*, i8** %17, i64 0
%19 = load i8*, i8** %18
%20 = call i32 @shell_error(i8* %19, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
store i32 1, i32* %9
br label %43
21: ; preds = %2
store i32 1, i32* %8
br label %22
22: ; preds = %38, %21
%23 = load i32, i32* %8
%24 = load i32, i32* %4
%25 = icmp slt i32 %23, %24
br i1 %25, label %26, label %41
26: ; preds = %22
%27 = load i8**, i8*** %5
%28 = load i32, i32* %8
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds i8*, i8** %27, i64 %29
%31 = load i8*, i8** %30
%32 = trunc i64 %11 to i32
%33 = call i32 @strncpy(i8* %13, i8* %31, i32 %32)
%34 = sub i64 %11, 1
%35 = getelementptr inbounds i8, i8* %13, i64 %34
store i8 0, i8* %35
%36 = call i32 @basename(i8* %13)
%37 = call i32 @puts(i32 %36)
br label %38
38: ; preds = %26
%39 = load i32, i32* %8
%40 = add nsw i32 %39, 1
store i32 %40, i32* %8
br label %22
41: ; preds = %22
br label %42
42: ; preds = %41
store i32 0, i32* %3
store i32 1, i32* %9
br label %43
43: ; preds = %42, %16
%44 = load i8*, i8** %6
%45 = load i32, i32* %3
ret i32 %45
}

LLVM IR (-O2):
@PATH_MAX = global i32 0
@.str = [16 x i8] c"missing operand\00"
define i32 @builtin_basename(i32 %0, i8** nocapture %1) {
%3 = load i32, i32* @PATH_MAX
%4 = zext i32 %3 to i64
%5 = alloca i8, i64 %4
%6 = icmp eq i32 %0, 1
br i1 %6, label %13, label %7
7: ; preds = %2
%8 = icmp sgt i32 %0, 1
br i1 %8, label %9, label %25
9: ; preds = %7
%10 = add nsw i64 %4, -1
%11 = getelementptr inbounds i8, i8* %5, i64 %10
%12 = zext i32 %0 to i64
br label %16
13: ; preds = %2
%14 = load i8*, i8** %1
%15 = tail call i32 @shell_error(i8* %14, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
br label %25
16: ; preds = %9, %16
%17 = phi i64 [ 1, %9 ], [ %23, %16 ]
%18 = getelementptr inbounds i8*, i8** %1, i64 %17
%19 = load i8*, i8** %18
%20 = call i32 @strncpy(i8* %5, i8* %19, i32 %3)
store i8 0, i8* %11
%21 = call i32 @basename(i8* %5)
%22 = call i32 @puts(i32 %21)
%23 = add nuw nsw i64 %17, 1
%24 = icmp eq i64 %23, %12
br i1 %24, label %25, label %16
25: ; preds = %16, %7, %13
%26 = phi i32 [ 1, %13 ], [ 0, %7 ], [ 0, %16 ]
ret i32 %26
}