#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 */
struct response {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ memcpy(char *, char const *, int);
int /*<<< orphan*/ resp_header_get(struct response const *, char const *,
                                   char const **, char const **);
int resp_header_copy(const struct response *resp, const char *name, char *buf,
                     int bufsize) {
  const char *b, *e;
  if (!resp_header_get(resp, name, &b, &e))
    return 0;
  if (bufsize) {
    int len = ((e - b) > (bufsize - 1) ? (bufsize - 1) : (e - b));
    memcpy(buf, b, len);
    buf[len] = '\0';
  }
  return 1;
}


x86 assembly (clang, -O0):
<resp_header_copy>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %ecx,-0x24(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
lea    -0x30(%rbp),%rdx
lea    -0x38(%rbp),%rcx
call   1030 <resp_header_get@plt>
cmp    $0x0,%eax
jne    1161 <resp_header_copy+0x41>
movl   $0x0,-0x4(%rbp)
jmp    11d2 <resp_header_copy+0xb2>
cmpl   $0x0,-0x24(%rbp)
je     11cb <resp_header_copy+0xab>
mov    -0x38(%rbp),%rax
mov    -0x30(%rbp),%rcx
sub    %rcx,%rax
mov    -0x24(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
cmp    %rcx,%rax
jle    1199 <resp_header_copy+0x79>
mov    -0x24(%rbp),%eax
sub    $0x1,%eax
cltq
mov    %rax,-0x48(%rbp)
jmp    11a8 <resp_header_copy+0x88>
mov    -0x38(%rbp),%rax
mov    -0x30(%rbp),%rcx
sub    %rcx,%rax
mov    %rax,-0x48(%rbp)
mov    -0x48(%rbp),%rax
mov    %eax,-0x3c(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    -0x3c(%rbp),%edx
call   1040 <memcpy@plt>
mov    -0x20(%rbp),%rax
movslq -0x3c(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<resp_header_copy>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %ecx,%ebx
mov    %rdx,%r14
lea    0x10(%rsp),%rdx
lea    0x8(%rsp),%rcx
call   1030 <resp_header_get@plt>
test   %eax,%eax
je     117a <resp_header_copy+0x5a>
mov    $0x1,%r15d
test   %ebx,%ebx
je     117d <resp_header_copy+0x5d>
mov    0x8(%rsp),%rbp
mov    0x10(%rsp),%rsi
sub    %rsi,%rbp
add    $0xffffffff,%ebx
movslq %ebx,%rax
cmp    %rax,%rbp
cmovg  %rax,%rbp
mov    %r14,%rdi
mov    %ebp,%edx
call   1040 <memcpy@plt>
movslq %ebp,%rax
movb   $0x0,(%r14,%rax,1)
jmp    117d <resp_header_copy+0x5d>
xor    %r15d,%r15d
mov    %r15d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.response = type { i32 }
define i32 @resp_header_copy(%struct.response* %0, i8* %1, i8* %2, i32 %3) {
%5 = alloca i32
%6 = alloca %struct.response*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i8*
%12 = alloca i32
store %struct.response* %0, %struct.response** %6
store i8* %1, i8** %7
store i8* %2, i8** %8
store i32 %3, i32* %9
%13 = load %struct.response*, %struct.response** %6
%14 = load i8*, i8** %7
%15 = call i32 @resp_header_get(%struct.response* %13, i8* %14, i8** %10, i8** %11)
%16 = icmp ne i32 %15, 0
br i1 %16, label %18, label %17
17: ; preds = %4
store i32 0, i32* %5
br label %53
18: ; preds = %4
%19 = load i32, i32* %9
%20 = icmp ne i32 %19, 0
br i1 %20, label %21, label %52
21: ; preds = %18
%22 = load i8*, i8** %11
%23 = load i8*, i8** %10
%24 = ptrtoint i8* %22 to i64
%25 = ptrtoint i8* %23 to i64
%26 = sub i64 %24, %25
%27 = load i32, i32* %9
%28 = sub nsw i32 %27, 1
%29 = sext i32 %28 to i64
%30 = icmp sgt i64 %26, %29
br i1 %30, label %31, label %35
31: ; preds = %21
%32 = load i32, i32* %9
%33 = sub nsw i32 %32, 1
%34 = sext i32 %33 to i64
br label %41
35: ; preds = %21
%36 = load i8*, i8** %11
%37 = load i8*, i8** %10
%38 = ptrtoint i8* %36 to i64
%39 = ptrtoint i8* %37 to i64
%40 = sub i64 %38, %39
br label %41
41: ; preds = %35, %31
%42 = phi i64 [ %34, %31 ], [ %40, %35 ]
%43 = trunc i64 %42 to i32
store i32 %43, i32* %12
%44 = load i8*, i8** %8
%45 = load i8*, i8** %10
%46 = load i32, i32* %12
%47 = call i32 @memcpy(i8* %44, i8* %45, i32 %46)
%48 = load i8*, i8** %8
%49 = load i32, i32* %12
%50 = sext i32 %49 to i64
%51 = getelementptr inbounds i8, i8* %48, i64 %50
store i8 0, i8* %51
br label %52
52: ; preds = %41, %18
store i32 1, i32* %5
br label %53
53: ; preds = %52, %17
%54 = load i32, i32* %5
ret i32 %54
}

LLVM IR (-O2):
%struct.response = type { i32 }
define i32 @resp_header_copy(%struct.response* %0, i8* %1, i8* %2, i32 %3) {
%5 = alloca i8*
%6 = alloca i8*
%7 = bitcast i8** %5 to i8*
%8 = bitcast i8** %6 to i8*
%9 = call i32 @resp_header_get(%struct.response* %0, i8* %1, i8** %5, i8** %6)
%10 = icmp eq i32 %9, 0
br i1 %10, label %28, label %11
11: ; preds = %4
%12 = icmp eq i32 %3, 0
br i1 %12, label %28, label %13
13: ; preds = %11
%14 = load i8*, i8** %6
%15 = load i8*, i8** %5
%16 = ptrtoint i8* %14 to i64
%17 = ptrtoint i8* %15 to i64
%18 = sub i64 %16, %17
%19 = add nsw i32 %3, -1
%20 = sext i32 %19 to i64
%21 = icmp sgt i64 %18, %20
%22 = select i1 %21, i64 %20, i64 %18
%23 = trunc i64 %22 to i32
%24 = call i32 @memcpy(i8* %2, i8* %15, i32 %23)
%25 = shl i64 %22, 32
%26 = ashr exact i64 %25, 32
%27 = getelementptr inbounds i8, i8* %2, i64 %26
store i8 0, i8* %27
br label %28
28: ; preds = %11, %13, %4
%29 = phi i32 [ 0, %4 ], [ 1, %13 ], [ 1, %11 ]
ret i32 %29
}