#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 user_desc {
  int /*<<< orphan*/ entry_number;
};

/* Variables and functions */
unsigned short LDT3(int /*<<< orphan*/);
int /*<<< orphan*/ SYS_modify_ldt;
int /*<<< orphan*/ printf(char *, char const *);
scalar_t__ syscall(int /*<<< orphan*/, int, struct user_desc const *, int);
void add_ldt(const struct user_desc *desc, unsigned short *var,
             const char *name) {
  if (syscall(SYS_modify_ldt, 1, desc, sizeof(*desc)) == 0) {
    *var = LDT3(desc->entry_number);
  } else {
    printf("[NOTE]\tFailed to create %s segment\n", name);
    *var = 0;
  }
}


x86 assembly (clang, -O0):
<add_ldt>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    0x2e8d(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
mov    $0x1,%esi
mov    $0x4,%ecx
call   1040 <syscall@plt>
cmp    $0x0,%rax
jne    1184 <add_ldt+0x54>
mov    -0x8(%rbp),%rax
mov    (%rax),%edi
call   1050 <LDT3@plt>
mov    %ax,%cx
mov    -0x10(%rbp),%rax
mov    %cx,(%rax)
jmp    119d <add_ldt+0x6d>
mov    -0x18(%rbp),%rsi
lea    0xe71(%rip),%rdi
call   1030 <printf@plt>
mov    -0x10(%rbp),%rax
movw   $0x0,(%rax)
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<add_ldt>:
push   %r15
push   %r14
push   %rbx
mov    %rdx,%r15
mov    %rsi,%r14
mov    %rdi,%rbx
mov    0x2e93(%rip),%rax
mov    (%rax),%edi
mov    $0x1,%esi
mov    %rbx,%rdx
mov    $0x4,%ecx
call   1040 <syscall@plt>
test   %rax,%rax
je     1171 <add_ldt+0x41>
lea    0xe9b(%rip),%rdi
mov    %r15,%rsi
call   1030 <printf@plt>
xor    %eax,%eax
jmp    1178 <add_ldt+0x48>
mov    (%rbx),%edi
call   1050 <LDT3@plt>
mov    %ax,(%r14)
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.user_desc = type { i32 }
@SYS_modify_ldt = global i32 0
@.str = [36 x i8] c"[NOTE]\09Failed to create %s segment\0A\00"
define void @add_ldt(%struct.user_desc* %0, i16* %1, i8* %2) {
%4 = alloca %struct.user_desc*
%5 = alloca i16*
%6 = alloca i8*
store %struct.user_desc* %0, %struct.user_desc** %4
store i16* %1, i16** %5
store i8* %2, i8** %6
%7 = load i32, i32* @SYS_modify_ldt
%8 = load %struct.user_desc*, %struct.user_desc** %4
%9 = call i64 @syscall(i32 %7, i32 1, %struct.user_desc* %8, i32 4)
%10 = icmp eq i64 %9, 0
br i1 %10, label %11, label %17
11: ; preds = %3
%12 = load %struct.user_desc*, %struct.user_desc** %4
%13 = getelementptr inbounds %struct.user_desc, %struct.user_desc* %12, i32 0, i32 0
%14 = load i32, i32* %13
%15 = call zeroext i16 @LDT3(i32 %14)
%16 = load i16*, i16** %5
store i16 %15, i16* %16
br label %21
17: ; preds = %3
%18 = load i8*, i8** %6
%19 = call i32 @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), i8* %18)
%20 = load i16*, i16** %5
store i16 0, i16* %20
br label %21
21: ; preds = %17, %11
ret void
}

LLVM IR (-O2):
%struct.user_desc = type { i32 }
@SYS_modify_ldt = global i32 0
@.str = [36 x i8] c"[NOTE]\09Failed to create %s segment\0A\00"
define void @add_ldt(%struct.user_desc* %0, i16* nocapture writeonly %1, i8* %2) {
%4 = load i32, i32* @SYS_modify_ldt
%5 = tail call i64 @syscall(i32 %4, i32 1, %struct.user_desc* %0, i32 4)
%6 = icmp eq i64 %5, 0
br i1 %6, label %7, label %11
7: ; preds = %3
%8 = getelementptr inbounds %struct.user_desc, %struct.user_desc* %0, i64 0, i32 0
%9 = load i32, i32* %8
%10 = tail call zeroext i16 @LDT3(i32 %9)
br label %13
11: ; preds = %3
%12 = tail call i32 @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), i8* %2)
br label %13
13: ; preds = %11, %7
%14 = phi i16 [ 0, %11 ], [ %10, %7 ]
store i16 %14, i16* %1
ret void
}