#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*/ free(char *);
scalar_t__ malloc(int);
int /*<<< orphan*/ my_puts(char *);
int my_strlen(char *);
char *my_strncpy(char *dest, char *src, int n) {
  char *tmp;
  if ((tmp = (char *)malloc(n * sizeof(char))) == NULL) {
    my_puts("ERROR: malloc failed\n");
    return NULL;
  }
  for (int i = 0; i < n; i++) {
    if (i < my_strlen(src)) {
      tmp[i] = src[i];
    } else {
      tmp[i] = '\0';
    }
  }
  dest = tmp;
  free(tmp);
  return dest;
}


x86 assembly (clang, -O0):
<my_strncpy>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
movslq -0x1c(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%edi
call   1060 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    1189 <my_strncpy+0x49>
lea    0xe89(%rip),%rdi
call   1050 <my_puts@plt>
movq   $0x0,-0x8(%rbp)
jmp    120b <my_strncpy+0xcb>
movl   $0x0,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    11f2 <my_strncpy+0xb2>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <my_strlen@plt>
mov    %eax,%ecx
mov    -0x30(%rbp),%eax
cmp    %ecx,%eax
jge    11d3 <my_strncpy+0x93>
mov    -0x18(%rbp),%rax
movslq -0x2c(%rbp),%rcx
mov    (%rax,%rcx,1),%dl
mov    -0x28(%rbp),%rax
movslq -0x2c(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
jmp    11df <my_strncpy+0x9f>
mov    -0x28(%rbp),%rax
movslq -0x2c(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    11e4 <my_strncpy+0xa4>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    1190 <my_strncpy+0x50>
mov    -0x28(%rbp),%rax
mov    %rax,-0x10(%rbp)
mov    -0x28(%rbp),%rdi
call   1040 <free@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<my_strncpy>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%ebp
mov    %rsi,%rbx
mov    %edx,%edi
call   1060 <malloc@plt>
test   %rax,%rax
je     119e <my_strncpy+0x5e>
mov    %rax,%r14
test   %ebp,%ebp
jle    1194 <my_strncpy+0x54>
mov    %ebp,%r15d
xor    %ebp,%ebp
jmp    1181 <my_strncpy+0x41>
cs nopw 0x0(%rax,%rax,1)
movzbl (%rbx,%rbp,1),%eax
mov    %al,(%r14,%rbp,1)
add    $0x1,%rbp
cmp    %rbp,%r15
je     1194 <my_strncpy+0x54>
mov    %rbx,%rdi
call   1030 <my_strlen@plt>
cltq
cmp    %rax,%rbp
jl     1170 <my_strncpy+0x30>
xor    %eax,%eax
jmp    1174 <my_strncpy+0x34>
mov    %r14,%rdi
call   1040 <free@plt>
jmp    11ad <my_strncpy+0x6d>
lea    0xe5b(%rip),%rdi
call   1050 <my_puts@plt>
xor    %r14d,%r14d
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [22 x i8] c"ERROR: malloc failed\0A\00"
define i8* @my_strncpy(i8* %0, i8* %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i32
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%10 = load i32, i32* %7
%11 = sext i32 %10 to i64
%12 = mul i64 %11, 1
%13 = trunc i64 %12 to i32
%14 = call i64 @malloc(i32 %13)
%15 = inttoptr i64 %14 to i8*
store i8* %15, i8** %8
%16 = icmp eq i8* %15, null
br i1 %16, label %17, label %19
17: ; preds = %3
%18 = call i32 @my_puts(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
store i8* null, i8** %4
br label %53
19: ; preds = %3
store i32 0, i32* %9
br label %20
20: ; preds = %45, %19
%21 = load i32, i32* %9
%22 = load i32, i32* %7
%23 = icmp slt i32 %21, %22
br i1 %23, label %24, label %48
24: ; preds = %20
%25 = load i32, i32* %9
%26 = load i8*, i8** %6
%27 = call i32 @my_strlen(i8* %26)
%28 = icmp slt i32 %25, %27
br i1 %28, label %29, label %39
29: ; preds = %24
%30 = load i8*, i8** %6
%31 = load i32, i32* %9
%32 = sext i32 %31 to i64
%33 = getelementptr inbounds i8, i8* %30, i64 %32
%34 = load i8, i8* %33
%35 = load i8*, i8** %8
%36 = load i32, i32* %9
%37 = sext i32 %36 to i64
%38 = getelementptr inbounds i8, i8* %35, i64 %37
store i8 %34, i8* %38
br label %44
39: ; preds = %24
%40 = load i8*, i8** %8
%41 = load i32, i32* %9
%42 = sext i32 %41 to i64
%43 = getelementptr inbounds i8, i8* %40, i64 %42
store i8 0, i8* %43
br label %44
44: ; preds = %39, %29
br label %45
45: ; preds = %44
%46 = load i32, i32* %9
%47 = add nsw i32 %46, 1
store i32 %47, i32* %9
br label %20
48: ; preds = %20
%49 = load i8*, i8** %8
store i8* %49, i8** %5
%50 = load i8*, i8** %8
%51 = call i32 @free(i8* %50)
%52 = load i8*, i8** %5
store i8* %52, i8** %4
br label %53
53: ; preds = %48, %17
%54 = load i8*, i8** %4
ret i8* %54
}

LLVM IR (-O2):
@.str = [22 x i8] c"ERROR: malloc failed\0A\00"
define i8* @my_strncpy(i8* nocapture readnone %0, i8* %1, i32 %2) {
%4 = tail call i64 @malloc(i32 %2)
%5 = inttoptr i64 %4 to i8*
%6 = icmp eq i64 %4, 0
br i1 %6, label %11, label %7
7: ; preds = %3
%8 = icmp sgt i32 %2, 0
br i1 %8, label %9, label %13
9: ; preds = %7
%10 = zext i32 %2 to i64
br label %15
11: ; preds = %3
%12 = tail call i32 @my_puts(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
br label %28
13: ; preds = %23, %7
%14 = tail call i32 @free(i8* %5)
br label %28
15: ; preds = %9, %23
%16 = phi i64 [ 0, %9 ], [ %26, %23 ]
%17 = tail call i32 @my_strlen(i8* %1)
%18 = sext i32 %17 to i64
%19 = icmp slt i64 %16, %18
br i1 %19, label %20, label %23
20: ; preds = %15
%21 = getelementptr inbounds i8, i8* %1, i64 %16
%22 = load i8, i8* %21
br label %23
23: ; preds = %15, %20
%24 = phi i8 [ %22, %20 ], [ 0, %15 ]
%25 = getelementptr inbounds i8, i8* %5, i64 %16
store i8 %24, i8* %25
%26 = add nuw nsw i64 %16, 1
%27 = icmp eq i64 %26, %10
br i1 %27, label %13, label %15
28: ; preds = %13, %11
%29 = phi i8* [ null, %11 ], [ %5, %13 ]
ret i8* %29
}