#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 */
scalar_t__ slurm_xmalloc(size_t, char *, int, char *);
int /*<<< orphan*/ strlcpy(char *, char const *, size_t);
size_t strlen(char const *);
char *xstrndup(const char *str, size_t n) {
  size_t siz;
  char *result;
  if (str == NULL)
    return NULL;
  siz = strlen(str);
  if (n < siz)
    siz = n;
  siz++;
  result = (char *)slurm_xmalloc(
      siz,
      "/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/"
      "2022-01-22/02-19-57/repos/Q-Leap-Networks/debian-slurm/refs/heads/"
      "master/src/common/xstring.c",
      403, "");
  (void)strlcpy(result, str, siz);
  return result;
}


x86 assembly (clang, -O0):
<xstrndup>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1158 <xstrndup+0x28>
movq   $0x0,-0x8(%rbp)
jmp    11c0 <xstrndup+0x90>
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rax
cmp    -0x20(%rbp),%rax
jae    117b <xstrndup+0x4b>
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
lea    0xe6e(%rip),%rsi
mov    $0x193,%edx
lea    0xeff(%rip),%rcx
call   1040 <slurm_xmalloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1050 <strlcpy@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<xstrndup>:
push   %r15
push   %r14
push   %rbx
test   %rdi,%rdi
je     117e <xstrndup+0x4e>
mov    %rsi,%rbx
mov    %rdi,%r14
call   1030 <strlen@plt>
cmp    %rbx,%rax
cmovbe %rax,%rbx
add    $0x1,%rbx
lea    0xea9(%rip),%rsi
lea    0xf3f(%rip),%rcx
mov    %rbx,%rdi
mov    $0x193,%edx
call   1040 <slurm_xmalloc@plt>
mov    %rax,%r15
mov    %rax,%rdi
mov    %r14,%rsi
mov    %rbx,%rdx
call   1050 <strlcpy@plt>
jmp    1181 <xstrndup+0x51>
xor    %r15d,%r15d
mov    %r15,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [158 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/Q-Leap-Networks/debian-slurm/refs/heads/master/src/common/xstring.c\00"
@.str.1 = [1 x i8] zeroinitializer
define i8* @xstrndup(i8* %0, i64 %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i64
%7 = alloca i8*
store i8* %0, i8** %4
store i64 %1, i64* %5
%8 = load i8*, i8** %4
%9 = icmp eq i8* %8, null
br i1 %9, label %10, label %11
10: ; preds = %2
store i8* null, i8** %3
br label %30
11: ; preds = %2
%12 = load i8*, i8** %4
%13 = call i64 @strlen(i8* %12)
store i64 %13, i64* %6
%14 = load i64, i64* %5
%15 = load i64, i64* %6
%16 = icmp ult i64 %14, %15
br i1 %16, label %17, label %19
17: ; preds = %11
%18 = load i64, i64* %5
store i64 %18, i64* %6
br label %19
19: ; preds = %17, %11
%20 = load i64, i64* %6
%21 = add i64 %20, 1
store i64 %21, i64* %6
%22 = load i64, i64* %6
%23 = call i64 @slurm_xmalloc(i64 %22, i8* getelementptr inbounds ([158 x i8], [158 x i8]* @.str, i64 0, i64 0), i32 403, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.1, i64 0, i64 0))
%24 = inttoptr i64 %23 to i8*
store i8* %24, i8** %7
%25 = load i8*, i8** %7
%26 = load i8*, i8** %4
%27 = load i64, i64* %6
%28 = call i32 @strlcpy(i8* %25, i8* %26, i64 %27)
%29 = load i8*, i8** %7
store i8* %29, i8** %3
br label %30
30: ; preds = %19, %10
%31 = load i8*, i8** %3
ret i8* %31
}

LLVM IR (-O2):
@.str = [158 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/Q-Leap-Networks/debian-slurm/refs/heads/master/src/common/xstring.c\00"
@.str.1 = [1 x i8] zeroinitializer
define i8* @xstrndup(i8* %0, i64 %1) {
%3 = icmp eq i8* %0, null
br i1 %3, label %12, label %4
4: ; preds = %2
%5 = tail call i64 @strlen(i8* %0)
%6 = icmp ugt i64 %5, %1
%7 = select i1 %6, i64 %1, i64 %5
%8 = add i64 %7, 1
%9 = tail call i64 @slurm_xmalloc(i64 %8, i8* getelementptr inbounds ([158 x i8], [158 x i8]* @.str, i64 0, i64 0), i32 403, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.1, i64 0, i64 0))
%10 = inttoptr i64 %9 to i8*
%11 = tail call i32 @strlcpy(i8* %10, i8* %0, i64 %8)
br label %12
12: ; preds = %2, %4
%13 = phi i8* [ %10, %4 ], [ null, %2 ]
ret i8* %13
}