#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*/ Error_write(int, char *, int, char *);
int /*<<< orphan*/ *strncpy(char *, char *, int);
char *StrUtil_strncpy(char *s1, char *s2, int len) {
  if (len == 0) {
    s1[0] = '\0';
    return s1;
  }
  if (s1 == NULL || s2 == NULL) {
    Error_write(11, "StrUtil_strncpy", 2, "s1 or s2");
    return NULL;
  }
  if (strncpy(s1, s2, len) == NULL) {
    Error_write(24, "StrUtil_strncpy", 2, "");
    return NULL;
  }
  s1[len] = '\0';
  return s1;
}


x86 assembly (clang, -O0):
<StrUtil_strncpy>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jne    1151 <StrUtil_strncpy+0x31>
mov    -0x10(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11e9 <StrUtil_strncpy+0xc9>
cmpq   $0x0,-0x10(%rbp)
je     1167 <StrUtil_strncpy+0x47>
cmpq   $0x0,-0x18(%rbp)
jne    1191 <StrUtil_strncpy+0x71>
mov    $0xb,%edi
lea    0xe8d(%rip),%rsi
mov    $0x2,%edx
lea    0xe91(%rip),%rcx
call   1040 <Error_write@plt>
movq   $0x0,-0x8(%rbp)
jmp    11e9 <StrUtil_strncpy+0xc9>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1030 <strncpy@plt>
cmp    $0x0,%rax
jne    11d5 <StrUtil_strncpy+0xb5>
mov    $0x18,%edi
lea    0xe49(%rip),%rsi
mov    $0x2,%edx
lea    0xe55(%rip),%rcx
call   1040 <Error_write@plt>
movq   $0x0,-0x8(%rbp)
jmp    11e9 <StrUtil_strncpy+0xc9>
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<StrUtil_strncpy>:
push   %rbp
push   %rbx
push   %rax
mov    %rdi,%rbx
test   %edx,%edx
je     114c <StrUtil_strncpy+0x2c>
test   %rbx,%rbx
je     1151 <StrUtil_strncpy+0x31>
test   %rsi,%rsi
je     1151 <StrUtil_strncpy+0x31>
mov    %edx,%ebp
mov    %rbx,%rdi
call   1030 <strncpy@plt>
test   %rax,%rax
je     1166 <StrUtil_strncpy+0x46>
movslq %ebp,%rax
movb   $0x0,(%rbx,%rax,1)
jmp    1185 <StrUtil_strncpy+0x65>
movb   $0x0,(%rbx)
jmp    1185 <StrUtil_strncpy+0x65>
lea    0xea8(%rip),%rsi
lea    0xeb1(%rip),%rcx
mov    $0xb,%edi
jmp    1179 <StrUtil_strncpy+0x59>
lea    0xe93(%rip),%rsi
lea    0xea4(%rip),%rcx
mov    $0x18,%edi
mov    $0x2,%edx
call   1040 <Error_write@plt>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [16 x i8] c"StrUtil_strncpy\00"
@.str.1 = [9 x i8] c"s1 or s2\00"
@.str.2 = [1 x i8] zeroinitializer
define i8* @StrUtil_strncpy(i8* %0, i8* %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%8 = load i32, i32* %7
%9 = icmp eq i32 %8, 0
br i1 %9, label %10, label %14
10: ; preds = %3
%11 = load i8*, i8** %5
%12 = getelementptr inbounds i8, i8* %11, i64 0
store i8 0, i8* %12
%13 = load i8*, i8** %5
store i8* %13, i8** %4
br label %36
14: ; preds = %3
%15 = load i8*, i8** %5
%16 = icmp eq i8* %15, null
br i1 %16, label %20, label %17
17: ; preds = %14
%18 = load i8*, i8** %6
%19 = icmp eq i8* %18, null
br i1 %19, label %20, label %22
20: ; preds = %17, %14
%21 = call i32 @Error_write(i32 11, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 2, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
store i8* null, i8** %4
br label %36
22: ; preds = %17
%23 = load i8*, i8** %5
%24 = load i8*, i8** %6
%25 = load i32, i32* %7
%26 = call i32* @strncpy(i8* %23, i8* %24, i32 %25)
%27 = icmp eq i32* %26, null
br i1 %27, label %28, label %30
28: ; preds = %22
%29 = call i32 @Error_write(i32 24, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 2, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.2, i64 0, i64 0))
store i8* null, i8** %4
br label %36
30: ; preds = %22
%31 = load i8*, i8** %5
%32 = load i32, i32* %7
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds i8, i8* %31, i64 %33
store i8 0, i8* %34
%35 = load i8*, i8** %5
store i8* %35, i8** %4
br label %36
36: ; preds = %30, %28, %20, %10
%37 = load i8*, i8** %4
ret i8* %37
}

LLVM IR (-O2):
@.str = [16 x i8] c"StrUtil_strncpy\00"
@.str.1 = [9 x i8] c"s1 or s2\00"
@.str.2 = [1 x i8] zeroinitializer
define i8* @StrUtil_strncpy(i8* %0, i8* %1, i32 %2) {
%4 = icmp eq i32 %2, 0
br i1 %4, label %5, label %6
5: ; preds = %3
store i8 0, i8* %0
br label %20
6: ; preds = %3
%7 = icmp eq i8* %0, null
%8 = icmp eq i8* %1, null
%9 = or i1 %7, %8
br i1 %9, label %10, label %12
10: ; preds = %6
%11 = tail call i32 @Error_write(i32 11, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 2, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
br label %20
12: ; preds = %6
%13 = tail call i32* @strncpy(i8* %0, i8* %1, i32 %2)
%14 = icmp eq i32* %13, null
br i1 %14, label %15, label %17
15: ; preds = %12
%16 = tail call i32 @Error_write(i32 24, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 2, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.2, i64 0, i64 0))
br label %20
17: ; preds = %12
%18 = sext i32 %2 to i64
%19 = getelementptr inbounds i8, i8* %0, i64 %18
store i8 0, i8* %19
br label %20
20: ; preds = %17, %15, %10, %5
%21 = phi i8* [ %0, %5 ], [ null, %10 ], [ null, %15 ], [ %0, %17 ]
ret i8* %21
}