#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*/ die(char *);
scalar_t__ strcmp(char *, char *);
int /*<<< orphan*/ strncat(char *, char *, int);
int main() {
  char buf[100] = "fnord";
  strncat(buf, "foo", 0);
  if (strcmp(buf, "fnord"))
    die("strncat did not work for length 0");
  strncat(buf, "foo", 2);
  if (strcmp(buf, "fnordfo"))
    die("strncat did not copy n bytes");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0x70(%rbp),%rdi
xor    %esi,%esi
mov    $0x64,%edx
call   1030 <memset@plt>
movb   $0x66,-0x70(%rbp)
movb   $0x6e,-0x6f(%rbp)
movb   $0x6f,-0x6e(%rbp)
movb   $0x72,-0x6d(%rbp)
movb   $0x64,-0x6c(%rbp)
lea    -0x70(%rbp),%rdi
lea    0xe82(%rip),%rsi
xor    %edx,%edx
call   1040 <strncat@plt>
lea    -0x70(%rbp),%rdi
lea    0xe74(%rip),%rsi
call   1050 <strcmp@plt>
cmp    $0x0,%rax
je     11ab <main+0x6b>
lea    0xe64(%rip),%rdi
call   1060 <die@plt>
lea    -0x70(%rbp),%rdi
lea    0xe4a(%rip),%rsi
mov    $0x2,%edx
call   1040 <strncat@plt>
lea    -0x70(%rbp),%rdi
lea    0xe61(%rip),%rsi
call   1050 <strcmp@plt>
cmp    $0x0,%rax
je     11e6 <main+0xa6>
lea    0xe53(%rip),%rdi
call   1060 <die@plt>
xor    %eax,%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x70,%rsp
xorps  %xmm0,%xmm0
movaps %xmm0,(%rsp)
movaps %xmm0,0x50(%rsp)
movaps %xmm0,0x40(%rsp)
movaps %xmm0,0x30(%rsp)
movaps %xmm0,0x20(%rsp)
movaps %xmm0,0x10(%rsp)
movl   $0x0,0x60(%rsp)
movl   $0x726f6e66,(%rsp)
movb   $0x64,0x4(%rsp)
lea    0xe90(%rip),%rsi
mov    %rsp,%rbx
mov    %rbx,%rdi
xor    %edx,%edx
call   1030 <strncat@plt>
lea    0xe80(%rip),%rsi
mov    %rbx,%rdi
call   1040 <strcmp@plt>
test   %rax,%rax
je     119d <main+0x6d>
lea    0xe72(%rip),%rdi
call   1050 <die@plt>
lea    0xe5c(%rip),%rsi
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x2,%edx
call   1030 <strncat@plt>
lea    0xe71(%rip),%rsi
mov    %rbx,%rdi
call   1040 <strcmp@plt>
test   %rax,%rax
je     11d4 <main+0xa4>
lea    0xe65(%rip),%rdi
call   1050 <die@plt>
xor    %eax,%eax
add    $0x70,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@.str = [4 x i8] c"foo\00"
@.str.1 = [6 x i8] c"fnord\00"
@.str.2 = [34 x i8] c"strncat did not work for length 0\00"
@.str.3 = [8 x i8] c"fnordfo\00"
@.str.4 = [29 x i8] c"strncat did not copy n bytes\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [100 x i8]
store i32 0, i32* %1
%3 = bitcast [100 x i8]* %2 to i8*
%4 = bitcast i8* %3 to [100 x i8]*
%5 = getelementptr inbounds [100 x i8], [100 x i8]* %4, i32 0, i32 0
store i8 102, i8* %5
%6 = getelementptr inbounds [100 x i8], [100 x i8]* %4, i32 0, i32 1
store i8 110, i8* %6
%7 = getelementptr inbounds [100 x i8], [100 x i8]* %4, i32 0, i32 2
store i8 111, i8* %7
%8 = getelementptr inbounds [100 x i8], [100 x i8]* %4, i32 0, i32 3
store i8 114, i8* %8
%9 = getelementptr inbounds [100 x i8], [100 x i8]* %4, i32 0, i32 4
store i8 100, i8* %9
%10 = getelementptr inbounds [100 x i8], [100 x i8]* %2, i64 0, i64 0
%11 = call i32 @strncat(i8* %10, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 0)
%12 = getelementptr inbounds [100 x i8], [100 x i8]* %2, i64 0, i64 0
%13 = call i64 @strcmp(i8* %12, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%14 = icmp ne i64 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %0
%16 = call i32 @die(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0))
br label %17
17: ; preds = %15, %0
%18 = getelementptr inbounds [100 x i8], [100 x i8]* %2, i64 0, i64 0
%19 = call i32 @strncat(i8* %18, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 2)
%20 = getelementptr inbounds [100 x i8], [100 x i8]* %2, i64 0, i64 0
%21 = call i64 @strcmp(i8* %20, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
%22 = icmp ne i64 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %17
%24 = call i32 @die(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.4, i64 0, i64 0))
br label %25
25: ; preds = %23, %17
ret i32 0
}

LLVM IR (-O2):
@.str = [4 x i8] c"foo\00"
@.str.1 = [6 x i8] c"fnord\00"
@.str.2 = [34 x i8] c"strncat did not work for length 0\00"
@.str.3 = [8 x i8] c"fnordfo\00"
@.str.4 = [29 x i8] c"strncat did not copy n bytes\00"
define i32 @main() {
%1 = alloca [100 x i8]
%2 = getelementptr inbounds [100 x i8], [100 x i8]* %1, i64 0, i64 0
store i8 102, i8* %2
%3 = getelementptr inbounds [100 x i8], [100 x i8]* %1, i64 0, i64 1
store i8 110, i8* %3
%4 = getelementptr inbounds [100 x i8], [100 x i8]* %1, i64 0, i64 2
store i8 111, i8* %4
%5 = getelementptr inbounds [100 x i8], [100 x i8]* %1, i64 0, i64 3
store i8 114, i8* %5
%6 = getelementptr inbounds [100 x i8], [100 x i8]* %1, i64 0, i64 4
store i8 100, i8* %6
%7 = call i32 @strncat(i8* %2, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 0)
%8 = call i64 @strcmp(i8* %2, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%9 = icmp eq i64 %8, 0
br i1 %9, label %12, label %10
10: ; preds = %0
%11 = call i32 @die(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0))
br label %12
12: ; preds = %10, %0
%13 = call i32 @strncat(i8* %2, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 2)
%14 = call i64 @strcmp(i8* %2, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
%15 = icmp eq i64 %14, 0
br i1 %15, label %18, label %16
16: ; preds = %12
%17 = call i32 @die(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.4, i64 0, i64 0))
br label %18
18: ; preds = %16, %12
ret i32 0
}