#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*/ free(char *);
char *malloc(int);
int /*<<< orphan*/ printf(char *);
int strlen(char *);
int /*<<< orphan*/ strncpy(char *, char *, int);
void context_set(char **structelem, char *fname) {
  char *c;
  int len;
  if (*structelem) {
    free(*structelem);
    *structelem = NULL;
  }
  if (fname) {
    len = strlen(fname);
    c = malloc((len + 1) * sizeof(char));
    if (c) {
      strncpy(c, fname, len);
      *(c + len) = 0;
      *structelem = c;
    } else {
      printf("FATAL: while allocating memory for context initialization\n");
    }
  }
}


x86 assembly (clang, -O0):
<context_set>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rax
cmpq   $0x0,(%rax)
je     1185 <context_set+0x35>
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
call   1030 <free@plt>
mov    -0x8(%rbp),%rax
movq   $0x0,(%rax)
cmpq   $0x0,-0x10(%rbp)
je     11fb <context_set+0xab>
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
cltq
shl    $0x0,%rax
mov    %eax,%edi
call   1070 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     11ea <context_set+0x9a>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1040 <strncpy@plt>
mov    -0x18(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%rcx
mov    -0x8(%rbp),%rax
mov    %rcx,(%rax)
jmp    11f6 <context_set+0xa6>
lea    0xe0f(%rip),%rdi
call   1060 <printf@plt>
jmp    11fb <context_set+0xab>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<context_set>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r12
mov    %rdi,%r14
mov    (%rdi),%rdi
test   %rdi,%rdi
je     1172 <context_set+0x22>
call   1030 <free@plt>
movq   $0x0,(%r14)
test   %r12,%r12
je     11ab <context_set+0x5b>
mov    %r12,%rdi
call   1060 <strlen@plt>
mov    %eax,%r15d
lea    0x1(%r15),%edi
call   1070 <malloc@plt>
test   %rax,%rax
je     11b7 <context_set+0x67>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r12,%rsi
mov    %r15d,%edx
call   1040 <strncpy@plt>
movslq %r15d,%rax
movb   $0x0,(%rbx,%rax,1)
mov    %rbx,(%r14)
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
lea    0xe42(%rip),%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
jmp    1050 <puts@plt>

LLVM IR (-O0):
@.str = [59 x i8] c"FATAL: while allocating memory for context initialization\0A\00"
define void @context_set(i8** %0, i8* %1) {
%3 = alloca i8**
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
store i8** %0, i8*** %3
store i8* %1, i8** %4
%7 = load i8**, i8*** %3
%8 = load i8*, i8** %7
%9 = icmp ne i8* %8, null
br i1 %9, label %10, label %15
10: ; preds = %2
%11 = load i8**, i8*** %3
%12 = load i8*, i8** %11
%13 = call i32 @free(i8* %12)
%14 = load i8**, i8*** %3
store i8* null, i8** %14
br label %15
15: ; preds = %10, %2
%16 = load i8*, i8** %4
%17 = icmp ne i8* %16, null
br i1 %17, label %18, label %43
18: ; preds = %15
%19 = load i8*, i8** %4
%20 = call i32 @strlen(i8* %19)
store i32 %20, i32* %6
%21 = load i32, i32* %6
%22 = add nsw i32 %21, 1
%23 = sext i32 %22 to i64
%24 = mul i64 %23, 1
%25 = trunc i64 %24 to i32
%26 = call i8* @malloc(i32 %25)
store i8* %26, i8** %5
%27 = load i8*, i8** %5
%28 = icmp ne i8* %27, null
br i1 %28, label %29, label %40
29: ; preds = %18
%30 = load i8*, i8** %5
%31 = load i8*, i8** %4
%32 = load i32, i32* %6
%33 = call i32 @strncpy(i8* %30, i8* %31, i32 %32)
%34 = load i8*, i8** %5
%35 = load i32, i32* %6
%36 = sext i32 %35 to i64
%37 = getelementptr inbounds i8, i8* %34, i64 %36
store i8 0, i8* %37
%38 = load i8*, i8** %5
%39 = load i8**, i8*** %3
store i8* %38, i8** %39
br label %42
40: ; preds = %18
%41 = call i32 @printf(i8* getelementptr inbounds ([59 x i8], [59 x i8]* @.str, i64 0, i64 0))
br label %42
42: ; preds = %40, %29
br label %43
43: ; preds = %42, %15
ret void
}

LLVM IR (-O2):
@str = [58 x i8] c"FATAL: while allocating memory for context initialization\00"
define void @context_set(i8** nocapture %0, i8* %1) {
%3 = load i8*, i8** %0
%4 = icmp eq i8* %3, null
br i1 %4, label %7, label %5
5: ; preds = %2
%6 = tail call i32 @free(i8* %3)
store i8* null, i8** %0
br label %7
7: ; preds = %5, %2
%8 = icmp eq i8* %1, null
br i1 %8, label %20, label %9
9: ; preds = %7
%10 = tail call i32 @strlen(i8* %1)
%11 = add nsw i32 %10, 1
%12 = tail call i8* @malloc(i32 %11)
%13 = icmp eq i8* %12, null
br i1 %13, label %18, label %14
14: ; preds = %9
%15 = tail call i32 @strncpy(i8* %12, i8* %1, i32 %10)
%16 = sext i32 %10 to i64
%17 = getelementptr inbounds i8, i8* %12, i64 %16
store i8 0, i8* %17
store i8* %12, i8** %0
br label %20
18: ; preds = %9
%19 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([58 x i8], [58 x i8]* @str, i64 0, i64 0))
br label %20
20: ; preds = %14, %18, %7
ret void
}