#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*/ assertStringsEqual(char *, char *);
int /*<<< orphan*/ assertTrue(char *, int);
int snprintf(char *, int, char *, char *);
void test_buffer_length() {
  char buffer[100];
  int ret;
  ret = snprintf(buffer, 10, "%s", "Test");
  assertTrue("ret == 4", ret == 4);
  ;
  ret = snprintf(buffer, 0, "%s", "Test");
  assertTrue("ret == 4", ret == 4);
  ;
  buffer[0] = (char)0xA5;
  ret = snprintf(buffer, 0, "%s", "Test");
  assertTrue("buffer[0] == (char)0xA5", buffer[0] == (char)0xA5);
  ;
  assertTrue("ret == 4", ret == 4);
  ;
  buffer[0] = (char)0xCC;
  snprintf(buffer, 1, "%s", "Test");
  assertTrue("buffer[0] == '\\0'", buffer[0] == '\0');
  ;
  snprintf(buffer, 2, "%s", "Hello");
  assertStringsEqual(buffer, "H");
}


x86 assembly (clang, -O0):
<test_buffer_length>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
lea    -0x70(%rbp),%rdi
mov    $0xa,%esi
lea    0xeb5(%rip),%rdx
lea    0xeb1(%rip),%rcx
call   1030 <snprintf@plt>
mov    %eax,-0x74(%rbp)
cmpl   $0x4,-0x74(%rbp)
sete   %al
and    $0x1,%al
movzbl %al,%esi
lea    0xe9b(%rip),%rdi
call   1040 <assertTrue@plt>
lea    -0x70(%rbp),%rdi
xor    %esi,%esi
lea    0xe81(%rip),%rdx
lea    0xe7d(%rip),%rcx
call   1030 <snprintf@plt>
mov    %eax,-0x74(%rbp)
cmpl   $0x4,-0x74(%rbp)
sete   %al
and    $0x1,%al
movzbl %al,%esi
lea    0xe67(%rip),%rdi
call   1040 <assertTrue@plt>
movb   $0xa5,-0x70(%rbp)
lea    -0x70(%rbp),%rdi
xor    %esi,%esi
lea    0xe49(%rip),%rdx
lea    0xe45(%rip),%rcx
call   1030 <snprintf@plt>
mov    %eax,-0x74(%rbp)
movsbl -0x70(%rbp),%eax
cmp    $0xffffffa5,%eax
sete   %al
and    $0x1,%al
movzbl %al,%esi
lea    0xe35(%rip),%rdi
call   1040 <assertTrue@plt>
cmpl   $0x4,-0x74(%rbp)
sete   %al
and    $0x1,%al
movzbl %al,%esi
lea    0xe14(%rip),%rdi
call   1040 <assertTrue@plt>
movb   $0xcc,-0x70(%rbp)
lea    -0x70(%rbp),%rdi
mov    $0x1,%esi
lea    0xdf3(%rip),%rdx
lea    0xdef(%rip),%rcx
call   1030 <snprintf@plt>
movsbl -0x70(%rbp),%eax
cmp    $0x0,%eax
sete   %al
and    $0x1,%al
movzbl %al,%esi
lea    0xdfa(%rip),%rdi
call   1040 <assertTrue@plt>
lea    -0x70(%rbp),%rdi
mov    $0x2,%esi
lea    0xdbc(%rip),%rdx
lea    0xdf0(%rip),%rcx
call   1030 <snprintf@plt>
lea    -0x70(%rbp),%rdi
lea    0xde6(%rip),%rsi
call   1050 <assertStringsEqual@plt>
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_buffer_length>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x70,%rsp
lea    0xebd(%rip),%r14
lea    0xeb9(%rip),%r15
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0xa,%esi
mov    %r14,%rdx
mov    %r15,%rcx
call   1030 <snprintf@plt>
xor    %esi,%esi
cmp    $0x4,%eax
sete   %sil
lea    0xe98(%rip),%r12
mov    %r12,%rdi
call   1040 <assertTrue@plt>
mov    %rbx,%rdi
xor    %esi,%esi
mov    %r14,%rdx
mov    %r15,%rcx
call   1030 <snprintf@plt>
xor    %esi,%esi
cmp    $0x4,%eax
sete   %sil
mov    %r12,%rdi
call   1040 <assertTrue@plt>
movb   $0xa5,(%rsp)
mov    %rbx,%rdi
xor    %esi,%esi
mov    %r14,%rdx
mov    %r15,%rcx
call   1030 <snprintf@plt>
mov    %eax,%ebp
xor    %esi,%esi
cmpb   $0xa5,(%rsp)
sete   %sil
lea    0xe51(%rip),%rdi
call   1040 <assertTrue@plt>
xor    %esi,%esi
cmp    $0x4,%ebp
sete   %sil
mov    %r12,%rdi
call   1040 <assertTrue@plt>
movb   $0xcc,(%rsp)
mov    %rbx,%rdi
mov    $0x1,%esi
mov    %r14,%rdx
mov    %r15,%rcx
call   1030 <snprintf@plt>
xor    %esi,%esi
cmpb   $0x0,(%rsp)
sete   %sil
lea    0xe2b(%rip),%rdi
call   1040 <assertTrue@plt>
lea    0xe31(%rip),%rcx
mov    %rbx,%rdi
mov    $0x2,%esi
mov    %r14,%rdx
call   1030 <snprintf@plt>
lea    0xe20(%rip),%rsi
mov    %rbx,%rdi
call   1050 <assertStringsEqual@plt>
add    $0x70,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"%s\00"
@.str.1 = [5 x i8] c"Test\00"
@.str.2 = [9 x i8] c"ret == 4\00"
@.str.3 = [24 x i8] c"buffer[0] == (char)0xA5\00"
@.str.4 = [18 x i8] c"buffer[0] == '\\0'\00"
@.str.5 = [6 x i8] c"Hello\00"
@.str.6 = [2 x i8] c"H\00"
define void @test_buffer_length() {
%1 = alloca [100 x i8]
%2 = alloca i32
%3 = getelementptr inbounds [100 x i8], [100 x i8]* %1, i64 0, i64 0
%4 = call i32 @snprintf(i8* %3, i32 10, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
store i32 %4, i32* %2
%5 = load i32, i32* %2
%6 = icmp eq i32 %5, 4
%7 = zext i1 %6 to i32
%8 = call i32 @assertTrue(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i32 %7)
%9 = getelementptr inbounds [100 x i8], [100 x i8]* %1, i64 0, i64 0
%10 = call i32 @snprintf(i8* %9, i32 0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
store i32 %10, i32* %2
%11 = load i32, i32* %2
%12 = icmp eq i32 %11, 4
%13 = zext i1 %12 to i32
%14 = call i32 @assertTrue(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i32 %13)
%15 = getelementptr inbounds [100 x i8], [100 x i8]* %1, i64 0, i64 0
store i8 -91, i8* %15
%16 = getelementptr inbounds [100 x i8], [100 x i8]* %1, i64 0, i64 0
%17 = call i32 @snprintf(i8* %16, i32 0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
store i32 %17, i32* %2
%18 = getelementptr inbounds [100 x i8], [100 x i8]* %1, i64 0, i64 0
%19 = load i8, i8* %18
%20 = sext i8 %19 to i32
%21 = icmp eq i32 %20, -91
%22 = zext i1 %21 to i32
%23 = call i32 @assertTrue(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0), i32 %22)
%24 = load i32, i32* %2
%25 = icmp eq i32 %24, 4
%26 = zext i1 %25 to i32
%27 = call i32 @assertTrue(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i32 %26)
%28 = getelementptr inbounds [100 x i8], [100 x i8]* %1, i64 0, i64 0
store i8 -52, i8* %28
%29 = getelementptr inbounds [100 x i8], [100 x i8]* %1, i64 0, i64 0
%30 = call i32 @snprintf(i8* %29, i32 1, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%31 = getelementptr inbounds [100 x i8], [100 x i8]* %1, i64 0, i64 0
%32 = load i8, i8* %31
%33 = sext i8 %32 to i32
%34 = icmp eq i32 %33, 0
%35 = zext i1 %34 to i32
%36 = call i32 @assertTrue(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0), i32 %35)
%37 = getelementptr inbounds [100 x i8], [100 x i8]* %1, i64 0, i64 0
%38 = call i32 @snprintf(i8* %37, i32 2, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0))
%39 = getelementptr inbounds [100 x i8], [100 x i8]* %1, i64 0, i64 0
%40 = call i32 @assertStringsEqual(i8* %39, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.6, i64 0, i64 0))
ret void
}

LLVM IR (-O2):
@.str = [3 x i8] c"%s\00"
@.str.1 = [5 x i8] c"Test\00"
@.str.2 = [9 x i8] c"ret == 4\00"
@.str.3 = [24 x i8] c"buffer[0] == (char)0xA5\00"
@.str.4 = [18 x i8] c"buffer[0] == '\\0'\00"
@.str.5 = [6 x i8] c"Hello\00"
@.str.6 = [2 x i8] c"H\00"
define void @test_buffer_length() {
%1 = alloca [100 x i8]
%2 = getelementptr inbounds [100 x i8], [100 x i8]* %1, i64 0, i64 0
%3 = call i32 @snprintf(i8* %2, i32 10, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%4 = icmp eq i32 %3, 4
%5 = zext i1 %4 to i32
%6 = call i32 @assertTrue(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i32 %5)
%7 = call i32 @snprintf(i8* %2, i32 0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%8 = icmp eq i32 %7, 4
%9 = zext i1 %8 to i32
%10 = call i32 @assertTrue(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i32 %9)
store i8 -91, i8* %2
%11 = call i32 @snprintf(i8* %2, i32 0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%12 = load i8, i8* %2
%13 = icmp eq i8 %12, -91
%14 = zext i1 %13 to i32
%15 = call i32 @assertTrue(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0), i32 %14)
%16 = icmp eq i32 %11, 4
%17 = zext i1 %16 to i32
%18 = call i32 @assertTrue(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i32 %17)
store i8 -52, i8* %2
%19 = call i32 @snprintf(i8* %2, i32 1, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%20 = load i8, i8* %2
%21 = icmp eq i8 %20, 0
%22 = zext i1 %21 to i32
%23 = call i32 @assertTrue(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0), i32 %22)
%24 = call i32 @snprintf(i8* %2, i32 2, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0))
%25 = call i32 @assertStringsEqual(i8* %2, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.6, i64 0, i64 0))
ret void
}