#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 */
char *malloc_or_die(int, char const *, int);
int safestrlen(char const *);
int /*<<< orphan*/ strcat(char *, char const *);
char *safestrdup3(const char *s1, const char *s2, const char *s3,
                  const char *file, int line) {
  int n = 1 + (s1 ? safestrlen(s1) : 0) + (s2 ? safestrlen(s2) : 0) +
          (s3 ? safestrlen(s3) : 0);
  char *s = malloc_or_die(n, file, line);
  s[0] = 0;
  if (s1)
    strcat(s, s1);
  if (s2)
    strcat(s, s2);
  if (s3)
    strcat(s, s3);
  return (s);
}


x86 assembly (clang, -O0):
<safestrdup3>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8d,-0x24(%rbp)
cmpq   $0x0,-0x8(%rbp)
je     1168 <safestrdup3+0x38>
mov    -0x8(%rbp),%rdi
call   1040 <safestrlen@plt>
mov    %eax,-0x34(%rbp)
jmp    1172 <safestrdup3+0x42>
xor    %eax,%eax
mov    %eax,-0x34(%rbp)
jmp    1172 <safestrdup3+0x42>
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x38(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1197 <safestrdup3+0x67>
mov    -0x10(%rbp),%rdi
call   1040 <safestrlen@plt>
mov    %eax,-0x3c(%rbp)
jmp    11a1 <safestrdup3+0x71>
xor    %eax,%eax
mov    %eax,-0x3c(%rbp)
jmp    11a1 <safestrdup3+0x71>
mov    -0x38(%rbp),%eax
mov    -0x3c(%rbp),%ecx
add    %ecx,%eax
mov    %eax,-0x40(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     11c8 <safestrdup3+0x98>
mov    -0x18(%rbp),%rdi
call   1040 <safestrlen@plt>
mov    %eax,-0x44(%rbp)
jmp    11d2 <safestrdup3+0xa2>
xor    %eax,%eax
mov    %eax,-0x44(%rbp)
jmp    11d2 <safestrdup3+0xa2>
mov    -0x40(%rbp),%eax
mov    -0x44(%rbp),%ecx
add    %ecx,%eax
mov    %eax,-0x28(%rbp)
mov    -0x28(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    -0x24(%rbp),%edx
call   1030 <malloc_or_die@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
movb   $0x0,(%rax)
cmpq   $0x0,-0x8(%rbp)
je     120f <safestrdup3+0xdf>
mov    -0x30(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   1050 <strcat@plt>
cmpq   $0x0,-0x10(%rbp)
je     1227 <safestrdup3+0xf7>
mov    -0x30(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1050 <strcat@plt>
cmpq   $0x0,-0x18(%rbp)
je     123f <safestrdup3+0x10f>
mov    -0x30(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1050 <strcat@plt>
mov    -0x30(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<safestrdup3>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %r8d,0xc(%rsp)
mov    %rcx,0x10(%rsp)
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%rbx
test   %rdi,%rdi
je     1167 <safestrdup3+0x37>
mov    %rbx,%rdi
call   1040 <safestrlen@plt>
mov    %eax,%r13d
add    $0x1,%r13d
jmp    116d <safestrdup3+0x3d>
mov    $0x1,%r13d
xor    %r12d,%r12d
mov    $0x0,%ebp
test   %r15,%r15
je     1184 <safestrdup3+0x54>
mov    %r15,%rdi
call   1040 <safestrlen@plt>
mov    %eax,%ebp
add    %r13d,%ebp
test   %r14,%r14
je     1197 <safestrdup3+0x67>
mov    %r14,%rdi
call   1040 <safestrlen@plt>
mov    %eax,%r12d
add    %r12d,%ebp
mov    %ebp,%edi
mov    0x10(%rsp),%rsi
mov    0xc(%rsp),%edx
call   1030 <malloc_or_die@plt>
mov    %rax,%rbp
movb   $0x0,(%rax)
test   %rbx,%rbx
je     11c0 <safestrdup3+0x90>
mov    %rbp,%rdi
mov    %rbx,%rsi
call   1050 <strcat@plt>
test   %r15,%r15
je     11d0 <safestrdup3+0xa0>
mov    %rbp,%rdi
mov    %r15,%rsi
call   1050 <strcat@plt>
test   %r14,%r14
je     11e0 <safestrdup3+0xb0>
mov    %rbp,%rdi
mov    %r14,%rsi
call   1050 <strcat@plt>
mov    %rbp,%rax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i8* @safestrdup3(i8* %0, i8* %1, i8* %2, i8* %3, i32 %4) {
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i32
%12 = alloca i8*
store i8* %0, i8** %6
store i8* %1, i8** %7
store i8* %2, i8** %8
store i8* %3, i8** %9
store i32 %4, i32* %10
%13 = load i8*, i8** %6
%14 = icmp ne i8* %13, null
br i1 %14, label %15, label %18
15: ; preds = %5
%16 = load i8*, i8** %6
%17 = call i32 @safestrlen(i8* %16)
br label %19
18: ; preds = %5
br label %19
19: ; preds = %18, %15
%20 = phi i32 [ %17, %15 ], [ 0, %18 ]
%21 = add nsw i32 1, %20
%22 = load i8*, i8** %7
%23 = icmp ne i8* %22, null
br i1 %23, label %24, label %27
24: ; preds = %19
%25 = load i8*, i8** %7
%26 = call i32 @safestrlen(i8* %25)
br label %28
27: ; preds = %19
br label %28
28: ; preds = %27, %24
%29 = phi i32 [ %26, %24 ], [ 0, %27 ]
%30 = add nsw i32 %21, %29
%31 = load i8*, i8** %8
%32 = icmp ne i8* %31, null
br i1 %32, label %33, label %36
33: ; preds = %28
%34 = load i8*, i8** %8
%35 = call i32 @safestrlen(i8* %34)
br label %37
36: ; preds = %28
br label %37
37: ; preds = %36, %33
%38 = phi i32 [ %35, %33 ], [ 0, %36 ]
%39 = add nsw i32 %30, %38
store i32 %39, i32* %11
%40 = load i32, i32* %11
%41 = load i8*, i8** %9
%42 = load i32, i32* %10
%43 = call i8* @malloc_or_die(i32 %40, i8* %41, i32 %42)
store i8* %43, i8** %12
%44 = load i8*, i8** %12
%45 = getelementptr inbounds i8, i8* %44, i64 0
store i8 0, i8* %45
%46 = load i8*, i8** %6
%47 = icmp ne i8* %46, null
br i1 %47, label %48, label %52
48: ; preds = %37
%49 = load i8*, i8** %12
%50 = load i8*, i8** %6
%51 = call i32 @strcat(i8* %49, i8* %50)
br label %52
52: ; preds = %48, %37
%53 = load i8*, i8** %7
%54 = icmp ne i8* %53, null
br i1 %54, label %55, label %59
55: ; preds = %52
%56 = load i8*, i8** %12
%57 = load i8*, i8** %7
%58 = call i32 @strcat(i8* %56, i8* %57)
br label %59
59: ; preds = %55, %52
%60 = load i8*, i8** %8
%61 = icmp ne i8* %60, null
br i1 %61, label %62, label %66
62: ; preds = %59
%63 = load i8*, i8** %12
%64 = load i8*, i8** %8
%65 = call i32 @strcat(i8* %63, i8* %64)
br label %66
66: ; preds = %62, %59
%67 = load i8*, i8** %12
ret i8* %67
}

LLVM IR (-O2):
define i8* @safestrdup3(i8* %0, i8* %1, i8* %2, i8* %3, i32 %4) {
%6 = icmp eq i8* %0, null
br i1 %6, label %10, label %7
7: ; preds = %5
%8 = tail call i32 @safestrlen(i8* %0)
%9 = add i32 %8, 1
br label %10
10: ; preds = %5, %7
%11 = phi i32 [ %9, %7 ], [ 1, %5 ]
%12 = icmp eq i8* %1, null
br i1 %12, label %15, label %13
13: ; preds = %10
%14 = tail call i32 @safestrlen(i8* %1)
br label %15
15: ; preds = %10, %13
%16 = phi i32 [ %14, %13 ], [ 0, %10 ]
%17 = add nsw i32 %16, %11
%18 = icmp eq i8* %2, null
br i1 %18, label %21, label %19
19: ; preds = %15
%20 = tail call i32 @safestrlen(i8* %2)
br label %21
21: ; preds = %15, %19
%22 = phi i32 [ %20, %19 ], [ 0, %15 ]
%23 = add nsw i32 %17, %22
%24 = tail call i8* @malloc_or_die(i32 %23, i8* %3, i32 %4)
store i8 0, i8* %24
br i1 %6, label %27, label %25
25: ; preds = %21
%26 = tail call i32 @strcat(i8* %24, i8* %0)
br label %27
27: ; preds = %25, %21
br i1 %12, label %30, label %28
28: ; preds = %27
%29 = tail call i32 @strcat(i8* %24, i8* %1)
br label %30
30: ; preds = %28, %27
br i1 %18, label %33, label %31
31: ; preds = %30
%32 = tail call i32 @strcat(i8* %24, i8* %2)
br label %33
33: ; preds = %31, %30
ret i8* %24
}