#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 */
int /*<<< orphan*/ gl_error(char *);
scalar_t__ malloc(int);
char *strchr(char *, char);
int /*<<< orphan*/ strcpy(char *, char *);
int strlen(char *);
int /*<<< orphan*/ strncpy(char *, char *, int);
char *hist_save(char *p) {
  char *s = 0;
  int len = strlen(p);
  char *nl = strchr(p, '\n');
  if (nl) {
    if ((s = (char *)malloc(len)) != 0) {
      strncpy(s, p, len - 1);
      s[len - 1] = 0;
    }
  } else {
    if ((s = (char *)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     11dc <hist_save+0x7c>
mov    -0x14(%rbp),%edi
call   1080 <malloc@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
je     11d7 <hist_save+0x77>
mov    -0x10(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x14(%rbp),%edx
sub    $0x1,%edx
call   1040 <strncpy@plt>
mov    -0x10(%rbp),%rax
mov    -0x14(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    1207 <hist_save+0xa7>
mov    -0x14(%rbp),%edi
add    $0x1,%edi
call   1080 <malloc@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
je     1202 <hist_save+0xa2>
mov    -0x10(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   1050 <strcpy@plt>
jmp    1207 <hist_save+0xa7>
cmpq   $0x0,-0x10(%rbp)
jne    121e <hist_save+0xbe>
lea    0xde7(%rip),%rdi
call   1030 <gl_error@plt>
mov    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<hist_save>:
push   %rbp
push   %r14
push   %rbx
mov    %rdi,%r14
call   1060 <strlen@plt>
mov    %eax,%ebp
mov    %r14,%rdi
mov    $0xa,%esi
call   1070 <strchr@plt>
test   %rax,%rax
je     11a8 <hist_save+0x48>
mov    %ebp,%edi
call   1080 <malloc@plt>
test   %rax,%rax
je     11c7 <hist_save+0x67>
mov    %rax,%rbx
movslq %ebp,%rbp
lea    -0x1(%rbp),%rdx
mov    %rax,%rdi
mov    %r14,%rsi
call   1040 <strncpy@plt>
movb   $0x0,-0x1(%rbx,%rbp,1)
jmp    11d5 <hist_save+0x75>
add    $0x1,%ebp
mov    %ebp,%edi
call   1080 <malloc@plt>
test   %rax,%rax
je     11c7 <hist_save+0x67>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r14,%rsi
call   1050 <strcpy@plt>
jmp    11d5 <hist_save+0x75>
lea    0xe32(%rip),%rdi
call   1030 <gl_error@plt>
xor    %ebx,%ebx
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %rbp
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 i32 @strlen(i8* %6)
store i32 %7, i32* %4
%8 = load i8*, i8** %2
%9 = call i8* @strchr(i8* %8, i8 signext 10)
store i8* %9, i8** %5
%10 = load i8*, i8** %5
%11 = icmp ne i8* %10, null
br i1 %11, label %12, label %29
12: ; preds = %1
%13 = load i32, i32* %4
%14 = call i64 @malloc(i32 %13)
%15 = inttoptr i64 %14 to i8*
store i8* %15, i8** %3
%16 = icmp ne i8* %15, null
br i1 %16, label %17, label %28
17: ; preds = %12
%18 = load i8*, i8** %3
%19 = load i8*, i8** %2
%20 = load i32, i32* %4
%21 = sub nsw i32 %20, 1
%22 = call i32 @strncpy(i8* %18, i8* %19, i32 %21)
%23 = load i8*, i8** %3
%24 = load i32, i32* %4
%25 = sub nsw i32 %24, 1
%26 = sext i32 %25 to i64
%27 = getelementptr inbounds i8, i8* %23, i64 %26
store i8 0, i8* %27
br label %28
28: ; preds = %17, %12
br label %40
29: ; preds = %1
%30 = load i32, i32* %4
%31 = add nsw i32 %30, 1
%32 = call i64 @malloc(i32 %31)
%33 = inttoptr i64 %32 to i8*
store i8* %33, i8** %3
%34 = icmp ne i8* %33, null
br i1 %34, label %35, label %39
35: ; preds = %29
%36 = load i8*, i8** %3
%37 = load i8*, i8** %2
%38 = call i32 @strcpy(i8* %36, i8* %37)
br label %39
39: ; preds = %35, %29
br label %40
40: ; preds = %39, %28
%41 = load i8*, i8** %3
%42 = icmp eq i8* %41, null
br i1 %42, label %43, label %45
43: ; preds = %40
%44 = call i32 @gl_error(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str, i64 0, i64 0))
br label %45
45: ; preds = %43, %40
%46 = load i8*, i8** %3
ret i8* %46
}

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 i32 @strlen(i8* %0)
%3 = tail call i8* @strchr(i8* dereferenceable(1) %0, i8 signext 10)
%4 = icmp eq i8* %3, null
br i1 %4, label %14, label %5
5: ; preds = %1
%6 = tail call i64 @malloc(i32 %2)
%7 = inttoptr i64 %6 to i8*
%8 = icmp eq i64 %6, 0
br i1 %8, label %21, label %9
9: ; preds = %5
%10 = add nsw i32 %2, -1
%11 = tail call i32 @strncpy(i8* %7, i8* %0, i32 %10)
%12 = sext i32 %10 to i64
%13 = getelementptr inbounds i8, i8* %7, i64 %12
store i8 0, i8* %13
br label %23
14: ; preds = %1
%15 = add nsw i32 %2, 1
%16 = tail call i64 @malloc(i32 %15)
%17 = inttoptr i64 %16 to i8*
%18 = icmp eq i64 %16, 0
br i1 %18, label %21, label %19
19: ; preds = %14
%20 = tail call i32 @strcpy(i8* %17, i8* %0)
br label %23
21: ; preds = %14, %5
%22 = tail call i32 @gl_error(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str, i64 0, i64 0))
br label %23
23: ; preds = %19, %9, %21
%24 = phi i8* [ null, %21 ], [ %17, %19 ], [ %7, %9 ]
ret i8* %24
}