#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 *);
char *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 = 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     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 %28
12: ; preds = %1
%13 = load i32, i32* %4
%14 = call i8* @malloc(i32 %13)
store i8* %14, i8** %3
%15 = icmp ne i8* %14, null
br i1 %15, label %16, label %27
16: ; preds = %12
%17 = load i8*, i8** %3
%18 = load i8*, i8** %2
%19 = load i32, i32* %4
%20 = sub nsw i32 %19, 1
%21 = call i32 @strncpy(i8* %17, i8* %18, i32 %20)
%22 = load i8*, i8** %3
%23 = load i32, i32* %4
%24 = sub nsw i32 %23, 1
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds i8, i8* %22, i64 %25
store i8 0, i8* %26
br label %27
27: ; preds = %16, %12
br label %38
28: ; preds = %1
%29 = load i32, i32* %4
%30 = add nsw i32 %29, 1
%31 = call i8* @malloc(i32 %30)
store i8* %31, i8** %3
%32 = icmp ne i8* %31, null
br i1 %32, label %33, label %37
33: ; preds = %28
%34 = load i8*, i8** %3
%35 = load i8*, i8** %2
%36 = call i32 @strcpy(i8* %34, i8* %35)
br label %37
37: ; preds = %33, %28
br label %38
38: ; preds = %37, %27
%39 = load i8*, i8** %3
%40 = icmp eq i8* %39, null
br i1 %40, label %41, label %43
41: ; preds = %38
%42 = call i32 @gl_error(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str, i64 0, i64 0))
br label %43
43: ; preds = %41, %38
%44 = load i8*, i8** %3
ret i8* %44
}

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