#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 */
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int ENAMETOOLONG;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, size_t, char const *,
                           int);
int /*<<< orphan*/ stderr;
int zsnprintf(char *, size_t, char *, char const *);
int test_zsnprintf(size_t len, const char *str, int expect_succ) {
  int ret;
  char buf[512];
  if (len > sizeof(buf)) {
    return -ENAMETOOLONG;
  }
  ret = zsnprintf(buf, len, "%s", str);
  if ((ret == 0) != (expect_succ != 0)) {
    fprintf(stderr,
            "test_zsnprintf(len=%Zd, str=%s, "
            "expect_succ=%d) failed\n",
            len, str, expect_succ);
    return 1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<test_zsnprintf>:
push   %rbp
mov    %rsp,%rbp
sub    $0x220,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
cmpq   $0x200,-0x10(%rbp)
jbe    1157 <test_zsnprintf+0x37>
xor    %eax,%eax
mov    0x2e8b(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    11ca <test_zsnprintf+0xaa>
lea    -0x220(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rcx
lea    0xe93(%rip),%rdx
call   1040 <zsnprintf@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
sete   %al
and    $0x1,%al
movzbl %al,%eax
cmpl   $0x0,-0x1c(%rbp)
setne  %cl
and    $0x1,%cl
movzbl %cl,%ecx
cmp    %ecx,%eax
je     11c3 <test_zsnprintf+0xa3>
mov    0x2e43(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
mov    -0x18(%rbp),%rcx
mov    -0x1c(%rbp),%r8d
lea    0xe51(%rip),%rsi
call   1030 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    11ca <test_zsnprintf+0xaa>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x220,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_zsnprintf>:
push   %rbp
push   %r14
push   %rbx
sub    $0x200,%rsp
cmp    $0x201,%rdi
jb     1141 <test_zsnprintf+0x21>
mov    0x2e9d(%rip),%rcx
xor    %eax,%eax
sub    (%rcx),%eax
jmp    1193 <test_zsnprintf+0x73>
mov    %edx,%r14d
mov    %rsi,%rbx
mov    %rdi,%rbp
lea    0xeaf(%rip),%rdx
mov    %rsp,%rdi
mov    %rbp,%rsi
mov    %rbx,%rcx
call   1040 <zsnprintf@plt>
test   %eax,%eax
setne  %cl
test   %r14d,%r14d
setne  %dl
xor    %eax,%eax
xor    %cl,%dl
jne    1193 <test_zsnprintf+0x73>
mov    0x2e69(%rip),%rax
mov    (%rax),%edi
lea    0xe83(%rip),%rsi
mov    %rbp,%rdx
mov    %rbx,%rcx
mov    %r14d,%r8d
call   1030 <fprintf@plt>
mov    $0x1,%eax
add    $0x200,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@ENAMETOOLONG = global i32 0
@.str = [3 x i8] c"%s\00"
@stderr = global i32 0
@.str.1 = [56 x i8] c"test_zsnprintf(len=%Zd, str=%s, expect_succ=%d) failed\0A\00"
define i32 @test_zsnprintf(i64 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i64
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca [512 x i8]
store i64 %0, i64* %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%10 = load i64, i64* %5
%11 = icmp ugt i64 %10, 512
br i1 %11, label %12, label %15
12: ; preds = %3
%13 = load i32, i32* @ENAMETOOLONG
%14 = sub nsw i32 0, %13
store i32 %14, i32* %4
br label %34
15: ; preds = %3
%16 = getelementptr inbounds [512 x i8], [512 x i8]* %9, i64 0, i64 0
%17 = load i64, i64* %5
%18 = load i8*, i8** %6
%19 = call i32 @zsnprintf(i8* %16, i64 %17, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %18)
store i32 %19, i32* %8
%20 = load i32, i32* %8
%21 = icmp eq i32 %20, 0
%22 = zext i1 %21 to i32
%23 = load i32, i32* %7
%24 = icmp ne i32 %23, 0
%25 = zext i1 %24 to i32
%26 = icmp ne i32 %22, %25
br i1 %26, label %27, label %33
27: ; preds = %15
%28 = load i32, i32* @stderr
%29 = load i64, i64* %5
%30 = load i8*, i8** %6
%31 = load i32, i32* %7
%32 = call i32 @fprintf(i32 %28, i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str.1, i64 0, i64 0), i64 %29, i8* %30, i32 %31)
store i32 1, i32* %4
br label %34
33: ; preds = %15
store i32 0, i32* %4
br label %34
34: ; preds = %33, %27, %12
%35 = load i32, i32* %4
ret i32 %35
}

LLVM IR (-O2):
@ENAMETOOLONG = global i32 0
@.str = [3 x i8] c"%s\00"
@stderr = global i32 0
@.str.1 = [56 x i8] c"test_zsnprintf(len=%Zd, str=%s, expect_succ=%d) failed\0A\00"
define i32 @test_zsnprintf(i64 %0, i8* %1, i32 %2) {
%4 = alloca [512 x i8]
%5 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
%6 = icmp ugt i64 %0, 512
br i1 %6, label %7, label %10
7: ; preds = %3
%8 = load i32, i32* @ENAMETOOLONG
%9 = sub nsw i32 0, %8
br label %18
10: ; preds = %3
%11 = call i32 @zsnprintf(i8* %5, i64 %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %1)
%12 = icmp ne i32 %11, 0
%13 = icmp ne i32 %2, 0
%14 = xor i1 %13, %12
br i1 %14, label %18, label %15
15: ; preds = %10
%16 = load i32, i32* @stderr
%17 = call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str.1, i64 0, i64 0), i64 %0, i8* %1, i32 %2)
br label %18
18: ; preds = %10, %15, %7
%19 = phi i32 [ %9, %7 ], [ 1, %15 ], [ 0, %10 ]
ret i32 %19
}