#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*/ EXIT_FAILURE;
int errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, int, char *,
                           int);
char *malloc(scalar_t__);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strcpy(char *, char const *);
char *strerror(int);
scalar_t__ strlen(char const *);
char *duplicate_string(const char *str) {
  char *new_string = malloc(strlen(str) + 1);
  if (!new_string) {
    fprintf(stderr,
            "Error: couldn't duplicate string: "
            "%s, line %d\n    %s (%d)\n",
            "/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/"
            "2022-01-22/02-19-57/repos/paulgriffiths/pgutils/refs/heads/master/"
            "datastruct/stringlist.c",
            234, strerror(errno), errno);
    exit(EXIT_FAILURE);
  }
  strcpy(new_string, str);
  return new_string;
}


x86 assembly (clang, -O0):
<duplicate_string>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,%rdi
add    $0x1,%rdi
call   1060 <malloc@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11e0 <duplicate_string+0x80>
mov    0x2e49(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    0x2e35(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x14(%rbp),%edi
mov    %rax,%r8
mov    0x2e21(%rip),%rax
mov    (%rax),%r9d
lea    0xe3f(%rip),%rsi
lea    0xe73(%rip),%rdx
mov    $0xea,%ecx
call   1050 <fprintf@plt>
mov    0x2df7(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
mov    -0x10(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<duplicate_string>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
call   1040 <strlen@plt>
lea    0x1(%rax),%rdi
call   1060 <malloc@plt>
test   %rax,%rax
je     1193 <duplicate_string+0x33>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r14,%rsi
call   1030 <strcpy@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
mov    0x2e46(%rip),%rax
mov    (%rax),%ebx
mov    0x2e35(%rip),%r14
mov    (%r14),%edi
call   1080 <strerror@plt>
mov    (%r14),%r9d
lea    0xe4b(%rip),%rsi
lea    0xe7f(%rip),%rdx
mov    %ebx,%edi
mov    $0xea,%ecx
mov    %rax,%r8
call   1050 <fprintf@plt>
mov    0x2dfe(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [59 x i8] c"Error: couldn't duplicate string: %s, line %d\0A %s (%d)\0A\00"
@.str.1 = [154 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/paulgriffiths/pgutils/refs/heads/master/datastruct/stringlist.c\00"
@errno = global i32 0
@EXIT_FAILURE = global i32 0
define i8* @duplicate_string(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
store i8* %0, i8** %2
%4 = load i8*, i8** %2
%5 = call i64 @strlen(i8* %4)
%6 = add nsw i64 %5, 1
%7 = call i8* @malloc(i64 %6)
store i8* %7, i8** %3
%8 = load i8*, i8** %3
%9 = icmp ne i8* %8, null
br i1 %9, label %18, label %10
10: ; preds = %1
%11 = load i32, i32* @stderr
%12 = load i32, i32* @errno
%13 = call i8* @strerror(i32 %12)
%14 = load i32, i32* @errno
%15 = call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([59 x i8], [59 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([154 x i8], [154 x i8]* @.str.1, i64 0, i64 0), i32 234, i8* %13, i32 %14)
%16 = load i32, i32* @EXIT_FAILURE
%17 = call i32 @exit(i32 %16)
unreachable
18: ; preds = %1
%19 = load i8*, i8** %3
%20 = load i8*, i8** %2
%21 = call i32 @strcpy(i8* %19, i8* %20)
%22 = load i8*, i8** %3
ret i8* %22
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [59 x i8] c"Error: couldn't duplicate string: %s, line %d\0A %s (%d)\0A\00"
@.str.1 = [154 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/paulgriffiths/pgutils/refs/heads/master/datastruct/stringlist.c\00"
@errno = global i32 0
@EXIT_FAILURE = global i32 0
define i8* @duplicate_string(i8* %0) {
%2 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%3 = add nsw i64 %2, 1
%4 = tail call i8* @malloc(i64 %3)
%5 = icmp eq i8* %4, null
br i1 %5, label %6, label %14
6: ; preds = %1
%7 = load i32, i32* @stderr
%8 = load i32, i32* @errno
%9 = tail call i8* @strerror(i32 %8)
%10 = load i32, i32* @errno
%11 = tail call i32 @fprintf(i32 %7, i8* getelementptr inbounds ([59 x i8], [59 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([154 x i8], [154 x i8]* @.str.1, i64 0, i64 0), i32 234, i8* %9, i32 %10)
%12 = load i32, i32* @EXIT_FAILURE
%13 = tail call i32 @exit(i32 %12)
unreachable
14: ; preds = %1
%15 = tail call i32 @strcpy(i8* %4, i8* %0)
ret i8* %4
}