#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*/ EXIT_FAILURE;
int /*<<< orphan*/ assert(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
char *memmem(char *, int, char const *, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int read_file(char *, char *, int);
int strlen(char const *);
void get_distro_codename(char *output, int max_length) {
  char buffer[1024];
  int length = read_file("/etc/lsb-release", &buffer[0], 1024);
  if (length == -1) {
    perror("[-] open/read(/etc/lsb-release)");
    exit(EXIT_FAILURE);
  }
  const char *needle = "DISTRIB_CODENAME=";
  int needle_length = strlen(needle);
  char *found = memmem(&buffer[0], length, needle, needle_length);
  if (found == NULL) {
    printf("[-] couldn't find DISTRIB_CODENAME in /etc/lsb-release\n");
    exit(EXIT_FAILURE);
  }
  int i;
  for (i = 0; found[needle_length + i] != '\n'; i++) {
    assert(i < max_length);
    assert((found - &buffer[0]) + needle_length + i < length);
    output[i] = found[needle_length + i];
  }
}


x86 assembly (clang, -O0):
<get_distro_codename>:
push   %rbp
mov    %rsp,%rbp
sub    $0x440,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
lea    -0x410(%rbp),%rsi
lea    0xe70(%rip),%rdi
mov    $0x400,%edx
call   1070 <read_file@plt>
mov    %eax,-0x414(%rbp)
cmpl   $0xffffffff,-0x414(%rbp)
jne    11c7 <get_distro_codename+0x57>
lea    0xe5d(%rip),%rdi
call   1080 <perror@plt>
mov    0x2e20(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
lea    0xe63(%rip),%rax
mov    %rax,-0x420(%rbp)
mov    -0x420(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x424(%rbp)
lea    -0x410(%rbp),%rdi
mov    -0x414(%rbp),%esi
mov    -0x420(%rbp),%rdx
mov    -0x424(%rbp),%ecx
call   1060 <memmem@plt>
mov    %rax,-0x430(%rbp)
cmpq   $0x0,-0x430(%rbp)
jne    1235 <get_distro_codename+0xc5>
lea    0xe21(%rip),%rdi
call   1040 <printf@plt>
mov    0x2db2(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
movl   $0x0,-0x434(%rbp)
mov    -0x430(%rbp),%rax
mov    -0x424(%rbp),%ecx
add    -0x434(%rbp),%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
je     12ef <get_distro_codename+0x17f>
mov    -0x434(%rbp),%eax
cmp    -0xc(%rbp),%eax
setl   %al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
mov    -0x430(%rbp),%rax
lea    -0x410(%rbp),%rcx
sub    %rcx,%rax
movslq -0x424(%rbp),%rcx
add    %rcx,%rax
movslq -0x434(%rbp),%rcx
add    %rcx,%rax
movslq -0x414(%rbp),%rcx
cmp    %rcx,%rax
setl   %al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
mov    -0x430(%rbp),%rax
mov    -0x424(%rbp),%ecx
add    -0x434(%rbp),%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,1),%dl
mov    -0x8(%rbp),%rax
movslq -0x434(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x434(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x434(%rbp)
jmp    123f <get_distro_codename+0xcf>
add    $0x440,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_distro_codename>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x408,%rsp
mov    %esi,%r15d
mov    %rdi,%r14
lea    0xeca(%rip),%rdi
mov    %rsp,%rsi
mov    $0x400,%edx
call   1070 <read_file@plt>
cmp    $0xffffffff,%eax
je     1235 <get_distro_codename+0xc5>
mov    %eax,%r12d
lea    0xe72(%rip),%r13
mov    %r13,%rdi
call   1040 <strlen@plt>
mov    %eax,%ebp
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    %r12d,%esi
mov    %r13,%rdx
mov    %eax,%ecx
call   1060 <memmem@plt>
test   %rax,%rax
je     1243 <get_distro_codename+0xd3>
movslq %ebp,%rcx
cmpb   $0xa,(%rax,%rcx,1)
je     1223 <get_distro_codename+0xb3>
movslq %r12d,%r12
movslq %r15d,%r13
lea    (%rax,%rcx,1),%r15
sub    %rbx,%r15
lea    (%rax,%rcx,1),%rbx
add    $0x1,%rbx
xor    %ebp,%ebp
xor    %edi,%edi
cmp    %r13,%rbp
setl   %dil
call   1050 <assert@plt>
lea    (%r15,%rbp,1),%rax
xor    %edi,%edi
cmp    %r12,%rax
setl   %dil
call   1050 <assert@plt>
movzbl -0x1(%rbx,%rbp,1),%eax
mov    %al,(%r14,%rbp,1)
cmpb   $0xa,(%rbx,%rbp,1)
lea    0x1(%rbp),%rbp
jne    11f0 <get_distro_codename+0x80>
add    $0x408,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdc4(%rip),%rdi
call   1080 <perror@plt>
jmp    124f <get_distro_codename+0xdf>
lea    0xde8(%rip),%rdi
call   1030 <puts@plt>
mov    0x2d8a(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [17 x i8] c"/etc/lsb-release\00"
@.str.1 = [32 x i8] c"[-] open/read(/etc/lsb-release)\00"
@EXIT_FAILURE = global i32 0
@.str.2 = [18 x i8] c"DISTRIB_CODENAME=\00"
@.str.3 = [56 x i8] c"[-] couldn't find DISTRIB_CODENAME in /etc/lsb-release\0A\00"
define void @get_distro_codename(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca [1024 x i8]
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i32
store i8* %0, i8** %3
store i32 %1, i32* %4
%11 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%12 = call i32 @read_file(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* %11, i32 1024)
store i32 %12, i32* %6
%13 = load i32, i32* %6
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %19
15: ; preds = %2
%16 = call i32 @perror(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0))
%17 = load i32, i32* @EXIT_FAILURE
%18 = call i32 @exit(i32 %17)
unreachable
19: ; preds = %2
store i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i8** %7
%20 = load i8*, i8** %7
%21 = call i32 @strlen(i8* %20)
store i32 %21, i32* %8
%22 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%23 = load i32, i32* %6
%24 = load i8*, i8** %7
%25 = load i32, i32* %8
%26 = call i8* @memmem(i8* %22, i32 %23, i8* %24, i32 %25)
store i8* %26, i8** %9
%27 = load i8*, i8** %9
%28 = icmp eq i8* %27, null
br i1 %28, label %29, label %33
29: ; preds = %19
%30 = call i32 @printf(i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str.3, i64 0, i64 0))
%31 = load i32, i32* @EXIT_FAILURE
%32 = call i32 @exit(i32 %31)
unreachable
33: ; preds = %19
store i32 0, i32* %10
br label %34
34: ; preds = %77, %33
%35 = load i8*, i8** %9
%36 = load i32, i32* %8
%37 = load i32, i32* %10
%38 = add nsw i32 %36, %37
%39 = sext i32 %38 to i64
%40 = getelementptr inbounds i8, i8* %35, i64 %39
%41 = load i8, i8* %40
%42 = sext i8 %41 to i32
%43 = icmp ne i32 %42, 10
br i1 %43, label %44, label %80
44: ; preds = %34
%45 = load i32, i32* %10
%46 = load i32, i32* %4
%47 = icmp slt i32 %45, %46
%48 = zext i1 %47 to i32
%49 = call i32 @assert(i32 %48)
%50 = load i8*, i8** %9
%51 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%52 = ptrtoint i8* %50 to i64
%53 = ptrtoint i8* %51 to i64
%54 = sub i64 %52, %53
%55 = load i32, i32* %8
%56 = sext i32 %55 to i64
%57 = add nsw i64 %54, %56
%58 = load i32, i32* %10
%59 = sext i32 %58 to i64
%60 = add nsw i64 %57, %59
%61 = load i32, i32* %6
%62 = sext i32 %61 to i64
%63 = icmp slt i64 %60, %62
%64 = zext i1 %63 to i32
%65 = call i32 @assert(i32 %64)
%66 = load i8*, i8** %9
%67 = load i32, i32* %8
%68 = load i32, i32* %10
%69 = add nsw i32 %67, %68
%70 = sext i32 %69 to i64
%71 = getelementptr inbounds i8, i8* %66, i64 %70
%72 = load i8, i8* %71
%73 = load i8*, i8** %3
%74 = load i32, i32* %10
%75 = sext i32 %74 to i64
%76 = getelementptr inbounds i8, i8* %73, i64 %75
store i8 %72, i8* %76
br label %77
77: ; preds = %44
%78 = load i32, i32* %10
%79 = add nsw i32 %78, 1
store i32 %79, i32* %10
br label %34
80: ; preds = %34
ret void
}

LLVM IR (-O2):
@.str = [17 x i8] c"/etc/lsb-release\00"
@.str.1 = [32 x i8] c"[-] open/read(/etc/lsb-release)\00"
@EXIT_FAILURE = global i32 0
@.str.2 = [18 x i8] c"DISTRIB_CODENAME=\00"
@str = [55 x i8] c"[-] couldn't find DISTRIB_CODENAME in /etc/lsb-release\00"
define void @get_distro_codename(i8* nocapture writeonly %0, i32 %1) {
%3 = alloca [1024 x i8]
%4 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%5 = call i32 @read_file(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* %4, i32 1024)
%6 = icmp eq i32 %5, -1
br i1 %6, label %7, label %11
7: ; preds = %2
%8 = call i32 @perror(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0))
%9 = load i32, i32* @EXIT_FAILURE
%10 = call i32 @exit(i32 %9)
unreachable
11: ; preds = %2
%12 = call i32 @strlen(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0))
%13 = call i8* @memmem(i8* %4, i32 %5, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i32 %12)
%14 = icmp eq i8* %13, null
br i1 %14, label %27, label %15
15: ; preds = %11
%16 = sext i32 %12 to i64
%17 = getelementptr inbounds i8, i8* %13, i64 %16
%18 = load i8, i8* %17
%19 = icmp eq i8 %18, 10
br i1 %19, label %48, label %20
20: ; preds = %15
%21 = ptrtoint i8* %13 to i64
%22 = ptrtoint [1024 x i8]* %3 to i64
%23 = sub i64 %16, %22
%24 = add i64 %23, %21
%25 = sext i32 %5 to i64
%26 = sext i32 %1 to i64
br label %31
27: ; preds = %11
%28 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([55 x i8], [55 x i8]* @str, i64 0, i64 0))
%29 = load i32, i32* @EXIT_FAILURE
%30 = call i32 @exit(i32 %29)
unreachable
31: ; preds = %20, %31
%32 = phi i64 [ 0, %20 ], [ %43, %31 ]
%33 = phi i8* [ %17, %20 ], [ %45, %31 ]
%34 = icmp slt i64 %32, %26
%35 = zext i1 %34 to i32
%36 = call i32 @assert(i32 %35)
%37 = add i64 %24, %32
%38 = icmp slt i64 %37, %25
%39 = zext i1 %38 to i32
%40 = call i32 @assert(i32 %39)
%41 = load i8, i8* %33
%42 = getelementptr inbounds i8, i8* %0, i64 %32
store i8 %41, i8* %42
%43 = add nuw i64 %32, 1
%44 = add nsw i64 %43, %16
%45 = getelementptr inbounds i8, i8* %13, i64 %44
%46 = load i8, i8* %45
%47 = icmp eq i8 %46, 10
br i1 %47, label %48, label %31
48: ; preds = %31, %15
ret void
}