char *hist_save(p)
char *p;
{
  char *s = 0;
  int len = strlen(p);
  char *nl = strchr(p, '\n');
  if (nl) {
    if ((s = malloc(len)) != 0) {
      strncpy(s, p, len - 1);
      s[len - 1] = 0;
    }
  } else {
    if ((s = malloc(len + 1)) != 0) {
      strcpy(s, p);
    }
  }
  if (s == 0)
    gl_error("\n*** Error: hist_save() failed on malloc\n");
  return s;
}


x86 assembly (clang, -O0):
<hist_save>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
movq   $0x0,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
call   1060 <strlen@plt>
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%rdi
mov    $0xa,%esi
call   1070 <strchr@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     11e0 <hist_save+0x80>
movslq -0x14(%rbp),%rdi
call   1080 <malloc@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
je     11db <hist_save+0x7b>
mov    -0x10(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x14(%rbp),%eax
sub    $0x1,%eax
movslq %eax,%rdx
call   1040 <strncpy@plt>
mov    -0x10(%rbp),%rax
mov    -0x14(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    120e <hist_save+0xae>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
movslq %eax,%rdi
call   1080 <malloc@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
je     1209 <hist_save+0xa9>
mov    -0x10(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   1050 <strcpy@plt>
jmp    120e <hist_save+0xae>
cmpq   $0x0,-0x10(%rbp)
jne    1227 <hist_save+0xc7>
lea    0xde0(%rip),%rdi
mov    $0x0,%al
call   1030 <gl_error@plt>
mov    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<hist_save>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%r14
call   1060 <strlen@plt>
mov    %rax,%rbx
mov    %r14,%rdi
mov    $0xa,%esi
call   1070 <strchr@plt>
shl    $0x20,%rbx
test   %rax,%rax
je     11c0 <hist_save+0x60>
mov    %rbx,%rdi
sar    $0x20,%rdi
call   1080 <malloc@plt>
test   %rax,%rax
je     11ee <hist_save+0x8e>
mov    %rax,%r15
movabs $0xffffffff00000000,%rax
ff ff ff
add    %rax,%rbx
sar    $0x20,%rbx
mov    %r15,%rdi
mov    %r14,%rsi
mov    %rbx,%rdx
call   1040 <strncpy@plt>
movb   $0x0,(%r15,%rbx,1)
jmp    11ff <hist_save+0x9f>
movabs $0x100000000,%rax
add    %rax,%rbx
sar    $0x20,%rbx
mov    %rbx,%rdi
call   1080 <malloc@plt>
test   %rax,%rax
je     11ee <hist_save+0x8e>
mov    %rax,%r15
mov    %rax,%rdi
mov    %r14,%rsi
call   1050 <strcpy@plt>
jmp    11ff <hist_save+0x9f>
lea    0xe0b(%rip),%rdi
xor    %r15d,%r15d
xor    %eax,%eax
call   1030 <gl_error@plt>
mov    %r15,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [42 x i8] c"\0A*** Error: hist_save() failed on malloc\0A\00"
define i8* @hist_save(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i8*
store i8* %0, i8** %2
store i8* null, i8** %3
%6 = load i8*, i8** %2
%7 = call i64 @strlen(i8* %6)
%8 = trunc i64 %7 to i32
store i32 %8, i32* %4
%9 = load i8*, i8** %2
%10 = call i8* @strchr(i8* %9, i32 10)
store i8* %10, i8** %5
%11 = load i8*, i8** %5
%12 = icmp ne i8* %11, null
br i1 %12, label %13, label %31
13: ; preds = %1
%14 = load i32, i32* %4
%15 = sext i32 %14 to i64
%16 = call i8* @malloc(i64 %15)
store i8* %16, i8** %3
%17 = icmp ne i8* %16, null
br i1 %17, label %18, label %30
18: ; preds = %13
%19 = load i8*, i8** %3
%20 = load i8*, i8** %2
%21 = load i32, i32* %4
%22 = sub nsw i32 %21, 1
%23 = sext i32 %22 to i64
%24 = call i8* @strncpy(i8* %19, i8* %20, i64 %23)
%25 = load i8*, i8** %3
%26 = load i32, i32* %4
%27 = sub nsw i32 %26, 1
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds i8, i8* %25, i64 %28
store i8 0, i8* %29
br label %30
30: ; preds = %18, %13
br label %42
31: ; preds = %1
%32 = load i32, i32* %4
%33 = add nsw i32 %32, 1
%34 = sext i32 %33 to i64
%35 = call i8* @malloc(i64 %34)
store i8* %35, i8** %3
%36 = icmp ne i8* %35, null
br i1 %36, label %37, label %41
37: ; preds = %31
%38 = load i8*, i8** %3
%39 = load i8*, i8** %2
%40 = call i8* @strcpy(i8* %38, i8* %39)
br label %41
41: ; preds = %37, %31
br label %42
42: ; preds = %41, %30
%43 = load i8*, i8** %3
%44 = icmp eq i8* %43, null
br i1 %44, label %45, label %47
45: ; preds = %42
%46 = call i32 (i8*, ...) bitcast (i32 (...)* @gl_error to i32 (i8*, ...)*)(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str, i64 0, i64 0))
br label %47
47: ; preds = %45, %42
%48 = load i8*, i8** %3
ret i8* %48
}

LLVM IR (-O2):
@.str = [42 x i8] c"\0A*** Error: hist_save() failed on malloc\0A\00"
define i8* @hist_save(i8* %0) {
%2 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%3 = tail call i8* @strchr(i8* dereferenceable(1) %0, i32 10)
%4 = icmp eq i8* %3, null
%5 = shl i64 %2, 32
br i1 %4, label %15, label %6
6: ; preds = %1
%7 = ashr exact i64 %5, 32
%8 = tail call i8* @malloc(i64 %7)
%9 = icmp eq i8* %8, null
br i1 %9, label %22, label %10
10: ; preds = %6
%11 = add i64 %5, -4294967296
%12 = ashr exact i64 %11, 32
%13 = tail call i8* @strncpy(i8* %8, i8* %0, i64 %12)
%14 = getelementptr inbounds i8, i8* %8, i64 %12
store i8 0, i8* %14
br label %24
15: ; preds = %1
%16 = add i64 %5, 4294967296
%17 = ashr exact i64 %16, 32
%18 = tail call i8* @malloc(i64 %17)
%19 = icmp eq i8* %18, null
br i1 %19, label %22, label %20
20: ; preds = %15
%21 = tail call i8* @strcpy(i8* %18, i8* dereferenceable(1) %0)
br label %24
22: ; preds = %6, %15
%23 = tail call i32 (i8*, ...) bitcast (i32 (...)* @gl_error to i32 (i8*, ...)*)(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str, i64 0, i64 0))
br label %24
24: ; preds = %10, %20, %22
%25 = phi i8* [ null, %22 ], [ %8, %10 ], [ %18, %20 ]
ret i8* %25
}