#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 http_pkg {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ add_header(struct http_pkg *, char *, char *);
int /*<<< orphan*/ free(char *);
char **get_header_val(struct http_pkg *, char *);
void set_header(struct http_pkg *pkg, char *key, char *val) {
  char **old_val = get_header_val(pkg, key);
  if (old_val == NULL) {
    add_header(pkg, key, *old_val);
  } else {
    do {
      if (*old_val != NULL) {
        free(*old_val);
        *old_val = NULL;
      }
    } while (0);
    *old_val = val;
  }
}


x86 assembly (clang, -O0):
<set_header>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1050 <get_header_val@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1179 <set_header+0x49>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x20(%rbp),%rax
mov    (%rax),%rdx
call   1040 <add_header@plt>
jmp    11b3 <set_header+0x83>
jmp    117e <set_header+0x4e>
mov    -0x20(%rbp),%rax
cmpq   $0x0,(%rax)
je     11a3 <set_header+0x73>
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
call   1030 <free@plt>
mov    -0x20(%rbp),%rax
movq   $0x0,(%rax)
jmp    11a8 <set_header+0x78>
mov    -0x18(%rbp),%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<set_header>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%r12
call   1050 <get_header_val@plt>
test   %rax,%rax
je     116a <set_header+0x3a>
mov    %rax,%rbx
mov    (%rax),%rdi
test   %rdi,%rdi
je     115b <set_header+0x2b>
call   1030 <free@plt>
mov    %r14,(%rbx)
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
mov    %r12,%rdi
mov    %r15,%rsi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
jmp    1040 <add_header@plt>

LLVM IR (-O0):
%struct.http_pkg = type { i32 }
define void @set_header(%struct.http_pkg* %0, i8* %1, i8* %2) {
%4 = alloca %struct.http_pkg*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8**
store %struct.http_pkg* %0, %struct.http_pkg** %4
store i8* %1, i8** %5
store i8* %2, i8** %6
%8 = load %struct.http_pkg*, %struct.http_pkg** %4
%9 = load i8*, i8** %5
%10 = call i8** @get_header_val(%struct.http_pkg* %8, i8* %9)
store i8** %10, i8*** %7
%11 = load i8**, i8*** %7
%12 = icmp eq i8** %11, null
br i1 %12, label %13, label %19
13: ; preds = %3
%14 = load %struct.http_pkg*, %struct.http_pkg** %4
%15 = load i8*, i8** %5
%16 = load i8**, i8*** %7
%17 = load i8*, i8** %16
%18 = call i32 @add_header(%struct.http_pkg* %14, i8* %15, i8* %17)
br label %33
19: ; preds = %3
br label %20
20: ; preds = %19
%21 = load i8**, i8*** %7
%22 = load i8*, i8** %21
%23 = icmp ne i8* %22, null
br i1 %23, label %24, label %29
24: ; preds = %20
%25 = load i8**, i8*** %7
%26 = load i8*, i8** %25
%27 = call i32 @free(i8* %26)
%28 = load i8**, i8*** %7
store i8* null, i8** %28
br label %29
29: ; preds = %24, %20
br label %30
30: ; preds = %29
%31 = load i8*, i8** %6
%32 = load i8**, i8*** %7
store i8* %31, i8** %32
br label %33
33: ; preds = %30, %13
ret void
}

LLVM IR (-O2):
%struct.http_pkg = type { i32 }
define void @set_header(%struct.http_pkg* %0, i8* %1, i8* %2) {
%4 = tail call i8** @get_header_val(%struct.http_pkg* %0, i8* %1)
%5 = icmp eq i8** %4, null
br i1 %5, label %6, label %8
6: ; preds = %3
%7 = tail call i32 @add_header(%struct.http_pkg* %0, i8* %1, i8* undef)
br label %14
8: ; preds = %3
%9 = load i8*, i8** %4
%10 = icmp eq i8* %9, null
br i1 %10, label %13, label %11
11: ; preds = %8
%12 = tail call i32 @free(i8* %9)
br label %13
13: ; preds = %11, %8
store i8* %2, i8** %4
br label %14
14: ; preds = %13, %6
ret void
}