#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 */
char *malloc(int);
int strlen(char const *);
int /*<<< orphan*/ strncpy(char *, char const *, int);
char *strrchr(char const *, char);
char *setjobname(const char *basename, int strip) {
  char *jobname;
  int n = strlen(basename);
  if (strip) {
    char *extension = strrchr(basename, '.');
    if (extension)
      n -= strlen(extension);
  }
  jobname = malloc((n + 1) * sizeof(char));
  strncpy(jobname, basename, n);
  jobname[n] = '\0';
  return jobname;
}


x86 assembly (clang, -O0):
<setjobname>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0xc(%rbp)
je     119a <setjobname+0x5a>
mov    -0x8(%rbp),%rdi
mov    $0x2e,%esi
call   1050 <strrchr@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     1195 <setjobname+0x55>
mov    -0x28(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x1c(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x1c(%rbp)
jmp    119a <setjobname+0x5a>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
cltq
shl    $0x0,%rax
mov    %eax,%edi
call   1060 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1030 <strncpy@plt>
mov    -0x18(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<setjobname>:
push   %rbp
push   %r14
push   %rbx
mov    %esi,%ebp
mov    %rdi,%r14
call   1040 <strlen@plt>
mov    %eax,%ebx
test   %ebp,%ebp
je     1170 <setjobname+0x30>
mov    %r14,%rdi
mov    $0x2e,%esi
call   1050 <strrchr@plt>
test   %rax,%rax
je     1170 <setjobname+0x30>
mov    %rax,%rdi
call   1040 <strlen@plt>
sub    %eax,%ebx
lea    0x1(%rbx),%edi
call   1060 <malloc@plt>
mov    %rax,%rbp
mov    %rax,%rdi
mov    %r14,%rsi
mov    %ebx,%edx
call   1030 <strncpy@plt>
movslq %ebx,%rax
movb   $0x0,0x0(%rbp,%rax,1)
mov    %rbp,%rax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
define i8* @setjobname(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i8*
store i8* %0, i8** %3
store i32 %1, i32* %4
%8 = load i8*, i8** %3
%9 = call i32 @strlen(i8* %8)
store i32 %9, i32* %6
%10 = load i32, i32* %4
%11 = icmp ne i32 %10, 0
br i1 %11, label %12, label %23
12: ; preds = %2
%13 = load i8*, i8** %3
%14 = call i8* @strrchr(i8* %13, i8 signext 46)
store i8* %14, i8** %7
%15 = load i8*, i8** %7
%16 = icmp ne i8* %15, null
br i1 %16, label %17, label %22
17: ; preds = %12
%18 = load i8*, i8** %7
%19 = call i32 @strlen(i8* %18)
%20 = load i32, i32* %6
%21 = sub nsw i32 %20, %19
store i32 %21, i32* %6
br label %22
22: ; preds = %17, %12
br label %23
23: ; preds = %22, %2
%24 = load i32, i32* %6
%25 = add nsw i32 %24, 1
%26 = sext i32 %25 to i64
%27 = mul i64 %26, 1
%28 = trunc i64 %27 to i32
%29 = call i8* @malloc(i32 %28)
store i8* %29, i8** %5
%30 = load i8*, i8** %5
%31 = load i8*, i8** %3
%32 = load i32, i32* %6
%33 = call i32 @strncpy(i8* %30, i8* %31, i32 %32)
%34 = load i8*, i8** %5
%35 = load i32, i32* %6
%36 = sext i32 %35 to i64
%37 = getelementptr inbounds i8, i8* %34, i64 %36
store i8 0, i8* %37
%38 = load i8*, i8** %5
ret i8* %38
}

LLVM IR (-O2):
define i8* @setjobname(i8* %0, i32 %1) {
%3 = tail call i32 @strlen(i8* %0)
%4 = icmp eq i32 %1, 0
br i1 %4, label %11, label %5
5: ; preds = %2
%6 = tail call i8* @strrchr(i8* dereferenceable(1) %0, i8 signext 46)
%7 = icmp eq i8* %6, null
br i1 %7, label %11, label %8
8: ; preds = %5
%9 = tail call i32 @strlen(i8* %6)
%10 = sub nsw i32 %3, %9
br label %11
11: ; preds = %5, %8, %2
%12 = phi i32 [ %3, %2 ], [ %10, %8 ], [ %3, %5 ]
%13 = add nsw i32 %12, 1
%14 = tail call i8* @malloc(i32 %13)
%15 = tail call i32 @strncpy(i8* %14, i8* %0, i32 %12)
%16 = sext i32 %12 to i64
%17 = getelementptr inbounds i8, i8* %14, i64 %16
store i8 0, i8* %17
ret i8* %14
}