#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 */
typedef struct TYPE_3__ TYPE_1__;

/* Type definitions */
typedef unsigned int ssize_t;
struct TYPE_3__ {
  unsigned int p_filesz;
  int /*<<< orphan*/ p_offset;
};
typedef TYPE_1__ Elf32_Phdr;

/* Variables and functions */
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ assert(int);
scalar_t__ lseek(int, int /*<<< orphan*/, int /*<<< orphan*/);
unsigned int read(int, char *, unsigned int);
void dl_copy_dl_name(int fd, Elf32_Phdr *segment, char *name) {
  unsigned int p_filesz = segment->p_filesz;
  if (p_filesz > 256)
    p_filesz = 256;
  if (lseek(fd, segment->p_offset, SEEK_SET) >= 0) {
    ssize_t res = read(fd, name, p_filesz);
    assert(res == p_filesz);
    name[segment->p_filesz] = '\0';
  }
}


x86 assembly (clang, -O0):
<dl_copy_dl_name>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
cmpl   $0x100,-0x1c(%rbp)
jbe    1160 <dl_copy_dl_name+0x30>
movl   $0x100,-0x1c(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rax
mov    0x4(%rax),%esi
mov    0x2e6f(%rip),%rax
mov    (%rax),%edx
call   1030 <lseek@plt>
cmp    $0x0,%rax
jl     11b5 <dl_copy_dl_name+0x85>
mov    -0x4(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1040 <read@plt>
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0x1c(%rbp),%eax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
mov    -0x18(%rbp),%rax
mov    -0x10(%rbp),%rcx
mov    (%rcx),%ecx
movb   $0x0,(%rax,%rcx,1)
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dl_copy_dl_name>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%rbx
mov    %edi,%r15d
mov    (%rsi),%eax
mov    0x4(%rsi),%esi
cmp    $0x100,%eax
mov    $0x100,%ebp
cmovb  %eax,%ebp
mov    0x2e87(%rip),%rax
mov    (%rax),%edx
call   1030 <lseek@plt>
test   %rax,%rax
js     1186 <dl_copy_dl_name+0x56>
mov    %r15d,%edi
mov    %r14,%rsi
mov    %ebp,%edx
call   1040 <read@plt>
xor    %edi,%edi
cmp    %ebp,%eax
sete   %dil
call   1050 <assert@plt>
mov    (%rbx),%eax
movb   $0x0,(%r14,%rax,1)
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.TYPE_3__ = type { i32, i32 }
@SEEK_SET = global i32 0
define void @dl_copy_dl_name(i32 %0, %struct.TYPE_3__* %1, i8* %2) {
%4 = alloca i32
%5 = alloca %struct.TYPE_3__*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
store i32 %0, i32* %4
store %struct.TYPE_3__* %1, %struct.TYPE_3__** %5
store i8* %2, i8** %6
%9 = load %struct.TYPE_3__*, %struct.TYPE_3__** %5
%10 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %9, i32 0, i32 0
%11 = load i32, i32* %10
store i32 %11, i32* %7
%12 = load i32, i32* %7
%13 = icmp ugt i32 %12, 256
br i1 %13, label %14, label %15
14: ; preds = %3
store i32 256, i32* %7
br label %15
15: ; preds = %14, %3
%16 = load i32, i32* %4
%17 = load %struct.TYPE_3__*, %struct.TYPE_3__** %5
%18 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %17, i32 0, i32 1
%19 = load i32, i32* %18
%20 = load i32, i32* @SEEK_SET
%21 = call i64 @lseek(i32 %16, i32 %19, i32 %20)
%22 = icmp sge i64 %21, 0
br i1 %22, label %23, label %39
23: ; preds = %15
%24 = load i32, i32* %4
%25 = load i8*, i8** %6
%26 = load i32, i32* %7
%27 = call i32 @read(i32 %24, i8* %25, i32 %26)
store i32 %27, i32* %8
%28 = load i32, i32* %8
%29 = load i32, i32* %7
%30 = icmp eq i32 %28, %29
%31 = zext i1 %30 to i32
%32 = call i32 @assert(i32 %31)
%33 = load i8*, i8** %6
%34 = load %struct.TYPE_3__*, %struct.TYPE_3__** %5
%35 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %34, i32 0, i32 0
%36 = load i32, i32* %35
%37 = zext i32 %36 to i64
%38 = getelementptr inbounds i8, i8* %33, i64 %37
store i8 0, i8* %38
br label %39
39: ; preds = %23, %15
ret void
}

LLVM IR (-O2):
%struct.TYPE_3__ = type { i32, i32 }
@SEEK_SET = global i32 0
define void @dl_copy_dl_name(i32 %0, %struct.TYPE_3__* nocapture %1, i8* nocapture %2) {
%4 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %1, i64 0, i32 0
%5 = load i32, i32* %4
%6 = icmp ult i32 %5, 256
%7 = select i1 %6, i32 %5, i32 256
%8 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %1, i64 0, i32 1
%9 = load i32, i32* %8
%10 = load i32, i32* @SEEK_SET
%11 = tail call i64 @lseek(i32 %0, i32 %9, i32 %10)
%12 = icmp sgt i64 %11, -1
br i1 %12, label %13, label %21
13: ; preds = %3
%14 = tail call i32 @read(i32 %0, i8* %2, i32 %7)
%15 = icmp eq i32 %14, %7
%16 = zext i1 %15 to i32
%17 = tail call i32 @assert(i32 %16)
%18 = load i32, i32* %4
%19 = zext i32 %18 to i64
%20 = getelementptr inbounds i8, i8* %2, i64 %19
store i8 0, i8* %20
br label %21
21: ; preds = %13, %3
ret void
}