#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*/ fprintf(int /*<<< orphan*/, char *);
char *malloc(int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strcpy(char *, char *);
int strlen(char *);
char *ostrdup(char *str) {
  char *tmp;
  int len;
  len = strlen(str);
  if (len < 0)
    return NULL;
  tmp = malloc(sizeof(char) * (len + 1));
  if (tmp == NULL) {
    fprintf(stderr, "ostrdup: Can't allocate mem.\n");
    perror("ostrdup");
    return NULL;
  }
  strcpy(tmp, str);
  return (tmp);
}


x86 assembly (clang, -O0):
<ostrdup>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    117f <ostrdup+0x2f>
movq   $0x0,-0x8(%rbp)
jmp    11e4 <ostrdup+0x94>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
cltq
shl    $0x0,%rax
mov    %eax,%edi
call   1060 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11cf <ostrdup+0x7f>
mov    0x2e38(%rip),%rax
mov    (%rax),%edi
lea    0xe4f(%rip),%rsi
call   1050 <fprintf@plt>
lea    0xe61(%rip),%rdi
call   1070 <perror@plt>
movq   $0x0,-0x8(%rbp)
jmp    11e4 <ostrdup+0x94>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ostrdup>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
call   1040 <strlen@plt>
test   %eax,%eax
js     11a0 <ostrdup+0x50>
add    $0x1,%eax
mov    %eax,%edi
call   1060 <malloc@plt>
test   %rax,%rax
je     117f <ostrdup+0x2f>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r14,%rsi
call   1030 <strcpy@plt>
jmp    11a2 <ostrdup+0x52>
mov    0x2e5a(%rip),%rax
mov    (%rax),%edi
lea    0xe71(%rip),%rsi
call   1050 <fprintf@plt>
lea    0xe83(%rip),%rdi
call   1070 <perror@plt>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [30 x i8] c"ostrdup: Can't allocate mem.\0A\00"
@.str.1 = [8 x i8] c"ostrdup\00"
define i8* @ostrdup(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
store i8* %0, i8** %3
%6 = load i8*, i8** %3
%7 = call i32 @strlen(i8* %6)
store i32 %7, i32* %5
%8 = load i32, i32* %5
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %11
10: ; preds = %1
store i8* null, i8** %2
br label %29
11: ; preds = %1
%12 = load i32, i32* %5
%13 = add nsw i32 %12, 1
%14 = sext i32 %13 to i64
%15 = mul i64 1, %14
%16 = trunc i64 %15 to i32
%17 = call i8* @malloc(i32 %16)
store i8* %17, i8** %4
%18 = load i8*, i8** %4
%19 = icmp eq i8* %18, null
br i1 %19, label %20, label %24
20: ; preds = %11
%21 = load i32, i32* @stderr
%22 = call i32 @fprintf(i32 %21, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0))
%23 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
store i8* null, i8** %2
br label %29
24: ; preds = %11
%25 = load i8*, i8** %4
%26 = load i8*, i8** %3
%27 = call i32 @strcpy(i8* %25, i8* %26)
%28 = load i8*, i8** %4
store i8* %28, i8** %2
br label %29
29: ; preds = %24, %20, %10
%30 = load i8*, i8** %2
ret i8* %30
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [30 x i8] c"ostrdup: Can't allocate mem.\0A\00"
@.str.1 = [8 x i8] c"ostrdup\00"
define i8* @ostrdup(i8* %0) {
%2 = tail call i32 @strlen(i8* %0)
%3 = icmp slt i32 %2, 0
br i1 %3, label %14, label %4
4: ; preds = %1
%5 = add nuw nsw i32 %2, 1
%6 = tail call i8* @malloc(i32 %5)
%7 = icmp eq i8* %6, null
br i1 %7, label %8, label %12
8: ; preds = %4
%9 = load i32, i32* @stderr
%10 = tail call i32 @fprintf(i32 %9, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0))
%11 = tail call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
br label %14
12: ; preds = %4
%13 = tail call i32 @strcpy(i8* %6, i8* %0)
br label %14
14: ; preds = %1, %12, %8
%15 = phi i8* [ null, %8 ], [ %6, %12 ], [ null, %1 ]
ret i8* %15
}