#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 */
scalar_t__ strcmp(char *, char *);
int /*<<< orphan*/ xfree(char *);
char *xstrdupn(char *, int);
int test_xstrdupn(void) {
  char *p;
  if ((p = xstrdupn("test_and_test", 4)) == NULL)
    return -1;
  if (strcmp(p, "test") != 0) {
    xfree(p);
    return -1;
  }
  xfree(p);
  return 0;
}


x86 assembly (clang, -O0):
<test_xstrdupn>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
lea    0xec1(%rip),%rdi
mov    $0x4,%esi
call   1030 <xstrdupn@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jne    1163 <test_xstrdupn+0x33>
movl   $0xffffffff,-0x4(%rbp)
jmp    11a2 <test_xstrdupn+0x72>
mov    -0x10(%rbp),%rdi
lea    0xe9b(%rip),%rsi
call   1050 <strcmp@plt>
cmp    $0x0,%rax
je     1192 <test_xstrdupn+0x62>
mov    -0x10(%rbp),%rdi
call   1040 <xfree@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11a2 <test_xstrdupn+0x72>
mov    -0x10(%rbp),%rdi
call   1040 <xfree@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_xstrdupn>:
push   %r14
push   %rbx
push   %rax
lea    0xec5(%rip),%rdi
mov    $0x4,%esi
call   1030 <xstrdupn@plt>
test   %rax,%rax
je     116e <test_xstrdupn+0x3e>
mov    %rax,%rbx
lea    0xeb5(%rip),%rsi
mov    %rax,%rdi
call   1050 <strcmp@plt>
mov    %rax,%r14
mov    %rbx,%rdi
call   1040 <xfree@plt>
neg    %r14
sbb    %eax,%eax
jmp    1173 <test_xstrdupn+0x43>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [14 x i8] c"test_and_test\00"
@.str.1 = [5 x i8] c"test\00"
define i32 @test_xstrdupn() {
%1 = alloca i32
%2 = alloca i8*
%3 = call i8* @xstrdupn(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 4)
store i8* %3, i8** %2
%4 = icmp eq i8* %3, null
br i1 %4, label %5, label %6
5: ; preds = %0
store i32 -1, i32* %1
br label %16
6: ; preds = %0
%7 = load i8*, i8** %2
%8 = call i64 @strcmp(i8* %7, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%9 = icmp ne i64 %8, 0
br i1 %9, label %10, label %13
10: ; preds = %6
%11 = load i8*, i8** %2
%12 = call i32 @xfree(i8* %11)
store i32 -1, i32* %1
br label %16
13: ; preds = %6
%14 = load i8*, i8** %2
%15 = call i32 @xfree(i8* %14)
store i32 0, i32* %1
br label %16
16: ; preds = %13, %10, %5
%17 = load i32, i32* %1
ret i32 %17
}

LLVM IR (-O2):
@.str = [14 x i8] c"test_and_test\00"
@.str.1 = [5 x i8] c"test\00"
define i32 @test_xstrdupn() {
%1 = tail call i8* @xstrdupn(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 4)
%2 = icmp eq i8* %1, null
br i1 %2, label %8, label %3
3: ; preds = %0
%4 = tail call i64 @strcmp(i8* %1, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%5 = icmp ne i64 %4, 0
%6 = tail call i32 @xfree(i8* %1)
%7 = sext i1 %5 to i32
br label %8
8: ; preds = %3, %0
%9 = phi i32 [ -1, %0 ], [ %7, %3 ]
ret i32 %9
}