#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(size_t);
char *memchr(char const *, char, size_t);
int /*<<< orphan*/ memcpy(char *, char const *, size_t);
char *strndup(register const char *s1, size_t n) {
  register char *s, *end;
  end = memchr(s1, '\n', n);
  n = end ? (size_t)(end - s1) : n;
  if ((s = malloc(n + 1)) != NULL) {
    memcpy(s, s1, n);
    s[n] = 0;
  }
  return s;
}


x86 assembly (clang, -O0):
<strndup>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    $0xa,%esi
call   1030 <memchr@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     1175 <strndup+0x45>
mov    -0x20(%rbp),%rax
mov    -0x8(%rbp),%rcx
sub    %rcx,%rax
mov    %rax,-0x28(%rbp)
jmp    117d <strndup+0x4d>
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
add    $0x1,%rdi
call   1050 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
je     11bd <strndup+0x8d>
mov    -0x18(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x10(%rbp),%rdx
call   1040 <memcpy@plt>
mov    -0x18(%rbp),%rax
mov    -0x10(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strndup>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%r15
mov    %rdi,%r14
mov    $0xa,%esi
mov    %r15,%rdx
call   1030 <memchr@plt>
mov    %rax,%rbx
sub    %r14,%rbx
test   %rax,%rax
cmove  %r15,%rbx
lea    0x1(%rbx),%rdi
call   1050 <malloc@plt>
mov    %rax,%r15
test   %rax,%rax
je     1179 <strndup+0x49>
mov    %r15,%rdi
mov    %r14,%rsi
mov    %rbx,%rdx
call   1040 <memcpy@plt>
movb   $0x0,(%r15,%rbx,1)
mov    %r15,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i8* @strndup(i8* %0, i64 %1) {
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i8*
%6 = alloca i8*
store i8* %0, i8** %3
store i64 %1, i64* %4
%7 = load i8*, i8** %3
%8 = load i64, i64* %4
%9 = call i8* @memchr(i8* %7, i8 signext 10, i64 %8)
store i8* %9, i8** %6
%10 = load i8*, i8** %6
%11 = icmp ne i8* %10, null
br i1 %11, label %12, label %18
12: ; preds = %2
%13 = load i8*, i8** %6
%14 = load i8*, i8** %3
%15 = ptrtoint i8* %13 to i64
%16 = ptrtoint i8* %14 to i64
%17 = sub i64 %15, %16
br label %20
18: ; preds = %2
%19 = load i64, i64* %4
br label %20
20: ; preds = %18, %12
%21 = phi i64 [ %17, %12 ], [ %19, %18 ]
store i64 %21, i64* %4
%22 = load i64, i64* %4
%23 = add i64 %22, 1
%24 = call i8* @malloc(i64 %23)
store i8* %24, i8** %5
%25 = icmp ne i8* %24, null
br i1 %25, label %26, label %34
26: ; preds = %20
%27 = load i8*, i8** %5
%28 = load i8*, i8** %3
%29 = load i64, i64* %4
%30 = call i32 @memcpy(i8* %27, i8* %28, i64 %29)
%31 = load i8*, i8** %5
%32 = load i64, i64* %4
%33 = getelementptr inbounds i8, i8* %31, i64 %32
store i8 0, i8* %33
br label %34
34: ; preds = %26, %20
%35 = load i8*, i8** %5
ret i8* %35
}

LLVM IR (-O2):
define i8* @strndup(i8* %0, i64 %1) {
%3 = tail call i8* @memchr(i8* %0, i8 signext 10, i64 %1)
%4 = icmp eq i8* %3, null
%5 = ptrtoint i8* %3 to i64
%6 = ptrtoint i8* %0 to i64
%7 = sub i64 %5, %6
%8 = select i1 %4, i64 %1, i64 %7
%9 = add i64 %8, 1
%10 = tail call i8* @malloc(i64 %9)
%11 = icmp eq i8* %10, null
br i1 %11, label %15, label %12
12: ; preds = %2
%13 = tail call i32 @memcpy(i8* %10, i8* %0, i64 %8)
%14 = getelementptr inbounds i8, i8* %10, i64 %8
store i8 0, i8* %14
br label %15
15: ; preds = %12, %2
ret i8* %10
}